Improve user profile picture loading speeds (#248)

* Improve user profile picture loading speeds

* Return null instead of an empty string

For users that don't exist
This commit is contained in:
Josh 2022-03-21 07:41:26 -05:00 committed by GitHub
commit 6588176551
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -32,6 +32,20 @@ public class UserController : ControllerBase
return user?.Serialize(gameVersion);
}
private async Task<string?> getSerializedUserPicture(string username)
{
// use an anonymous type to only fetch certain columns
var partialUser = await this.database.Users.Where(u => u.Username == username)
.Select(u => new
{
u.Username,
u.IconHash,
}).FirstOrDefaultAsync();
if (partialUser == null) return null;
string user = LbpSerializer.TaggedStringElement("npHandle", partialUser.Username, "icon", partialUser.IconHash);
return LbpSerializer.TaggedStringElement("user", user, "type", "user");
}
[HttpGet("user/{username}")]
public async Task<IActionResult> GetUser(string username)
{
@ -51,7 +65,7 @@ public class UserController : ControllerBase
if (token == null) return this.StatusCode(403, "");
List<string?> serializedUsers = new();
foreach (string userId in u) serializedUsers.Add(await this.getSerializedUser(userId, token.GameVersion));
foreach (string userId in u) serializedUsers.Add(await this.getSerializedUserPicture(userId));
string serialized = serializedUsers.Aggregate(string.Empty, (current, user) => user == null ? current : current + user);
@ -154,4 +168,4 @@ public class UserController : ControllerBase
return this.Ok("[{\"StatusCode\":200}]");
}
}
}