Skip to content

Commit f2b9018

Browse files
committed
Add messaging APIs
1 parent 7408814 commit f2b9018

File tree

10 files changed

+210
-10
lines changed

10 files changed

+210
-10
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ dependencies {
5050
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.9.1'
5151
testImplementation 'org.junit.jupiter:junit-jupiter-engine:5.9.1'
5252
testImplementation 'org.junit.jupiter:junit-jupiter-params:5.9.1'
53-
testImplementation 'org.testcontainers:junit-jupiter:1.18.3'
53+
testImplementation 'org.testcontainers:junit-jupiter:1.20.1'
5454
testImplementation 'org.mockito:mockito-core:4.11.0'
5555
testImplementation 'org.mockito:mockito-junit-jupiter:4.11.0'
5656
testImplementation 'org.awaitility:awaitility:4.2.0'

src/main/java/net/luckperms/rest/LuckPermsRestClient.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import net.luckperms.rest.service.ActionService;
2929
import net.luckperms.rest.service.EventService;
3030
import net.luckperms.rest.service.GroupService;
31+
import net.luckperms.rest.service.MessagingService;
3132
import net.luckperms.rest.service.MiscService;
3233
import net.luckperms.rest.service.TrackService;
3334
import net.luckperms.rest.service.UserService;
@@ -76,6 +77,13 @@ static Builder builder() {
7677
*/
7778
ActionService actions();
7879

80+
/**
81+
* Gets the messaging service.
82+
*
83+
* @return the messaging service
84+
*/
85+
MessagingService messaging();
86+
7987
/**
8088
* Gets the event service.
8189
*

src/main/java/net/luckperms/rest/LuckPermsRestClientImpl.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import net.luckperms.rest.service.ActionService;
2929
import net.luckperms.rest.service.EventService;
3030
import net.luckperms.rest.service.GroupService;
31+
import net.luckperms.rest.service.MessagingService;
3132
import net.luckperms.rest.service.MiscService;
3233
import net.luckperms.rest.service.TrackService;
3334
import net.luckperms.rest.service.UserService;
@@ -50,6 +51,7 @@ class LuckPermsRestClientImpl implements LuckPermsRestClient {
5051
private final GroupService groupService;
5152
private final TrackService trackService;
5253
private final ActionService actionService;
54+
private final MessagingService messagingService;
5355
private final EventService eventService;
5456
private final MiscService miscService;
5557

@@ -76,6 +78,7 @@ class LuckPermsRestClientImpl implements LuckPermsRestClient {
7678
this.groupService = retrofit.create(GroupService.class);
7779
this.trackService = retrofit.create(TrackService.class);
7880
this.actionService = retrofit.create(ActionService.class);
81+
this.messagingService = retrofit.create(MessagingService.class);
7982
this.eventService = retrofit.create(EventService.class);
8083
this.miscService = retrofit.create(MiscService.class);
8184
}
@@ -99,6 +102,11 @@ public ActionService actions() {
99102
return this.actionService;
100103
}
101104

105+
@Override
106+
public MessagingService messaging() {
107+
return this.messagingService;
108+
}
109+
102110
@Override
103111
public EventService events() {
104112
return this.eventService;
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* This file is part of LuckPerms, licensed under the MIT License.
3+
*
4+
* Copyright (c) lucko (Luck) <[email protected]>
5+
* Copyright (c) contributors
6+
*
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy
8+
* of this software and associated documentation files (the "Software"), to deal
9+
* in the Software without restriction, including without limitation the rights
10+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
* copies of the Software, and to permit persons to whom the Software is
12+
* furnished to do so, subject to the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be included in all
15+
* copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23+
* SOFTWARE.
24+
*/
25+
26+
package net.luckperms.rest.model;
27+
28+
public class CustomMessage extends AbstractModel {
29+
private final String channelId;
30+
private final String payload;
31+
32+
public CustomMessage(String channelId, String payload) {
33+
this.channelId = channelId;
34+
this.payload = payload;
35+
}
36+
37+
public String channelId() {
38+
return this.channelId;
39+
}
40+
41+
public String payload() {
42+
return this.payload;
43+
}
44+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* This file is part of LuckPerms, licensed under the MIT License.
3+
*
4+
* Copyright (c) lucko (Luck) <[email protected]>
5+
* Copyright (c) contributors
6+
*
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy
8+
* of this software and associated documentation files (the "Software"), to deal
9+
* in the Software without restriction, including without limitation the rights
10+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
* copies of the Software, and to permit persons to whom the Software is
12+
* furnished to do so, subject to the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be included in all
15+
* copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23+
* SOFTWARE.
24+
*/
25+
26+
package net.luckperms.rest.model;
27+
28+
public class CustomMessageReceiveEvent extends AbstractModel {
29+
private final String channelId;
30+
private final String payload;
31+
32+
public CustomMessageReceiveEvent(String channelId, String payload) {
33+
this.channelId = channelId;
34+
this.payload = payload;
35+
}
36+
37+
public String channelId() {
38+
return this.channelId;
39+
}
40+
41+
public String payload() {
42+
return this.payload;
43+
}
44+
}

src/main/java/net/luckperms/rest/service/EventService.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
package net.luckperms.rest.service;
2727

2828
import net.luckperms.rest.event.EventCall;
29+
import net.luckperms.rest.model.CustomMessageReceiveEvent;
2930
import net.luckperms.rest.model.LogBroadcastEvent;
3031
import net.luckperms.rest.model.PostNetworkSyncEvent;
3132
import net.luckperms.rest.model.PostSyncEvent;
@@ -50,4 +51,7 @@ public interface EventService {
5051
@GET("/event/pre-sync")
5152
EventCall<PreSyncEvent> preSync();
5253

54+
@GET("/event/custom-message-receive")
55+
EventCall<CustomMessageReceiveEvent> customMessageReceive();
56+
5357
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* This file is part of LuckPerms, licensed under the MIT License.
3+
*
4+
* Copyright (c) lucko (Luck) <[email protected]>
5+
* Copyright (c) contributors
6+
*
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy
8+
* of this software and associated documentation files (the "Software"), to deal
9+
* in the Software without restriction, including without limitation the rights
10+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
* copies of the Software, and to permit persons to whom the Software is
12+
* furnished to do so, subject to the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be included in all
15+
* copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23+
* SOFTWARE.
24+
*/
25+
26+
package net.luckperms.rest.service;
27+
28+
import net.luckperms.rest.model.CustomMessage;
29+
import retrofit2.Call;
30+
import retrofit2.http.Body;
31+
import retrofit2.http.POST;
32+
import retrofit2.http.Path;
33+
34+
import java.util.UUID;
35+
36+
public interface MessagingService {
37+
38+
@POST("/messaging/update")
39+
Call<Void> pushUpdate();
40+
41+
@POST("/messaging/update/{uniqueId}")
42+
Call<Void> pushUserUpdate(@Path("uniqueId") UUID uniqueId);
43+
44+
@POST("/messaging/custom")
45+
Call<Void> sendCustomMessage(@Body CustomMessage message);
46+
47+
}

src/test/java/me/lucko/luckperms/rest/AbstractIntegrationTest.java

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,23 @@
4343
public class AbstractIntegrationTest {
4444

4545
@Container
46-
private static final GenericContainer<?> container = new GenericContainer<>(DockerImageName.parse("ghcr.io/luckperms/rest-api"))
47-
.withLogConsumer(new Slf4jLogConsumer(LoggerFactory.getLogger(AbstractIntegrationTest.class)))
48-
.withExposedPorts(8080)
49-
.waitingFor(new WaitAllStrategy()
50-
.withStrategy(Wait.forListeningPort())
51-
.withStrategy(Wait.forLogMessage(".*Successfully enabled.*", 1))
52-
);
46+
private static final GenericContainer<?> container = createContainer();
5347

5448
protected LuckPermsRestClient createClient() {
49+
return createClient(container);
50+
}
51+
52+
protected static GenericContainer<?> createContainer() {
53+
return new GenericContainer<>(DockerImageName.parse("ghcr.io/luckperms/rest-api"))
54+
.withLogConsumer(new Slf4jLogConsumer(LoggerFactory.getLogger(AbstractIntegrationTest.class)))
55+
.withExposedPorts(8080)
56+
.waitingFor(new WaitAllStrategy()
57+
.withStrategy(Wait.forListeningPort())
58+
.withStrategy(Wait.forLogMessage(".*Successfully enabled.*", 1))
59+
);
60+
}
61+
62+
protected static LuckPermsRestClient createClient(GenericContainer<?> container) {
5563
assertTrue(container.isRunning());
5664

5765
String host = container.getHost();

src/test/java/me/lucko/luckperms/rest/ActionServiceTest.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,6 @@
3333
import retrofit2.Response;
3434

3535
import java.io.IOException;
36-
import java.util.ArrayList;
37-
import java.util.List;
3836
import java.util.UUID;
3937
import java.util.stream.Collectors;
4038

src/test/java/me/lucko/luckperms/rest/EventServiceTest.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,14 @@
2929
import net.luckperms.rest.event.EventCall;
3030
import net.luckperms.rest.event.EventProducer;
3131
import net.luckperms.rest.model.Action;
32+
import net.luckperms.rest.model.CustomMessage;
33+
import net.luckperms.rest.model.CustomMessageReceiveEvent;
3234
import net.luckperms.rest.model.LogBroadcastEvent;
3335
import org.junit.jupiter.api.Disabled;
3436
import org.junit.jupiter.api.Test;
37+
import org.testcontainers.containers.GenericContainer;
38+
import org.testcontainers.containers.Network;
39+
import org.testcontainers.utility.DockerImageName;
3540

3641
import java.io.IOException;
3742
import java.util.ArrayList;
@@ -41,6 +46,7 @@
4146
import java.util.concurrent.CountDownLatch;
4247
import java.util.concurrent.TimeUnit;
4348
import java.util.function.Consumer;
49+
import java.util.function.Supplier;
4450

4551
import static org.junit.jupiter.api.Assertions.assertEquals;
4652
import static org.junit.jupiter.api.Assertions.assertNotSame;
@@ -72,6 +78,39 @@ public void testLogBroadcastEvent() throws Exception {
7278
assertNotSame(event.entry(), exampleAction);
7379
}
7480

81+
@Test
82+
public void testCustomMessageReceiveEvent() throws Exception {
83+
try (Network network = Network.newNetwork(); GenericContainer<?> redis = new GenericContainer<>(DockerImageName.parse("redis"))) {
84+
redis.withNetwork(network).withNetworkAliases("redis").start();
85+
86+
Supplier<GenericContainer<?>> restSupplier = () -> createContainer()
87+
.withNetwork(network)
88+
.withEnv("LUCKPERMS_MESSAGING_SERVICE", "redis")
89+
.withEnv("LUCKPERMS_REDIS_ENABLED", "true")
90+
.withEnv("LUCKPERMS_REDIS_ADDRESS", "redis:6379");
91+
92+
try (GenericContainer<?> restA = restSupplier.get(); GenericContainer<?> restB = restSupplier.get()) {
93+
restA.start();
94+
restB.start();
95+
96+
LuckPermsRestClient clientA = createClient(restA);
97+
LuckPermsRestClient clientB = createClient(restB);
98+
99+
EventCall<CustomMessageReceiveEvent> call = clientA.events().customMessageReceive();
100+
CustomMessageReceiveEvent event = testEvent(call, 5, () -> {
101+
try {
102+
clientB.messaging().sendCustomMessage(new CustomMessage("custom:test", "aaabbbccc")).execute();
103+
} catch (IOException e) {
104+
throw new RuntimeException(e);
105+
}
106+
});
107+
108+
assertEquals("custom:test", event.channelId());
109+
assertEquals("aaabbbccc", event.payload());
110+
}
111+
}
112+
}
113+
75114
@Test
76115
@Disabled("takes too long to run")
77116
public void testLogBroadcastEventLongWait() throws Exception {

0 commit comments

Comments
 (0)