From 3f0f867a208883ab175b1f5657850530591f87c8 Mon Sep 17 00:00:00 2001 From: Baptiste Parmantier Date: Mon, 9 Sep 2024 21:31:51 +0200 Subject: [PATCH] feat: rebase and apply new marshaller system --- .../datastore/parts/server_part.dart | 151 ------------------ .../common/embed/message_embed_builder.dart | 6 - lib/src/api/server/server.dart | 49 +++--- .../datastore/parts/server_message_part.dart | 51 +++--- .../datastore/parts/server_part.dart | 14 ++ .../serializers/embed_serializer.dart | 5 +- 6 files changed, 76 insertions(+), 200 deletions(-) delete mode 100644 lib/infrastructure/internals/datastore/parts/server_part.dart diff --git a/lib/infrastructure/internals/datastore/parts/server_part.dart b/lib/infrastructure/internals/datastore/parts/server_part.dart deleted file mode 100644 index 1787d942b..000000000 --- a/lib/infrastructure/internals/datastore/parts/server_part.dart +++ /dev/null @@ -1,151 +0,0 @@ -import 'dart:async'; -import 'dart:io'; - -import 'package:mineral/api/common/snowflake.dart'; -import 'package:mineral/api/server/role.dart'; -import 'package:mineral/api/server/server.dart'; -import 'package:mineral/infrastructure/internals/datastore/data_store_part.dart'; -import 'package:mineral/infrastructure/internals/http/discord_header.dart'; -import 'package:mineral/infrastructure/kernel/kernel.dart'; -import 'package:mineral/infrastructure/services/http/http_client_status.dart'; -import 'package:mineral/infrastructure/services/http/http_request_option.dart'; -import 'package:mineral/infrastructure/services/http/response.dart'; - -final class ServerPart implements DataStorePart { - final KernelContract _kernel; - - HttpClientStatus get status => _kernel.dataStore.client.status; - - ServerPart(this._kernel); - - Future updateServer( - Snowflake id, Map payload, String? reason) async { - final response = await _kernel.dataStore.client.patch('/guilds/$id', - body: payload, - option: HttpRequestOptionImpl( - headers: {DiscordHeader.auditLogReason(reason)})); - - print('${response.statusCode} / ${response.bodyString}'); - - final Server? server = await _serializeServerResponse(response); - - if (server != null) { - final chacheKey = _kernel.marshaller.cacheKey.server(id); - final rawServer = await _kernel.marshaller.serializers.server.deserialize(server); - - await _kernel.marshaller.cache.put(chacheKey, rawServer); - } - - return server; - } - - Future getServer(Snowflake id) async { - final cacheKey = _kernel.marshaller.cacheKey; - final serverCacheKey = cacheKey.server(id); - - if (await _kernel.marshaller.cache.has(serverCacheKey)) { - return _kernel.marshaller.serializers.server - .serializeCache({'id': id.value}); - } - - final [serverResponse, channelsResponse, membersResponse] = - await Future.wait([ - _kernel.dataStore.client.get('/guilds/$id'), - _kernel.dataStore.client.get('/guilds/$id/channels'), - _kernel.dataStore.client.get('/guilds/$id/members') - ]); - - final server = await _kernel.marshaller.serializers.server.serializeRemote({ - ...serverResponse.body, - 'channels': channelsResponse.body, - 'members': membersResponse.body - }); - - final rawServer = - await _kernel.marshaller.serializers.server.deserialize(server); - - await Future.wait([ - _kernel.marshaller.cache.put(serverCacheKey, rawServer), - ...server.channels.list.values.map((channel) { - final channelCacheKey = - cacheKey.serverChannel(serverId: id, channelId: channel.id); - final rawChannel = - _kernel.marshaller.serializers.channels.deserialize(channel); - - return _kernel.marshaller.cache.put(channelCacheKey, rawChannel); - }), - ...server.members.list.values.map((member) { - final memberCacheKey = - cacheKey.serverMember(serverId: id, memberId: member.id); - final rawMember = - _kernel.marshaller.serializers.member.deserialize(member); - - return _kernel.marshaller.cache.put(memberCacheKey, rawMember); - }) - ]); - - return server; - } - - Future getRole(Snowflake guildId, Snowflake id) async { - final cachedRawRole = await _kernel.marshaller.cache.get(id.value); - if (cachedRawRole != null) { - return _kernel.marshaller.serializers.role.serializeRemote(cachedRawRole); - } - - final response = - await _kernel.dataStore.client.get('/guilds/$guildId/roles'); - final roles = await _serializeRolesResponse(response); - final role = roles.firstWhere((role) => role.id == id); - - final roleCacheKey = - _kernel.marshaller.cacheKey.serverRole(serverId: guildId, roleId: id); - final rawRole = await _kernel.marshaller.serializers.role.deserialize(role); - await _kernel.marshaller.cache.put(roleCacheKey, rawRole); - - return role; - } - - Future> getRoles(Snowflake guildId, {bool force = false}) async { - if (force) { - final response = - await _kernel.dataStore.client.get('/guilds/$guildId/roles'); - final roles = await _serializeRolesResponse(response); - - for (final role in roles) { - final roleCacheKey = _kernel.marshaller.cacheKey - .serverRole(serverId: guildId, roleId: role.id); - final rawRole = - await _kernel.marshaller.serializers.role.deserialize(role); - - await _kernel.marshaller.cache.put(roleCacheKey, rawRole); - } - - return roles; - } - - final rowServer = await _kernel.dataStore.server.getServer(guildId); - return rowServer.roles.list.values.toList(); - } - - Future> _serializeRolesResponse(Response response) { - return switch (response.statusCode) { - int() when status.isSuccess(response.statusCode) => - List.from(response.body).map((element) => - _kernel.marshaller.serializers.channels.serializeRemote(element)), - int() when status.isError(response.statusCode) => - throw HttpException(response.body), - _ => throw Exception('Unknown status code: ${response.statusCode}'), - } as Future>; - } - - Future _serializeServerResponse(Response response) async { - return switch (response.statusCode) { - int() when status.isSuccess(response.statusCode) => - _kernel.marshaller.serializers.server.serializeRemote(response.body), - int() when status.isError(response.statusCode) => - throw HttpException(response.body), - _ => throw Exception('Unknown status code: ${response.statusCode}'), - } as Future; - } -} diff --git a/lib/src/api/common/embed/message_embed_builder.dart b/lib/src/api/common/embed/message_embed_builder.dart index 1b68e6ac4..c29cd13aa 100644 --- a/lib/src/api/common/embed/message_embed_builder.dart +++ b/lib/src/api/common/embed/message_embed_builder.dart @@ -1,10 +1,4 @@ import 'package:mineral/api.dart'; -import 'package:mineral/src/api/common/embed/message_embed.dart'; -import 'package:mineral/src/api/common/embed/message_embed_assets.dart'; -import 'package:mineral/src/api/common/embed/message_embed_author.dart'; -import 'package:mineral/src/api/common/embed/message_embed_field.dart'; -import 'package:mineral/src/api/common/embed/message_embed_footer.dart'; -import 'package:mineral/src/api/common/embed/message_embed_image.dart'; final class MessageEmbedBuilder { String? title; diff --git a/lib/src/api/server/server.dart b/lib/src/api/server/server.dart index 9521a7a6c..3ba3f398b 100644 --- a/lib/src/api/server/server.dart +++ b/lib/src/api/server/server.dart @@ -1,16 +1,6 @@ -import 'package:mineral/api/common/snowflake.dart'; -import 'package:mineral/api/server/enums/default_message_notification.dart'; -import 'package:mineral/api/server/enums/explicit_content_filter.dart'; -import 'package:mineral/api/server/managers/channel_manager.dart'; -import 'package:mineral/api/server/managers/member_manager.dart'; -import 'package:mineral/api/server/managers/role_manager.dart'; -import 'package:mineral/api/server/member.dart'; -import 'package:mineral/api/server/server_assets.dart'; -import 'package:mineral/api/server/server_settings.dart'; -import 'package:mineral/infrastructure/internals/container/ioc_container.dart'; -import 'package:mineral/infrastructure/internals/datastore/data_store.dart'; -import 'package:mineral/infrastructure/internals/datastore/parts/server_part.dart'; import 'package:mineral/src/api/common/snowflake.dart'; +import 'package:mineral/src/api/server/enums/default_message_notification.dart'; +import 'package:mineral/src/api/server/enums/explicit_content_filter.dart'; import 'package:mineral/src/api/server/managers/channel_manager.dart'; import 'package:mineral/src/api/server/managers/member_manager.dart'; import 'package:mineral/src/api/server/managers/role_manager.dart'; @@ -18,6 +8,9 @@ import 'package:mineral/src/api/server/managers/threads_manager.dart'; import 'package:mineral/src/api/server/member.dart'; import 'package:mineral/src/api/server/server_assets.dart'; import 'package:mineral/src/api/server/server_settings.dart'; +import 'package:mineral/src/infrastructure/internals/container/ioc_container.dart'; +import 'package:mineral/src/infrastructure/internals/datastore/data_store.dart'; +import 'package:mineral/src/infrastructure/internals/datastore/parts/server_part.dart'; final class Server { ServerPart get _serverPart => ioc.resolve().server; @@ -56,12 +49,16 @@ final class Server { await _serverPart.updateServer(id, {'description': description}, reason); } - Future setDefaultMessageNotifications(DefaultMessageNotification value, {String? reason}) async { - await _serverPart.updateServer(id, {'default_message_notifications': value.value}, reason); + Future setDefaultMessageNotifications(DefaultMessageNotification value, + {String? reason}) async { + await _serverPart.updateServer( + id, {'default_message_notifications': value.value}, reason); } - Future setExplicitContentFilter(ExplicitContentFilter value, {String? reason}) async { - await _serverPart.updateServer(id, {'explicit_content_filter': value.value}, reason); + Future setExplicitContentFilter(ExplicitContentFilter value, + {String? reason}) async { + await _serverPart.updateServer( + id, {'explicit_content_filter': value.value}, reason); } Future setAfkTimeout(int value, {String? reason}) async { @@ -73,22 +70,28 @@ final class Server { } Future setSystemChannel(String? channelId, {String? reason}) async { - await _serverPart.updateServer(id, {'system_channel_id': channelId}, reason); + await _serverPart.updateServer( + id, {'system_channel_id': channelId}, reason); } Future setRulesChannel(String? channelId, {String? reason}) async { await _serverPart.updateServer(id, {'rules_channel_id': channelId}, reason); } - - Future setPublicUpdatesChannel(String? channelId, {String? reason}) async { - await _serverPart.updateServer(id, {'public_updates_channel_id': channelId}, reason); + + Future setPublicUpdatesChannel(String? channelId, + {String? reason}) async { + await _serverPart.updateServer( + id, {'public_updates_channel_id': channelId}, reason); } Future enablePremiumProgressBar(bool value, {String? reason}) async { - await _serverPart.updateServer(id, {'premium_progress_bar_enabled': value}, reason); + await _serverPart.updateServer( + id, {'premium_progress_bar_enabled': value}, reason); } - Future setSafetyAlertsChannel(String? channelId, {String? reason}) async { - await _serverPart.updateServer(id, {'safety_alerts_channel_id': channelId}, reason); + Future setSafetyAlertsChannel(String? channelId, + {String? reason}) async { + await _serverPart.updateServer( + id, {'safety_alerts_channel_id': channelId}, reason); } } diff --git a/lib/src/infrastructure/internals/datastore/parts/server_message_part.dart b/lib/src/infrastructure/internals/datastore/parts/server_message_part.dart index e93c0760b..5be75fa7c 100644 --- a/lib/src/infrastructure/internals/datastore/parts/server_message_part.dart +++ b/lib/src/infrastructure/internals/datastore/parts/server_message_part.dart @@ -23,21 +23,26 @@ final class ServerMessagePart implements DataStorePart { List? embeds, List? components, }) async { - final response = - await _kernel.dataStore.client.patch('/channels/$channelId/messages/$id', body: { + final response = await _kernel.dataStore.client + .patch('/channels/$channelId/messages/$id', body: { 'content': content, 'embeds': await Helper.createOrNullAsync( field: embeds, - fn: () async => embeds?.map(_kernel.marshaller.serializers.embed.deserialize).toList()), + fn: () async => embeds + ?.map(_kernel.marshaller.serializers.embed.deserialize) + .toList()), 'components': components?.map((c) => c.toJson()).toList() }); final server = await _kernel.dataStore.server.getServer(serverId); - final channel = await _kernel.dataStore.channel.getChannel(channelId); + final channel = + await _kernel.dataStore.channel.getChannel(channelId); - final Map serverMessage = - await _kernel.marshaller.serializers.serverMessage.normalize(response.body); - final message = await _kernel.marshaller.serializers.serverMessage.serialize(serverMessage); + final Map serverMessage = await _kernel + .marshaller.serializers.serverMessage + .normalize(response.body); + final message = await _kernel.marshaller.serializers.serverMessage + .serialize(serverMessage); if (channel != null) { channel.server = server; @@ -54,17 +59,21 @@ final class ServerMessagePart implements DataStorePart { String? content, List? embeds, List? components}) async { - final response = await _kernel.dataStore.client.post('/channels/$channelId/messages', body: { + final response = await _kernel.dataStore.client + .post('/channels/$channelId/messages', body: { 'content': content, 'embeds': Helper.createOrNull( - field: embeds, - fn: () => embeds?.map(_kernel.marshaller.serializers.embed.deserialize))?.toList(), + field: embeds, + fn: () => + embeds?.map(_kernel.marshaller.serializers.embed.deserialize)) + ?.toList(), 'components': components?.map((c) => c.toJson()).toList(), 'message_reference': {'message_id': id, 'channel_id': channelId} }); final server = await _kernel.dataStore.server.getServer(serverId); - final channel = await _kernel.dataStore.channel.getChannel(channelId); + final channel = + await _kernel.dataStore.channel.getChannel(channelId); final Map rawMessage = await _kernel.marshaller.serializers.serverMessage.normalize({ @@ -72,8 +81,9 @@ final class ServerMessagePart implements DataStorePart { 'server_id': serverId, }); - final ServerMessage message = - await _kernel.marshaller.serializers.serverMessage.serialize(rawMessage); + final ServerMessage message = await _kernel + .marshaller.serializers.serverMessage + .serialize(rawMessage); if (channel != null) { channel.server = server; @@ -83,19 +93,24 @@ final class ServerMessagePart implements DataStorePart { return message; } - Future pin({required Snowflake id, required Snowflake channelId}) async { + Future pin( + {required Snowflake id, required Snowflake channelId}) async { await _kernel.dataStore.client.put('/channels/$channelId/pins/$id'); } - Future unpin({required Snowflake id, required Snowflake channelId}) async { + Future unpin( + {required Snowflake id, required Snowflake channelId}) async { await _kernel.dataStore.client.delete('/channels/$channelId/pins/$id'); } - Future crosspost({required Snowflake id, required Snowflake channelId}) async { - await _kernel.dataStore.client.post('/channels/$channelId/messages/$id/crosspost'); + Future crosspost( + {required Snowflake id, required Snowflake channelId}) async { + await _kernel.dataStore.client + .post('/channels/$channelId/messages/$id/crosspost'); } - Future delete({required Snowflake id, required Snowflake channelId}) async { + Future delete( + {required Snowflake id, required Snowflake channelId}) async { await _kernel.dataStore.client.delete('/channels/$channelId/messages/$id'); } } diff --git a/lib/src/infrastructure/internals/datastore/parts/server_part.dart b/lib/src/infrastructure/internals/datastore/parts/server_part.dart index cd4c982f4..c276f9764 100644 --- a/lib/src/infrastructure/internals/datastore/parts/server_part.dart +++ b/lib/src/infrastructure/internals/datastore/parts/server_part.dart @@ -6,8 +6,10 @@ import 'package:mineral/src/api/server/channels/server_channel.dart'; import 'package:mineral/src/api/server/role.dart'; import 'package:mineral/src/api/server/server.dart'; import 'package:mineral/src/infrastructure/internals/datastore/data_store_part.dart'; +import 'package:mineral/src/infrastructure/internals/http/discord_header.dart'; import 'package:mineral/src/infrastructure/kernel/kernel.dart'; import 'package:mineral/src/infrastructure/services/http/http_client_status.dart'; +import 'package:mineral/src/infrastructure/services/http/http_request_option.dart'; final class ServerPart implements DataStorePart { final KernelContract _kernel; @@ -35,6 +37,18 @@ final class ServerPart implements DataStorePart { return _kernel.marshaller.serializers.server.serialize(payload); } + Future updateServer( + Snowflake id, Map payload, String? reason) async { + final response = await _kernel.dataStore.client.patch('/guilds/$id', + body: payload, + option: HttpRequestOptionImpl( + headers: {DiscordHeader.auditLogReason(reason)})); + + final rawServer = + await _kernel.marshaller.serializers.server.normalize(response.body); + return _kernel.marshaller.serializers.server.serialize(rawServer); + } + Future> getChannels(Snowflake id) async { final response = await _kernel.dataStore.client.get('/guilds/$id/channels'); if (status.isError(response.statusCode)) { diff --git a/lib/src/infrastructure/internals/marshaller/serializers/embed_serializer.dart b/lib/src/infrastructure/internals/marshaller/serializers/embed_serializer.dart index c6d1b0304..9b190a6d8 100644 --- a/lib/src/infrastructure/internals/marshaller/serializers/embed_serializer.dart +++ b/lib/src/infrastructure/internals/marshaller/serializers/embed_serializer.dart @@ -1,5 +1,4 @@ import 'dart:async'; -import 'dart:convert'; import 'package:mineral/src/api/common/color.dart'; import 'package:mineral/src/api/common/embed/message_embed.dart'; @@ -57,7 +56,9 @@ final class EmbedSerializer implements SerializerContract { fn: () => MessageEmbedProvider.fromJson(json['provider'])), fields: Helper.createOrNull( field: json['fields'], - fn: () => List.from(json['fields']).map((element) => MessageEmbedField.fromJson(element)).toList()), + fn: () => List.from(json['fields']) + .map((element) => MessageEmbedField.fromJson(element)) + .toList()), color: Helper.createOrNull( field: json['color'], fn: () => Color.of(json['color'])), );