Online reports can now be updated without re-sending all the data (ie we don't want to update all the chat messages when a member of staff handles a report).

Misc. changes and fixes.
This commit is contained in:
Keir 2015-12-16 15:51:01 +00:00
parent cd18d2a94f
commit db5327d2e4
4 changed files with 66 additions and 20 deletions

View File

@ -1,12 +1,18 @@
package mineplex.chatsnap;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.nio.file.Files;
import java.util.Arrays;
import java.util.Map;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import redis.clients.jedis.JedisPubSub;
/**
@ -16,7 +22,11 @@ public class JedisPubSubHandler extends JedisPubSub
{
private File _directory;
private Gson _gson = new Gson();
private Gson _gson = new GsonBuilder()
.setPrettyPrinting()
.create();
private JsonParser _jsonParser = new JsonParser();
public JedisPubSubHandler(File directory)
{
@ -35,15 +45,42 @@ public class JedisPubSubHandler extends JedisPubSub
{
if (channel.equals(ReportServer.CHANNEL_DEPLOY))
{
Map<String, Object> data = _gson.fromJson(dataString, Map.class);
String token = (String) data.get("token");
String json = dataString;
JsonObject jsonObject = _jsonParser.parse(json).getAsJsonObject();
String token = jsonObject.get("token").getAsString();
File target = new File(_directory, token + ".json");
System.out.println("Received ChatSnap, token = " + token + ", writing to file.");
if (target.exists() && !jsonObject.has("snapshots"))
{
try (BufferedReader bufferedReader = new BufferedReader(new FileReader(target)))
{
JsonObject originalJsonObject = _jsonParser.parse(bufferedReader).getAsJsonObject();
JsonObject usernamesObject = jsonObject.get("usernames").getAsJsonObject();
// retrieve snapshots from original file and add to jsonObject
jsonObject.add("snapshots", originalJsonObject.get("snapshots").getAsJsonArray());
// add new UUID->Usernames, update existing usernames
for (Map.Entry<String, JsonElement> entry : originalJsonObject.get("usernames").getAsJsonObject().entrySet())
{
usernamesObject.addProperty(entry.getKey(), entry.getValue().getAsJsonPrimitive().getAsString());
}
// re-write json after updating
json = _gson.toJson(jsonObject);
}
catch (Exception e)
{
System.out.println("Error updating original JSON file.");
e.printStackTrace();
}
}
try
{
Files.write(target.toPath(), Arrays.asList(dataString.split("\n")));
Files.write(target.toPath(), Arrays.asList(json.split("\n")));
}
catch (IOException e)
{

View File

@ -68,7 +68,7 @@ public class Report implements Data
_lastActivity = System.currentTimeMillis();
}
public boolean hasChatSnapToken()
public boolean hasToken()
{
return _token != null;
}
@ -79,7 +79,7 @@ public class Report implements Data
*
* @return the full token
*/
public String getUniqueToken()
public String getToken()
{
// since we don't always use this, only generate a token when we need it
if (_token == null)

View File

@ -124,7 +124,7 @@ public class ReportManager {
if (report.getCategory() == ReportCategory.CHAT_ABUSE) // only chat abuse reports have chat logs published
{
_snapshotManager.getPublisher().unpublishChatLog(report.getUniqueToken());
_snapshotManager.getPublisher().unpublishChatLog(report.getToken());
}
}
}
@ -140,6 +140,7 @@ public class ReportManager {
} else {
String handlerName = reportHandler.getName();
report.setHandler(reportHandler.getUniqueId());
publishChatSnap(report, false); // so handler is displayed on the web side
saveReport(report);
sendStaffNotification(F.main(getReportPrefix(reportId), String.format("%s is handling this report.", handlerName)));
@ -193,12 +194,14 @@ public class ReportManager {
}
incrementTotalStat(reporter.getName());
saveReport(report);
// only start notifying staff when
if (/*report.getReporters().size() >= category.getNotifyThreshold()*/true) // TODO REMVOE
{
publishChatAbuse(report);
if (report.getCategory() == ReportCategory.CHAT_ABUSE)
{
publishChatSnap(report, true);
}
int reportId = report.getReportId();
String prefix = getReportPrefix(reportId);
@ -229,30 +232,37 @@ public class ReportManager {
}
}
// save later so that token is saved (if created)
saveReport(report);
return report;
}
return null;
}
public void publishChatAbuse(Report report)
public void publishChatSnap(Report report, boolean updateChat)
{
SnapshotPublisher publisher = _snapshotManager.getPublisher();
Gson gson = SnapshotPublisher.getGson();
Set<Snapshot> snapshots = _snapshotManager.getSnapshots(report.getSuspect());
Set<UUID> uuids = getUUIDs(report);
uuids.addAll(publisher.getUUIDs(snapshots));
Map<UUID, String> usernameMap = publisher.getUsernameMap(uuids);
JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("timezone", SnapshotPublisher.getZoneId().getId());
jsonObject.add("generated", gson.toJsonTree(LocalDateTime.now()));
jsonObject.add("report", gson.toJsonTree(report));
jsonObject.add("snapshots", gson.toJsonTree(snapshots));
if (updateChat)
{
Set<Snapshot> snapshots = _snapshotManager.getSnapshots(report.getSuspect());
uuids.addAll(publisher.getUUIDs(snapshots));
jsonObject.add("snapshots", gson.toJsonTree(snapshots));
}
Map<UUID, String> usernameMap = publisher.getUsernameMap(uuids);
jsonObject.add("usernames", gson.toJsonTree(usernameMap));
publisher.publishChatLog(report.getUniqueToken(), jsonObject);
publisher.publishChatLog(report.getToken(), jsonObject);
}
private Set<UUID> getUUIDs(Report report)

View File

@ -6,7 +6,6 @@ import java.util.UUID;
import org.bukkit.Bukkit;
import org.bukkit.scheduler.BukkitRunnable;
import mineplex.core.chatsnap.SnapshotManager;
import mineplex.core.chatsnap.publishing.SnapshotPublisher;
import mineplex.core.common.jsonchat.ClickEvent;
import mineplex.core.common.jsonchat.JsonMessage;
@ -40,7 +39,6 @@ public class ReportHandlerMessageTask extends BukkitRunnable
if (_reportManager.isActiveReport(reportId))
{
Report report = _reportManager.getReport(reportId);
String suspectName = Bukkit.getOfflinePlayer(_report.getSuspect()).getName();
JsonMessage jsonMessage = new JsonMessage(BORDER
@ -51,12 +49,13 @@ public class ReportHandlerMessageTask extends BukkitRunnable
+ "\n\n"
+ C.cAqua + "Suspect: " + C.cGold + suspectName
+ "\n"
+ (_report.hasChatSnapToken() ? C.cAqua + "Chat Log: " + C.cGold + SnapshotPublisher.getURL(_report.getUniqueToken()) + "\n" : "")
+ (_report.hasToken() ? C.cAqua + "Chat Log: " + C.cGold + SnapshotPublisher.getURL(_report.getToken()) + "\n" : "")
+ "\n"
+ C.cAqua + "Type " + C.cGold + "/reportclose " + reportId + " <reason>" + C.cAqua + " to close this report."
+ "\n"
+ BORDER).click(ClickEvent.SUGGEST_COMMAND, "/reportclose " + reportId);
new ReportHandlerNotification(report, jsonMessage).publish();
new ReportHandlerNotification(_report, jsonMessage).publish();
}
else
{