Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for avatar decorations #260

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ public interface IPartialUser
/// <inheritdoc cref="IUser.Avatar" />
Optional<IImageHash?> Avatar { get; }

/// <inheritdoc cref="IUser.AvatarDecoration" />
Optional<IImageHash?> AvatarDecoration { get; }

/// <inheritdoc cref="IUser.IsBot" />
Optional<bool> IsBot { get; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ public interface IUser : IPartialUser
/// </summary>
new IImageHash? Avatar { get; }

/// <summary>
/// Gets the user's avatar decoration.
/// </summary>
new Optional<IImageHash?> AvatarDecoration { get; }

/// <summary>
/// Gets a value indicating whether the user is a bot, belonging to an OAuth2 application.
/// </summary>
Expand Down Expand Up @@ -120,6 +125,9 @@ public interface IUser : IPartialUser
/// <inheritdoc/>
Optional<IImageHash?> IPartialUser.Avatar => new(this.Avatar);

/// <inheritdoc/>
Optional<IImageHash?> IPartialUser.AvatarDecoration => this.AvatarDecoration;

/// <inheritdoc/>
Optional<bool> IPartialUser.IsBot => this.IsBot;

Expand Down
69 changes: 69 additions & 0 deletions Backend/Remora.Discord.API/API/CDN.cs
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,75 @@ public static Result<Uri> GetGuildBannerUrl
return ub.Uri;
}

/// <summary>
/// Gets the CDN URI of the given user's avatar decoration.
/// </summary>
/// <param name="user">The user.</param>
/// <param name="imageFormat">The requested image format.</param>
/// <param name="imageSize">The requested image size. May be any power of two between 16 and 4096.</param>
/// <returns>A result which may or may not have succeeded.</returns>
public static Result<Uri> GetUserAvatarDecorationUrl
(
IUser user,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IPartialUser might be better to use to also support those instances.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's IUser to be consistent with the other methods (such as GetUserBannerUrl below)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In general I think IPartialUser should be used.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that'd be better suited for its own PR to overhaul the entire CDN class. cc @Nihlus?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would say so aswell with its own PR then.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, let's do that separately.

Optional<CDNImageFormat> imageFormat = default,
Optional<ushort> imageSize = default
)
{
return user.AvatarDecoration.IsDefined(out var decoration)
? GetUserAvatarDecorationUrl(user.ID, decoration, imageFormat, imageSize)
: new ImageNotFoundError();
}

/// <summary>
/// Gets the CDN URI of the given user's avatar decoration.
/// </summary>
/// <param name="userID">The ID of the user.</param>
/// <param name="avatarDecorationHash">The image hash of the user's avatar decoration.</param>
/// <param name="imageFormat">The requested image format.</param>
/// <param name="imageSize">The requested image size. May be any power of two between 16 and 4096.</param>
/// <returns>A result which may or may not have succeeded.</returns>
public static Result<Uri> GetUserAvatarDecorationUrl
(
Snowflake userID,
IImageHash avatarDecorationHash,
Optional<CDNImageFormat> imageFormat = default,
Optional<ushort> imageSize = default
)
{
var formatValidation = ValidateOrDefaultImageFormat
(
imageFormat,
CDNImageFormat.PNG,
CDNImageFormat.JPEG,
CDNImageFormat.WebP
);

if (!formatValidation.IsSuccess)
{
return Result<Uri>.FromError(formatValidation);
}

var format = formatValidation.Entity;

var checkImageSize = CheckImageSize(imageSize);
if (!checkImageSize.IsSuccess)
{
return Result<Uri>.FromError(checkImageSize);
}

var ub = new UriBuilder(Constants.CDNBaseURL)
{
Path = $"avatar-decorations/{userID}/{avatarDecorationHash.Value}.{format.ToFileExtension()}"
};

if (imageSize.HasValue)
{
ub.Query = $"size={imageSize.Value}";
}

return ub.Uri;
}

/// <summary>
/// Gets the CDN URI of the given user's banner.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public record UserUpdate
string Username,
ushort Discriminator,
IImageHash? Avatar,
Optional<IImageHash?> AvatarDecoration = default,
Optional<bool> IsBot = default,
Optional<bool> IsSystem = default,
Optional<bool> IsMFAEnabled = default,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public record PartialUser
Optional<string> Username = default,
Optional<ushort> Discriminator = default,
Optional<IImageHash?> Avatar = default,
Optional<IImageHash?> AvatarDecoration = default,
Optional<bool> IsBot = default,
Optional<bool> IsSystem = default,
Optional<bool> IsMFAEnabled = default,
Expand Down
1 change: 1 addition & 0 deletions Backend/Remora.Discord.API/API/Objects/Users/User.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public record User
string Username,
ushort Discriminator,
IImageHash? Avatar,
Optional<IImageHash?> AvatarDecoration = default,
Optional<bool> IsBot = default,
Optional<bool> IsSystem = default,
Optional<bool> IsMFAEnabled = default,
Expand Down
2 changes: 2 additions & 0 deletions Backend/Remora.Discord.API/API/Objects/Users/UserMention.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public record UserMention
string Username,
ushort Discriminator,
IImageHash? Avatar,
Optional<IImageHash?> AvatarDecoration = default,
Optional<bool> IsBot = default,
Optional<bool> IsSystem = default,
Optional<bool> IsMFAEnabled = default,
Expand All @@ -53,6 +54,7 @@ public record UserMention
Username,
Discriminator,
Avatar,
AvatarDecoration,
IsBot,
IsSystem,
IsMFAEnabled,
Expand Down
1 change: 1 addition & 0 deletions Tests/Remora.Discord.Tests/Samples/Objects/USER/USER.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"username": "none",
"discriminator": "9999",
"avatar": "68b329da9893e34099c7d8ad5cb9c940",
"avatar_decoration": "68b329da9893e34099c7d8ad5cb9c940",
"bot": false,
"system": false,
"mfa_enabled": false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"username": "none",
"discriminator": "9999",
"avatar": "68b329da9893e34099c7d8ad5cb9c940",
"avatar_decoration": "68b329da9893e34099c7d8ad5cb9c940",
"bot": false,
"system": false,
"mfa_enabled": false,
Expand Down