Entity utils tests
This commit is contained in:
parent
0bc1e37e27
commit
7aeef08fb9
|
@ -56,7 +56,7 @@ class Guild extends SnowflakeEntity {
|
||||||
late final Cacheable<Snowflake, TextChannel>? rulesChannel;
|
late final Cacheable<Snowflake, TextChannel>? rulesChannel;
|
||||||
|
|
||||||
/// The guild owner's ID
|
/// The guild owner's ID
|
||||||
late final Cacheable<Snowflake, User>? owner;
|
late final Cacheable<Snowflake, User> owner;
|
||||||
|
|
||||||
/// The guild's members.
|
/// The guild's members.
|
||||||
late final Cache<Snowflake, Member> members;
|
late final Cache<Snowflake, Member> members;
|
||||||
|
@ -142,6 +142,13 @@ class Guild extends SnowflakeEntity {
|
||||||
this.splash = raw["splash"] as String?;
|
this.splash = raw["splash"] as String?;
|
||||||
this.embedEnabled = raw["embed_enabled"] as bool?;
|
this.embedEnabled = raw["embed_enabled"] as bool?;
|
||||||
|
|
||||||
|
this.systemChannelFlags = raw["system_channel_flags"] as int;
|
||||||
|
this.premiumTier = PremiumTier.from(raw["premium_tier"] as int);
|
||||||
|
this.premiumSubscriptionCount = raw["premium_subscription_count"] as int?;
|
||||||
|
this.preferredLocale = raw["preferred_locale"] as String;
|
||||||
|
|
||||||
|
this.owner = _UserCacheable(client, Snowflake(raw["owner_id"]));
|
||||||
|
|
||||||
this.roles = _SnowflakeCache<Role>();
|
this.roles = _SnowflakeCache<Role>();
|
||||||
if (raw["roles"] != null) {
|
if (raw["roles"] != null) {
|
||||||
raw["roles"].forEach((o) {
|
raw["roles"].forEach((o) {
|
||||||
|
@ -176,11 +183,6 @@ class Guild extends SnowflakeEntity {
|
||||||
this.afkChannel = _ChannelCacheable(client, Snowflake(raw["afk_channel_id"]));
|
this.afkChannel = _ChannelCacheable(client, Snowflake(raw["afk_channel_id"]));
|
||||||
}
|
}
|
||||||
|
|
||||||
this.systemChannelFlags = raw["system_channel_flags"] as int;
|
|
||||||
this.premiumTier = PremiumTier.from(raw["premium_tier"] as int);
|
|
||||||
this.premiumSubscriptionCount = raw["premium_subscription_count"] as int?;
|
|
||||||
this.preferredLocale = raw["preferred_locale"] as String;
|
|
||||||
|
|
||||||
this.members = _SnowflakeCache();
|
this.members = _SnowflakeCache();
|
||||||
this.voiceStates = _SnowflakeCache();
|
this.voiceStates = _SnowflakeCache();
|
||||||
|
|
||||||
|
@ -211,8 +213,6 @@ class Guild extends SnowflakeEntity {
|
||||||
// }
|
// }
|
||||||
// });
|
// });
|
||||||
|
|
||||||
this.owner = _UserCacheable(client, Snowflake(raw["owner_id"]));
|
|
||||||
|
|
||||||
if (raw["voice_states"] != null) {
|
if (raw["voice_states"] != null) {
|
||||||
raw["voice_states"].forEach((o) {
|
raw["voice_states"].forEach((o) {
|
||||||
final state = VoiceState._new(client, o as Map<String, dynamic>);
|
final state = VoiceState._new(client, o as Map<String, dynamic>);
|
||||||
|
|
|
@ -82,7 +82,7 @@ class Member extends SnowflakeEntity {
|
||||||
/// Returns total permissions of user.
|
/// Returns total permissions of user.
|
||||||
Future<Permissions> get effectivePermissions async {
|
Future<Permissions> get effectivePermissions async {
|
||||||
final guildInstance = await guild.getOrDownload();
|
final guildInstance = await guild.getOrDownload();
|
||||||
final owner = await guildInstance.owner?.getOrDownload();
|
final owner = await guildInstance.owner.getOrDownload();
|
||||||
if (this == owner) {
|
if (this == owner) {
|
||||||
return Permissions.all();
|
return Permissions.all();
|
||||||
}
|
}
|
||||||
|
|
4
nyxx/lib/src/internal/cache/Cache.dart
vendored
4
nyxx/lib/src/internal/cache/Cache.dart
vendored
|
@ -2,7 +2,7 @@ part of nyxx;
|
||||||
|
|
||||||
/// Generic interface for caching entities.
|
/// Generic interface for caching entities.
|
||||||
/// Wraps [Map] interface and provides utilities for manipulating cache.
|
/// Wraps [Map] interface and provides utilities for manipulating cache.
|
||||||
abstract class Cache<T, S> implements Disposable {
|
class Cache<T, S> implements Disposable {
|
||||||
late Map<T, S> _cache;
|
late Map<T, S> _cache;
|
||||||
|
|
||||||
/// Returns values of cache
|
/// Returns values of cache
|
||||||
|
@ -66,9 +66,11 @@ abstract class Cache<T, S> implements Disposable {
|
||||||
Iterable<S> takeLast(int count) => values.toList().sublist(values.length - count);
|
Iterable<S> takeLast(int count) => values.toList().sublist(values.length - count);
|
||||||
|
|
||||||
/// Get first element
|
/// Get first element
|
||||||
|
@override
|
||||||
S? get first => _cache.values.first;
|
S? get first => _cache.values.first;
|
||||||
|
|
||||||
/// Get last element
|
/// Get last element
|
||||||
|
@override
|
||||||
S get last => _cache.values.last;
|
S get last => _cache.values.last;
|
||||||
|
|
||||||
/// Get number of elements from cache
|
/// Get number of elements from cache
|
||||||
|
|
2
nyxx/lib/src/internal/cache/CachePolicy.dart
vendored
2
nyxx/lib/src/internal/cache/CachePolicy.dart
vendored
|
@ -76,7 +76,7 @@ class MemberCachePolicy extends CachePolicy<Member> {
|
||||||
static final CachePolicy<Member> voice = MemberCachePolicy((member) => member.voiceState != null);
|
static final CachePolicy<Member> voice = MemberCachePolicy((member) => member.voiceState != null);
|
||||||
|
|
||||||
/// Cache only member which are owner of guild
|
/// Cache only member which are owner of guild
|
||||||
static final CachePolicy<Member> owner = MemberCachePolicy((member) => member.guild.getFromCache()?.owner?.id == member.id);
|
static final CachePolicy<Member> owner = MemberCachePolicy((member) => member.guild.getFromCache()?.owner.id == member.id);
|
||||||
|
|
||||||
/// Default policy is [owner] or [voice]. So it caches guild owners and users in voice channels
|
/// Default policy is [owner] or [voice]. So it caches guild owners and users in voice channels
|
||||||
static final CachePolicy<Member> def = owner.or(voice);
|
static final CachePolicy<Member> def = owner.or(voice);
|
||||||
|
|
|
@ -29,8 +29,8 @@ class EntityUtility {
|
||||||
/// Guild guild = EntityUtility.createGuild(bot, rawJson);
|
/// Guild guild = EntityUtility.createGuild(bot, rawJson);
|
||||||
/// }
|
/// }
|
||||||
/// ```
|
/// ```
|
||||||
static Guild createGuild(INyxx client, Map<String, dynamic> rawJson) =>
|
static Guild createGuild(INyxx client, Map<String, dynamic> rawJson, [bool guildCreate = false]) =>
|
||||||
Guild._new(client, rawJson);
|
Guild._new(client, rawJson, guildCreate);
|
||||||
|
|
||||||
/// Creates a Role object, can be used for other classes where you have correct rawJson data from the API.
|
/// Creates a Role object, can be used for other classes where you have correct rawJson data from the API.
|
||||||
/// ```dart
|
/// ```dart
|
||||||
|
|
|
@ -163,6 +163,12 @@ class PermissionsBuilder {
|
||||||
..viewGuildInsights = permissions.viewGuildInsights;
|
..viewGuildInsights = permissions.viewGuildInsights;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Calculates permission int
|
||||||
|
int calculatePermissionValue() {
|
||||||
|
final set = this._build();
|
||||||
|
|
||||||
|
return set.allow & ~set.deny;
|
||||||
|
}
|
||||||
|
|
||||||
_PermissionsSet _build() {
|
_PermissionsSet _build() {
|
||||||
final permissionSet = _PermissionsSet();
|
final permissionSet = _PermissionsSet();
|
||||||
|
|
|
@ -28,12 +28,82 @@ final sampleMemberData = {
|
||||||
"deaf": false,
|
"deaf": false,
|
||||||
"mute": false,
|
"mute": false,
|
||||||
"roles": [
|
"roles": [
|
||||||
"1234564"
|
"1234564",
|
||||||
"1234563"
|
"5434534"
|
||||||
],
|
],
|
||||||
"joined_at": DateTime.now().toIso8601String()
|
"joined_at": DateTime.now().toIso8601String()
|
||||||
};
|
};
|
||||||
|
|
||||||
|
final sampleRoleData = {
|
||||||
|
"id": 456,
|
||||||
|
"name": "This is role",
|
||||||
|
"position": 1,
|
||||||
|
"hoist": false,
|
||||||
|
"managed": false,
|
||||||
|
"mentionable": false,
|
||||||
|
"permissions": (
|
||||||
|
PermissionsBuilder()
|
||||||
|
..sendMessages = true
|
||||||
|
..readMessageHistory = true
|
||||||
|
).calculatePermissionValue().toString(),
|
||||||
|
"color": DiscordColor.aquamarine.value,
|
||||||
|
};
|
||||||
|
|
||||||
|
final sampleTextChannel = {
|
||||||
|
"id": 1234,
|
||||||
|
"name": "This is text channel",
|
||||||
|
"position": 0,
|
||||||
|
"nsfw": true,
|
||||||
|
"topic": "This is topic",
|
||||||
|
"type": 0
|
||||||
|
};
|
||||||
|
|
||||||
|
final sampleVoiceChannel = {
|
||||||
|
"id": 4321,
|
||||||
|
"name": "This is voice channel",
|
||||||
|
"position": 1,
|
||||||
|
"type": 2
|
||||||
|
};
|
||||||
|
|
||||||
|
final sampleDMChannel = {
|
||||||
|
"id": 1234,
|
||||||
|
"type": 1,
|
||||||
|
"recipient": sampleUserRawData
|
||||||
|
};
|
||||||
|
|
||||||
|
final sampleEmoji = {
|
||||||
|
"id": 123,
|
||||||
|
"roles": []
|
||||||
|
};
|
||||||
|
|
||||||
|
final sampleGuildData = {
|
||||||
|
"id": 123,
|
||||||
|
"name": "This is guild name",
|
||||||
|
"region": "Europe",
|
||||||
|
"afk_timeout": 10,
|
||||||
|
"mfa_level": 1,
|
||||||
|
"verification_level": 1,
|
||||||
|
"default_message_notifications": 1,
|
||||||
|
"icon": null,
|
||||||
|
"discoverySplash": null,
|
||||||
|
"system_channel_flags": 1,
|
||||||
|
"premium_tier": 1,
|
||||||
|
"premium_subscription_count": 15,
|
||||||
|
"preferred_locale": "en_US",
|
||||||
|
"roles": [
|
||||||
|
sampleRoleData
|
||||||
|
],
|
||||||
|
"emojis": [
|
||||||
|
sampleEmoji
|
||||||
|
],
|
||||||
|
"owner_id": 321,
|
||||||
|
"channels": [
|
||||||
|
sampleTextChannel,
|
||||||
|
sampleVoiceChannel
|
||||||
|
],
|
||||||
|
"features": [ "FEATURE" ]
|
||||||
|
};
|
||||||
|
|
||||||
final client = NyxxRest("dum", 0);
|
final client = NyxxRest("dum", 0);
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
|
@ -186,6 +256,71 @@ void main() {
|
||||||
test("Create member object", () {
|
test("Create member object", () {
|
||||||
final member = EntityUtility.createGuildMember(client, 123.toSnowflake(), sampleMemberData);
|
final member = EntityUtility.createGuildMember(client, 123.toSnowflake(), sampleMemberData);
|
||||||
|
|
||||||
|
expect(member.id, 123.toSnowflake());
|
||||||
|
expect(member.nickname, "This is nick");
|
||||||
|
expect(member.deaf, false);
|
||||||
|
expect(member.mute, false);
|
||||||
|
expect(member.roles, hasLength(2));
|
||||||
|
expect(member.joinedAt, isNotNull);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("Create guild object", () {
|
||||||
|
final guild = EntityUtility.createGuild(client, sampleGuildData, true);
|
||||||
|
|
||||||
|
expect(guild.id, 123.toSnowflake());
|
||||||
|
expect(guild.name, "This is guild name");
|
||||||
|
expect(guild.region, "Europe");
|
||||||
|
expect(guild.afkTimeout, 10);
|
||||||
|
expect(guild.mfaLevel, 1);
|
||||||
|
expect(guild.verificationLevel, 1);
|
||||||
|
expect(guild.notificationLevel, 1);
|
||||||
|
expect(guild.iconURL(), isNull);
|
||||||
|
expect(guild.discoveryURL(), isNull);
|
||||||
|
expect(guild.systemChannelFlags, 1);
|
||||||
|
expect(guild.premiumTier, PremiumTier.tier1);
|
||||||
|
expect(guild.premiumSubscriptionCount, 15);
|
||||||
|
expect(guild.preferredLocale, "en_US");
|
||||||
|
expect(guild.roles.count, 1);
|
||||||
|
expect(guild.roles.first!.id, 456.toSnowflake());
|
||||||
|
expect(guild.emojis.count, 1);
|
||||||
|
expect(guild.emojis.first!.id, 123.toSnowflake());
|
||||||
|
expect(guild.channels.toList().length, 2);
|
||||||
|
expect(guild.channels.first.id, 1234.toSnowflake());
|
||||||
|
expect(guild.channels.last.id, 4321.toSnowflake());
|
||||||
|
expect(guild.owner.id, 321.toSnowflake());
|
||||||
|
});
|
||||||
|
|
||||||
|
test("Create Text channel", () {
|
||||||
|
final channel = EntityUtility.createTextGuildChannel(client, 123.toSnowflake(), sampleTextChannel);
|
||||||
|
|
||||||
|
expect(channel.id, 1234.toSnowflake());
|
||||||
|
expect(channel.name, "This is text channel");
|
||||||
|
expect(channel.position, 0);
|
||||||
|
expect(channel.isNsfw, true);
|
||||||
|
expect(channel.topic, "This is topic");
|
||||||
|
expect(channel.channelType, ChannelType.text);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("Create Voice channel", () {
|
||||||
|
final channel = EntityUtility.createVoiceGuildChannel(client, 123.toSnowflake(), sampleVoiceChannel);
|
||||||
|
|
||||||
|
expect(channel.id, 4321.toSnowflake());
|
||||||
|
expect(channel.name, "This is voice channel");
|
||||||
|
expect(channel.position, 1);
|
||||||
|
expect(channel.channelType, ChannelType.voice);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("Create Role", () {
|
||||||
|
final role = EntityUtility.createRole(client, 123.toSnowflake(), sampleRoleData);
|
||||||
|
|
||||||
|
expect(role.id, 456.toSnowflake());
|
||||||
|
expect(role.name, "This is role");
|
||||||
|
expect(role.position, 1);
|
||||||
|
expect(role.hoist, false);
|
||||||
|
expect(role.managed, false);
|
||||||
|
expect(role.mentionable, false);
|
||||||
|
expect(role.color, DiscordColor.aquamarine);
|
||||||
|
expect(role.permissions.raw, PermissionsConstants.sendMessages | PermissionsConstants.readMessageHistory);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
Loading…
Reference in a new issue