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

fix user avatar decoration, add member avatar decoration #718

Merged
merged 2 commits into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 4 additions & 0 deletions lib/src/http/managers/member_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ class MemberManager extends Manager<Member> {

@override
Member parse(Map<String, Object?> raw, {Snowflake? userId}) {
final avatarDecorationData = maybeParse(raw['avatar_decoration_data'], client.users.parseAvatarDecorationData);

return Member(
id: maybeParse((raw['user'] as Map<String, Object?>?)?['id'], Snowflake.parse) ?? userId ?? Snowflake.zero,
manager: this,
Expand All @@ -38,6 +40,8 @@ class MemberManager extends Manager<Member> {
isPending: raw['pending'] as bool? ?? false,
permissions: maybeParse(raw['permissions'], (String raw) => Permissions(int.parse(raw))),
communicationDisabledUntil: maybeParse(raw['communication_disabled_until'], DateTime.parse),
avatarDecorationData: avatarDecorationData,
avatarDecorationHash: avatarDecorationData?.asset,
);
}

Expand Down
13 changes: 12 additions & 1 deletion lib/src/http/managers/user_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import 'package:nyxx/src/models/locale.dart';
import 'package:nyxx/src/models/oauth2.dart';
import 'package:nyxx/src/models/snowflake.dart';
import 'package:nyxx/src/models/user/application_role_connection.dart';
import 'package:nyxx/src/models/user/avatar_decoration_data.dart';
import 'package:nyxx/src/models/user/connection.dart';
import 'package:nyxx/src/models/user/user.dart';
import 'package:nyxx/src/utils/cache_helpers.dart';
Expand All @@ -37,6 +38,7 @@ class UserManager extends ReadOnlyManager<User> {
final hasFlags = raw['flags'] != null;
final hasPremiumType = raw['premium_type'] != null;
final hasPublicFlags = raw['public_flags'] != null;
final avatarDecorationData = maybeParse(raw['avatar_decoration_data'], parseAvatarDecorationData);

return User(
manager: this,
Expand All @@ -54,7 +56,8 @@ class UserManager extends ReadOnlyManager<User> {
flags: hasFlags ? UserFlags(raw['flags'] as int) : null,
nitroType: hasPremiumType ? NitroType(raw['premium_type'] as int) : NitroType.none,
publicFlags: hasPublicFlags ? UserFlags(raw['public_flags'] as int) : null,
avatarDecorationHash: raw['avatar_decoration'] as String?,
avatarDecorationHash: avatarDecorationData?.asset,
avatarDecorationData: avatarDecorationData,
);
}

Expand Down Expand Up @@ -90,6 +93,14 @@ class UserManager extends ReadOnlyManager<User> {
);
}

/// Parse an [AvatarDecorationData] from [raw].
AvatarDecorationData parseAvatarDecorationData(Map<String, Object?> raw) {
return AvatarDecorationData(
asset: raw['asset'] as String,
skuId: Snowflake.parse(raw['sku_id']!),
);
}

@override
Future<User> fetch(Snowflake id) async {
final route = HttpRoute()..users(id: id.toString());
Expand Down
3 changes: 3 additions & 0 deletions lib/src/http/route.dart
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,9 @@ extension RouteHelpers on HttpRoute {
/// Adds the [`avatar-decorations`](https://discord.com/developers/docs/reference#image-formatting-cdn-endpoints) part to this [HttpRoute].
void avatarDecorations({String? id}) => add(HttpRoutePart('avatar-decorations', [if (id != null) HttpRouteParam(id)]));

/// Adds the [`avatar-decoration-presets`](https://discord.com/developers/docs/reference#image-formatting-cdn-endpoints) part to this [HttpRoute].
void avatarDecorationPresets() => add(HttpRoutePart('avatar-decoration-presets'));

/// Adds the [`recipients`](https://discord.com/developers/docs/resources/channel#group-dm-add-recipient) part to this [HttpRoute].
void recipients({String? id}) => add(HttpRoutePart('recipients', [if (id != null) HttpRouteParam(id)]));

Expand Down
17 changes: 17 additions & 0 deletions lib/src/models/guild/member.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import 'package:nyxx/src/models/permissions.dart';
import 'package:nyxx/src/models/role.dart';
import 'package:nyxx/src/models/snowflake.dart';
import 'package:nyxx/src/models/snowflake_entity/snowflake_entity.dart';
import 'package:nyxx/src/models/user/avatar_decoration_data.dart';
import 'package:nyxx/src/models/user/user.dart';
import 'package:nyxx/src/utils/flags.dart';

Expand Down Expand Up @@ -88,6 +89,12 @@ class Member extends PartialMember {
/// The time until which this member is timed out.
final DateTime? communicationDisabledUntil;

/// The member's guild avatar decoration data.
final AvatarDecorationData? avatarDecorationData;

/// The member's guild avatar decoration.
final String? avatarDecorationHash;

/// {@macro member}
/// @nodoc
Member({
Expand All @@ -105,6 +112,8 @@ class Member extends PartialMember {
required this.isPending,
required this.permissions,
required this.communicationDisabledUntil,
required this.avatarDecorationData,
required this.avatarDecorationHash,
});

/// The roles this member has.
Expand All @@ -121,6 +130,14 @@ class Member extends PartialMember {
..avatars(),
hash: avatarHash!,
);

CdnAsset? get avatarDecoration => avatarDecorationHash == null
? null
: CdnAsset(
client: manager.client,
base: HttpRoute()..avatarDecorationPresets(),
hash: avatarDecorationHash!,
);
}

/// Flags that can be applied to a [Member].
Expand Down
17 changes: 17 additions & 0 deletions lib/src/models/user/avatar_decoration_data.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import 'package:nyxx/src/models/snowflake.dart';
import 'package:nyxx/src/utils/to_string_helper/to_string_helper.dart';

/// {@template avatar_decoration_data}
/// The data for the user's [avatar decoration](https://support.discord.com/hc/en-us/articles/13410113109911-Avatar-Decorations).
/// {@endtemplate}
class AvatarDecorationData with ToStringHelper {
/// The ID of the avatar decoration's SKU.
final Snowflake skuId;

/// The avatar decoration hash.
final String asset;

/// {@macro avatar_decoration_data}
/// @nodoc
AvatarDecorationData({required this.skuId, required this.asset});
}
7 changes: 6 additions & 1 deletion lib/src/models/user/user.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import 'package:nyxx/src/models/discord_color.dart';
import 'package:nyxx/src/models/locale.dart';
import 'package:nyxx/src/models/message/author.dart';
import 'package:nyxx/src/models/snowflake_entity/snowflake_entity.dart';
import 'package:nyxx/src/models/user/avatar_decoration_data.dart';
import 'package:nyxx/src/utils/enum_like.dart';
import 'package:nyxx/src/utils/flags.dart';

Expand Down Expand Up @@ -73,6 +74,9 @@ class User extends PartialUser implements MessageAuthor, CommandOptionMentionabl
/// The hash of this user's avatar decoration.
final String? avatarDecorationHash;

/// The user's avatar deciration data.
final AvatarDecorationData? avatarDecorationData;

/// {@macro user}
/// @nodoc
User({
Expand All @@ -92,6 +96,7 @@ class User extends PartialUser implements MessageAuthor, CommandOptionMentionabl
required this.nitroType,
required this.publicFlags,
required this.avatarDecorationHash,
required this.avatarDecorationData,
});

/// This user's banner.
Expand Down Expand Up @@ -125,7 +130,7 @@ class User extends PartialUser implements MessageAuthor, CommandOptionMentionabl
? null
: CdnAsset(
client: manager.client,
base: HttpRoute()..avatarDecorations(id: id.toString()),
base: HttpRoute()..avatarDecorationPresets(),
hash: avatarDecorationHash!,
);
}
Expand Down
Loading