From ff17685bc3650928062fecb20fe9b31698c0d839 Mon Sep 17 00:00:00 2001 From: Salman Ahmed Date: Fri, 23 Jun 2023 22:37:44 +0300 Subject: [PATCH 1/2] Fix `OpenUserProfile` links having multiple argument types --- osu.Game/Graphics/Containers/LinkFlowContainer.cs | 8 +++++++- osu.Game/OsuGame.cs | 10 +++------- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/osu.Game/Graphics/Containers/LinkFlowContainer.cs b/osu.Game/Graphics/Containers/LinkFlowContainer.cs index 2d27ce906b..a2c9b0ac32 100644 --- a/osu.Game/Graphics/Containers/LinkFlowContainer.cs +++ b/osu.Game/Graphics/Containers/LinkFlowContainer.cs @@ -74,7 +74,13 @@ namespace osu.Game.Graphics.Containers } public void AddUserLink(IUser user, Action creationParameters = null) - => createLink(CreateChunkFor(user.Username, true, CreateSpriteText, creationParameters), new LinkDetails(LinkAction.OpenUserProfile, user), "view profile"); + { + string argument = user.OnlineID > 1 + ? user.OnlineID.ToString() + : user.Username; + + createLink(CreateChunkFor(user.Username, true, CreateSpriteText, creationParameters), new LinkDetails(LinkAction.OpenUserProfile, argument), "view profile"); + } private void createLink(ITextPart textPart, LinkDetails link, LocalisableString tooltipText, Action action = null) { diff --git a/osu.Game/OsuGame.cs b/osu.Game/OsuGame.cs index a80639d4ff..2db8872524 100644 --- a/osu.Game/OsuGame.cs +++ b/osu.Game/OsuGame.cs @@ -446,15 +446,11 @@ namespace osu.Game break; case LinkAction.OpenUserProfile: - if (!(link.Argument is IUser user)) - { - user = int.TryParse(argString, out int userId) - ? new APIUser { Id = userId } - : new APIUser { Username = argString }; - } + var user = int.TryParse(argString, out int userId) + ? new APIUser { Id = userId } + : new APIUser { Username = argString }; ShowUser(user); - break; case LinkAction.OpenWiki: From 9a5f033a0fa6a066d8994322777f2478780463e9 Mon Sep 17 00:00:00 2001 From: Salman Ahmed Date: Sat, 24 Jun 2023 18:11:34 +0300 Subject: [PATCH 2/2] Change `OpenUserProfile` argument type to always use `IUser` --- osu.Game/Graphics/Containers/LinkFlowContainer.cs | 8 +------- osu.Game/Online/Chat/MessageFormatter.cs | 14 +++++++++++--- osu.Game/OsuGame.cs | 7 +------ .../Sections/Recent/DrawableRecentActivity.cs | 2 +- 4 files changed, 14 insertions(+), 17 deletions(-) diff --git a/osu.Game/Graphics/Containers/LinkFlowContainer.cs b/osu.Game/Graphics/Containers/LinkFlowContainer.cs index a2c9b0ac32..2d27ce906b 100644 --- a/osu.Game/Graphics/Containers/LinkFlowContainer.cs +++ b/osu.Game/Graphics/Containers/LinkFlowContainer.cs @@ -74,13 +74,7 @@ namespace osu.Game.Graphics.Containers } public void AddUserLink(IUser user, Action creationParameters = null) - { - string argument = user.OnlineID > 1 - ? user.OnlineID.ToString() - : user.Username; - - createLink(CreateChunkFor(user.Username, true, CreateSpriteText, creationParameters), new LinkDetails(LinkAction.OpenUserProfile, argument), "view profile"); - } + => createLink(CreateChunkFor(user.Username, true, CreateSpriteText, creationParameters), new LinkDetails(LinkAction.OpenUserProfile, user), "view profile"); private void createLink(ITextPart textPart, LinkDetails link, LocalisableString tooltipText, Action action = null) { diff --git a/osu.Game/Online/Chat/MessageFormatter.cs b/osu.Game/Online/Chat/MessageFormatter.cs index 523185a7cb..f89939d7cf 100644 --- a/osu.Game/Online/Chat/MessageFormatter.cs +++ b/osu.Game/Online/Chat/MessageFormatter.cs @@ -5,6 +5,7 @@ using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; +using osu.Game.Online.API.Requests.Responses; namespace osu.Game.Online.Chat { @@ -172,7 +173,7 @@ namespace osu.Game.Online.Chat case "u": case "users": - return new LinkDetails(LinkAction.OpenUserProfile, mainArg); + return getUserLink(mainArg); case "wiki": return new LinkDetails(LinkAction.OpenWiki, string.Join('/', args.Skip(3))); @@ -230,8 +231,7 @@ namespace osu.Game.Online.Chat break; case "u": - linkType = LinkAction.OpenUserProfile; - break; + return getUserLink(args[2]); default: return new LinkDetails(LinkAction.External, url); @@ -246,6 +246,14 @@ namespace osu.Game.Online.Chat return new LinkDetails(LinkAction.External, url); } + private static LinkDetails getUserLink(string argument) + { + if (int.TryParse(argument, out int userId)) + return new LinkDetails(LinkAction.OpenUserProfile, new APIUser { Id = userId }); + + return new LinkDetails(LinkAction.OpenUserProfile, new APIUser { Username = argument }); + } + private static MessageFormatterResult format(string toFormat, int startIndex = 0, int space = 3) { var result = new MessageFormatterResult(toFormat); diff --git a/osu.Game/OsuGame.cs b/osu.Game/OsuGame.cs index 2db8872524..d8eb63caee 100644 --- a/osu.Game/OsuGame.cs +++ b/osu.Game/OsuGame.cs @@ -45,7 +45,6 @@ using osu.Game.Input.Bindings; using osu.Game.IO; using osu.Game.Localisation; using osu.Game.Online; -using osu.Game.Online.API.Requests.Responses; using osu.Game.Online.Chat; using osu.Game.Overlays; using osu.Game.Overlays.BeatmapListing; @@ -446,11 +445,7 @@ namespace osu.Game break; case LinkAction.OpenUserProfile: - var user = int.TryParse(argString, out int userId) - ? new APIUser { Id = userId } - : new APIUser { Username = argString }; - - ShowUser(user); + ShowUser((IUser)link.Argument); break; case LinkAction.OpenWiki: diff --git a/osu.Game/Overlays/Profile/Sections/Recent/DrawableRecentActivity.cs b/osu.Game/Overlays/Profile/Sections/Recent/DrawableRecentActivity.cs index 0479ab7c16..8a0003b4ea 100644 --- a/osu.Game/Overlays/Profile/Sections/Recent/DrawableRecentActivity.cs +++ b/osu.Game/Overlays/Profile/Sections/Recent/DrawableRecentActivity.cs @@ -223,7 +223,7 @@ namespace osu.Game.Overlays.Profile.Sections.Recent private void addBeatmapsetLink() => content.AddLink(activity.Beatmapset.AsNonNull().Title, LinkAction.OpenBeatmapSet, getLinkArgument(activity.Beatmapset.AsNonNull().Url), creationParameters: t => t.Font = getLinkFont()); - private string getLinkArgument(string url) => MessageFormatter.GetLinkDetails($"{api.WebsiteRootUrl}{url}").Argument.ToString().AsNonNull(); + private object getLinkArgument(string url) => MessageFormatter.GetLinkDetails($"{api.WebsiteRootUrl}{url}").Argument.AsNonNull(); private FontUsage getLinkFont(FontWeight fontWeight = FontWeight.Regular) => OsuFont.GetFont(size: font_size, weight: fontWeight, italics: true);