Skip to content

Commit

Permalink
Add actionlog query methods
Browse files Browse the repository at this point in the history
  • Loading branch information
lucko committed Jul 14, 2024
1 parent c2ed958 commit c549525
Show file tree
Hide file tree
Showing 5 changed files with 163 additions and 1 deletion.
6 changes: 5 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,12 @@ version = '0.1-SNAPSHOT'

java {
toolchain {
languageVersion = JavaLanguageVersion.of(8)
languageVersion = JavaLanguageVersion.of(11)
}

sourceCompatibility = '1.8'
targetCompatibility = '1.8'

withJavadocJar()
withSourcesJar()
}
Expand Down
46 changes: 46 additions & 0 deletions src/main/java/net/luckperms/rest/model/ActionPage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* This file is part of LuckPerms, licensed under the MIT License.
*
* Copyright (c) lucko (Luck) <[email protected]>
* Copyright (c) contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package net.luckperms.rest.model;

import java.util.Collection;

public class ActionPage extends AbstractModel {
private final Collection<Action> entries;
private final int overallSize;

public ActionPage(Collection<Action> entries, int overallSize) {
this.entries = entries;
this.overallSize = overallSize;
}

public Collection<Action> entries() {
return this.entries;
}

public int overallSize() {
return this.overallSize;
}
}
41 changes: 41 additions & 0 deletions src/main/java/net/luckperms/rest/service/ActionService.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,53 @@
package net.luckperms.rest.service;

import net.luckperms.rest.model.Action;
import net.luckperms.rest.model.ActionPage;
import retrofit2.Call;
import retrofit2.http.Body;
import retrofit2.http.GET;
import retrofit2.http.POST;
import retrofit2.http.Query;

import java.util.UUID;

public interface ActionService {

@GET("/action")
Call<ActionPage> query();

@GET("/action")
Call<ActionPage> query(@Query("pageSize") int pageSize, @Query("pageNumber") int pageNumber);

@GET("/action")
Call<ActionPage> querySource(@Query("source") UUID source);

@GET("/action")
Call<ActionPage> querySource(@Query("source") UUID source, @Query("pageSize") int pageSize, @Query("pageNumber") int pageNumber);

@GET("/action")
Call<ActionPage> queryTargetUser(@Query("user") UUID user);

@GET("/action")
Call<ActionPage> queryTargetUser(@Query("user") UUID user, @Query("pageSize") int pageSize, @Query("pageNumber") int pageNumber);

@GET("/action")
Call<ActionPage> queryTargetGroup(@Query("group") String group);

@GET("/action")
Call<ActionPage> queryTargetGroup(@Query("group") String group, @Query("pageSize") int pageSize, @Query("pageNumber") int pageNumber);

@GET("/action")
Call<ActionPage> queryTargetTrack(@Query("track") String track);

@GET("/action")
Call<ActionPage> queryTargetTrack(@Query("track") String track, @Query("pageSize") int pageSize, @Query("pageNumber") int pageNumber);

@GET("/action")
Call<ActionPage> querySearch(@Query("search") String search);

@GET("/action")
Call<ActionPage> querySearch(@Query("search") String search, @Query("pageSize") int pageSize, @Query("pageNumber") int pageNumber);

@POST("/action")
Call<Void> submit(@Body Action action);

Expand Down
3 changes: 3 additions & 0 deletions src/main/java/net/luckperms/rest/service/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ public interface UserService {
@DELETE("/user/{uniqueId}")
Call<Void> delete(@Path("uniqueId") UUID uniqueId);

@DELETE("/user/{uniqueId}")
Call<Void> delete(@Path("uniqueId") UUID uniqueId, @Query("playerDataOnly") boolean playerDataOnly);

@GET("/user/{uniqueId}/nodes")
Call<List<Node>> nodes(@Path("uniqueId") UUID uniqueId);

Expand Down
68 changes: 68 additions & 0 deletions src/test/java/me/lucko/luckperms/rest/ActionServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,19 @@

import net.luckperms.rest.LuckPermsRestClient;
import net.luckperms.rest.model.Action;
import net.luckperms.rest.model.ActionPage;
import org.junit.jupiter.api.Test;
import org.testcontainers.shaded.com.google.common.collect.ImmutableList;
import retrofit2.Response;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class ActionServiceTest extends AbstractIntegrationTest {
Expand All @@ -50,4 +57,65 @@ public void testSubmit() throws IOException {
assertTrue(resp.isSuccessful());
}

@Test
public void testQuery() throws IOException {
LuckPermsRestClient client = createClient();

long time = System.currentTimeMillis() / 1000L;
Action.Source source = new Action.Source(UUID.randomUUID(), randomName());

Action testUserAction = new Action(time, source,
new Action.Target(UUID.randomUUID(), randomName(), Action.Target.Type.USER),
"test user action"
);
Action testGroupAction = new Action(time + 1, source,
new Action.Target(null, randomName(), Action.Target.Type.GROUP),
"test group action"
);
Action testTrackAction = new Action(time + 2, source,
new Action.Target(null, randomName(), Action.Target.Type.TRACK),
"test track action"
);

client.actions().submit(testUserAction).execute();
client.actions().submit(testGroupAction).execute();
client.actions().submit(testTrackAction).execute();

// test simple query all
Response<ActionPage> resp1 = client.actions().query().execute();
assertTrue(resp1.isSuccessful());
ActionPage body1 = resp1.body();
assertNotNull(body1);
assertEquals(3, body1.overallSize());
assertEquals(ImmutableList.of(testTrackAction, testGroupAction, testUserAction), body1.entries());

for (int i = 0; i < 20; i++) {
Action testAction = new Action(
time + 100 - i,
new Action.Source(UUID.randomUUID(), randomName()),
new Action.Target(null, randomName(), Action.Target.Type.GROUP),
"test " + i
);
client.actions().submit(testAction).execute();
}

// test pagination
Response<ActionPage> resp2 = client.actions().query(5, 2).execute();
assertTrue(resp2.isSuccessful());
ActionPage body2 = resp2.body();
assertNotNull(body2);
assertEquals(23, body2.overallSize());
assertEquals(
ImmutableList.of("test 5", "test 6", "test 7", "test 8", "test 9"),
body2.entries().stream().map(Action::description).collect(Collectors.toList())
);

// test query by source
Response<ActionPage> resp3 = client.actions().querySource(source.uniqueId(), 5, 1).execute();
assertTrue(resp3.isSuccessful());
ActionPage body3 = resp3.body();
assertNotNull(body3);
assertEquals(3, body3.overallSize());
}

}

0 comments on commit c549525

Please sign in to comment.