De-couple chatsnap from report system
This commit is contained in:
parent
469a72afc0
commit
cd9423a8e4
@ -1,15 +1,21 @@
|
||||
package mineplex.core.chatsnap;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Set;
|
||||
import java.util.TreeSet;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.logging.Level;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
import com.google.common.cache.Cache;
|
||||
import com.google.common.cache.CacheBuilder;
|
||||
import mineplex.core.report.data.Report;
|
||||
|
||||
/**
|
||||
* Handles temporary storage of {@link Snapshot} instances.
|
||||
* Handles temporary storage of {@link SnapshotMessage} instances.
|
||||
*/
|
||||
public class SnapshotManager
|
||||
{
|
||||
@ -17,15 +23,17 @@ public class SnapshotManager
|
||||
// For an easy work around, we store values as the Key
|
||||
// For the value we just use some dummy object
|
||||
// I went with Boolean as it's the smallest data type
|
||||
private final Cache<Snapshot, Boolean> _snapshots = CacheBuilder.newBuilder()
|
||||
.concurrencyLevel(4)
|
||||
.expireAfterWrite(30, TimeUnit.MINUTES)
|
||||
.build();
|
||||
private final Cache<SnapshotMessage, Boolean> _snapshots = CacheBuilder.newBuilder()
|
||||
.concurrencyLevel(4)
|
||||
.expireAfterWrite(30, TimeUnit.MINUTES)
|
||||
.build();
|
||||
|
||||
private final JavaPlugin _javaPlugin;
|
||||
private final SnapshotRepository _snapshotRepository;
|
||||
|
||||
public SnapshotManager(SnapshotRepository snapshotRepository)
|
||||
public SnapshotManager(JavaPlugin javaPlugin, SnapshotRepository snapshotRepository)
|
||||
{
|
||||
_javaPlugin = javaPlugin;
|
||||
_snapshotRepository = snapshotRepository;
|
||||
}
|
||||
|
||||
@ -35,14 +43,14 @@ public class SnapshotManager
|
||||
}
|
||||
|
||||
/**
|
||||
* Keeps a snapshot in memory temporarily (30 minutes) and then discards it.
|
||||
* Keeps a message in memory temporarily (30 minutes) and then discards it.
|
||||
* During this time, other modules (such as the Report module) can access it for their own use.
|
||||
*
|
||||
* @param snapshot the snapshot to temporarily store
|
||||
* @param message the message to temporarily store
|
||||
*/
|
||||
public void cacheSnapshot(Snapshot snapshot)
|
||||
public void cacheSnapshot(SnapshotMessage message)
|
||||
{
|
||||
_snapshots.put(snapshot, true);
|
||||
_snapshots.put(message, true);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -51,25 +59,42 @@ public class SnapshotManager
|
||||
*
|
||||
* @return a set containing all snapshots
|
||||
*/
|
||||
public Set<Snapshot> getSnapshots()
|
||||
public Set<SnapshotMessage> getSnapshots()
|
||||
{
|
||||
// The compareTo method in Snapshot will ensure this in chronological order
|
||||
Set<Snapshot> snapshots = new TreeSet<>();
|
||||
snapshots.addAll(_snapshots.asMap().keySet());
|
||||
return snapshots;
|
||||
// The compareTo method in SnapshotMessage will ensure this in chronological order
|
||||
Set<SnapshotMessage> messages = new TreeSet<>();
|
||||
messages.addAll(_snapshots.asMap().keySet());
|
||||
return messages;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets all instances of {@link Snapshot} which involve a particular user.
|
||||
* Gets all instances of {@link SnapshotMessage} which involve a particular user.
|
||||
* The user may be the sender or recipient of a message.
|
||||
*
|
||||
* @param accountId the user to search for snaps involved in
|
||||
* @return the snaps that the user is involved in
|
||||
*/
|
||||
public Set<Snapshot> getSnapshots(int accountId)
|
||||
public Set<SnapshotMessage> getSnapshots(int accountId)
|
||||
{
|
||||
return _snapshots.asMap().keySet().stream()
|
||||
.filter(snapshot -> snapshot.getSenderId() == accountId || snapshot.getRecipientIds().contains(accountId))
|
||||
.collect(Collectors.toCollection(TreeSet::new));
|
||||
}
|
||||
|
||||
public CompletableFuture<Integer> saveReportSnapshot(Report report, Collection<SnapshotMessage> messages)
|
||||
{
|
||||
return _snapshotRepository
|
||||
.saveReportSnapshot(report, messages)
|
||||
.whenComplete((snapshotId, throwable) ->
|
||||
{
|
||||
if (throwable == null)
|
||||
{
|
||||
report.setSnapshotId(snapshotId);
|
||||
}
|
||||
else
|
||||
{
|
||||
_javaPlugin.getLogger().log(Level.SEVERE, "Error whilst saving snapshot.", throwable);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
@ -15,7 +15,7 @@ import static com.google.common.base.Preconditions.checkNotNull;
|
||||
/**
|
||||
* Represents a message sent by a player.
|
||||
*/
|
||||
public class Snapshot implements Comparable<Snapshot>
|
||||
public class SnapshotMessage implements Comparable<SnapshotMessage>
|
||||
{
|
||||
protected Long _id = null;
|
||||
private final MessageType _messageType;
|
||||
@ -23,18 +23,19 @@ public class Snapshot implements Comparable<Snapshot>
|
||||
private final Collection<Integer> _recipientIds;
|
||||
private final String _message;
|
||||
private final LocalDateTime _time;
|
||||
private final Set<Integer> linkedSnapshots = new HashSet<>();
|
||||
|
||||
public Snapshot(int senderId, int recipientId, String message)
|
||||
public SnapshotMessage(int senderId, int recipientId, String message)
|
||||
{
|
||||
this(MessageType.PM, senderId, Collections.singletonList(recipientId), message, LocalDateTime.now());
|
||||
}
|
||||
|
||||
public Snapshot(MessageType messageType, int senderId, Collection<Integer> recipientIds, String message)
|
||||
public SnapshotMessage(MessageType messageType, int senderId, Collection<Integer> recipientIds, String message)
|
||||
{
|
||||
this(messageType, senderId, recipientIds, message, LocalDateTime.now());
|
||||
}
|
||||
|
||||
public Snapshot(MessageType messageType, int senderId, Collection<Integer> recipientIds, String message, LocalDateTime time)
|
||||
public SnapshotMessage(MessageType messageType, int senderId, Collection<Integer> recipientIds, String message, LocalDateTime time)
|
||||
{
|
||||
_messageType = messageType;
|
||||
_senderId = checkNotNull(senderId);
|
||||
@ -44,7 +45,7 @@ public class Snapshot implements Comparable<Snapshot>
|
||||
|
||||
if (messageType == MessageType.PM && recipientIds.size() > 1)
|
||||
{
|
||||
throw new IllegalArgumentException("Snapshot type PM may not have more than 1 recipient.");
|
||||
throw new IllegalArgumentException("SnapshotMessage type PM may not have more than 1 recipient.");
|
||||
}
|
||||
}
|
||||
|
||||
@ -78,8 +79,18 @@ public class Snapshot implements Comparable<Snapshot>
|
||||
return _time;
|
||||
}
|
||||
|
||||
public Set<Integer> getLinkedSnapshots()
|
||||
{
|
||||
return linkedSnapshots;
|
||||
}
|
||||
|
||||
public void addLinkedSnapshot(int snapshotId)
|
||||
{
|
||||
linkedSnapshots.add(snapshotId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int compareTo(Snapshot o)
|
||||
public int compareTo(SnapshotMessage o)
|
||||
{
|
||||
return getSentTime().compareTo(o.getSentTime());
|
||||
}
|
||||
@ -89,7 +100,7 @@ public class Snapshot implements Comparable<Snapshot>
|
||||
{
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
Snapshot that = (Snapshot) o;
|
||||
SnapshotMessage that = (SnapshotMessage) o;
|
||||
return _time == that._time &&
|
||||
Objects.equals(_senderId, that._senderId) &&
|
||||
Objects.equals(_recipientIds, that._recipientIds) &&
|
||||
@ -105,7 +116,7 @@ public class Snapshot implements Comparable<Snapshot>
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
return "Snapshot{" +
|
||||
return "SnapshotMessage{" +
|
||||
"sender=" + _senderId +
|
||||
", recipients=" + _recipientIds +
|
||||
", message='" + ChatColor.stripColor(_message) + '\'' +
|
@ -56,7 +56,7 @@ public class SnapshotPlugin extends MiniPlugin
|
||||
return players.stream().map(_clientManager::getAccountId).collect(Collectors.toSet());
|
||||
}
|
||||
|
||||
public Snapshot createSnapshot(AsyncPlayerChatEvent e)
|
||||
public SnapshotMessage createSnapshot(AsyncPlayerChatEvent e)
|
||||
{
|
||||
MessageType messageType = MessageType.CHAT;
|
||||
int senderId = _clientManager.getAccountId(e.getPlayer());
|
||||
@ -68,14 +68,14 @@ public class SnapshotPlugin extends MiniPlugin
|
||||
messageType = MessageType.PARTY;
|
||||
}
|
||||
|
||||
return new Snapshot(messageType, senderId, recipientIds, e.getMessage());
|
||||
return new SnapshotMessage(messageType, senderId, recipientIds, e.getMessage());
|
||||
}
|
||||
|
||||
public Snapshot createSnapshot(PrivateMessageEvent e)
|
||||
public SnapshotMessage createSnapshot(PrivateMessageEvent e)
|
||||
{
|
||||
int senderId = _clientManager.getAccountId(e.getSender());
|
||||
int recipientId = _clientManager.getAccountId(e.getRecipient());
|
||||
String message = e.getMessage();
|
||||
return new Snapshot(senderId, recipientId, message);
|
||||
return new SnapshotMessage(senderId, recipientId, message);
|
||||
}
|
||||
}
|
||||
|
@ -4,11 +4,15 @@ import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Types;
|
||||
import java.util.Collection;
|
||||
import java.util.Optional;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import mineplex.core.common.util.UtilTime;
|
||||
import mineplex.core.report.data.Report;
|
||||
import mineplex.serverdata.database.DBPool;
|
||||
|
||||
/**
|
||||
@ -23,104 +27,205 @@ public class SnapshotRepository
|
||||
|
||||
private static final String URL_PREFIX = "http://report.mineplex.com/chatsnap/view.php?id=";
|
||||
|
||||
private static final String INSERT_SNAPSHOT = "INSERT INTO snapshots (senderId, `server`, `time`, message, snapshotType) VALUES (?, ?, ?, ?, ?);";
|
||||
private static final String INSERT_SNAPSHOT_RECIPIENT = "INSERT INTO snapshotRecipients (snapshotId, recipientId) VALUES (?, ?);";
|
||||
private static final String INSERT_REPORT_SNAPSHOT_MAPPING = "INSERT IGNORE INTO reportSnapshots (reportId, snapshotId) VALUES (?, ?);";
|
||||
private static final String INSERT_SNAPSHOT = "INSERT INTO snapshots (creator) VALUES (?);";
|
||||
private static final String INSERT_MESSAGE = "INSERT INTO snapshotMessages (senderId, `server`, `time`, message, snapshotType) VALUES (?, ?, ?, ?, ?);";
|
||||
private static final String INSERT_MESSAGE_RECIPIENT = "INSERT INTO snapshotRecipients (messageId, recipientId) VALUES (?, ?);";
|
||||
private static final String INSERT_MESSAGE_MAPPING = "INSERT INTO snapshotMessageMap (snapshotId, messageId) VALUES (?, ?);";
|
||||
|
||||
private final String _serverName;
|
||||
private final Logger _logger;
|
||||
|
||||
public SnapshotRepository(String serverName)
|
||||
public SnapshotRepository(String serverName, Logger logger)
|
||||
{
|
||||
_serverName = serverName;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public CompletableFuture<Void> insertSnapshots(long reportId, Collection<Snapshot> snapshots)
|
||||
{
|
||||
return CompletableFuture.supplyAsync(() ->
|
||||
{
|
||||
|
||||
try (Connection connection = DBPool.getAccount().getConnection())
|
||||
{
|
||||
PreparedStatement insertSnapshotStatement = connection.prepareStatement(INSERT_SNAPSHOT, new String[]{"id"});
|
||||
|
||||
for (Snapshot snapshot : snapshots)
|
||||
{
|
||||
insertSnapshot(connection, insertSnapshotStatement, reportId, snapshot);
|
||||
}
|
||||
|
||||
insertSnapshotStatement.close();
|
||||
}
|
||||
catch (SQLException e)
|
||||
{
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
return null;
|
||||
});
|
||||
}
|
||||
|
||||
public CompletableFuture<Void> insertSnapshot(long reportId, Snapshot snapshot)
|
||||
public CompletableFuture<Integer> saveReportSnapshot(Report report, Collection<SnapshotMessage> messages)
|
||||
{
|
||||
return CompletableFuture.supplyAsync(() ->
|
||||
{
|
||||
try (Connection connection = DBPool.getAccount().getConnection())
|
||||
{
|
||||
PreparedStatement insertSnapshotStatement = connection.prepareStatement(INSERT_SNAPSHOT, new String[]{"id"});
|
||||
insertSnapshot(connection, insertSnapshotStatement, reportId, snapshot);
|
||||
insertSnapshotStatement.close();
|
||||
int snapshotId = report.getSnapshotId().orElseThrow(() ->
|
||||
new IllegalStateException("Report does not have associated snapshot id."));
|
||||
|
||||
insertMessages(snapshotId, messages, connection);
|
||||
return snapshotId;
|
||||
}
|
||||
catch (SQLException e)
|
||||
{
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
return null;
|
||||
});
|
||||
}
|
||||
|
||||
// this allows snapshots to be inserted in batch
|
||||
private void insertSnapshot(Connection connection, PreparedStatement insertSnapshotStatement, long reportId, Snapshot snapshot) throws SQLException
|
||||
public CompletableFuture<Integer> saveSnapshot(Collection<SnapshotMessage> messages)
|
||||
{
|
||||
Optional<Long> snapshotIdOptional = snapshot.getId();
|
||||
long snapshotId;
|
||||
|
||||
if (!snapshotIdOptional.isPresent())
|
||||
return CompletableFuture.supplyAsync(() ->
|
||||
{
|
||||
insertSnapshotStatement.setInt(1, snapshot.getSenderId());
|
||||
insertSnapshotStatement.setString(2, _serverName);
|
||||
insertSnapshotStatement.setTimestamp(3, UtilTime.toTimestamp(snapshot.getSentTime()));
|
||||
insertSnapshotStatement.setString(4, snapshot.getMessage());
|
||||
insertSnapshotStatement.setInt(5, snapshot.getType().getId());
|
||||
insertSnapshotStatement.execute();
|
||||
|
||||
ResultSet resultSet = insertSnapshotStatement.getGeneratedKeys();
|
||||
if (resultSet.next())
|
||||
try (Connection connection = DBPool.getAccount().getConnection())
|
||||
{
|
||||
snapshotId = resultSet.getLong(1);
|
||||
snapshot._id = snapshotId;
|
||||
int snapshotId = createSnapshot(connection, null);
|
||||
insertMessages(snapshotId, messages, connection);
|
||||
return snapshotId;
|
||||
}
|
||||
else
|
||||
catch (SQLException e)
|
||||
{
|
||||
throw new IllegalStateException("Query did not return a message id (we need one).");
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
insertRecipients(connection, snapshotId, snapshot.getRecipientIds());
|
||||
public CompletableFuture<Integer> createSnapshot(Integer creatorAccountId)
|
||||
{
|
||||
return CompletableFuture.supplyAsync(() ->
|
||||
{
|
||||
try (Connection connection = DBPool.getAccount().getConnection())
|
||||
{
|
||||
return createSnapshot(connection, creatorAccountId);
|
||||
}
|
||||
catch (SQLException e)
|
||||
{
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private int createSnapshot(Connection connection, Integer creatorAccount) throws SQLException
|
||||
{
|
||||
PreparedStatement insertSnapshotStatement = connection.prepareStatement(INSERT_SNAPSHOT, new String[]{"id"});
|
||||
|
||||
if (creatorAccount != null)
|
||||
{
|
||||
insertSnapshotStatement.setInt(1, creatorAccount);
|
||||
}
|
||||
else
|
||||
{
|
||||
snapshotId = snapshotIdOptional.get();
|
||||
insertSnapshotStatement.setNull(1, Types.INTEGER);
|
||||
}
|
||||
|
||||
insertReportMapping(connection, reportId, snapshotId);
|
||||
insertSnapshotStatement.execute();
|
||||
|
||||
try (ResultSet resultSet = insertSnapshotStatement.getGeneratedKeys())
|
||||
{
|
||||
if (resultSet.next())
|
||||
{
|
||||
return resultSet.getInt(1);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new IllegalStateException("Query did not return a snapshot id.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void insertRecipients(Connection connection, long snapshotId, Collection<Integer> recipients) throws SQLException
|
||||
private void insertMessages(int snapshotId, Collection<SnapshotMessage> messages, Connection connection) throws SQLException
|
||||
{
|
||||
PreparedStatement insertRecipientStatement = connection.prepareStatement(INSERT_SNAPSHOT_RECIPIENT);
|
||||
try (PreparedStatement insertSnapshotStatement = connection.prepareStatement(INSERT_MESSAGE, new String[]{"id"}))
|
||||
{
|
||||
try (PreparedStatement insertRecipientStatement = connection.prepareStatement(INSERT_MESSAGE_RECIPIENT))
|
||||
{
|
||||
try (PreparedStatement insertMappingStatement = connection.prepareStatement(INSERT_MESSAGE_MAPPING))
|
||||
{
|
||||
for (SnapshotMessage message : messages)
|
||||
{
|
||||
try
|
||||
{
|
||||
insertMessage(insertSnapshotStatement, insertRecipientStatement, insertMappingStatement, snapshotId, message);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_logger.log(Level.SEVERE, "Error inserting snapshot message.", e);
|
||||
}
|
||||
}
|
||||
|
||||
insertRecipientStatement.executeBatch();
|
||||
insertMappingStatement.executeBatch();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public CompletableFuture<Void> insertMessage(int snapshotId, SnapshotMessage message)
|
||||
{
|
||||
return CompletableFuture.supplyAsync(() ->
|
||||
{
|
||||
try (Connection connection = DBPool.getAccount().getConnection())
|
||||
{
|
||||
try (PreparedStatement insertSnapshotStatement = connection.prepareStatement(INSERT_MESSAGE, new String[]{"id"}))
|
||||
{
|
||||
try (PreparedStatement insertRecipientStatement = connection.prepareStatement(INSERT_MESSAGE_RECIPIENT))
|
||||
{
|
||||
try (PreparedStatement insertMappingStatement = connection.prepareStatement(INSERT_MESSAGE_MAPPING))
|
||||
{
|
||||
insertMessage(insertSnapshotStatement, insertRecipientStatement, insertMappingStatement, snapshotId, message);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (SQLException e)
|
||||
{
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
return null;
|
||||
});
|
||||
}
|
||||
|
||||
private void insertMessage(PreparedStatement insertSnapshotStatement, PreparedStatement insertRecipientStatement, PreparedStatement insertMappingStatement, int snapshotId, SnapshotMessage message) throws SQLException
|
||||
{
|
||||
boolean freshInsert = insertMessage(insertSnapshotStatement, message);
|
||||
|
||||
long messageId = message.getId().orElseThrow(() ->
|
||||
new IllegalStateException("Message id not present (perhaps insert failed?)."));
|
||||
|
||||
if (freshInsert)
|
||||
{
|
||||
insertRecipients(insertRecipientStatement, messageId, message.getRecipientIds());
|
||||
}
|
||||
|
||||
if (!message.getLinkedSnapshots().contains(snapshotId))
|
||||
{
|
||||
insertMessageMapping(insertMappingStatement, snapshotId, messageId);
|
||||
}
|
||||
}
|
||||
|
||||
private boolean insertMessage(PreparedStatement insertSnapshotStatement, SnapshotMessage message) throws SQLException
|
||||
{
|
||||
Optional<Long> messageIdOptional = message.getId();
|
||||
boolean freshInsert = !messageIdOptional.isPresent();
|
||||
|
||||
if (freshInsert)
|
||||
{
|
||||
insertSnapshotStatement.setInt(1, message.getSenderId());
|
||||
insertSnapshotStatement.setString(2, _serverName);
|
||||
insertSnapshotStatement.setTimestamp(3, UtilTime.toTimestamp(message.getSentTime()));
|
||||
insertSnapshotStatement.setString(4, message.getMessage());
|
||||
insertSnapshotStatement.setInt(5, message.getType().getId());
|
||||
insertSnapshotStatement.execute();
|
||||
|
||||
try (ResultSet resultSet = insertSnapshotStatement.getGeneratedKeys())
|
||||
{
|
||||
if (resultSet.next())
|
||||
{
|
||||
message._id = resultSet.getLong(1);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new IllegalStateException("Query did not return a message id.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return freshInsert;
|
||||
}
|
||||
|
||||
private void insertRecipients(PreparedStatement insertRecipientStatement, long messageId, Collection<Integer> recipients) throws SQLException
|
||||
{
|
||||
for (int recipientId : recipients)
|
||||
{
|
||||
insertRecipientStatement.setLong(1, snapshotId);
|
||||
insertRecipientStatement.setLong(1, messageId);
|
||||
insertRecipientStatement.setInt(2, recipientId);
|
||||
insertRecipientStatement.addBatch();
|
||||
}
|
||||
@ -128,11 +233,10 @@ public class SnapshotRepository
|
||||
insertRecipientStatement.executeBatch();
|
||||
}
|
||||
|
||||
private void insertReportMapping(Connection connection, long reportId, long messageId) throws SQLException
|
||||
private void insertMessageMapping(PreparedStatement insertMappingStatement, int snapshotId, long messageId) throws SQLException
|
||||
{
|
||||
PreparedStatement reportSnapshotMapping = connection.prepareStatement(INSERT_REPORT_SNAPSHOT_MAPPING);
|
||||
reportSnapshotMapping.setLong(1, reportId);
|
||||
reportSnapshotMapping.setLong(2, messageId);
|
||||
reportSnapshotMapping.execute();
|
||||
insertMappingStatement.setInt(1, snapshotId);
|
||||
insertMappingStatement.setLong(2, messageId);
|
||||
insertMappingStatement.execute();
|
||||
}
|
||||
}
|
||||
|
@ -1,11 +1,9 @@
|
||||
package mineplex.core.chatsnap.command;
|
||||
|
||||
import java.util.Set;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import mineplex.core.chatsnap.Snapshot;
|
||||
import mineplex.core.chatsnap.SnapshotMessage;
|
||||
import mineplex.core.chatsnap.SnapshotManager;
|
||||
import mineplex.core.report.ReportManager;
|
||||
import mineplex.serverdata.commands.CommandCallback;
|
||||
import mineplex.serverdata.commands.ServerCommand;
|
||||
|
||||
@ -14,13 +12,13 @@ import mineplex.serverdata.commands.ServerCommand;
|
||||
*/
|
||||
public class PushSnapshotsHandler implements CommandCallback
|
||||
{
|
||||
private final ReportManager _reportManager;
|
||||
private final SnapshotManager _snapshotManager;
|
||||
private final Logger _logger;
|
||||
|
||||
public PushSnapshotsHandler(SnapshotManager snapshotManager, Logger logger)
|
||||
public PushSnapshotsHandler(ReportManager reportManager, SnapshotManager snapshotManager)
|
||||
{
|
||||
_reportManager = reportManager;
|
||||
_snapshotManager = snapshotManager;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -31,15 +29,12 @@ public class PushSnapshotsHandler implements CommandCallback
|
||||
PushSnapshotsCommand pushCommand = (PushSnapshotsCommand) command;
|
||||
int accountId = pushCommand.getAccountId();
|
||||
long reportId = pushCommand.getReportId();
|
||||
Set<Snapshot> snapshots = _snapshotManager.getSnapshots(accountId);
|
||||
Set<SnapshotMessage> messages = _snapshotManager.getSnapshots(accountId);
|
||||
|
||||
if (snapshots.size() > 0)
|
||||
if (messages.size() > 0)
|
||||
{
|
||||
_snapshotManager.getSnapshotRepository().insertSnapshots(reportId, snapshots)
|
||||
.exceptionally(throwable -> {
|
||||
_logger.log(Level.SEVERE, "Error whilst inserting snapshots into database (" + reportId + ").", throwable);
|
||||
return null;
|
||||
});
|
||||
_reportManager.getReportRepository().getReport(reportId).thenCompose(report ->
|
||||
_snapshotManager.saveReportSnapshot(report, messages));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -47,11 +47,12 @@ public class ReportManager
|
||||
private static final String NAME = "Report";
|
||||
private static final int ABUSE_BAN_THRESHOLD = 3;
|
||||
|
||||
private JavaPlugin _plugin;
|
||||
private CoreClientManager _clientManager;
|
||||
private IncognitoManager _incognitoManager;
|
||||
private String _serverName;
|
||||
private int _serverWeight;
|
||||
private final JavaPlugin _plugin;
|
||||
private final SnapshotManager _snapshotManager;
|
||||
private final CoreClientManager _clientManager;
|
||||
private final IncognitoManager _incognitoManager;
|
||||
private final String _serverName;
|
||||
private final int _serverWeight;
|
||||
|
||||
private final ReportRepository _reportRepository;
|
||||
private final ReportProfileRepository _reportProfileRepository;
|
||||
@ -60,6 +61,7 @@ public class ReportManager
|
||||
IncognitoManager incognitoManager, String serverName, int serverWeight)
|
||||
{
|
||||
_plugin = plugin;
|
||||
_snapshotManager = snapshotManager;
|
||||
_clientManager = clientManager;
|
||||
_incognitoManager = incognitoManager;
|
||||
_serverName = serverName;
|
||||
@ -71,7 +73,7 @@ public class ReportManager
|
||||
|
||||
ServerCommandManager commandManager = ServerCommandManager.getInstance();
|
||||
ReportRedisManager notificationCallback = new ReportRedisManager(this, _serverName);
|
||||
PushSnapshotsHandler pushHandler = new PushSnapshotsHandler(snapshotManager, _plugin.getLogger());
|
||||
PushSnapshotsHandler pushHandler = new PushSnapshotsHandler(this, snapshotManager);
|
||||
commandManager.registerCommandType("HandlerNotification", HandlerNotification.class, notificationCallback);
|
||||
commandManager.registerCommandType("ReportersNotification", ReportersNotification.class, notificationCallback);
|
||||
commandManager.registerCommandType("PushSnapshotsCommand", PushSnapshotsCommand.class, pushHandler);
|
||||
@ -101,9 +103,11 @@ public class ReportManager
|
||||
int reporterId = _clientManager.Get(reporter).getAccountId();
|
||||
int suspectId = _clientManager.Get(suspect).getAccountId();
|
||||
|
||||
return _reportRepository.getOngoingReport(suspectId, category)
|
||||
CompletableFuture<Report> future = _reportRepository.getOngoingReport(suspectId, category)
|
||||
.thenCompose(reportIdOptional ->
|
||||
reportIdOptional.isPresent() ? _reportRepository.getReport(reportIdOptional.get()) : CompletableFuture.completedFuture(new Report(suspectId, category))
|
||||
reportIdOptional.isPresent() ? _reportRepository
|
||||
.getReport(reportIdOptional.get()) : CompletableFuture
|
||||
.completedFuture(new Report(suspectId, category))
|
||||
).whenComplete((report, throwable) ->
|
||||
{
|
||||
if (report != null)
|
||||
@ -120,9 +124,18 @@ public class ReportManager
|
||||
report.addReportReason(reportMessage);
|
||||
}
|
||||
|
||||
// create snapshot id ahead of time
|
||||
if (category == ReportCategory.CHAT_ABUSE)
|
||||
{
|
||||
int snapshotId = _snapshotManager.getSnapshotRepository().createSnapshot(null).join();
|
||||
report.setSnapshotId(snapshotId);
|
||||
}
|
||||
|
||||
saveReport(report).join();
|
||||
}
|
||||
});
|
||||
|
||||
return future;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -322,8 +335,8 @@ public class ReportManager
|
||||
{
|
||||
future.thenAccept(reportId ->
|
||||
{
|
||||
PushSnapshotsCommand pushCommand = new PushSnapshotsCommand(report.getSuspectId(), reportId);
|
||||
pushCommand.publish();
|
||||
PushSnapshotsCommand command = new PushSnapshotsCommand(report.getSuspectId(), reportId);
|
||||
command.publish();
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -20,6 +20,7 @@ public class Report
|
||||
// set of player account ids and the reason they reported this player
|
||||
private final Map<Integer, ReportMessage> _reportMessages = new HashMap<>();
|
||||
private Integer _handlerId = null;
|
||||
private Integer _snapshotId = null;
|
||||
private ReportResult _reportResult = null;
|
||||
|
||||
private ReportHandlerTask _handlerTask = null;
|
||||
@ -46,6 +47,11 @@ public class Report
|
||||
return _suspectId;
|
||||
}
|
||||
|
||||
public ReportCategory getCategory()
|
||||
{
|
||||
return _category;
|
||||
}
|
||||
|
||||
public Map<Integer, ReportMessage> getMessages()
|
||||
{
|
||||
return _reportMessages;
|
||||
@ -81,9 +87,14 @@ public class Report
|
||||
_handlerId = handlerId;
|
||||
}
|
||||
|
||||
public ReportCategory getCategory()
|
||||
public Optional<Integer> getSnapshotId()
|
||||
{
|
||||
return _category;
|
||||
return Optional.ofNullable(_snapshotId);
|
||||
}
|
||||
|
||||
public void setSnapshotId(Integer snapshotId)
|
||||
{
|
||||
_snapshotId = snapshotId;
|
||||
}
|
||||
|
||||
public Optional<ReportResult> getResult()
|
||||
|
@ -5,6 +5,7 @@ import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Timestamp;
|
||||
import java.sql.Types;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
@ -21,7 +22,6 @@ import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
|
||||
@ -29,9 +29,7 @@ import com.google.common.cache.Cache;
|
||||
import com.google.common.cache.CacheBuilder;
|
||||
import com.google.common.collect.HashMultimap;
|
||||
import com.google.common.collect.Multimap;
|
||||
import com.google.common.collect.TreeMultimap;
|
||||
import com.mysql.jdbc.Statement;
|
||||
import mineplex.core.common.util.UtilFuture;
|
||||
import mineplex.core.common.util.UtilTime;
|
||||
import mineplex.core.report.ReportCategory;
|
||||
import mineplex.core.report.ReportManager;
|
||||
@ -46,7 +44,9 @@ import org.apache.commons.lang3.StringUtils;
|
||||
*/
|
||||
public class ReportRepository
|
||||
{
|
||||
private static final String INSERT_REPORT = "INSERT INTO reports (suspectId, categoryId) VALUES (?, ?);";
|
||||
private static final String INSERT_REPORT = "INSERT INTO reports (suspectId, categoryId, snapshotId)\n" +
|
||||
"VALUES (?, ?, ?)\n" +
|
||||
"ON DUPLICATE KEY UPDATE snapshotId = ?;";
|
||||
|
||||
private static final String SET_REPORT_MESSAGE = "REPLACE INTO reportReasons (reportId, reporterId, reason, `server`, weight, `time`)" +
|
||||
" VALUES (?, ?, ?, ?, ?, ?);";
|
||||
@ -302,6 +302,13 @@ public class ReportRepository
|
||||
ReportCategory reportCategory = ReportCategory.getById(resultSet.getInt("categoryId"));
|
||||
|
||||
Report report = new Report(reportId, suspectId, reportCategory);
|
||||
|
||||
int snapshotId = resultSet.getInt("snapshotId");
|
||||
if (!resultSet.wasNull())
|
||||
{
|
||||
report.setSnapshotId(snapshotId);
|
||||
}
|
||||
|
||||
int handlerId = resultSet.getInt("handlerId");
|
||||
if (!resultSet.wasNull())
|
||||
{
|
||||
@ -473,32 +480,46 @@ public class ReportRepository
|
||||
{
|
||||
try (Connection connection = DBPool.getAccount().getConnection())
|
||||
{
|
||||
long reportId = report.getId().orElseGet(() ->
|
||||
{
|
||||
try
|
||||
{
|
||||
PreparedStatement preparedStatement = connection.prepareStatement(INSERT_REPORT, Statement.RETURN_GENERATED_KEYS);
|
||||
preparedStatement.setInt(1, report.getSuspectId());
|
||||
preparedStatement.setInt(2, report.getCategory().getId());
|
||||
preparedStatement.executeUpdate();
|
||||
PreparedStatement insertUpdateReportStatement = connection.prepareStatement(INSERT_REPORT, Statement.RETURN_GENERATED_KEYS);
|
||||
insertUpdateReportStatement.setInt(1, report.getSuspectId());
|
||||
insertUpdateReportStatement.setInt(2, report.getCategory().getId());
|
||||
|
||||
ResultSet resultSet = preparedStatement.getGeneratedKeys();
|
||||
if (resultSet.next())
|
||||
{
|
||||
long id = resultSet.getLong(1);
|
||||
report._reportId = id;
|
||||
return id;
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new IllegalStateException("Query did not return a report id (we need one).");
|
||||
}
|
||||
}
|
||||
catch (SQLException e)
|
||||
Optional<Integer> snapshotIdOptional = report.getSnapshotId();
|
||||
|
||||
if (snapshotIdOptional.isPresent())
|
||||
{
|
||||
int snapshotId = snapshotIdOptional.get();
|
||||
insertUpdateReportStatement.setInt(3, snapshotId);
|
||||
insertUpdateReportStatement.setInt(4, snapshotId);
|
||||
}
|
||||
else
|
||||
{
|
||||
insertUpdateReportStatement.setNull(3, Types.INTEGER);
|
||||
insertUpdateReportStatement.setNull(4, Types.INTEGER);
|
||||
}
|
||||
|
||||
insertUpdateReportStatement.executeUpdate();
|
||||
|
||||
Optional<Long> reportIdOptional = report.getId();
|
||||
long reportId;
|
||||
|
||||
if (reportIdOptional.isPresent())
|
||||
{
|
||||
reportId = reportIdOptional.get();
|
||||
}
|
||||
else
|
||||
{
|
||||
ResultSet resultSet = insertUpdateReportStatement.getGeneratedKeys();
|
||||
if (resultSet.next())
|
||||
{
|
||||
throw new RuntimeException(e);
|
||||
reportId = resultSet.getLong(1);
|
||||
report._reportId = reportId;
|
||||
}
|
||||
});
|
||||
else
|
||||
{
|
||||
throw new IllegalStateException("Query did not return a report id (we need one).");
|
||||
}
|
||||
}
|
||||
|
||||
PreparedStatement setReportMessageStatement = connection.prepareStatement(SET_REPORT_MESSAGE);
|
||||
|
||||
|
@ -45,6 +45,8 @@ import mineplex.core.portal.Portal;
|
||||
import mineplex.core.preferences.PreferencesManager;
|
||||
import mineplex.core.punish.Punish;
|
||||
import mineplex.core.recharge.Recharge;
|
||||
import mineplex.core.report.ReportManager;
|
||||
import mineplex.core.report.ReportPlugin;
|
||||
import mineplex.core.resourcepack.ResourcePackManager;
|
||||
import mineplex.core.serverConfig.ServerConfiguration;
|
||||
import mineplex.core.spawn.Spawn;
|
||||
@ -231,7 +233,7 @@ public class Clans extends JavaPlugin
|
||||
Pair.create(MinecraftVersion.Version1_9, "http://file.mineplex.com/ResClans19.zip")
|
||||
}, true);
|
||||
|
||||
SnapshotManager snapshotManager = new SnapshotManager(new SnapshotRepository(serverStatusManager.getCurrentServerName()));
|
||||
SnapshotManager snapshotManager = new SnapshotManager(this, new SnapshotRepository(serverStatusManager.getCurrentServerName(), getLogger()));
|
||||
new SnapshotPlugin(this, snapshotManager, _clientManager);
|
||||
new ReportPlugin(this, new ReportManager(this, snapshotManager, _clientManager, incognito, serverStatusManager.getCurrentServerName(), 1));
|
||||
|
||||
|
@ -185,7 +185,7 @@ public class Hub extends JavaPlugin implements IRelation
|
||||
new GlobalPacketManager(this, clientManager, serverStatusManager, inventoryManager, donationManager, petManager, statsManager, hubManager.getBonusManager().getRewardManager());
|
||||
//new Replay(this, packetHandler);
|
||||
|
||||
SnapshotManager snapshotManager = new SnapshotManager(new SnapshotRepository(serverStatusManager.getCurrentServerName()));
|
||||
SnapshotManager snapshotManager = new SnapshotManager(this, new SnapshotRepository(serverStatusManager.getCurrentServerName(), getLogger()));
|
||||
ReportManager reportManager = new ReportManager(this, snapshotManager, clientManager, incognito, serverStatusManager.getCurrentServerName(), 3);
|
||||
new SnapshotPlugin(this, snapshotManager, clientManager);
|
||||
new ReportPlugin(this, reportManager);
|
||||
|
@ -162,7 +162,7 @@ public class Arcade extends JavaPlugin
|
||||
Chat chat = new Chat(this, incognito, _clientManager, preferenceManager, achievementManager, serverStatusManager.getCurrentServerName());
|
||||
new MessageManager(this, incognito, _clientManager, preferenceManager, ignoreManager, punish, friendManager, chat);
|
||||
|
||||
SnapshotManager snapshotManager = new SnapshotManager(new SnapshotRepository(serverStatusManager.getCurrentServerName()));
|
||||
SnapshotManager snapshotManager = new SnapshotManager(this, new SnapshotRepository(serverStatusManager.getCurrentServerName(), getLogger()));
|
||||
ReportManager reportManager = new ReportManager(this, snapshotManager, _clientManager, incognito, serverStatusManager.getCurrentServerName(), 1);
|
||||
new SnapshotPlugin(this, snapshotManager, _clientManager);
|
||||
new ReportPlugin(this, reportManager);
|
||||
|
Loading…
Reference in New Issue
Block a user