From 2949c7208acf85a4c4710b4300b68f68f32774c4 Mon Sep 17 00:00:00 2001 From: ElGodDakio <95530219+DakioCode@users.noreply.github.com> Date: Mon, 9 Sep 2024 21:33:14 +0200 Subject: [PATCH] Feat/implement server methods (#199) * feat(Server) create setName() method * feat(Server) create setDefaultMessageNotifications and setExplicitContentFilter method * feat(Server) create setAfkTimeout method * feat(Server) create setAfkChannel method * feat(Server) create setSytemChannel method * feat(Server) create setRulesChannel method * feat(Server) create setPublicUpdatesChannel method * feat(Server) create enablePremiumProgressBar method * feat(Server) create setSafetyAlertsChannel method * feat: rebase and apply new marshaller system --------- Co-authored-by: Baptiste Parmantier --- .../common/embed/message_embed_builder.dart | 6 -- lib/src/api/server/server.dart | 61 +++++++++++++++++++ .../datastore/parts/server_message_part.dart | 51 ++++++++++------ .../datastore/parts/server_part.dart | 14 +++++ .../serializers/embed_serializer.dart | 5 +- 5 files changed, 111 insertions(+), 26 deletions(-) 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 4d9eed449..3ba3f398b 100644 --- a/lib/src/api/server/server.dart +++ b/lib/src/api/server/server.dart @@ -1,4 +1,6 @@ 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'; @@ -6,8 +8,13 @@ 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; + final Snowflake id; final String? applicationId; final String name; @@ -33,4 +40,58 @@ final class Server { required this.owner, required this.threads, }); + + Future setName(String name, {String? reason}) async { + await _serverPart.updateServer(id, {'name': name}, reason); + } + + Future setDescription(String description, {String? reason}) async { + 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 setExplicitContentFilter(ExplicitContentFilter value, + {String? reason}) async { + await _serverPart.updateServer( + id, {'explicit_content_filter': value.value}, reason); + } + + Future setAfkTimeout(int value, {String? reason}) async { + await _serverPart.updateServer(id, {'afk_timeout': value}, reason); + } + + Future setAfkChannel(String? channelId, {String? reason}) async { + await _serverPart.updateServer(id, {'afk_channel_id': channelId}, reason); + } + + Future setSystemChannel(String? channelId, {String? reason}) async { + 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 enablePremiumProgressBar(bool value, {String? reason}) async { + 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); + } } 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'])), );