PC-958 Don't allow chat abuse reports of players who haven't spoken

in chat
This commit is contained in:
Keir Nellyer 2016-09-07 11:28:20 +01:00
parent 640a8d3516
commit e2840f36b5
5 changed files with 76 additions and 19 deletions

View File

@ -23,7 +23,7 @@ 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<SnapshotMessage, Boolean> _snapshots = CacheBuilder.newBuilder()
private final Cache<SnapshotMessage, Boolean> _messages = CacheBuilder.newBuilder()
.concurrencyLevel(4)
.expireAfterWrite(30, TimeUnit.MINUTES)
.build();
@ -48,9 +48,9 @@ public class SnapshotManager
*
* @param message the message to temporarily store
*/
public void cacheSnapshot(SnapshotMessage message)
public void cacheMessage(SnapshotMessage message)
{
_snapshots.put(message, true);
_messages.put(message, true);
}
/**
@ -59,25 +59,52 @@ public class SnapshotManager
*
* @return a set containing all snapshots
*/
public Set<SnapshotMessage> getSnapshots()
public Set<SnapshotMessage> getMessages()
{
// The compareTo method in SnapshotMessage will ensure this in chronological order
Set<SnapshotMessage> messages = new TreeSet<>();
messages.addAll(_snapshots.asMap().keySet());
messages.addAll(_messages.asMap().keySet());
return messages;
}
/**
* Gets all instances of {@link SnapshotMessage} which involve a particular user.
* Gets all messages an account is involved in.
* 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
* @param accountId the account to search for messages involved in
* @return the messages that the account is involved in
*/
public Set<SnapshotMessage> getSnapshots(int accountId)
public Set<SnapshotMessage> getMessagesInvolving(int accountId)
{
return _snapshots.asMap().keySet().stream()
.filter(snapshot -> snapshot.getSenderId() == accountId || snapshot.getRecipientIds().contains(accountId))
Set<SnapshotMessage> messagesInvolved = new TreeSet<>();
messagesInvolved.addAll(getMessagesFrom(accountId));
messagesInvolved.addAll(getMessagesReceived(accountId));
return messagesInvolved;
}
/**
* Gets all messages sent by an account.
*
* @param senderId the account to search for messages involved in
* @return the messages that the account is involved in
*/
public Set<SnapshotMessage> getMessagesFrom(int senderId)
{
return _messages.asMap().keySet().stream()
.filter(message -> message.getSenderId() == senderId)
.collect(Collectors.toCollection(TreeSet::new));
}
/**
* Gets all messages received by an account.
*
* @param recipientId the account to search for messages received
* @return the messages that the account is involved in
*/
public Set<SnapshotMessage> getMessagesReceived(int recipientId)
{
return _messages.asMap().keySet().stream()
.filter(message -> message.getRecipientIds().contains(recipientId))
.collect(Collectors.toCollection(TreeSet::new));
}

View File

@ -42,13 +42,13 @@ public class SnapshotPlugin extends MiniPlugin
@EventHandler(priority = EventPriority.MONITOR)
public void onPlayerChat(AsyncPlayerChatEvent e)
{
_snapshotManager.cacheSnapshot(createSnapshot(e));
_snapshotManager.cacheMessage(createSnapshot(e));
}
@EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true)
public void onPrivateMessage(PrivateMessageEvent e)
{
_snapshotManager.cacheSnapshot(createSnapshot(e));
_snapshotManager.cacheMessage(createSnapshot(e));
}
public Set<Integer> getAccountIds(Set<Player> players)

View File

@ -29,7 +29,7 @@ public class PushSnapshotsHandler implements CommandCallback
PushSnapshotsCommand pushCommand = (PushSnapshotsCommand) command;
int accountId = pushCommand.getAccountId();
long reportId = pushCommand.getReportId();
Set<SnapshotMessage> messages = _snapshotManager.getSnapshots(accountId);
Set<SnapshotMessage> messages = _snapshotManager.getMessagesInvolving(accountId);
if (messages.size() > 0)
{

View File

@ -80,6 +80,11 @@ public class ReportManager
commandManager.registerCommandType("PushSnapshotsCommand", PushSnapshotsCommand.class, pushHandler);
}
public SnapshotManager getSnapshotManager()
{
return _snapshotManager;
}
/**
* Gets the {@link ReportRepository} we are using.
*

View File

@ -8,6 +8,7 @@ import org.bukkit.event.HandlerList;
import mineplex.core.account.CoreClient;
import mineplex.core.common.util.C;
import mineplex.core.common.util.F;
import mineplex.core.common.util.UtilPlayer;
import mineplex.core.gui.SimpleGui;
import mineplex.core.report.ReportCategory;
import mineplex.core.report.ReportManager;
@ -18,17 +19,17 @@ import mineplex.core.report.ReportPlugin;
*/
public class ReportCategoryPage extends SimpleGui
{
private final ReportPlugin _reportPlugin;
private final ReportPlugin _plugin;
private final Player _reporter;
private final int _reporterId;
private final CoreClient _suspect;
private final String _reason;
public ReportCategoryPage(ReportPlugin reportPlugin, Player reporter, int reporterId, CoreClient suspect, String reason)
public ReportCategoryPage(ReportPlugin plugin, Player reporter, int reporterId, CoreClient suspect, String reason)
{
super(reportPlugin.getPlugin(), reporter, "Report " + suspect.getName(), 9 * 3);
super(plugin.getPlugin(), reporter, "Report " + suspect.getName(), 9 * 3);
_reportPlugin = reportPlugin;
_plugin = plugin;
_reporter = reporter;
_reporterId = reporterId;
_suspect = suspect;
@ -49,7 +50,31 @@ public class ReportCategoryPage extends SimpleGui
_reporter.closeInventory();
unregisterListener();
_reportPlugin.getReportManager().createReport(_reporterId, _suspect.getAccountId(), category, _reason)
if (category == ReportCategory.CHAT_ABUSE)
{
if (hasSentMessage(_suspect.getAccountId()))
{
createReport(category);
}
else
{
UtilPlayer.message(_reporter, F.main(_plugin.getName(), C.cRed + "You have not received a message from that player"));
}
}
else
{
createReport(category);
}
}
private boolean hasSentMessage(int accountId)
{
return _plugin.getReportManager().getSnapshotManager().getMessagesFrom(accountId).size() > 0;
}
private void createReport(ReportCategory category)
{
_plugin.getReportManager().createReport(_reporterId, _suspect.getAccountId(), category, _reason)
.thenAccept(report -> {
boolean error = true;