diff --git a/osu.Game.Tests/Visual/Online/TestSceneChatOverlay.cs b/osu.Game.Tests/Visual/Online/TestSceneChatOverlay.cs index 48db5691ce..08337fbff9 100644 --- a/osu.Game.Tests/Visual/Online/TestSceneChatOverlay.cs +++ b/osu.Game.Tests/Visual/Online/TestSceneChatOverlay.cs @@ -8,6 +8,7 @@ using System.Linq; using System.Collections.Generic; using System.Net; using System.Threading; +using System.Threading.Tasks; using JetBrains.Annotations; using NUnit.Framework; using osu.Framework.Allocation; @@ -32,6 +33,7 @@ using osu.Game.Overlays.Chat.ChannelList; using osuTK; using osuTK.Input; using osu.Game.Graphics.UserInterfaceV2; +using osu.Game.Overlays.Comments; namespace osu.Game.Tests.Visual.Online { @@ -55,6 +57,9 @@ namespace osu.Game.Tests.Visual.Online private int currentMessageId; + private DummyAPIAccess dummyAPI => (DummyAPIAccess)API; + private readonly ManualResetEventSlim requestLock = new ManualResetEventSlim(); + [SetUp] public void SetUp() => Schedule(() => { @@ -581,6 +586,8 @@ namespace osu.Game.Tests.Visual.Online [Test] public void TestChatReport() { + ChatReportRequest request = null; + AddStep("Show overlay with channel", () => { chatOverlay.Show(); @@ -590,6 +597,26 @@ namespace osu.Game.Tests.Visual.Online AddAssert("Overlay is visible", () => chatOverlay.State.Value == Visibility.Visible); waitForChannel1Visible(); + AddStep("Setup request handling", () => + { + requestLock.Reset(); + + dummyAPI.HandleRequest = r => + { + if (!(r is ChatReportRequest req)) + return false; + + Task.Run(() => + { + request = req; + requestLock.Wait(10000); + req.TriggerSuccess(); + }); + + return true; + }; + }); + AddStep("Show report popover", () => this.ChildrenOfType().First().ShowPopover()); AddStep("Try to report", () => @@ -599,7 +626,11 @@ namespace osu.Game.Tests.Visual.Online InputManager.Click(MouseButton.Left); }); - AddAssert("Report message sended", () => channelManager.CurrentChannel.Value.Messages.Any(x => x.Content.Contains("!report"))); + AddWaitStep("Wait", 3); + + AddAssert("Overlay closed", () => !this.ChildrenOfType().Any()); + AddStep("Complete request", () => requestLock.Set()); + AddUntilStep("Request sent", () => request != null); } private void joinTestChannel(int i) diff --git a/osu.Game/Online/API/Requests/ChatReportRequest.cs b/osu.Game/Online/API/Requests/ChatReportRequest.cs new file mode 100644 index 0000000000..00d314be68 --- /dev/null +++ b/osu.Game/Online/API/Requests/ChatReportRequest.cs @@ -0,0 +1,38 @@ +// Copyright (c) ppy Pty Ltd . Licensed under the MIT Licence. +// See the LICENCE file in the repository root for full licence text. + +using System.Net.Http; +using osu.Framework.IO.Network; +using osu.Game.Online.Chat; +using osu.Game.Overlays.Chat; + +namespace osu.Game.Online.API.Requests; + +public class ChatReportRequest : APIRequest +{ + public readonly long? MessageId; + public readonly ChatReportReason Reason; + public readonly string Comment; + + public ChatReportRequest(long? id, ChatReportReason reason, string comment) + { + MessageId = id; + Reason = reason; + Comment = comment; + } + + protected override WebRequest CreateWebRequest() + { + var req = base.CreateWebRequest(); + req.Method = HttpMethod.Post; + + req.AddParameter(@"reportable_type", @"message"); + req.AddParameter(@"reportable_id", $"{MessageId}"); + req.AddParameter(@"reason", Reason.ToString()); + req.AddParameter(@"comments", Comment); + + return req; + } + + protected override string Target => @"reports"; +} diff --git a/osu.Game/Overlays/Chat/ChatLine.cs b/osu.Game/Overlays/Chat/ChatLine.cs index 70c3bf181c..0e69501ae9 100644 --- a/osu.Game/Overlays/Chat/ChatLine.cs +++ b/osu.Game/Overlays/Chat/ChatLine.cs @@ -92,7 +92,7 @@ namespace osu.Game.Overlays.Chat Font = OsuFont.GetFont(size: FontSize * 0.75f, weight: FontWeight.SemiBold, fixedWidth: true), AlwaysPresent = true, }, - drawableUsername = new DrawableUsername(message.Sender) + drawableUsername = new DrawableUsername(message) { Width = UsernameWidth, FontSize = FontSize, diff --git a/osu.Game/Overlays/Chat/DrawableUsername.cs b/osu.Game/Overlays/Chat/DrawableUsername.cs index 640ce2eca6..c9b0b018e3 100644 --- a/osu.Game/Overlays/Chat/DrawableUsername.cs +++ b/osu.Game/Overlays/Chat/DrawableUsername.cs @@ -21,6 +21,7 @@ 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; @@ -72,13 +73,15 @@ namespace osu.Game.Overlays.Chat private Bindable? currentChannel { get; set; } private readonly APIUser user; + private readonly Message message; private readonly OsuSpriteText drawableText; private readonly Drawable colouredDrawable; - public DrawableUsername(APIUser user) + public DrawableUsername(Message message) { - this.user = user; + this.message = message; + user = message.Sender; Action = openUserProfile; @@ -171,7 +174,8 @@ namespace osu.Game.Overlays.Chat })); } - items.Add(new OsuMenuItem("Report", MenuItemType.Destructive, this.ShowPopover)); + if (!user.Equals(api.LocalUser.Value)) + items.Add(new OsuMenuItem("Report", MenuItemType.Destructive, this.ShowPopover)); return items.ToArray(); } @@ -179,7 +183,19 @@ namespace osu.Game.Overlays.Chat private void report(ChatReportReason reason, string comments) { - chatManager?.PostMessage($"!report {user.Username} ({reason.GetDescription()}): {comments}"); + var request = new ChatReportRequest(message.Id, 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()