Merge remote-tracking branch 'runelite/master' into 2308-merge
This commit is contained in:
@@ -262,6 +262,8 @@ public class AccountService
|
||||
return;
|
||||
}
|
||||
|
||||
auth.invalidate(session.getUuid());
|
||||
|
||||
try (Connection con = sql2o.open())
|
||||
{
|
||||
con.createQuery("delete from sessions where uuid = :uuid")
|
||||
|
||||
@@ -24,14 +24,18 @@
|
||||
*/
|
||||
package net.runelite.http.service.account;
|
||||
|
||||
import com.google.common.cache.Cache;
|
||||
import com.google.common.cache.CacheBuilder;
|
||||
import com.google.common.cache.RemovalNotification;
|
||||
import java.io.IOException;
|
||||
import net.runelite.http.service.account.beans.SessionEntry;
|
||||
import java.sql.Timestamp;
|
||||
import java.time.Instant;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import net.runelite.http.api.RuneLiteAPI;
|
||||
import net.runelite.http.service.account.beans.SessionEntry;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.stereotype.Service;
|
||||
@@ -43,6 +47,12 @@ public class AuthFilter
|
||||
{
|
||||
private final Sql2o sql2o;
|
||||
|
||||
private final Cache<UUID, SessionEntry> sessionCache = CacheBuilder.newBuilder()
|
||||
.maximumSize(10000L)
|
||||
.expireAfterAccess(30, TimeUnit.MINUTES)
|
||||
.removalListener(this::removalListener)
|
||||
.build();
|
||||
|
||||
@Autowired
|
||||
public AuthFilter(@Qualifier("Runelite SQL2O") Sql2o sql2o)
|
||||
{
|
||||
@@ -59,30 +69,48 @@ public class AuthFilter
|
||||
}
|
||||
|
||||
UUID uuid = UUID.fromString(runeliteAuth);
|
||||
SessionEntry sessionEntry = sessionCache.getIfPresent(uuid);
|
||||
if (sessionEntry != null)
|
||||
{
|
||||
return sessionEntry;
|
||||
}
|
||||
|
||||
try (Connection con = sql2o.open())
|
||||
{
|
||||
SessionEntry sessionEntry = con.createQuery("select user, uuid, created from sessions where uuid = :uuid")
|
||||
sessionEntry = con.createQuery("select user, uuid, created, last_used as lastUsed from sessions where uuid = :uuid")
|
||||
.addParameter("uuid", uuid.toString())
|
||||
.executeAndFetchFirst(SessionEntry.class);
|
||||
}
|
||||
|
||||
if (sessionEntry == null)
|
||||
{
|
||||
response.sendError(401, "Access denied");
|
||||
return null;
|
||||
}
|
||||
if (sessionEntry == null)
|
||||
{
|
||||
response.sendError(401, "Access denied");
|
||||
return null;
|
||||
}
|
||||
|
||||
Instant now = Instant.now();
|
||||
sessionCache.put(uuid, sessionEntry);
|
||||
|
||||
return sessionEntry;
|
||||
}
|
||||
|
||||
private void removalListener(RemovalNotification<UUID, SessionEntry> notification)
|
||||
{
|
||||
UUID uuid = notification.getKey();
|
||||
Instant now = Instant.now();
|
||||
|
||||
try (Connection con = sql2o.open())
|
||||
{
|
||||
con.createQuery("update sessions set last_used = :last_used where uuid = :uuid")
|
||||
.addParameter("last_used", Timestamp.from(now))
|
||||
.addParameter("uuid", uuid.toString())
|
||||
.executeUpdate();
|
||||
|
||||
sessionEntry.setLastUsed(now);
|
||||
|
||||
return sessionEntry;
|
||||
}
|
||||
}
|
||||
|
||||
public void invalidate(UUID uuid)
|
||||
{
|
||||
// If we ever run multiple services, may need to publish something here to invalidate...
|
||||
sessionCache.invalidate(uuid);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -53,7 +53,7 @@ public class LootTrackerController
|
||||
private AuthFilter auth;
|
||||
|
||||
@RequestMapping(method = RequestMethod.POST)
|
||||
public void storeLootRecord(HttpServletRequest request, HttpServletResponse response, @RequestBody LootRecord record) throws IOException
|
||||
public void storeLootRecord(HttpServletRequest request, HttpServletResponse response, @RequestBody Collection<LootRecord> records) throws IOException
|
||||
{
|
||||
SessionEntry e = auth.handle(request, response);
|
||||
if (e == null)
|
||||
@@ -62,7 +62,7 @@ public class LootTrackerController
|
||||
return;
|
||||
}
|
||||
|
||||
service.store(record, e.getUser());
|
||||
service.store(records, e.getUser());
|
||||
response.setStatus(HttpStatusCodes.STATUS_CODE_OK);
|
||||
}
|
||||
|
||||
|
||||
@@ -64,7 +64,7 @@ public class LootTrackerService
|
||||
|
||||
// Queries for inserting kills
|
||||
private static final String INSERT_KILL_QUERY = "INSERT INTO kills (accountId, type, eventId) VALUES (:accountId, :type, :eventId)";
|
||||
private static final String INSERT_DROP_QUERY = "INSERT INTO drops (killId, itemId, itemQuantity) VALUES (LAST_INSERT_ID(), :itemId, :itemQuantity)";
|
||||
private static final String INSERT_DROP_QUERY = "INSERT INTO drops (killId, itemId, itemQuantity) VALUES (:killId, :itemId, :itemQuantity)";
|
||||
|
||||
private static final String SELECT_LOOT_QUERY = "SELECT killId,time,type,eventId,itemId,itemQuantity FROM kills JOIN drops ON drops.killId = kills.id WHERE accountId = :accountId ORDER BY TIME DESC LIMIT :limit OFFSET :offset";
|
||||
|
||||
@@ -89,29 +89,49 @@ public class LootTrackerService
|
||||
/**
|
||||
* Store LootRecord
|
||||
*
|
||||
* @param record LootRecord to store
|
||||
* @param records LootRecords to store
|
||||
* @param accountId runelite account id to tie data too
|
||||
*/
|
||||
public void store(LootRecord record, int accountId)
|
||||
public void store(Collection<LootRecord> records, int accountId)
|
||||
{
|
||||
try (Connection con = sql2o.beginTransaction())
|
||||
{
|
||||
// Kill Entry Query
|
||||
con.createQuery(INSERT_KILL_QUERY, true)
|
||||
.addParameter("accountId", accountId)
|
||||
.addParameter("type", record.getType())
|
||||
.addParameter("eventId", record.getEventId())
|
||||
.executeUpdate();
|
||||
Query killQuery = con.createQuery(INSERT_KILL_QUERY, true);
|
||||
|
||||
for (LootRecord record : records)
|
||||
{
|
||||
killQuery
|
||||
.addParameter("accountId", accountId)
|
||||
.addParameter("type", record.getType())
|
||||
.addParameter("eventId", record.getEventId())
|
||||
.addToBatch();
|
||||
}
|
||||
|
||||
killQuery.executeBatch();
|
||||
Object[] keys = con.getKeys();
|
||||
|
||||
if (keys.length != records.size())
|
||||
{
|
||||
throw new RuntimeException("Mismatch in keys vs records size");
|
||||
}
|
||||
|
||||
Query insertDrop = con.createQuery(INSERT_DROP_QUERY);
|
||||
|
||||
// Append all queries for inserting drops
|
||||
for (GameItem drop : record.getDrops())
|
||||
int idx = 0;
|
||||
for (LootRecord record : records)
|
||||
{
|
||||
insertDrop
|
||||
.addParameter("itemId", drop.getId())
|
||||
.addParameter("itemQuantity", drop.getQty())
|
||||
.addToBatch();
|
||||
for (GameItem drop : record.getDrops())
|
||||
{
|
||||
insertDrop
|
||||
.addParameter("killId", keys[idx])
|
||||
.addParameter("itemId", drop.getId())
|
||||
.addParameter("itemQuantity", drop.getQty())
|
||||
.addToBatch();
|
||||
}
|
||||
|
||||
++idx;
|
||||
}
|
||||
|
||||
insertDrop.executeBatch();
|
||||
|
||||
Reference in New Issue
Block a user