Correctly handle weird cases whereby an account may be involved in

multiple open reports of the same category
This commit is contained in:
Keir Nellyer 2016-09-14 14:26:12 +01:00
parent 2be0522599
commit 464099dc87
2 changed files with 42 additions and 35 deletions

View File

@ -1,6 +1,7 @@
package mineplex.core.report; package mineplex.core.report;
import java.util.Collection; import java.util.Collection;
import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Optional; import java.util.Optional;
@ -126,37 +127,42 @@ public class ReportManager
public CompletableFuture<Report> createReport(int reporterId, int suspectId, ReportCategory category, String message) public CompletableFuture<Report> createReport(int reporterId, int suspectId, ReportCategory category, String message)
{ {
// TODO only allow report if suspect actually did something // TODO only allow report if suspect actually did something
return _reportRepository.getOngoingReport(suspectId, category) return _reportRepository.getOngoingReports(suspectId, category).thenCompose(reportIds ->
.thenCompose(reportIdOptional -> {
reportIdOptional.isPresent() ? _reportRepository if (reportIds.size() > 0)
.getReport(reportIdOptional.get()) : CompletableFuture {
.completedFuture(new Report(suspectId, category)) return _reportRepository.getReport(Collections.max(reportIds));
).whenComplete((report, throwable) -> }
else
{
return CompletableFuture.completedFuture(new Report(suspectId, category));
}
}).whenComplete((report, throwable) ->
{
if (report != null)
{
ReportMessage reportMessage = report.getReportMessage(reporterId);
if (reportMessage != null)
{ {
if (report != null) reportMessage.setMessage(message);
{ }
ReportMessage reportMessage = report.getReportMessage(reporterId); else
{
reportMessage = new ReportMessage(reporterId, message, _serverName, _serverWeight);
report.addReportReason(reportMessage);
}
if (reportMessage != null) // create snapshot id ahead of time
{ if (category == ReportCategory.CHAT_ABUSE)
reportMessage.setMessage(message); {
} int snapshotId = _snapshotManager.getSnapshotRepository().createSnapshot(null).join();
else report.setSnapshotId(snapshotId);
{ }
reportMessage = new ReportMessage(reporterId, message, _serverName, _serverWeight);
report.addReportReason(reportMessage);
}
// create snapshot id ahead of time saveReport(report).join();
if (category == ReportCategory.CHAT_ABUSE) }
{ });
int snapshotId = _snapshotManager.getSnapshotRepository().createSnapshot(null).join();
report.setSnapshotId(snapshotId);
}
saveReport(report).join();
}
});
} }
/** /**

View File

@ -445,9 +445,9 @@ public class ReportRepository
return future; return future;
} }
public CompletableFuture<Optional<Integer>> getOngoingReport(int accountId, ReportCategory category) public CompletableFuture<List<Integer>> getOngoingReports(int accountId, ReportCategory category)
{ {
CompletableFuture<Optional<Integer>> future = CompletableFuture.supplyAsync(() -> CompletableFuture<List<Integer>> future = CompletableFuture.supplyAsync(() ->
{ {
try (Connection connection = DBPool.getAccount().getConnection()) try (Connection connection = DBPool.getAccount().getConnection())
{ {
@ -456,23 +456,24 @@ public class ReportRepository
preparedStatement.setInt(2, category.getId()); preparedStatement.setInt(2, category.getId());
ResultSet resultSet = preparedStatement.executeQuery(); ResultSet resultSet = preparedStatement.executeQuery();
if (resultSet.next()) List<Integer> reports = new ArrayList<>();
while (resultSet.next())
{ {
return Optional.of(resultSet.getInt("id")); reports.add(resultSet.getInt("id"));
} }
return reports;
} }
catch (SQLException e) catch (SQLException e)
{ {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
return Optional.empty();
}); });
future.exceptionally(throwable -> future.exceptionally(throwable ->
{ {
_logger.log(Level.SEVERE, "Error fetching ongoing report for account: " + accountId + ", category: " + category + ".", throwable); _logger.log(Level.SEVERE, "Error fetching ongoing report for account: " + accountId + ", category: " + category + ".", throwable);
return Optional.empty(); return new ArrayList<>();
}); });
return future; return future;