loottracker: submit loot when not logged in

This is to aid the wiki drop log project. The data is not otherwise
stored.
This commit is contained in:
Adam
2020-10-01 10:03:50 -04:00
parent 32638ccf63
commit 1799c9e593
7 changed files with 54 additions and 27 deletions

View File

@@ -35,7 +35,9 @@ import java.util.Collection;
import java.util.List; import java.util.List;
import java.util.UUID; import java.util.UUID;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
import lombok.AllArgsConstructor; import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import net.runelite.http.api.RuneLiteAPI; import net.runelite.http.api.RuneLiteAPI;
import static net.runelite.http.api.RuneLiteAPI.JSON; import static net.runelite.http.api.RuneLiteAPI.JSON;
@@ -48,13 +50,15 @@ import okhttp3.RequestBody;
import okhttp3.Response; import okhttp3.Response;
@Slf4j @Slf4j
@AllArgsConstructor @RequiredArgsConstructor
public class LootTrackerClient public class LootTrackerClient
{ {
private static final Gson GSON = RuneLiteAPI.GSON; private static final Gson GSON = RuneLiteAPI.GSON;
private final OkHttpClient client; private final OkHttpClient client;
private final UUID uuid; @Getter
@Setter
private UUID uuid;
public CompletableFuture<Void> submit(Collection<LootRecord> lootRecords) public CompletableFuture<Void> submit(Collection<LootRecord> lootRecords)
{ {
@@ -64,13 +68,16 @@ public class LootTrackerClient
.addPathSegment("loottracker") .addPathSegment("loottracker")
.build(); .build();
Request request = new Request.Builder() Request.Builder requestBuilder = new Request.Builder();
.header(RuneLiteAPI.RUNELITE_AUTH, uuid.toString()) if (uuid != null)
.post(RequestBody.create(JSON, GSON.toJson(lootRecords))) {
requestBuilder.header(RuneLiteAPI.RUNELITE_AUTH, uuid.toString());
}
requestBuilder.post(RequestBody.create(JSON, GSON.toJson(lootRecords)))
.url(url) .url(url)
.build(); .build();
client.newCall(request).enqueue(new Callback() client.newCall(requestBuilder.build()).enqueue(new Callback()
{ {
@Override @Override
public void onFailure(Call call, IOException e) public void onFailure(Call call, IOException e)

View File

@@ -65,14 +65,22 @@ public class LootTrackerController
@RequestMapping(method = RequestMethod.POST) @RequestMapping(method = RequestMethod.POST)
public void storeLootRecord(HttpServletRequest request, HttpServletResponse response, @RequestBody Collection<LootRecord> records) throws IOException public void storeLootRecord(HttpServletRequest request, HttpServletResponse response, @RequestBody Collection<LootRecord> records) throws IOException
{ {
SessionEntry e = auth.handle(request, response); SessionEntry session = null;
if (e == null) if (request.getHeader(RuneLiteAPI.RUNELITE_AUTH) != null)
{ {
response.setStatus(HttpStatusCodes.STATUS_CODE_UNAUTHORIZED); session = auth.handle(request, response);
return; if (session == null)
{
// error is set here on the response, so we shouldn't continue
return;
}
} }
Integer userId = session == null ? null : session.getUser();
service.store(records, e.getUser()); if (userId != null)
{
service.store(records, userId);
}
response.setStatus(HttpStatusCodes.STATUS_CODE_OK); response.setStatus(HttpStatusCodes.STATUS_CODE_OK);
try (Jedis jedis = redisPool.getResource()) try (Jedis jedis = redisPool.getResource())

View File

@@ -27,6 +27,7 @@ package net.runelite.http.service.loottracker;
import java.io.IOException; import java.io.IOException;
import java.time.Instant; import java.time.Instant;
import java.util.Collections; import java.util.Collections;
import java.util.UUID;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import net.runelite.http.api.RuneLiteAPI; import net.runelite.http.api.RuneLiteAPI;
@@ -91,7 +92,10 @@ public class LootTrackerControllerTest
lootRecord.setDrops(Collections.singletonList(new GameItem(4151, 1))); lootRecord.setDrops(Collections.singletonList(new GameItem(4151, 1)));
String data = RuneLiteAPI.GSON.toJson(Collections.singletonList(lootRecord)); String data = RuneLiteAPI.GSON.toJson(Collections.singletonList(lootRecord));
mockMvc.perform(post("/loottracker").content(data).contentType(MediaType.APPLICATION_JSON)) mockMvc.perform(post("/loottracker")
.header(RuneLiteAPI.RUNELITE_AUTH, UUID.nameUUIDFromBytes("test".getBytes()))
.content(data)
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk()); .andExpect(status().isOk());
verify(lootTrackerService).store(eq(Collections.singletonList(lootRecord)), anyInt()); verify(lootTrackerService).store(eq(Collections.singletonList(lootRecord)), anyInt());

View File

@@ -82,7 +82,7 @@ public interface LootTrackerConfig extends Config
@ConfigItem( @ConfigItem(
keyName = "saveLoot", keyName = "saveLoot",
name = "Submit loot tracker data", name = "Submit loot tracker data",
description = "Submit loot tracker data (requires being logged in)" description = "Submit loot tracker data"
) )
default boolean saveLoot() default boolean saveLoot()
{ {

View File

@@ -279,7 +279,7 @@ class LootTrackerPanel extends PluginPanel
reset.addActionListener(e -> reset.addActionListener(e ->
{ {
final LootTrackerClient client = plugin.getLootTrackerClient(); final LootTrackerClient client = plugin.getLootTrackerClient();
final boolean syncLoot = client != null && config.syncPanel(); final boolean syncLoot = client.getUuid() != null && config.syncPanel();
final int result = JOptionPane.showOptionDialog(overallPanel, final int result = JOptionPane.showOptionDialog(overallPanel,
syncLoot ? SYNC_RESET_ALL_WARNING_TEXT : NO_SYNC_RESET_ALL_WARNING_TEXT, syncLoot ? SYNC_RESET_ALL_WARNING_TEXT : NO_SYNC_RESET_ALL_WARNING_TEXT,
"Are you sure?", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE, "Are you sure?", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE,
@@ -568,7 +568,7 @@ class LootTrackerPanel extends PluginPanel
LootTrackerClient client = plugin.getLootTrackerClient(); LootTrackerClient client = plugin.getLootTrackerClient();
// Without loot being grouped we have no way to identify single kills to be deleted // Without loot being grouped we have no way to identify single kills to be deleted
if (client != null && groupLoot && config.syncPanel()) if (client.getUuid() != null && groupLoot && config.syncPanel())
{ {
client.delete(box.getId()); client.delete(box.getId());
} }

View File

@@ -262,9 +262,6 @@ public class LootTrackerPlugin extends Plugin
@Inject @Inject
private LootManager lootManager; private LootManager lootManager;
@Inject
private OkHttpClient okHttpClient;
private LootTrackerPanel panel; private LootTrackerPanel panel;
private NavigationButton navButton; private NavigationButton navButton;
@VisibleForTesting @VisibleForTesting
@@ -281,6 +278,7 @@ public class LootTrackerPlugin extends Plugin
private Multiset<Integer> inventorySnapshot; private Multiset<Integer> inventorySnapshot;
@Getter(AccessLevel.PACKAGE) @Getter(AccessLevel.PACKAGE)
@Inject
private LootTrackerClient lootTrackerClient; private LootTrackerClient lootTrackerClient;
private final List<LootRecord> queuedLoots = new ArrayList<>(); private final List<LootRecord> queuedLoots = new ArrayList<>();
@@ -313,6 +311,12 @@ public class LootTrackerPlugin extends Plugin
return list; return list;
} }
@Provides
LootTrackerClient provideLootTrackerClient(OkHttpClient okHttpClient)
{
return new LootTrackerClient(okHttpClient);
}
@Provides @Provides
LootTrackerConfig provideConfig(ConfigManager configManager) LootTrackerConfig provideConfig(ConfigManager configManager)
{ {
@@ -325,11 +329,11 @@ public class LootTrackerPlugin extends Plugin
AccountSession accountSession = sessionManager.getAccountSession(); AccountSession accountSession = sessionManager.getAccountSession();
if (accountSession.getUuid() != null) if (accountSession.getUuid() != null)
{ {
lootTrackerClient = new LootTrackerClient(okHttpClient, accountSession.getUuid()); lootTrackerClient.setUuid(accountSession.getUuid());
} }
else else
{ {
lootTrackerClient = null; lootTrackerClient.setUuid(null);
} }
} }
@@ -337,7 +341,7 @@ public class LootTrackerPlugin extends Plugin
public void onSessionClose(SessionClose sessionClose) public void onSessionClose(SessionClose sessionClose)
{ {
submitLoot(); submitLoot();
lootTrackerClient = null; lootTrackerClient.setUuid(null);
} }
@Subscribe @Subscribe
@@ -373,7 +377,7 @@ public class LootTrackerPlugin extends Plugin
AccountSession accountSession = sessionManager.getAccountSession(); AccountSession accountSession = sessionManager.getAccountSession();
if (accountSession != null) if (accountSession != null)
{ {
lootTrackerClient = new LootTrackerClient(okHttpClient, accountSession.getUuid()); lootTrackerClient.setUuid(accountSession.getUuid());
clientThread.invokeLater(() -> clientThread.invokeLater(() ->
{ {
@@ -420,7 +424,7 @@ public class LootTrackerPlugin extends Plugin
{ {
submitLoot(); submitLoot();
clientToolbar.removeNavigation(navButton); clientToolbar.removeNavigation(navButton);
lootTrackerClient = null; lootTrackerClient.setUuid(null);
chestLooted = false; chestLooted = false;
} }
@@ -831,15 +835,14 @@ public class LootTrackerPlugin extends Plugin
queuedLoots.clear(); queuedLoots.clear();
} }
if (lootTrackerClient == null || !config.saveLoot()) if (!config.saveLoot())
{ {
return null; return null;
} }
log.debug("Submitting {} loot records", copy.size()); log.debug("Submitting {} loot records", copy.size());
CompletableFuture<Void> future = lootTrackerClient.submit(copy); return lootTrackerClient.submit(copy);
return future;
} }
private void setEvent(LootRecordType lootRecordType, String eventType, Object metadata) private void setEvent(LootRecordType lootRecordType, String eventType, Object metadata)

View File

@@ -61,6 +61,7 @@ import net.runelite.client.game.SpriteManager;
import net.runelite.client.ui.overlay.infobox.InfoBoxManager; import net.runelite.client.ui.overlay.infobox.InfoBoxManager;
import net.runelite.http.api.item.ItemPrice; import net.runelite.http.api.item.ItemPrice;
import net.runelite.http.api.loottracker.LootRecordType; import net.runelite.http.api.loottracker.LootRecordType;
import net.runelite.http.api.loottracker.LootTrackerClient;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull; import static org.junit.Assert.assertNull;
import org.junit.Before; import org.junit.Before;
@@ -135,6 +136,10 @@ public class LootTrackerPluginTest
@Bind @Bind
private ChatMessageManager chatMessageManager; private ChatMessageManager chatMessageManager;
@Mock
@Bind
private LootTrackerClient lootTrackerClient;
@Before @Before
public void setUp() public void setUp()
{ {