| 1 | package net.minecraft.client.multiplayer.chat.report;
|
| 2 |
|
| 3 | import com.google.common.collect.Lists;
|
| 4 | import com.mojang.authlib.minecraft.report.AbuseReport;
|
| 5 | import com.mojang.authlib.minecraft.report.AbuseReportLimits;
|
| 6 | import com.mojang.authlib.minecraft.report.ReportChatMessage;
|
| 7 | import com.mojang.authlib.minecraft.report.ReportEvidence;
|
| 8 | import com.mojang.authlib.minecraft.report.ReportedEntity;
|
| 9 | import com.mojang.datafixers.util.Either;
|
| 10 | import it.unimi.dsi.fastutil.ints.IntOpenHashSet;
|
| 11 | import it.unimi.dsi.fastutil.ints.IntSet;
|
| 12 | import java.nio.ByteBuffer;
|
| 13 | import java.time.Instant;
|
| 14 | import java.util.ArrayList;
|
| 15 | import java.util.List;
|
| 16 | import java.util.Objects;
|
| 17 | import java.util.UUID;
|
| 18 | import net.fabricmc.api.EnvType;
|
| 19 | import net.fabricmc.api.Environment;
|
| 20 | import net.minecraft.Optionull;
|
| 21 | import net.minecraft.client.gui.screens.Screen;
|
| 22 | import net.minecraft.client.gui.screens.reporting.ChatReportScreen;
|
| 23 | import net.minecraft.client.multiplayer.chat.LoggedChatMessage;
|
| 24 | import net.minecraft.network.chat.MessageSignature;
|
| 25 | import net.minecraft.network.chat.SignedMessageBody;
|
| 26 | import net.minecraft.network.chat.SignedMessageLink;
|
| 27 | import org.apache.commons.lang3.StringUtils;
|
| 28 | import org.jspecify.annotations.Nullable;
|
| 29 |
|
| 30 | @Environment(EnvType.CLIENT)
|
| 31 | public class ChatReport extends Report {
|
| 32 | private final IntSet reportedMessages = new IntOpenHashSet();
|
| 33 |
|
| 34 | private ChatReport(final UUID reportId, final Instant createdAt, final UUID reportedProfileId) {
|
| 35 | super(reportId, createdAt, reportedProfileId);
|
| 36 | }
|
| 37 |
|
| 38 | public void toggleReported(final int id, final AbuseReportLimits limits) {
|
| 39 | if (this.reportedMessages.contains(id)) {
|
| 40 | this.reportedMessages.remove(id);
|
| 41 | } else if (this.reportedMessages.size() < limits.maxReportedMessageCount()) {
|
| 42 | this.reportedMessages.add(id);
|
| 43 | }
|
| 44 | }
|
| 45 |
|
| 46 | public ChatReport copy() {
|
| 47 | ChatReport result = new ChatReport(this.reportId, this.createdAt, this.reportedProfileId);
|
| 48 | result.reportedMessages.addAll(this.reportedMessages);
|
| 49 | result.comments = this.comments;
|
| 50 | result.reason = this.reason;
|
| 51 | result.attested = this.attested;
|
| 52 | return result;
|
| 53 | }
|
| 54 |
|
| 55 | @Override
|
| 56 | public Screen createScreen(final Screen lastScreen, final ReportingContext context) {
|
| 57 | return new ChatReportScreen(lastScreen, context, this);
|
| 58 | }
|
| 59 |
|
| 60 | @Environment(EnvType.CLIENT)
|
| 61 | public static class Builder extends Report.Builder<ChatReport> {
|
| 62 | public Builder(final ChatReport report, final AbuseReportLimits limits) {
|
| 63 | super(report, limits);
|
| 64 | }
|
| 65 |
|
| 66 | public Builder(final UUID reportedProfileId, final AbuseReportLimits limits) {
|
| 67 | super(new ChatReport(UUID.randomUUID(), Instant.now(), reportedProfileId), limits);
|
| 68 | }
|
| 69 |
|
| 70 | public IntSet reportedMessages() {
|
| 71 | return this.report.reportedMessages;
|
| 72 | }
|
| 73 |
|
| 74 | public void toggleReported(final int id) {
|
| 75 | this.report.toggleReported(id, this.limits);
|
| 76 | }
|
| 77 |
|
| 78 | public boolean isReported(final int id) {
|
| 79 | return this.report.reportedMessages.contains(id);
|
| 80 | }
|
| 81 |
|
| 82 | @Override
|
| 83 | public boolean hasContent() {
|
| 84 | return StringUtils.isNotEmpty(this.comments()) || !this.reportedMessages().isEmpty() || this.reason() != null;
|
| 85 | }
|
| 86 |
|
| 87 | @Override
|
| 88 | public Report.@Nullable CannotBuildReason checkBuildable() {
|
| 89 | if (this.report.reportedMessages.isEmpty()) {
|
| 90 | return Report.CannotBuildReason.NO_REPORTED_MESSAGES;
|
| 91 | } else if (this.report.reportedMessages.size() > this.limits.maxReportedMessageCount()) {
|
| 92 | return Report.CannotBuildReason.TOO_MANY_MESSAGES;
|
| 93 | } else if (this.report.reason == null) {
|
| 94 | return Report.CannotBuildReason.NO_REASON;
|
| 95 | } else {
|
| 96 | return this.report.comments.length() > this.limits.maxOpinionCommentsLength() ? Report.CannotBuildReason.COMMENT_TOO_LONG : super.checkBuildable();
|
| 97 | }
|
| 98 | }
|
| 99 |
|
| 100 | @Override
|
| 101 | public Either<Report.Result, Report.CannotBuildReason> build(final ReportingContext reportingContext) {
|
| 102 | Report.CannotBuildReason error = this.checkBuildable();
|
| 103 | if (error != null) {
|
| 104 | return Either.right(error);
|
| 105 | }
|
| 106 |
|
| 107 | String reason = Objects.requireNonNull(this.report.reason).backendName();
|
| 108 | ReportEvidence evidence = this.buildEvidence(reportingContext);
|
| 109 | ReportedEntity reportedEntity = new ReportedEntity(this.report.reportedProfileId);
|
| 110 | AbuseReport abuseReport = AbuseReport.chat(this.report.comments, reason, evidence, reportedEntity, this.report.createdAt);
|
| 111 | return Either.left(new Report.Result(this.report.reportId, ReportType.CHAT, abuseReport));
|
| 112 | }
|
| 113 |
|
| 114 | private ReportEvidence buildEvidence(final ReportingContext reportingContext) {
|
| 115 | List<ReportChatMessage> allReportMessages = new ArrayList<>();
|
| 116 | ChatReportContextBuilder contextBuilder = new ChatReportContextBuilder(this.limits.leadingContextMessageCount());
|
| 117 | contextBuilder.collectAllContext(
|
| 118 | reportingContext.chatLog(), this.report.reportedMessages, (id, event) -> allReportMessages.add(this.buildReportedChatMessage(event, this.isReported(id)))
|
| 119 | );
|
| 120 | return new ReportEvidence(Lists.reverse(allReportMessages));
|
| 121 | }
|
| 122 |
|
| 123 | private ReportChatMessage buildReportedChatMessage(final LoggedChatMessage.Player chat, final boolean reported) {
|
| 124 | SignedMessageLink link = chat.message().link();
|
| 125 | SignedMessageBody body = chat.message().signedBody();
|
| 126 | List<ByteBuffer> lastSeen = body.lastSeen().entries().stream().map(MessageSignature::asByteBuffer).toList();
|
| 127 | ByteBuffer signature = Optionull.map(chat.message().signature(), MessageSignature::asByteBuffer);
|
| 128 | return new ReportChatMessage(link.index(), link.sender(), link.sessionId(), body.timeStamp(), body.salt(), lastSeen, body.content(), signature, reported);
|
| 129 | }
|
| 130 |
|
| 131 | public ChatReport.Builder copy() {
|
| 132 | return new ChatReport.Builder(this.report.copy(), this.limits);
|
| 133 | }
|
| 134 | }
|
| 135 | }
|
| 136 |
|