Merge pull request #14571 from rednir/get-user-from-username

Add ability to open user profile links with username instead of user ID
This commit is contained in:
Dean Herbert
2021-09-06 00:15:09 +09:00
committed by GitHub
4 changed files with 41 additions and 4 deletions

View File

@ -104,6 +104,9 @@ namespace osu.Game.Tests.Visual.Online
CoverUrl = @"https://osu.ppy.sh/images/headers/profile-covers/c4.jpg"
}, api.IsLoggedIn));
AddStep("Show ppy from username", () => profile.ShowUser(@"peppy"));
AddStep("Show flyte from username", () => profile.ShowUser(@"flyte"));
AddStep("Hide", profile.Hide);
AddStep("Show without reload", profile.Show);
}

View File

@ -8,15 +8,38 @@ namespace osu.Game.Online.API.Requests
{
public class GetUserRequest : APIRequest<User>
{
private readonly long? userId;
private readonly string userIdentifier;
public readonly RulesetInfo Ruleset;
/// <summary>
/// Gets the currently logged-in user.
/// </summary>
public GetUserRequest()
{
}
/// <summary>
/// Gets a user from their ID.
/// </summary>
/// <param name="userId">The user to get.</param>
/// <param name="ruleset">The ruleset to get the user's info for.</param>
public GetUserRequest(long? userId = null, RulesetInfo ruleset = null)
{
this.userId = userId;
this.userIdentifier = userId.ToString();
Ruleset = ruleset;
}
protected override string Target => userId.HasValue ? $@"users/{userId}/{Ruleset?.ShortName}" : $@"me/{Ruleset?.ShortName}";
/// <summary>
/// Gets a user from their username.
/// </summary>
/// <param name="username">The user to get.</param>
/// <param name="ruleset">The ruleset to get the user's info for.</param>
public GetUserRequest(string username = null, RulesetInfo ruleset = null)
{
this.userIdentifier = username;
Ruleset = ruleset;
}
protected override string Target => userIdentifier != null ? $@"users/{userIdentifier}/{Ruleset?.ShortName}" : $@"me/{Ruleset?.ShortName}";
}
}

View File

@ -333,6 +333,9 @@ namespace osu.Game
case LinkAction.OpenUserProfile:
if (int.TryParse(link.Argument, out int userId))
ShowUser(userId);
else
ShowUser(link.Argument);
break;
case LinkAction.OpenWiki:
@ -380,6 +383,12 @@ namespace osu.Game
/// <param name="userId">The user to display.</param>
public void ShowUser(int userId) => waitForReady(() => userProfile, _ => userProfile.ShowUser(userId));
/// <summary>
/// Show a user's profile as an overlay.
/// </summary>
/// <param name="username">The user to display.</param>
public void ShowUser(string username) => waitForReady(() => userProfile, _ => userProfile.ShowUser(username));
/// <summary>
/// Show a beatmap's set as an overlay, displaying the given beatmap.
/// </summary>

View File

@ -40,6 +40,8 @@ namespace osu.Game.Overlays
public void ShowUser(int userId) => ShowUser(new User { Id = userId });
public void ShowUser(string username) => ShowUser(new User { Username = username });
public void ShowUser(User user, bool fetchOnline = true)
{
if (user == User.SYSTEM_USER)
@ -116,7 +118,7 @@ namespace osu.Game.Overlays
if (fetchOnline)
{
userReq = new GetUserRequest(user.Id);
userReq = user.Id > 1 ? new GetUserRequest(user.Id) : new GetUserRequest(user.Username);
userReq.Success += userLoadComplete;
API.Queue(userReq);
}