Skip to content

Commit

Permalink
Merge pull request #85 from mineral-dart/feat-improve-users
Browse files Browse the repository at this point in the history
feat: Improve users
  • Loading branch information
LeadcodeDev authored Mar 11, 2023
2 parents b4e0268 + d0bc1a7 commit 56bd0e8
Show file tree
Hide file tree
Showing 29 changed files with 270 additions and 41 deletions.
3 changes: 3 additions & 0 deletions lib/core/api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,8 @@ export '../src/api/users/user.dart' show User;
export '../src/api/utils.dart';
export '../src/api/webhook.dart' show Webhook;
export '../src/internal/services/intent_service.dart' show Intent;
export '../src/api/users/premium_type.dart' show PremiumType;
export '../src/api/users/user_decoration.dart' show UserDecoration;
export '../src/api/users/user_flags/user_flag_contract.dart' show UserFlagContract;

typedef Snowflake = String;
2 changes: 1 addition & 1 deletion lib/src/api/builders/embed_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ class EmbedBuilder {
image: preview.discoverySplash != null ? Image(url: preview.discoverySplash!) : null,
color: Color.invisible,
timestamp: DateTime.now(),
author: Author(name: client.user.username, iconUrl: client.user.defaultAvatar)
author: Author(name: client.user.username, iconUrl: client.user.decoration.defaultAvatarUrl)
);

embed.addField(name: 'Identifier', value: preview.id);
Expand Down
2 changes: 1 addition & 1 deletion lib/src/api/guilds/guild_member.dart
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ class GuildMember {
/// ```dart
/// print(member.isBot());
/// ```
bool get isBot => user.bot;
bool get isBot => user.isBot;

/// ### Returns whether of this is pending
///
Expand Down
25 changes: 25 additions & 0 deletions lib/src/api/managers/user_manager.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,30 @@
import 'dart:convert';

import 'package:http/http.dart';
import 'package:mineral/core.dart';
import 'package:mineral/core/api.dart';
import 'package:mineral/exception.dart';
import 'package:mineral/framework.dart';
import 'package:mineral/src/api/managers/cache_manager.dart';
import 'package:mineral_ioc/ioc.dart';

class UserManager extends CacheManager<User> {
Future<User> resolve (Snowflake id) async {
if (cache.containsKey(id)) {
return cache.getOrFail(id);
}

final Response response = await ioc.use<DiscordApiHttpService>()
.get(url: '/users/$id')
.build();

if (response.statusCode == 200) {
User user = User.from(jsonDecode(response.body));
cache.putIfAbsent(user.id, () => user);

return user;
}

throw ApiException('Unable to fetch channel with id #$id');
}
}
16 changes: 10 additions & 6 deletions lib/src/api/users/dm_user.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ class DmUser extends User {
super._username,
super._discriminator,
super._bot,
super._system,
super._publicFlags,
super._avatar,
super._avatarDecoration,
super._flags,
super._decoration,
super.premiumType,
super._lang,
this._channelId
);
Expand All @@ -28,12 +30,14 @@ class DmUser extends User {
user.id,
user.username,
user.discriminator,
user.bot,
user.isBot,
user.isSystem,
user.publicFlags,
user.avatar,
user.avatarDecoration,
user.flags,
user.decoration,
user.lang.locale,
channelId
user.premiumType,
channelId,
);
}
}
9 changes: 9 additions & 0 deletions lib/src/api/users/premium_type.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
enum PremiumType {
none(0),
classicNitro(1),
nitro(2),
basicNitro(3);

final int value;
const PremiumType(this.value);
}
73 changes: 50 additions & 23 deletions lib/src/api/users/user.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,49 +5,48 @@ import 'package:mineral/core.dart';
import 'package:mineral/core/api.dart';
import 'package:mineral/core/builders.dart';
import 'package:mineral/framework.dart';
import 'package:mineral/src/api/channels/dm_channel.dart';
import 'package:mineral/src/api/messages/dm_message.dart';
import 'package:mineral/src/api/users/user_flag.dart';
import 'package:mineral/src/internal/mixins/mineral_client.dart';
import 'package:mineral_cli/mineral_cli.dart';
import 'package:mineral_ioc/ioc.dart';

