From 8391e2a538c7ca7777e2c04a9cdf90a9f191abf7 Mon Sep 17 00:00:00 2001 From: Dean Herbert Date: Fri, 5 May 2023 13:58:13 +0900 Subject: [PATCH] Move reporting code out of `DrawableChatUsername` into more correct locations --- .../Visual/Online/TestSceneChatOverlay.cs | 2 +- osu.Game/Overlays/Chat/ChatLine.cs | 14 +++++--- .../Overlays/Chat/DrawableChatUsername.cs | 34 +++---------------- osu.Game/Overlays/Chat/ReportChatPopover.cs | 31 +++++++++++++++-- 4 files changed, 44 insertions(+), 37 deletions(-) diff --git a/osu.Game.Tests/Visual/Online/TestSceneChatOverlay.cs b/osu.Game.Tests/Visual/Online/TestSceneChatOverlay.cs index 339d861d3b..99ce837665 100644 --- a/osu.Game.Tests/Visual/Online/TestSceneChatOverlay.cs +++ b/osu.Game.Tests/Visual/Online/TestSceneChatOverlay.cs @@ -616,7 +616,7 @@ namespace osu.Game.Tests.Visual.Online }; }); - AddStep("Show report popover", () => this.ChildrenOfType().First().ShowPopover()); + AddStep("Show report popover", () => this.ChildrenOfType().First().ShowPopover()); AddStep("Set report reason to other", () => { diff --git a/osu.Game/Overlays/Chat/ChatLine.cs b/osu.Game/Overlays/Chat/ChatLine.cs index dca34128d1..2f4c175ac4 100644 --- a/osu.Game/Overlays/Chat/ChatLine.cs +++ b/osu.Game/Overlays/Chat/ChatLine.cs @@ -1,25 +1,28 @@ // Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. // See the LICENCE file in the repository root for full licence text. -using System.Linq; using System.Collections.Generic; +using System.Linq; using osu.Framework.Allocation; using osu.Framework.Bindables; +using osu.Framework.Extensions; using osu.Framework.Extensions.Color4Extensions; using osu.Framework.Extensions.LocalisationExtensions; using osu.Framework.Graphics; using osu.Framework.Graphics.Containers; +using osu.Framework.Graphics.Cursor; using osu.Framework.Graphics.Shapes; +using osu.Framework.Graphics.Sprites; +using osu.Framework.Graphics.UserInterface; using osu.Game.Configuration; using osu.Game.Graphics; using osu.Game.Graphics.Containers; using osu.Game.Graphics.Sprites; using osu.Game.Online.Chat; -using osu.Framework.Graphics.Sprites; namespace osu.Game.Overlays.Chat { - public partial class ChatLine : CompositeDrawable + public partial class ChatLine : CompositeDrawable, IHasPopover { private Message message = null!; @@ -92,7 +95,7 @@ namespace osu.Game.Overlays.Chat Font = OsuFont.GetFont(size: FontSize * 0.75f, weight: FontWeight.SemiBold, fixedWidth: true), AlwaysPresent = true, }, - drawableUsername = new DrawableChatUsername(message.Sender, message.Id) + drawableUsername = new DrawableChatUsername(message.Sender) { Width = UsernameWidth, FontSize = FontSize, @@ -100,6 +103,7 @@ namespace osu.Game.Overlays.Chat Origin = Anchor.TopRight, Anchor = Anchor.TopRight, Margin = new MarginPadding { Horizontal = Spacing }, + ReportRequested = this.ShowPopover, }, drawableContentFlow = new LinkFlowContainer(styleMessageContent) { @@ -128,6 +132,8 @@ namespace osu.Game.Overlays.Chat FinishTransforms(true); } + public Popover GetPopover() => new ReportChatPopover(message); + /// /// Performs a highlight animation on this . /// diff --git a/osu.Game/Overlays/Chat/DrawableChatUsername.cs b/osu.Game/Overlays/Chat/DrawableChatUsername.cs index b3bbc74be7..4b4afc204c 100644 --- a/osu.Game/Overlays/Chat/DrawableChatUsername.cs +++ b/osu.Game/Overlays/Chat/DrawableChatUsername.cs @@ -5,7 +5,6 @@ using System; using System.Collections.Generic; using osu.Framework.Allocation; using osu.Framework.Bindables; -using osu.Framework.Extensions; using osu.Framework.Extensions.Color4Extensions; using osu.Framework.Graphics; using osu.Framework.Graphics.Containers; @@ -21,7 +20,6 @@ using osu.Game.Graphics.Sprites; using osu.Game.Graphics.UserInterface; using osu.Game.Localisation; using osu.Game.Online.API; -using osu.Game.Online.API.Requests; using osu.Game.Online.API.Requests.Responses; using osu.Game.Online.Chat; using osu.Game.Resources.Localisation.Web; @@ -31,8 +29,10 @@ using ChatStrings = osu.Game.Localisation.ChatStrings; namespace osu.Game.Overlays.Chat { - public partial class DrawableChatUsername : OsuClickableContainer, IHasContextMenu, IHasPopover + public partial class DrawableChatUsername : OsuClickableContainer, IHasContextMenu { + public Action? ReportRequested; + public Color4 AccentColour { get; } public override bool ReceivePositionalInputAt(Vector2 screenSpacePos) => @@ -73,15 +73,13 @@ namespace osu.Game.Overlays.Chat private Bindable? currentChannel { get; set; } private readonly APIUser user; - private readonly long? messageId; private readonly OsuSpriteText drawableText; private readonly Drawable colouredDrawable; - public DrawableChatUsername(APIUser user, long? messageId) + public DrawableChatUsername(APIUser user) { this.user = user; - this.messageId = messageId; Action = openUserProfile; @@ -174,34 +172,12 @@ namespace osu.Game.Overlays.Chat } if (!user.Equals(api.LocalUser.Value)) - items.Add(new OsuMenuItem("Report", MenuItemType.Destructive, this.ShowPopover)); + items.Add(new OsuMenuItem("Report", MenuItemType.Destructive, ReportRequested)); return items.ToArray(); } } - public Popover GetPopover() => new ReportChatPopover(user) - { - Action = report - }; - - private void report(ChatReportReason reason, string comments) - { - var request = new ChatReportRequest(messageId, reason, comments); - - request.Failure += _ => Schedule(() => - { - currentChannel?.Value?.AddNewMessages(new ErrorMessage("Report failed to send, please retry")); - }); - - request.Success += () => Schedule(() => - { - currentChannel?.Value?.AddNewMessages(new InfoMessage("Report has been sent")); - }); - - api.Queue(request); - } - private void openUserChannel() { chatManager?.OpenPrivateChannel(user); diff --git a/osu.Game/Overlays/Chat/ReportChatPopover.cs b/osu.Game/Overlays/Chat/ReportChatPopover.cs index de5f3268b8..f02e52eb2e 100644 --- a/osu.Game/Overlays/Chat/ReportChatPopover.cs +++ b/osu.Game/Overlays/Chat/ReportChatPopover.cs @@ -1,22 +1,47 @@ // Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. // See the LICENCE file in the repository root for full licence text. +using osu.Framework.Allocation; using osu.Game.Graphics.UserInterfaceV2; -using osu.Game.Online.API.Requests.Responses; +using osu.Game.Online.API; +using osu.Game.Online.API.Requests; +using osu.Game.Online.Chat; using osu.Game.Resources.Localisation.Web; namespace osu.Game.Overlays.Chat { public partial class ReportChatPopover : ReportPopover { - public ReportChatPopover(APIUser? user) - : base(ReportStrings.UserTitle(user?.Username ?? @"Someone")) + [Resolved] + private IAPIProvider api { get; set; } = null!; + + [Resolved] + private ChannelManager channelManager { get; set; } = null!; + + private readonly Message message; + + public ReportChatPopover(Message message) + : base(ReportStrings.UserTitle(message.Sender?.Username ?? @"Someone")) { + this.message = message; + Action = report; } protected override bool CheckCanSubmitEmptyComment(ChatReportReason reason) { return reason != ChatReportReason.Other; } + + private void report(ChatReportReason reason, string comments) + { + var request = new ChatReportRequest(message.Id, reason, comments); + + request.Success += () => Schedule(() => + { + channelManager.CurrentChannel.Value.AddNewMessages(new InfoMessage(UsersStrings.ReportThanks.ToString())); + }); + + api.Queue(request); + } } }