diff --git a/Plugins/Mineplex.Core/src/mineplex/core/chatsnap/SnapshotMetadata.java b/Plugins/Mineplex.Core/src/mineplex/core/chatsnap/SnapshotMetadata.java index b85b706ab..d46cc2796 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/chatsnap/SnapshotMetadata.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/chatsnap/SnapshotMetadata.java @@ -8,14 +8,14 @@ public class SnapshotMetadata protected String _token = null; protected Integer _creatorId = null; - public SnapshotMetadata(int id, String token, int creatorId) + public SnapshotMetadata(int id, String token, Integer creatorId) { _id = id; _token = token; _creatorId = creatorId; } - public SnapshotMetadata(int id, int creatorId) + public SnapshotMetadata(int id, Integer creatorId) { _id = id; _creatorId = creatorId; diff --git a/Plugins/Mineplex.Core/src/mineplex/core/chatsnap/SnapshotRepository.java b/Plugins/Mineplex.Core/src/mineplex/core/chatsnap/SnapshotRepository.java index 2b055cca1..e353d8bba 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/chatsnap/SnapshotRepository.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/chatsnap/SnapshotRepository.java @@ -20,17 +20,24 @@ import mineplex.serverdata.database.DBPool; */ public class SnapshotRepository { + private static final String URL_PREFIX = "http://report.mineplex.com/chatsnap/view.php?id="; + public static String getURL(long reportId) { return URL_PREFIX + reportId; } - private static final String URL_PREFIX = "http://report.mineplex.com/chatsnap/view.php?id="; + public static String generateToken() + { + // TODO: implement + return null; + } - private static final String INSERT_SNAPSHOT = "INSERT INTO snapshots (creator) VALUES (?);"; + private static final String INSERT_SNAPSHOT = "INSERT INTO snapshots (token, 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 static final String GET_ID_FROM_TOKEN = "SELECT snapshots.id FROM snapshots WHERE snapshots.token = ?;"; private final String _serverName; private final Logger _logger; @@ -60,15 +67,15 @@ public class SnapshotRepository }); } - public CompletableFuture saveSnapshot(Collection messages) + public CompletableFuture saveSnapshot(Collection messages) { return CompletableFuture.supplyAsync(() -> { try (Connection connection = DBPool.getAccount().getConnection()) { - int snapshotId = createSnapshot(connection, null); - insertMessages(snapshotId, messages, connection); - return snapshotId; + SnapshotMetadata snapshotMetadata = createSnapshot(connection, null); + insertMessages(snapshotMetadata.getId(), messages, connection); + return snapshotMetadata; } catch (SQLException e) { @@ -77,7 +84,7 @@ public class SnapshotRepository }); } - public CompletableFuture createSnapshot(Integer creatorAccountId) + public CompletableFuture createSnapshot(Integer creatorAccountId) { return CompletableFuture.supplyAsync(() -> { @@ -92,17 +99,19 @@ public class SnapshotRepository }); } - private int createSnapshot(Connection connection, Integer creatorAccount) throws SQLException + private SnapshotMetadata createSnapshot(Connection connection, Integer creatorAccount) throws SQLException { + String token = getToken(connection); PreparedStatement insertSnapshotStatement = connection.prepareStatement(INSERT_SNAPSHOT, new String[]{"id"}); + insertSnapshotStatement.setString(1, token); // TODO: correct data type if (creatorAccount != null) { - insertSnapshotStatement.setInt(1, creatorAccount); + insertSnapshotStatement.setInt(2, creatorAccount); } else { - insertSnapshotStatement.setNull(1, Types.INTEGER); + insertSnapshotStatement.setNull(2, Types.INTEGER); } insertSnapshotStatement.execute(); @@ -111,7 +120,7 @@ public class SnapshotRepository { if (resultSet.next()) { - return resultSet.getInt(1); + return new SnapshotMetadata(resultSet.getInt(1), token, creatorAccount); } else { @@ -120,6 +129,40 @@ public class SnapshotRepository } } + private String getToken(Connection connection) throws SQLException + { + String token; + + do + { + token = generateToken(); + } + while(getByToken(connection, token).isPresent()); + + return token; + } + + private Optional getByToken(Connection connection, String token) throws SQLException + { + try (PreparedStatement statement = connection.prepareStatement(GET_ID_FROM_TOKEN)) + { + statement.setString(1, token); // TODO: correct data type + + try (ResultSet resultSet = statement.executeQuery()) + { + if (resultSet.next()) + { + int snapshotId = resultSet.getInt("id"); + return Optional.of(snapshotId); + } + else + { + return Optional.empty(); + } + } + } + } + private void insertMessages(int snapshotId, Collection messages, Connection connection) throws SQLException { try (PreparedStatement insertSnapshotStatement = connection.prepareStatement(INSERT_MESSAGE, new String[]{"id"})) diff --git a/Plugins/Mineplex.Core/src/mineplex/core/report/ReportManager.java b/Plugins/Mineplex.Core/src/mineplex/core/report/ReportManager.java index 4f5a2e263..e91ebfeed 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/report/ReportManager.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/report/ReportManager.java @@ -20,6 +20,7 @@ import org.bukkit.plugin.java.JavaPlugin; import mineplex.core.account.CoreClient; import mineplex.core.account.CoreClientManager; import mineplex.core.chatsnap.SnapshotManager; +import mineplex.core.chatsnap.SnapshotMetadata; import mineplex.core.chatsnap.command.PushSnapshotsCommand; import mineplex.core.chatsnap.command.PushSnapshotsHandler; import mineplex.core.command.CommandCenter; @@ -149,8 +150,8 @@ public class ReportManager // create snapshot id ahead of time if (category == ReportCategory.CHAT_ABUSE) { - int snapshotId = _snapshotManager.getSnapshotRepository().createSnapshot(null).join(); - report.setSnapshotId(snapshotId); + SnapshotMetadata snapshotMetadata = _snapshotManager.getSnapshotRepository().createSnapshot(null).join(); + report.setSnapshotId(snapshotMetadata.getId()); } saveReport(report).join(); diff --git a/Plugins/Mineplex.ReportSite/sql/snapshot-tokens.sql b/Plugins/Mineplex.ReportSite/sql/snapshot-tokens.sql index cca1ffe91..3dcb33f5d 100644 --- a/Plugins/Mineplex.ReportSite/sql/snapshot-tokens.sql +++ b/Plugins/Mineplex.ReportSite/sql/snapshot-tokens.sql @@ -1,3 +1,4 @@ ALTER TABLE Account.snapshots ADD token CHAR(6); +CREATE UNIQUE INDEX snapshots_token_uindex ON snapshots (token); ALTER TABLE Account.snapshots MODIFY COLUMN creator INT(11) AFTER token; \ No newline at end of file