class User {
Snowflake _id;
String _username;
String _discriminator;
bool _bot;
int _publicFlags;
ImageFormater? _avatar;
ImageFormater? _avatarDecoration;
bool _system;
List<UserFlagContract> _publicFlags;
List<UserFlagContract> _flags;
UserDecoration _decoration;
String _lang;
late Status status;
PremiumType premiumType;
// late Status status;

User(
this._id,
this._username,
this._discriminator,
this._bot,
this._system,
this._publicFlags,
this._avatar,
this._avatarDecoration,
this._flags,
this._decoration,
this._lang,
this.premiumType
);

Snowflake get id => _id;
String get username => _username;
String get discriminator => _discriminator;
bool get bot => _bot;
int get publicFlags => _publicFlags;
ImageFormater? get avatar => _avatar;
ImageFormater? get avatarDecoration => _avatarDecoration;
bool get isBot => _bot;
bool get isSystem => _system;
List<UserFlagContract> get publicFlags => _publicFlags;
List<UserFlagContract> get flags => _flags;
UserDecoration get decoration => _decoration;
String get tag => '$_username#$_discriminator';
Locale get lang => Locale.values.firstWhere((element) => element.locale == _lang);


/// ### Returns the absolute url to the user's avatar
String get defaultAvatar => _avatar != null
? '${Constants.cdnUrl}/avatars/$_id/${_avatar?.url}'
: '${Constants.cdnUrl}/embed/avatars/${int.parse(_discriminator) % 5 }.png';

/// Return [GuildMember] of [Guild] context for this
GuildMember? toGuildMember (Snowflake guildId) {
MineralClient client = ioc.use<MineralClient>();
Expand Down Expand Up @@ -84,30 +83,58 @@ class User {
attachments: attachments
);

if (response.statusCode == 200) {
dynamic payload = jsonDecode(response.body);
final payload = jsonDecode(response.body);

if (response.statusCode == 200) {
DmMessage message = DmMessage.from(channel: channel, payload: payload);
channel.messages.cache.putIfAbsent(message.id, () => message);

return message;
}

ioc.use<MineralCli>().console.warn(payload['message']);
return null;
}

@override
String toString () => "<@$_id>";

factory User.from(dynamic payload) {
final List<UserFlagContract> publicFlags = [];
if (payload['public_flags'] != null) {
for (int element in UserFlag.values) {
if ((payload['public_flags'] & element) == element) {
publicFlags.add(UserFlag.find(element));
}
}
}

final List<UserFlagContract> flags = [];
if (payload['flags'] != null) {
for (int element in UserFlag.values) {
if ((payload['flags'] & element) == element) {
flags.add(UserFlag.find(element));
}
}
}

return User(
payload['id'],
payload['username'],
payload['discriminator'],
payload['bot'] == true,
payload['public_flags'] ?? 0,
payload['avatar'] != null ? ImageFormater(payload['avatar'], 'avatars/${payload['id']}') : null,
payload['avatar_decoration'] != null ? ImageFormater(payload['avatar'], 'avatars/${payload['id']}') : null,
payload['system'] == true,
publicFlags,
flags,
UserDecoration(
payload['discriminator'],
payload['avatar'] != null ? ImageFormater(payload['avatar'], 'avatars/${payload['id']}') : null,
payload['avatar_decoration'] != null ? ImageFormater(payload['avatar'], 'avatars/${payload['id']}') : null,
),
payload['locale'] ?? 'en-GB',
payload['premium_type'] != null
? PremiumType.values.firstWhere((element) => element.value == payload['premium_type'])
: PremiumType.none
);
}
}
19 changes: 19 additions & 0 deletions lib/src/api/users/user_decoration.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import 'package:mineral/core.dart';
import 'package:mineral/core/api.dart';

class UserDecoration {
final String _discriminator;

final ImageFormater? _avatar;
final ImageFormater? _avatarDecoration;

UserDecoration(this._discriminator, this._avatar, this._avatarDecoration);

ImageFormater? get avatar => _avatar;
ImageFormater? get avatarDecoration => _avatarDecoration;

/// ### Returns the absolute url to the user's avatar
String get defaultAvatarUrl => _avatar != null
? '${_avatar?.url}'
: '${Constants.cdnUrl}/embed/avatars/${int.parse(_discriminator) % 5 }.png';
}
57 changes: 57 additions & 0 deletions lib/src/api/users/user_flag.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import 'package:mineral/core/api.dart';
import 'package:mineral/src/api/users/user_flags/active_developer_flag.dart';
import 'package:mineral/src/api/users/user_flags/bot_http_interactions_flag.dart';
import 'package:mineral/src/api/users/user_flags/bug_hunter_level_1_flag.dart';
import 'package:mineral/src/api/users/user_flags/bug_hunter_level_2_flag.dart';
import 'package:mineral/src/api/users/user_flags/certified_moderator_flag.dart';
import 'package:mineral/src/api/users/user_flags/hype_squad_flag.dart';
import 'package:mineral/src/api/users/user_flags/hype_squad_online_house_1_flag.dart';
import 'package:mineral/src/api/users/user_flags/hype_squad_online_house_2_flag.dart';
import 'package:mineral/src/api/users/user_flags/hype_squad_online_house_3_flag.dart';
import 'package:mineral/src/api/users/user_flags/partner_flag.dart';
import 'package:mineral/src/api/users/user_flags/premium_early_supporter_flag.dart';
import 'package:mineral/src/api/users/user_flags/staff_flag.dart';
import 'package:mineral/src/api/users/user_flags/team_pseudo_user_flag.dart';
import 'package:mineral/src/api/users/user_flags/verified_bot_flag.dart';
import 'package:mineral/src/api/users/user_flags/verified_developer_flag.dart';

class UserFlag {
static get staff => StaffFlag();
static get partner => PartnerFlag();
static get hypeSquad => HypeSquadFlag();
static get bugHunterLevel1 => BugHunterLevel1Flag();
static get hypeSquadOnlineHouse1 => HypeSquadOnlineHouse1Flag();
static get hypeSquadOnlineHouse2 => HypeSquadOnlineHouse2Flag();
static get hypeSquadOnlineHouse3 => HypeSquadOnlineHouse3Flag();
static get premiumEarlySupporter => PremiumEarlySupporterFlag();
static get teamPseudoUser => TeamPseudoUserFlag();
static get bugHunterLevel2 => BugHunterLevel2Flag();
static get verifiedBot => VerifiedBotFlag();
static get verifiedDeveloper => VerifiedDeveloperFlag();
static get certifiedModerator => CertifiedModeratorFlag();
static get botHttpInteractions => BotHttpInteractionsFlag();
static get activeDeveloper => ActiveDeveloperFlag();

static final List<UserFlagContract> _flags = [
UserFlag.staff,
UserFlag.partner,
UserFlag.hypeSquad,
UserFlag.bugHunterLevel1,
UserFlag.hypeSquadOnlineHouse1,
UserFlag.hypeSquadOnlineHouse2,
UserFlag.hypeSquadOnlineHouse3,
UserFlag.premiumEarlySupporter,
UserFlag.teamPseudoUser,
UserFlag.bugHunterLevel2,
UserFlag.verifiedBot,
UserFlag.verifiedDeveloper,
UserFlag.certifiedModerator,
UserFlag.botHttpInteractions,
UserFlag.activeDeveloper,
];

static UserFlagContract find (int value) => _flags.firstWhere((element) => element.value == value);

static get values => _flags.map((e) => e.value);
UserFlag._();
}
5 changes: 5 additions & 0 deletions lib/src/api/users/user_flags/active_developer_flag.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import 'package:mineral/core/api.dart';

class ActiveDeveloperFlag extends UserFlagContract {
ActiveDeveloperFlag(): super('User is an active developer', 1 << 22, '26c7a60fb1654315e0be26107bd47470.svg');
}
5 changes: 5 additions & 0 deletions lib/src/api/users/user_flags/bot_http_interactions_flag.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import 'package:mineral/core/api.dart';

class BotHttpInteractionsFlag extends UserFlagContract {
BotHttpInteractionsFlag(): super('Bot use only HTTP interactions', 1 << 19, '498591d63b352256a1bf18061eff9d57.svg');
}
5 changes: 5 additions & 0 deletions lib/src/api/users/user_flags/bug_hunter_level_1_flag.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import 'package:mineral/core/api.dart';

class BugHunterLevel1Flag extends UserFlagContract {
BugHunterLevel1Flag(): super('Bug Hunter Level 1', 1 << 3, '8353d89b529e13365c415aef08d1d1f4.svg');
}
5 changes: 5 additions & 0 deletions lib/src/api/users/user_flags/bug_hunter_level_2_flag.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import 'package:mineral/core/api.dart';

class BugHunterLevel2Flag extends UserFlagContract {
BugHunterLevel2Flag(): super('Bug Hunter Level 2', 1 << 14, 'f599063762165e0d23e8b11b684765a8.svg');
}
5 changes: 5 additions & 0 deletions lib/src/api/users/user_flags/certified_moderator_flag.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import 'package:mineral/core/api.dart';

class CertifiedModeratorFlag extends UserFlagContract {
CertifiedModeratorFlag(): super('Moderator Programs Alumni', 1 << 18, 'b945002f0e0fd7f11990d800e98b5504.svg');
}
5 changes: 5 additions & 0 deletions lib/src/api/users/user_flags/hype_squad_flag.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import 'package:mineral/core/api.dart';

class HypeSquadFlag extends UserFlagContract {
HypeSquadFlag(): super('HypeSquad Events Member', 1 << 2, 'e666a84a7a5ea2abbbfa73adf22e627b.svg');
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import 'package:mineral/core/api.dart';

class HypeSquadOnlineHouse1Flag extends UserFlagContract {
HypeSquadOnlineHouse1Flag(): super('House Bravery Member', 1 << 6, '34306011e46e87f8ef25f3415d3b99ca.svg');
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import 'package:mineral/core/api.dart';

class HypeSquadOnlineHouse2Flag extends UserFlagContract {
HypeSquadOnlineHouse2Flag(): super('House Brilliance Member', 1 << 7, 'ec8e92568a7c8f19a052ef42f862ff18.svg');
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import 'package:mineral/core/api.dart';

class HypeSquadOnlineHouse3Flag extends UserFlagContract {
HypeSquadOnlineHouse3Flag(): super('House Balance Member', 1 << 8, '9f00b18e292e10fc0ae84ff5332e8b0b.svg');
}
5 changes: 5 additions & 0 deletions lib/src/api/users/user_flags/partner_flag.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import 'package:mineral/core/api.dart';

class PartnerFlag extends UserFlagContract {
PartnerFlag(): super('Partnered Server Owner', 1 << 1, '34306011e46e87f8ef25f3415d3b99ca.svg');
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import 'package:mineral/core/api.dart';

class PremiumEarlySupporterFlag extends UserFlagContract {
PremiumEarlySupporterFlag(): super('Early Nitro Supporter', 1 << 9, 'b802e9af134ff492276d94220e36ec5c.svg');
}
5 changes: 5 additions & 0 deletions lib/src/api/users/user_flags/staff_flag.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import 'package:mineral/core/api.dart';

class StaffFlag extends UserFlagContract {
StaffFlag(): super('Discord Employee', 1 << 0, '48d5bdcffe9e7848067c2e187f1ef951.svg');
}
Loading

0 comments on commit 56bd0e8

Please sign in to comment.