Fix `LinkFlowContainer` not creating user links supporting full `IUser` specification

This commit is contained in:
Dean Herbert 2021-11-08 14:17:47 +09:00
parent dede0e56ce
commit 6b6dd93e9e
5 changed files with 29 additions and 22 deletions

View File

@ -46,7 +46,7 @@ public void AddLinks(string text, List<Link> links)
AddText(text[previousLinkEnd..link.Index]);
string displayText = text.Substring(link.Index, link.Length);
string linkArgument = link.Argument;
object linkArgument = link.Argument;
string tooltip = displayText == link.Url ? null : link.Url;
AddLink(displayText, link.Action, linkArgument, tooltip);
@ -62,16 +62,16 @@ public void AddLink(LocalisableString text, string url, Action<SpriteText> creat
public void AddLink(LocalisableString text, Action action, string tooltipText = null, Action<SpriteText> creationParameters = null)
=> createLink(CreateChunkFor(text, true, CreateSpriteText, creationParameters), new LinkDetails(LinkAction.Custom, string.Empty), tooltipText, action);
public void AddLink(LocalisableString text, LinkAction action, string argument, string tooltipText = null, Action<SpriteText> creationParameters = null)
public void AddLink(LocalisableString text, LinkAction action, object argument, string tooltipText = null, Action<SpriteText> creationParameters = null)
=> createLink(CreateChunkFor(text, true, CreateSpriteText, creationParameters), new LinkDetails(action, argument), tooltipText);
public void AddLink(IEnumerable<SpriteText> text, LinkAction action, string linkArgument, string tooltipText = null)
public void AddLink(IEnumerable<SpriteText> text, LinkAction action, object linkArgument, string tooltipText = null)
{
createLink(new TextPartManual(text), new LinkDetails(action, linkArgument), tooltipText);
}
public void AddUserLink(IUser user, Action<SpriteText> creationParameters = null)
=> createLink(CreateChunkFor(user.Username, true, CreateSpriteText, creationParameters), new LinkDetails(LinkAction.OpenUserProfile, user.OnlineID.ToString()), "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)
{
@ -83,7 +83,7 @@ private void createLink(ITextPart textPart, LinkDetails link, LocalisableString
game.HandleLink(link);
// fallback to handle cases where OsuGame is not available, ie. tournament client.
else if (link.Action == LinkAction.External)
host.OpenUrlExternally(link.Argument);
host.OpenUrlExternally(link.Argument.ToString());
};
AddPart(new TextLink(textPart, tooltipText, onClickAction));

View File

@ -320,9 +320,9 @@ public class LinkDetails
{
public readonly LinkAction Action;
public readonly string Argument;
public readonly object Argument;
public LinkDetails(LinkAction action, string argument)
public LinkDetails(LinkAction action, object argument)
{
Action = action;
Argument = argument;
@ -351,9 +351,9 @@ public class Link : IComparable<Link>
public int Index;
public int Length;
public LinkAction Action;
public string Argument;
public object Argument;
public Link(string url, int startIndex, int length, LinkAction action, string argument)
public Link(string url, int startIndex, int length, LinkAction action, object argument)
{
Url = url;
Index = startIndex;

View File

@ -289,25 +289,27 @@ private void load()
/// <param name="link">The link to load.</param>
public void HandleLink(LinkDetails link) => Schedule(() =>
{
string argString = link.Argument.ToString();
switch (link.Action)
{
case LinkAction.OpenBeatmap:
// TODO: proper query params handling
if (int.TryParse(link.Argument.Contains('?') ? link.Argument.Split('?')[0] : link.Argument, out int beatmapId))
if (int.TryParse(argString.Contains('?') ? argString.Split('?')[0] : argString, out int beatmapId))
ShowBeatmap(beatmapId);
break;
case LinkAction.OpenBeatmapSet:
if (int.TryParse(link.Argument, out int setId))
if (int.TryParse(argString, out int setId))
ShowBeatmapSet(setId);
break;
case LinkAction.OpenChannel:
ShowChannel(link.Argument);
ShowChannel(argString);
break;
case LinkAction.SearchBeatmapSet:
SearchBeatmapSet(link.Argument);
SearchBeatmapSet(argString);
break;
case LinkAction.OpenEditorTimestamp:
@ -321,26 +323,31 @@ public void HandleLink(LinkDetails link) => Schedule(() =>
break;
case LinkAction.External:
OpenUrlExternally(link.Argument);
OpenUrlExternally(argString);
break;
case LinkAction.OpenUserProfile:
ShowUser(int.TryParse(link.Argument, out int userId)
? new APIUser { Id = userId }
: new APIUser { Username = link.Argument });
if (!(link.Argument is IUser user))
{
user = int.TryParse(argString, out int userId)
? new APIUser { Id = userId }
: new APIUser { Username = argString };
}
ShowUser(user);
break;
case LinkAction.OpenWiki:
ShowWiki(link.Argument);
ShowWiki(argString);
break;
case LinkAction.OpenChangelog:
if (string.IsNullOrEmpty(link.Argument))
if (string.IsNullOrEmpty(argString))
ShowChangelogListing();
else
{
string[] changelogArgs = link.Argument.Split("/");
string[] changelogArgs = argString.Split("/");
ShowChangelogBuild(changelogArgs[0], changelogArgs[1]);
}

View File

@ -209,7 +209,7 @@ private void updateMessageContent()
username.Text = $@"{message.Sender.Username}" + (senderHasBackground || message.IsAction ? "" : ":");
// remove non-existent channels from the link list
message.Links.RemoveAll(link => link.Action == LinkAction.OpenChannel && chatManager?.AvailableChannels.Any(c => c.Name == link.Argument) != true);
message.Links.RemoveAll(link => link.Action == LinkAction.OpenChannel && chatManager?.AvailableChannels.Any(c => c.Name == link.Argument.ToString()) != true);
ContentFlow.Clear();
ContentFlow.AddLinks(message.DisplayContent, message.Links);

View File

@ -216,7 +216,7 @@ private void addBeatmapLink()
private void addBeatmapsetLink()
=> content.AddLink(activity.Beatmapset?.Title, LinkAction.OpenBeatmapSet, getLinkArgument(activity.Beatmapset?.Url), creationParameters: t => t.Font = getLinkFont());
private string getLinkArgument(string url) => MessageFormatter.GetLinkDetails($"{api.APIEndpointUrl}{url}").Argument;
private string getLinkArgument(string url) => MessageFormatter.GetLinkDetails($"{api.APIEndpointUrl}{url}").Argument.ToString();
private FontUsage getLinkFont(FontWeight fontWeight = FontWeight.Regular)
=> OsuFont.GetFont(size: font_size, weight: fontWeight, italics: true);