From 2e02a853cdb989ab0fdd4c3c02d82dab19c6bee1 Mon Sep 17 00:00:00 2001 From: DakioCode Date: Thu, 11 Jul 2024 08:49:52 +0200 Subject: [PATCH 01/20] ServerMessage.delete(), ServerMessage.edit(), ServerMessage.reply() --- .../internals/datastore/data_store.dart | 60 +++++++++++++++++++ .../datastore/parts/server_message_part.dart | 36 +++++++++++ lib/src/api/server/server_message.dart | 22 ++++++- 3 files changed, 116 insertions(+), 2 deletions(-) create mode 100644 lib/infrastructure/internals/datastore/data_store.dart create mode 100644 lib/infrastructure/internals/datastore/parts/server_message_part.dart diff --git a/lib/infrastructure/internals/datastore/data_store.dart b/lib/infrastructure/internals/datastore/data_store.dart new file mode 100644 index 000000000..6013a0303 --- /dev/null +++ b/lib/infrastructure/internals/datastore/data_store.dart @@ -0,0 +1,60 @@ +import 'package:mineral/infrastructure/internals/datastore/parts/channel_part.dart'; +import 'package:mineral/infrastructure/internals/datastore/parts/interaction_part.dart'; +import 'package:mineral/infrastructure/internals/datastore/parts/member_part.dart'; +import 'package:mineral/infrastructure/internals/datastore/parts/role_part.dart'; +import 'package:mineral/infrastructure/internals/datastore/parts/server_message_part.dart'; +import 'package:mineral/infrastructure/internals/datastore/parts/server_part.dart'; +import 'package:mineral/infrastructure/kernel/kernel.dart'; +import 'package:mineral/infrastructure/services/http/http_client.dart'; + +abstract class DataStoreContract { + HttpClient get client; + + ChannelPart get channel; + + ServerPart get server; + + MemberPart get member; + + RolePart get role; + + InteractionPart get interaction; + + ServerMessagePart get serverMessage; +} + +final class DataStore implements DataStoreContract { + @override + final HttpClient client; + + late final KernelContract kernel; + + @override + late final ChannelPart channel; + + @override + late final ServerPart server; + + @override + late final MemberPart member; + + @override + late final RolePart role; + + @override + late final ServerMessagePart serverMessage; + + @override + late final InteractionPart interaction; + + DataStore(this.client); + + void init() { + channel = ChannelPart(kernel); + server = ServerPart(kernel); + member = MemberPart(kernel); + role = RolePart(kernel); + interaction = InteractionPart(kernel); + serverMessage = ServerMessagePart(kernel); + } +} diff --git a/lib/infrastructure/internals/datastore/parts/server_message_part.dart b/lib/infrastructure/internals/datastore/parts/server_message_part.dart new file mode 100644 index 000000000..a8851cd7f --- /dev/null +++ b/lib/infrastructure/internals/datastore/parts/server_message_part.dart @@ -0,0 +1,36 @@ +import 'package:mineral/api/common/embed/message_embed.dart'; +import 'package:mineral/api/common/snowflake.dart'; +import 'package:mineral/infrastructure/commons/helper.dart'; +import 'package:mineral/infrastructure/internals/datastore/data_store_part.dart'; +import 'package:mineral/infrastructure/kernel/kernel.dart'; +import 'package:mineral/infrastructure/services/http/http_client_status.dart'; + +final class ServerMessagePart implements DataStorePart { + final KernelContract _kernel; + + HttpClientStatus get status => _kernel.dataStore.client.status; + + ServerMessagePart(this._kernel); + + Future update({ required Snowflake id, required Snowflake channelId, required Map payload }) async { + await _kernel.dataStore.client.patch('/channels/$channelId/messages/$id', body: payload); + } + + Future reply({ required Snowflake id, required Snowflake channelId, String? content, List? embeds }) async { + await _kernel.dataStore.client.post('/channels/$channelId/messages', body: { + 'content': content, + 'embeds': await Helper.createOrNullAsync( + field: embeds, + fn: () async => + embeds?.map(_kernel.marshaller.serializers.embed.deserialize).toList()), + 'message_reference': { + 'message_id': id, + 'channel_id': channelId + } + }); + } + + Future delete({ required Snowflake id, required Snowflake channelId }) async { + await _kernel.dataStore.client.delete('/channels/$channelId/messages/$id'); + } +} diff --git a/lib/src/api/server/server_message.dart b/lib/src/api/server/server_message.dart index 3f8cb56e9..93c59fc29 100644 --- a/lib/src/api/server/server_message.dart +++ b/lib/src/api/server/server_message.dart @@ -6,6 +6,8 @@ import 'package:mineral/src/api/server/channels/server_channel.dart'; import 'package:mineral/src/api/server/member.dart'; final class ServerMessage extends Message { + ServerMessagePart get _dataStoreServerMessage => + ioc.resolve().serverMessage; final MessageProperties _properties; @override @@ -28,8 +30,24 @@ final class ServerMessage extends Message { final Member author; - ServerMessage( - this._properties, { + ServerMessage(this._properties, { required this.author, }); + + Future edit(String content) async { + _dataStoreServerMessage + .update(id: id, channelId: channelId, payload: {'content': content}); + } + + Future reply({String? content, List? embeds}) async { + if (channel.type != ChannelType.guildText) { + return; + } + + _dataStoreServerMessage.reply(id: id, channelId: channelId, content: content, embeds: embeds); + } + + Future delete() async { + await _dataStoreServerMessage.delete(id: id, channelId: channelId); + } } From 0a03ce6dcb8a27ad89c38488ae34d60f8660c648 Mon Sep 17 00:00:00 2001 From: DakioCode Date: Mon, 15 Jul 2024 20:18:47 +0200 Subject: [PATCH 02/20] feat(server-messages) ServerMessage.pin() --- .../datastore/parts/server_message_part.dart | 4 ++ .../listeners/channel_pins_update_packet.dart | 56 +++++++++++++++++++ lib/src/api/server/server_message.dart | 4 ++ 3 files changed, 64 insertions(+) create mode 100644 lib/infrastructure/internals/packets/listeners/channel_pins_update_packet.dart diff --git a/lib/infrastructure/internals/datastore/parts/server_message_part.dart b/lib/infrastructure/internals/datastore/parts/server_message_part.dart index a8851cd7f..de24fa0ca 100644 --- a/lib/infrastructure/internals/datastore/parts/server_message_part.dart +++ b/lib/infrastructure/internals/datastore/parts/server_message_part.dart @@ -30,6 +30,10 @@ final class ServerMessagePart implements DataStorePart { }); } + Future pin({ required Snowflake id, required Snowflake channelId }) async { + await _kernel.dataStore.client.put('/channels/$channelId/pins/$id'); + } + Future delete({ required Snowflake id, required Snowflake channelId }) async { await _kernel.dataStore.client.delete('/channels/$channelId/messages/$id'); } diff --git a/lib/infrastructure/internals/packets/listeners/channel_pins_update_packet.dart b/lib/infrastructure/internals/packets/listeners/channel_pins_update_packet.dart new file mode 100644 index 000000000..0e027a12d --- /dev/null +++ b/lib/infrastructure/internals/packets/listeners/channel_pins_update_packet.dart @@ -0,0 +1,56 @@ +import 'package:mineral/api/private/channels/private_channel.dart'; +import 'package:mineral/api/server/channels/server_channel.dart'; +import 'package:mineral/infrastructure/services/logger/logger.dart'; +import 'package:mineral/infrastructure/internals/packets/listenable_packet.dart'; +import 'package:mineral/infrastructure/internals/packets/packet_type.dart'; +import 'package:mineral/infrastructure/internals/marshaller/marshaller.dart'; +import 'package:mineral/domains/events/event.dart'; +import 'package:mineral/infrastructure/internals/wss/shard_message.dart'; + +final class ChannelPinsUpdatePacket implements ListenablePacket { + @override + PacketType get packetType => PacketType.channelPinsUpdate; + + final LoggerContract logger; + final MarshallerContract marshaller; + + const ChannelPinsUpdatePacket(this.logger, this.marshaller); + + @override + Future listen(ShardMessage message, DispatchEvent dispatch) async { + final channel = await marshaller.serializers.channels.serialize(message.payload); + + return switch (channel) { + ServerChannel() => registerServerChannelPins(channel, dispatch), + PrivateChannel() => registerPrivateChannelPins(channel, dispatch), + _ => logger.warn("Unknown channel type: $channel contact Mineral's core team.") + }; + } + + Future registerServerChannelPins(ServerChannel channel, DispatchEvent dispatch) async { + final server = await marshaller.dataStore.server.getServer(channel.guildId); + final serverCacheKey = marshaller.cacheKey.server(server.id); + final channelCacheKey = marshaller.cacheKey.serverChannel(serverId: server.id, channelId: channel.id); + + server.channels.list.update(channel.id, (_) => channel); + + final rawServer = await marshaller.serializers.server.deserialize(server); + final rawChannel = await marshaller.serializers.channels.deserialize(channel); + + await Future.wait([ + marshaller.cache.put(serverCacheKey, rawServer), + marshaller.cache.put(channelCacheKey, rawChannel) + ]); + + dispatch(event: Event.serverChannelPinsUpdate, params: [server, channel]); + } + + Future registerPrivateChannelPins(PrivateChannel channel, DispatchEvent dispatch) async { + final channelCacheKey = marshaller.cacheKey.privateChannel(channel.id); + + final rawChannel = await marshaller.serializers.channels.deserialize(channel); + await marshaller.cache.put(channelCacheKey, rawChannel); + + dispatch(event: Event.privateChannelPinsUpdate, params: [channel]); + } +} diff --git a/lib/src/api/server/server_message.dart b/lib/src/api/server/server_message.dart index 93c59fc29..44e7f6c85 100644 --- a/lib/src/api/server/server_message.dart +++ b/lib/src/api/server/server_message.dart @@ -47,6 +47,10 @@ final class ServerMessage extends Message { _dataStoreServerMessage.reply(id: id, channelId: channelId, content: content, embeds: embeds); } + Future pin() async { + await _dataStoreServerMessage.pin(id: id, channelId: channelId); + } + Future delete() async { await _dataStoreServerMessage.delete(id: id, channelId: channelId); } From ad24688efb9a2f1c3ea48af329265c5db6623bfa Mon Sep 17 00:00:00 2001 From: DakioCode Date: Mon, 15 Jul 2024 21:50:03 +0200 Subject: [PATCH 03/20] feat(server-messages): ServerMessage.unpin() --- .../datastore/parts/server_message_part.dart | 4 ++++ lib/src/api/server/server_message.dart | 17 ++++++++++++++++- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/lib/infrastructure/internals/datastore/parts/server_message_part.dart b/lib/infrastructure/internals/datastore/parts/server_message_part.dart index de24fa0ca..a9f2918d3 100644 --- a/lib/infrastructure/internals/datastore/parts/server_message_part.dart +++ b/lib/infrastructure/internals/datastore/parts/server_message_part.dart @@ -34,6 +34,10 @@ final class ServerMessagePart implements DataStorePart { await _kernel.dataStore.client.put('/channels/$channelId/pins/$id'); } + Future unpin({ required Snowflake id, required Snowflake channelId }) async { + await _kernel.dataStore.client.delete('/channels/$channelId/pins/$id'); + } + Future delete({ required Snowflake id, required Snowflake channelId }) async { await _kernel.dataStore.client.delete('/channels/$channelId/messages/$id'); } diff --git a/lib/src/api/server/server_message.dart b/lib/src/api/server/server_message.dart index 44e7f6c85..ef3aa0598 100644 --- a/lib/src/api/server/server_message.dart +++ b/lib/src/api/server/server_message.dart @@ -4,6 +4,16 @@ import 'package:mineral/src/api/common/message_properties.dart'; import 'package:mineral/src/api/common/snowflake.dart'; import 'package:mineral/src/api/server/channels/server_channel.dart'; import 'package:mineral/src/api/server/member.dart'; +import 'package:mineral/api/common/embed/message_embed.dart'; +import 'package:mineral/api/common/message.dart'; +import 'package:mineral/api/common/message_properties.dart'; +import 'package:mineral/api/common/snowflake.dart'; +import 'package:mineral/api/server/channels/server_channel.dart'; +import 'package:mineral/api/server/channels/server_text_channel.dart'; +import 'package:mineral/api/server/member.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_message_part.dart'; final class ServerMessage extends Message { ServerMessagePart get _dataStoreServerMessage => @@ -30,7 +40,8 @@ final class ServerMessage extends Message { final Member author; - ServerMessage(this._properties, { + ServerMessage( + this._properties, { required this.author, }); @@ -51,6 +62,10 @@ final class ServerMessage extends Message { await _dataStoreServerMessage.pin(id: id, channelId: channelId); } + Future unpin() async { + await _dataStoreServerMessage.unpin(id: id, channelId: channelId); + } + Future delete() async { await _dataStoreServerMessage.delete(id: id, channelId: channelId); } From 30f5fc0ba42948ba625db0f81a3ecfb3a58743bc Mon Sep 17 00:00:00 2001 From: DakioCode Date: Mon, 15 Jul 2024 22:00:39 +0200 Subject: [PATCH 04/20] feat(server-messages): ServerMessage.crosspost() --- .../internals/datastore/parts/server_message_part.dart | 4 ++++ lib/src/api/server/server_message.dart | 9 +++++++++ 2 files changed, 13 insertions(+) diff --git a/lib/infrastructure/internals/datastore/parts/server_message_part.dart b/lib/infrastructure/internals/datastore/parts/server_message_part.dart index a9f2918d3..81d348e28 100644 --- a/lib/infrastructure/internals/datastore/parts/server_message_part.dart +++ b/lib/infrastructure/internals/datastore/parts/server_message_part.dart @@ -38,6 +38,10 @@ final class ServerMessagePart implements DataStorePart { 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 delete({ required Snowflake id, required Snowflake channelId }) async { await _kernel.dataStore.client.delete('/channels/$channelId/messages/$id'); } diff --git a/lib/src/api/server/server_message.dart b/lib/src/api/server/server_message.dart index ef3aa0598..42f585668 100644 --- a/lib/src/api/server/server_message.dart +++ b/lib/src/api/server/server_message.dart @@ -8,6 +8,7 @@ import 'package:mineral/api/common/embed/message_embed.dart'; import 'package:mineral/api/common/message.dart'; import 'package:mineral/api/common/message_properties.dart'; import 'package:mineral/api/common/snowflake.dart'; +import 'package:mineral/api/common/types/channel_type.dart'; import 'package:mineral/api/server/channels/server_channel.dart'; import 'package:mineral/api/server/channels/server_text_channel.dart'; import 'package:mineral/api/server/member.dart'; @@ -66,6 +67,14 @@ final class ServerMessage extends Message { await _dataStoreServerMessage.unpin(id: id, channelId: channelId); } + Future crosspost() async { + if (channel.type != ChannelType.guildAnnouncement) { + return; + } + + await _dataStoreServerMessage.crosspost(id: id, channelId: channelId); + } + Future delete() async { await _dataStoreServerMessage.delete(id: id, channelId: channelId); } From 7c2c447ec87bb0d9e3e304fa2e07e33b92469001 Mon Sep 17 00:00:00 2001 From: DakioCode Date: Tue, 16 Jul 2024 11:06:12 +0200 Subject: [PATCH 05/20] feat(server-messages) added cache put in ServerMessagePart.update() and ServerMessagePart.reply() --- .../datastore/parts/server_message_part.dart | 40 +++++++++++++++++-- lib/src/api/server/server_message.dart | 8 ++-- 2 files changed, 40 insertions(+), 8 deletions(-) diff --git a/lib/infrastructure/internals/datastore/parts/server_message_part.dart b/lib/infrastructure/internals/datastore/parts/server_message_part.dart index 81d348e28..204153ab2 100644 --- a/lib/infrastructure/internals/datastore/parts/server_message_part.dart +++ b/lib/infrastructure/internals/datastore/parts/server_message_part.dart @@ -1,9 +1,13 @@ +import 'dart:io'; + import 'package:mineral/api/common/embed/message_embed.dart'; import 'package:mineral/api/common/snowflake.dart'; +import 'package:mineral/api/server/server_message.dart'; import 'package:mineral/infrastructure/commons/helper.dart'; import 'package:mineral/infrastructure/internals/datastore/data_store_part.dart'; import 'package:mineral/infrastructure/kernel/kernel.dart'; import 'package:mineral/infrastructure/services/http/http_client_status.dart'; +import 'package:mineral/infrastructure/services/http/response.dart'; final class ServerMessagePart implements DataStorePart { final KernelContract _kernel; @@ -12,12 +16,21 @@ final class ServerMessagePart implements DataStorePart { ServerMessagePart(this._kernel); - Future update({ required Snowflake id, required Snowflake channelId, required Map payload }) async { - await _kernel.dataStore.client.patch('/channels/$channelId/messages/$id', body: payload); + Future update({ required Snowflake id, required Snowflake channelId, required Map payload }) async { + final response = await _kernel.dataStore.client.patch('/channels/$channelId/messages/$id', body: payload); + + final ServerMessage? serverMessage = await serializeResponse(response); + + if (serverMessage != null) { + final rawServerMessage = _kernel.marshaller.serializers.message.deserialize(response.body); + await _kernel.marshaller.cache.put(serverMessage.id, rawServerMessage); + } + + return serverMessage; } - Future reply({ required Snowflake id, required Snowflake channelId, String? content, List? embeds }) async { - await _kernel.dataStore.client.post('/channels/$channelId/messages', body: { + Future reply({ required Snowflake id, required Snowflake channelId, String? content, List? embeds }) async { + final response = await _kernel.dataStore.client.post('/channels/$channelId/messages', body: { 'content': content, 'embeds': await Helper.createOrNullAsync( field: embeds, @@ -28,6 +41,15 @@ final class ServerMessagePart implements DataStorePart { 'channel_id': channelId } }); + + final ServerMessage? serverMessage = await serializeResponse(response); + + if (serverMessage != null) { + final rawServerMessage = _kernel.marshaller.serializers.message.deserialize(response.body); + await _kernel.marshaller.cache.put(serverMessage.id, rawServerMessage); + } + + return serverMessage; } Future pin({ required Snowflake id, required Snowflake channelId }) async { @@ -45,4 +67,14 @@ final class ServerMessagePart implements DataStorePart { Future delete({ required Snowflake id, required Snowflake channelId }) async { await _kernel.dataStore.client.delete('/channels/$channelId/messages/$id'); } + + Future serializeResponse(Response response) async { + return switch(response.statusCode) { + int() when (status.isSuccess(response.statusCode)) => + _kernel.marshaller.serializers.message.serialize(response.body), + int() when (status.isError(response.statusCode)) => + throw HttpException(response.bodyString), + _ => throw Exception('Unknown status code: ${response.statusCode} ${response.bodyString}') + } as Future; + } } diff --git a/lib/src/api/server/server_message.dart b/lib/src/api/server/server_message.dart index 42f585668..743069c4e 100644 --- a/lib/src/api/server/server_message.dart +++ b/lib/src/api/server/server_message.dart @@ -10,7 +10,6 @@ import 'package:mineral/api/common/message_properties.dart'; import 'package:mineral/api/common/snowflake.dart'; import 'package:mineral/api/common/types/channel_type.dart'; import 'package:mineral/api/server/channels/server_channel.dart'; -import 'package:mineral/api/server/channels/server_text_channel.dart'; import 'package:mineral/api/server/member.dart'; import 'package:mineral/infrastructure/internals/container/ioc_container.dart'; import 'package:mineral/infrastructure/internals/datastore/data_store.dart'; @@ -46,8 +45,8 @@ final class ServerMessage extends Message { required this.author, }); - Future edit(String content) async { - _dataStoreServerMessage + Future edit(String content) async { + return _dataStoreServerMessage .update(id: id, channelId: channelId, payload: {'content': content}); } @@ -56,7 +55,8 @@ final class ServerMessage extends Message { return; } - _dataStoreServerMessage.reply(id: id, channelId: channelId, content: content, embeds: embeds); + _dataStoreServerMessage.reply( + id: id, channelId: channelId, content: content, embeds: embeds); } Future pin() async { From 2b1ae527aa2bd8fe96a21f31759512f220360524 Mon Sep 17 00:00:00 2001 From: DakioCode Date: Thu, 11 Jul 2024 08:49:52 +0200 Subject: [PATCH 06/20] ServerMessage.delete(), ServerMessage.edit(), ServerMessage.reply() --- .../internals/datastore/data_store.dart | 5 +++-- .../datastore/parts/server_message_part.dart | 6 ++---- lib/src/api/server/server_message.dart | 17 +++++++++++++++++ 3 files changed, 22 insertions(+), 6 deletions(-) diff --git a/lib/infrastructure/internals/datastore/data_store.dart b/lib/infrastructure/internals/datastore/data_store.dart index 6013a0303..e2c383c29 100644 --- a/lib/infrastructure/internals/datastore/data_store.dart +++ b/lib/infrastructure/internals/datastore/data_store.dart @@ -18,9 +18,9 @@ abstract class DataStoreContract { RolePart get role; - InteractionPart get interaction; - ServerMessagePart get serverMessage; + + InteractionPart get interaction; } final class DataStore implements DataStoreContract { @@ -56,5 +56,6 @@ final class DataStore implements DataStoreContract { role = RolePart(kernel); interaction = InteractionPart(kernel); serverMessage = ServerMessagePart(kernel); + serverMessage = ServerMessagePart(kernel); } } diff --git a/lib/infrastructure/internals/datastore/parts/server_message_part.dart b/lib/infrastructure/internals/datastore/parts/server_message_part.dart index 204153ab2..98c9840c4 100644 --- a/lib/infrastructure/internals/datastore/parts/server_message_part.dart +++ b/lib/infrastructure/internals/datastore/parts/server_message_part.dart @@ -1,5 +1,3 @@ -import 'dart:io'; - import 'package:mineral/api/common/embed/message_embed.dart'; import 'package:mineral/api/common/snowflake.dart'; import 'package:mineral/api/server/server_message.dart'; @@ -18,7 +16,7 @@ final class ServerMessagePart implements DataStorePart { Future update({ required Snowflake id, required Snowflake channelId, required Map payload }) async { final response = await _kernel.dataStore.client.patch('/channels/$channelId/messages/$id', body: payload); - + final ServerMessage? serverMessage = await serializeResponse(response); if (serverMessage != null) { @@ -72,7 +70,7 @@ final class ServerMessagePart implements DataStorePart { return switch(response.statusCode) { int() when (status.isSuccess(response.statusCode)) => _kernel.marshaller.serializers.message.serialize(response.body), - int() when (status.isError(response.statusCode)) => + int() when (status.isError(response.statusCode)) => throw HttpException(response.bodyString), _ => throw Exception('Unknown status code: ${response.statusCode} ${response.bodyString}') } as Future; diff --git a/lib/src/api/server/server_message.dart b/lib/src/api/server/server_message.dart index 743069c4e..45c62e006 100644 --- a/lib/src/api/server/server_message.dart +++ b/lib/src/api/server/server_message.dart @@ -45,6 +45,23 @@ final class ServerMessage extends Message { required this.author, }); + Future edit(String content) async { + _dataStoreServerMessage + .update(id: id, channelId: channelId, payload: {'content': content}); + } + + Future reply({String? content, List? embeds}) async { + if (channel.type != ChannelType.guildText) { + return; + } + + _dataStoreServerMessage.reply(id: id, channelId: channelId, content: content, embeds: embeds); + } + + Future delete() async { + await _dataStoreServerMessage.delete(id: id, channelId: channelId); + } + Future edit(String content) async { return _dataStoreServerMessage .update(id: id, channelId: channelId, payload: {'content': content}); From fe3e1dda63f9ca0138b552b0f030cd0053a8c9b6 Mon Sep 17 00:00:00 2001 From: DakioCode Date: Mon, 15 Jul 2024 20:18:47 +0200 Subject: [PATCH 07/20] feat(server-messages) ServerMessage.pin() --- .../internals/datastore/parts/server_message_part.dart | 4 ++++ lib/src/api/server/server_message.dart | 4 ++++ 2 files changed, 8 insertions(+) diff --git a/lib/infrastructure/internals/datastore/parts/server_message_part.dart b/lib/infrastructure/internals/datastore/parts/server_message_part.dart index 98c9840c4..2033d9747 100644 --- a/lib/infrastructure/internals/datastore/parts/server_message_part.dart +++ b/lib/infrastructure/internals/datastore/parts/server_message_part.dart @@ -62,6 +62,10 @@ final class ServerMessagePart implements DataStorePart { await _kernel.dataStore.client.post('/channels/$channelId/messages/$id/crosspost'); } + Future pin({ required Snowflake id, required Snowflake channelId }) async { + await _kernel.dataStore.client.put('/channels/$channelId/pins/$id'); + } + Future delete({ required Snowflake id, required Snowflake channelId }) async { await _kernel.dataStore.client.delete('/channels/$channelId/messages/$id'); } diff --git a/lib/src/api/server/server_message.dart b/lib/src/api/server/server_message.dart index 45c62e006..70a7d9e51 100644 --- a/lib/src/api/server/server_message.dart +++ b/lib/src/api/server/server_message.dart @@ -58,6 +58,10 @@ final class ServerMessage extends Message { _dataStoreServerMessage.reply(id: id, channelId: channelId, content: content, embeds: embeds); } + Future pin() async { + await _dataStoreServerMessage.pin(id: id, channelId: channelId); + } + Future delete() async { await _dataStoreServerMessage.delete(id: id, channelId: channelId); } From 28693334201687e0280acce1fb81efdfe7abc2b6 Mon Sep 17 00:00:00 2001 From: DakioCode Date: Mon, 15 Jul 2024 21:50:03 +0200 Subject: [PATCH 08/20] feat(server-messages): ServerMessage.unpin() --- .../datastore/parts/server_message_part.dart | 4 ++++ lib/src/api/server/server_message.dart | 11 +++++++---- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/lib/infrastructure/internals/datastore/parts/server_message_part.dart b/lib/infrastructure/internals/datastore/parts/server_message_part.dart index 2033d9747..10c58aed5 100644 --- a/lib/infrastructure/internals/datastore/parts/server_message_part.dart +++ b/lib/infrastructure/internals/datastore/parts/server_message_part.dart @@ -66,6 +66,10 @@ final class ServerMessagePart implements DataStorePart { await _kernel.dataStore.client.put('/channels/$channelId/pins/$id'); } + Future unpin({ required Snowflake id, required Snowflake channelId }) async { + await _kernel.dataStore.client.delete('/channels/$channelId/pins/$id'); + } + Future delete({ required Snowflake id, required Snowflake channelId }) async { await _kernel.dataStore.client.delete('/channels/$channelId/messages/$id'); } diff --git a/lib/src/api/server/server_message.dart b/lib/src/api/server/server_message.dart index 70a7d9e51..5d8845e25 100644 --- a/lib/src/api/server/server_message.dart +++ b/lib/src/api/server/server_message.dart @@ -8,8 +8,8 @@ import 'package:mineral/api/common/embed/message_embed.dart'; import 'package:mineral/api/common/message.dart'; import 'package:mineral/api/common/message_properties.dart'; import 'package:mineral/api/common/snowflake.dart'; -import 'package:mineral/api/common/types/channel_type.dart'; import 'package:mineral/api/server/channels/server_channel.dart'; +import 'package:mineral/api/server/channels/server_text_channel.dart'; import 'package:mineral/api/server/member.dart'; import 'package:mineral/infrastructure/internals/container/ioc_container.dart'; import 'package:mineral/infrastructure/internals/datastore/data_store.dart'; @@ -46,12 +46,11 @@ final class ServerMessage extends Message { }); Future edit(String content) async { - _dataStoreServerMessage - .update(id: id, channelId: channelId, payload: {'content': content}); + _dataStoreServerMessage.update(id: id, channelId: channelId, payload: {'content': content}); } Future reply({String? content, List? embeds}) async { - if (channel.type != ChannelType.guildText) { + if (channel is ServerTextChannel) { return; } @@ -62,6 +61,10 @@ final class ServerMessage extends Message { await _dataStoreServerMessage.pin(id: id, channelId: channelId); } + Future unpin() async { + await _dataStoreServerMessage.unpin(id: id, channelId: channelId); + } + Future delete() async { await _dataStoreServerMessage.delete(id: id, channelId: channelId); } From 0e2d57fb8e0cdb243c5198184832790a5d749dd8 Mon Sep 17 00:00:00 2001 From: DakioCode Date: Mon, 15 Jul 2024 22:00:39 +0200 Subject: [PATCH 09/20] feat(server-messages): ServerMessage.crosspost() --- .../datastore/parts/server_message_part.dart | 4 ++++ lib/src/api/server/server_message.dart | 11 ++++++++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/lib/infrastructure/internals/datastore/parts/server_message_part.dart b/lib/infrastructure/internals/datastore/parts/server_message_part.dart index 10c58aed5..1da6b394e 100644 --- a/lib/infrastructure/internals/datastore/parts/server_message_part.dart +++ b/lib/infrastructure/internals/datastore/parts/server_message_part.dart @@ -70,6 +70,10 @@ final class ServerMessagePart implements DataStorePart { 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 delete({ required Snowflake id, required Snowflake channelId }) async { await _kernel.dataStore.client.delete('/channels/$channelId/messages/$id'); } diff --git a/lib/src/api/server/server_message.dart b/lib/src/api/server/server_message.dart index 5d8845e25..33c457cc5 100644 --- a/lib/src/api/server/server_message.dart +++ b/lib/src/api/server/server_message.dart @@ -8,6 +8,7 @@ import 'package:mineral/api/common/embed/message_embed.dart'; import 'package:mineral/api/common/message.dart'; import 'package:mineral/api/common/message_properties.dart'; import 'package:mineral/api/common/snowflake.dart'; +import 'package:mineral/api/common/types/channel_type.dart'; import 'package:mineral/api/server/channels/server_channel.dart'; import 'package:mineral/api/server/channels/server_text_channel.dart'; import 'package:mineral/api/server/member.dart'; @@ -50,7 +51,7 @@ final class ServerMessage extends Message { } Future reply({String? content, List? embeds}) async { - if (channel is ServerTextChannel) { + if (channel.type != ChannelType.guildText) { return; } @@ -65,6 +66,14 @@ final class ServerMessage extends Message { await _dataStoreServerMessage.unpin(id: id, channelId: channelId); } + Future crosspost() async { + if (channel.type != ChannelType.guildAnnouncement) { + return; + } + + await _dataStoreServerMessage.crosspost(id: id, channelId: channelId); + } + Future delete() async { await _dataStoreServerMessage.delete(id: id, channelId: channelId); } From 54e9274dc323da3d43381ac0e9b959dcd1f2f998 Mon Sep 17 00:00:00 2001 From: Baptiste Parmantier Date: Sun, 4 Aug 2024 18:33:54 +0200 Subject: [PATCH 10/20] feat(messages): remove duplicate methods --- .../internals/datastore/data_store.dart | 1 - lib/src/api/server/server_message.dart | 39 ++----------------- 2 files changed, 3 insertions(+), 37 deletions(-) diff --git a/lib/infrastructure/internals/datastore/data_store.dart b/lib/infrastructure/internals/datastore/data_store.dart index e2c383c29..c866c3e78 100644 --- a/lib/infrastructure/internals/datastore/data_store.dart +++ b/lib/infrastructure/internals/datastore/data_store.dart @@ -56,6 +56,5 @@ final class DataStore implements DataStoreContract { role = RolePart(kernel); interaction = InteractionPart(kernel); serverMessage = ServerMessagePart(kernel); - serverMessage = ServerMessagePart(kernel); } } diff --git a/lib/src/api/server/server_message.dart b/lib/src/api/server/server_message.dart index 33c457cc5..0d7853e3f 100644 --- a/lib/src/api/server/server_message.dart +++ b/lib/src/api/server/server_message.dart @@ -12,13 +12,13 @@ import 'package:mineral/api/common/types/channel_type.dart'; import 'package:mineral/api/server/channels/server_channel.dart'; import 'package:mineral/api/server/channels/server_text_channel.dart'; import 'package:mineral/api/server/member.dart'; +import 'package:mineral/api/server/server.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_message_part.dart'; final class ServerMessage extends Message { - ServerMessagePart get _dataStoreServerMessage => - ioc.resolve().serverMessage; + ServerMessagePart get _dataStoreServerMessage => ioc.resolve().serverMessage; final MessageProperties _properties; @override @@ -50,46 +50,13 @@ final class ServerMessage extends Message { _dataStoreServerMessage.update(id: id, channelId: channelId, payload: {'content': content}); } - Future reply({String? content, List? embeds}) async { - if (channel.type != ChannelType.guildText) { - return; - } - - _dataStoreServerMessage.reply(id: id, channelId: channelId, content: content, embeds: embeds); - } - - Future pin() async { - await _dataStoreServerMessage.pin(id: id, channelId: channelId); - } - - Future unpin() async { - await _dataStoreServerMessage.unpin(id: id, channelId: channelId); - } - - Future crosspost() async { - if (channel.type != ChannelType.guildAnnouncement) { - return; - } - - await _dataStoreServerMessage.crosspost(id: id, channelId: channelId); - } - - Future delete() async { - await _dataStoreServerMessage.delete(id: id, channelId: channelId); - } - - Future edit(String content) async { - return _dataStoreServerMessage - .update(id: id, channelId: channelId, payload: {'content': content}); - } - Future reply({String? content, List? embeds}) async { if (channel.type != ChannelType.guildText) { return; } _dataStoreServerMessage.reply( - id: id, channelId: channelId, content: content, embeds: embeds); + id: id, channelId: channelId, serverId: channel.guildId, content: content, embeds: embeds); } Future pin() async { From 6ebffb18ab58004cd468d5e6422b5e72717cdb84 Mon Sep 17 00:00:00 2001 From: Baptiste Parmantier Date: Sun, 4 Aug 2024 18:34:15 +0200 Subject: [PATCH 11/20] feat(messages): enhance serialization --- .../datastore/parts/server_message_part.dart | 83 ++++++++++--------- .../marshaller/serializer_bucket.dart | 0 .../serializers/message_serializer.dart | 44 ++++++++++ .../marshaller/types/message_factory.dart | 0 .../listeners/channel_pins_update_packet.dart | 8 +- 5 files changed, 91 insertions(+), 44 deletions(-) create mode 100644 lib/infrastructure/internals/marshaller/serializer_bucket.dart create mode 100644 lib/infrastructure/internals/marshaller/serializers/message_serializer.dart create mode 100644 lib/infrastructure/internals/marshaller/types/message_factory.dart diff --git a/lib/infrastructure/internals/datastore/parts/server_message_part.dart b/lib/infrastructure/internals/datastore/parts/server_message_part.dart index 1da6b394e..a9ae1087e 100644 --- a/lib/infrastructure/internals/datastore/parts/server_message_part.dart +++ b/lib/infrastructure/internals/datastore/parts/server_message_part.dart @@ -1,11 +1,11 @@ import 'package:mineral/api/common/embed/message_embed.dart'; import 'package:mineral/api/common/snowflake.dart'; +import 'package:mineral/api/server/channels/server_channel.dart'; import 'package:mineral/api/server/server_message.dart'; import 'package:mineral/infrastructure/commons/helper.dart'; import 'package:mineral/infrastructure/internals/datastore/data_store_part.dart'; import 'package:mineral/infrastructure/kernel/kernel.dart'; import 'package:mineral/infrastructure/services/http/http_client_status.dart'; -import 'package:mineral/infrastructure/services/http/response.dart'; final class ServerMessagePart implements DataStorePart { final KernelContract _kernel; @@ -14,59 +14,72 @@ final class ServerMessagePart implements DataStorePart { ServerMessagePart(this._kernel); - Future update({ required Snowflake id, required Snowflake channelId, required Map payload }) async { - final response = await _kernel.dataStore.client.patch('/channels/$channelId/messages/$id', body: payload); + Future update( + {required Snowflake id, + required Snowflake channelId, + required Map payload}) async { + final response = + await _kernel.dataStore.client.patch('/channels/$channelId/messages/$id', body: payload); - final ServerMessage? serverMessage = await serializeResponse(response); + final ServerMessage? serverMessage = + await _kernel.marshaller.serializers.message.serializeRemote(response.body); if (serverMessage != null) { + final serverKey = _kernel.marshaller.cacheKey + .serverMessage(serverId: serverMessage.channel.server.id, messageId: serverMessage.id); final rawServerMessage = _kernel.marshaller.serializers.message.deserialize(response.body); - await _kernel.marshaller.cache.put(serverMessage.id, rawServerMessage); + + await _kernel.marshaller.cache.put(serverKey, rawServerMessage); } return serverMessage; } - Future reply({ required Snowflake id, required Snowflake channelId, String? content, List? embeds }) async { + Future reply( + {required Snowflake id, + required Snowflake channelId, + required Snowflake serverId, + String? content, + List? embeds}) async { final response = await _kernel.dataStore.client.post('/channels/$channelId/messages', body: { 'content': content, 'embeds': await Helper.createOrNullAsync( field: embeds, - fn: () async => - embeds?.map(_kernel.marshaller.serializers.embed.deserialize).toList()), - 'message_reference': { - 'message_id': id, - 'channel_id': channelId - } + fn: () async => embeds?.map(_kernel.marshaller.serializers.embed.deserialize).toList()), + 'message_reference': {'message_id': id, 'channel_id': channelId} }); - final ServerMessage? serverMessage = await serializeResponse(response); + final server = await _kernel.dataStore.server.getServer(serverId); + final channel = await _kernel.dataStore.channel.getChannel(channelId, serverId: serverId); + final ServerMessage? serverMessage = + await _kernel.marshaller.serializers.message.serializeRemote({ + ...response.body, + 'guild_id': serverId, + }); - if (serverMessage != null) { - final rawServerMessage = _kernel.marshaller.serializers.message.deserialize(response.body); - await _kernel.marshaller.cache.put(serverMessage.id, rawServerMessage); + if (channel is ServerChannel) { + channel.server = server; + serverMessage?.channel = channel; } - return serverMessage; - } - - Future pin({ required Snowflake id, required Snowflake channelId }) async { - await _kernel.dataStore.client.put('/channels/$channelId/pins/$id'); - } + if (serverMessage != null) { + final messageKey = _kernel.marshaller.cacheKey + .serverMessage(serverId: serverMessage.channel.server.id, messageId: serverMessage.id); + final message = await _kernel.marshaller.serializers.message + .serializeRemote({...response.body, 'guild_id': serverId}); + final rawServerMessage = await _kernel.marshaller.serializers.message.deserialize(message); - Future unpin({ required Snowflake id, required Snowflake channelId }) async { - await _kernel.dataStore.client.delete('/channels/$channelId/pins/$id'); - } + await _kernel.marshaller.cache.put(messageKey, rawServerMessage); + } - Future crosspost({required Snowflake id, required Snowflake channelId}) async { - await _kernel.dataStore.client.post('/channels/$channelId/messages/$id/crosspost'); + return serverMessage; } - 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'); } @@ -74,17 +87,7 @@ final class ServerMessagePart implements DataStorePart { 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'); } - - Future serializeResponse(Response response) async { - return switch(response.statusCode) { - int() when (status.isSuccess(response.statusCode)) => - _kernel.marshaller.serializers.message.serialize(response.body), - int() when (status.isError(response.statusCode)) => - throw HttpException(response.bodyString), - _ => throw Exception('Unknown status code: ${response.statusCode} ${response.bodyString}') - } as Future; - } } diff --git a/lib/infrastructure/internals/marshaller/serializer_bucket.dart b/lib/infrastructure/internals/marshaller/serializer_bucket.dart new file mode 100644 index 000000000..e69de29bb diff --git a/lib/infrastructure/internals/marshaller/serializers/message_serializer.dart b/lib/infrastructure/internals/marshaller/serializers/message_serializer.dart new file mode 100644 index 000000000..19ee2f9a5 --- /dev/null +++ b/lib/infrastructure/internals/marshaller/serializers/message_serializer.dart @@ -0,0 +1,44 @@ +import 'package:mineral/api/common/snowflake.dart'; +import 'package:mineral/api/private/channels/private_channel.dart'; +import 'package:mineral/api/server/channels/server_channel.dart'; +import 'package:mineral/infrastructure/internals/marshaller/factories/messages/private_message_factory.dart'; +import 'package:mineral/infrastructure/internals/marshaller/factories/messages/server_message_factory.dart'; +import 'package:mineral/infrastructure/internals/marshaller/marshaller.dart'; +import 'package:mineral/infrastructure/internals/marshaller/types/message_factory.dart'; +import 'package:mineral/infrastructure/internals/marshaller/types/serializer.dart'; + +final class MessageSerializer implements SerializerContract { + final MarshallerContract marshaller; + + final _serverMessageFactory = ServerMessageFactory(); + final _privateMessageFactory = PrivateMessageFactory(); + + MessageSerializer(this.marshaller); + + @override + Future serializeRemote(Map json) async { + final serverId = json['message_reference']?['guild_id']; + final channel = await marshaller.dataStore.channel.getChannel(Snowflake(json['channel_id']), + serverId: serverId != null ? Snowflake(serverId) : null); + + final factory = switch (channel) { + ServerChannel() => _serverMessageFactory, + PrivateChannel() => _privateMessageFactory, + _ => throw Exception('Channel type not found ${channel.runtimeType}'), + } as MessageFactory; + + return factory.serialize(marshaller, json); + } + + @override + Future serializeCache(Map json) { + throw UnimplementedError(); + } + + @override + Map deserialize(dynamic object) { + return { + 'id': object.id, + }; + } +} diff --git a/lib/infrastructure/internals/marshaller/types/message_factory.dart b/lib/infrastructure/internals/marshaller/types/message_factory.dart new file mode 100644 index 000000000..e69de29bb diff --git a/lib/infrastructure/internals/packets/listeners/channel_pins_update_packet.dart b/lib/infrastructure/internals/packets/listeners/channel_pins_update_packet.dart index 0e027a12d..ff037b935 100644 --- a/lib/infrastructure/internals/packets/listeners/channel_pins_update_packet.dart +++ b/lib/infrastructure/internals/packets/listeners/channel_pins_update_packet.dart @@ -1,11 +1,11 @@ import 'package:mineral/api/private/channels/private_channel.dart'; import 'package:mineral/api/server/channels/server_channel.dart'; -import 'package:mineral/infrastructure/services/logger/logger.dart'; +import 'package:mineral/domains/events/event.dart'; +import 'package:mineral/infrastructure/internals/marshaller/marshaller.dart'; import 'package:mineral/infrastructure/internals/packets/listenable_packet.dart'; import 'package:mineral/infrastructure/internals/packets/packet_type.dart'; -import 'package:mineral/infrastructure/internals/marshaller/marshaller.dart'; -import 'package:mineral/domains/events/event.dart'; import 'package:mineral/infrastructure/internals/wss/shard_message.dart'; +import 'package:mineral/infrastructure/services/logger/logger.dart'; final class ChannelPinsUpdatePacket implements ListenablePacket { @override @@ -18,7 +18,7 @@ final class ChannelPinsUpdatePacket implements ListenablePacket { @override Future listen(ShardMessage message, DispatchEvent dispatch) async { - final channel = await marshaller.serializers.channels.serialize(message.payload); + final channel = await marshaller.serializers.channels.serializeRemote(message.payload); return switch (channel) { ServerChannel() => registerServerChannelPins(channel, dispatch), From 79e02ed49b413ad0cb8013a008a57b29bfd62f9d Mon Sep 17 00:00:00 2001 From: Baptiste Parmantier Date: Sun, 4 Aug 2024 19:31:39 +0200 Subject: [PATCH 12/20] feat(marshaller): enhance message serialization --- .../internals/marshaller/serializers/message_serializer.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/infrastructure/internals/marshaller/serializers/message_serializer.dart b/lib/infrastructure/internals/marshaller/serializers/message_serializer.dart index 19ee2f9a5..10ed3a639 100644 --- a/lib/infrastructure/internals/marshaller/serializers/message_serializer.dart +++ b/lib/infrastructure/internals/marshaller/serializers/message_serializer.dart @@ -27,7 +27,7 @@ final class MessageSerializer implements SerializerContract { _ => throw Exception('Channel type not found ${channel.runtimeType}'), } as MessageFactory; - return factory.serialize(marshaller, json); + return factory.serializeRemote(marshaller, json); } @override From 1216e737ac10911674bf1d849f07abf24380a175 Mon Sep 17 00:00:00 2001 From: Baptiste Parmantier Date: Mon, 5 Aug 2024 09:27:19 +0200 Subject: [PATCH 13/20] feat(marshaller): compute channel --- .../marshaller/factories/messages/private_message_factory.dart | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 lib/infrastructure/internals/marshaller/factories/messages/private_message_factory.dart diff --git a/lib/infrastructure/internals/marshaller/factories/messages/private_message_factory.dart b/lib/infrastructure/internals/marshaller/factories/messages/private_message_factory.dart new file mode 100644 index 000000000..e69de29bb From c9021b7e529a5930d3e804c12a6d1ec460b52882 Mon Sep 17 00:00:00 2001 From: DakioCode Date: Thu, 8 Aug 2024 16:53:17 +0200 Subject: [PATCH 14/20] feat(ServerMessage) removed type checking in reply method --- lib/src/api/server/server_message.dart | 4 ---- 1 file changed, 4 deletions(-) diff --git a/lib/src/api/server/server_message.dart b/lib/src/api/server/server_message.dart index 0d7853e3f..742c9999a 100644 --- a/lib/src/api/server/server_message.dart +++ b/lib/src/api/server/server_message.dart @@ -51,10 +51,6 @@ final class ServerMessage extends Message { } Future reply({String? content, List? embeds}) async { - if (channel.type != ChannelType.guildText) { - return; - } - _dataStoreServerMessage.reply( id: id, channelId: channelId, serverId: channel.guildId, content: content, embeds: embeds); } From 30c196889245a6a554719a81a4af1a4b7c31250d Mon Sep 17 00:00:00 2001 From: DakioCode Date: Thu, 8 Aug 2024 17:02:45 +0200 Subject: [PATCH 15/20] feat(ServerMessage) added embeds in reply method (ServerMessagePart) removed possibility of null in update method --- .../datastore/parts/server_message_part.dart | 55 +++++++++++-------- 1 file changed, 33 insertions(+), 22 deletions(-) diff --git a/lib/infrastructure/internals/datastore/parts/server_message_part.dart b/lib/infrastructure/internals/datastore/parts/server_message_part.dart index a9ae1087e..b2cf65165 100644 --- a/lib/infrastructure/internals/datastore/parts/server_message_part.dart +++ b/lib/infrastructure/internals/datastore/parts/server_message_part.dart @@ -14,23 +14,23 @@ final class ServerMessagePart implements DataStorePart { ServerMessagePart(this._kernel); - Future update( + Future update( {required Snowflake id, required Snowflake channelId, required Map payload}) async { - final response = - await _kernel.dataStore.client.patch('/channels/$channelId/messages/$id', body: payload); + final response = await _kernel.dataStore.client + .patch('/channels/$channelId/messages/$id', body: payload); - final ServerMessage? serverMessage = - await _kernel.marshaller.serializers.message.serializeRemote(response.body); + final ServerMessage serverMessage = await _kernel + .marshaller.serializers.message + .serializeRemote(response.body); - if (serverMessage != null) { - final serverKey = _kernel.marshaller.cacheKey - .serverMessage(serverId: serverMessage.channel.server.id, messageId: serverMessage.id); - final rawServerMessage = _kernel.marshaller.serializers.message.deserialize(response.body); + final serverKey = _kernel.marshaller.cacheKey.serverMessage( + serverId: serverMessage.channel.server.id, messageId: serverMessage.id); + final rawServerMessage = + _kernel.marshaller.serializers.message.deserialize(response.body); - await _kernel.marshaller.cache.put(serverKey, rawServerMessage); - } + await _kernel.marshaller.cache.put(serverKey, rawServerMessage); return serverMessage; } @@ -41,16 +41,20 @@ final class ServerMessagePart implements DataStorePart { required Snowflake serverId, String? content, List? embeds}) 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': 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()), 'message_reference': {'message_id': id, 'channel_id': channelId} }); final server = await _kernel.dataStore.server.getServer(serverId); - final channel = await _kernel.dataStore.channel.getChannel(channelId, serverId: serverId); + final channel = await _kernel.dataStore.channel + .getChannel(channelId, serverId: serverId); final ServerMessage? serverMessage = await _kernel.marshaller.serializers.message.serializeRemote({ ...response.body, @@ -63,11 +67,13 @@ final class ServerMessagePart implements DataStorePart { } if (serverMessage != null) { - final messageKey = _kernel.marshaller.cacheKey - .serverMessage(serverId: serverMessage.channel.server.id, messageId: serverMessage.id); + final messageKey = _kernel.marshaller.cacheKey.serverMessage( + serverId: serverMessage.channel.server.id, + messageId: serverMessage.id); final message = await _kernel.marshaller.serializers.message .serializeRemote({...response.body, 'guild_id': serverId}); - final rawServerMessage = await _kernel.marshaller.serializers.message.deserialize(message); + final rawServerMessage = + await _kernel.marshaller.serializers.message.deserialize(message); await _kernel.marshaller.cache.put(messageKey, rawServerMessage); } @@ -75,19 +81,24 @@ final class ServerMessagePart implements DataStorePart { return serverMessage; } - 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'); } } From 3c56c42c4002990cc8f2ab09b124347992a6f8ed Mon Sep 17 00:00:00 2001 From: DakioCode Date: Thu, 8 Aug 2024 17:13:45 +0200 Subject: [PATCH 16/20] feat(ServerMessage) allowed embeds and components editing --- .../datastore/parts/server_message_part.dart | 22 ++++++++++++++----- lib/src/api/server/server_message.dart | 10 ++------- 2 files changed, 19 insertions(+), 13 deletions(-) diff --git a/lib/infrastructure/internals/datastore/parts/server_message_part.dart b/lib/infrastructure/internals/datastore/parts/server_message_part.dart index b2cf65165..27874836e 100644 --- a/lib/infrastructure/internals/datastore/parts/server_message_part.dart +++ b/lib/infrastructure/internals/datastore/parts/server_message_part.dart @@ -1,3 +1,4 @@ +import 'package:mineral/api/common/components/message_component.dart'; import 'package:mineral/api/common/embed/message_embed.dart'; import 'package:mineral/api/common/snowflake.dart'; import 'package:mineral/api/server/channels/server_channel.dart'; @@ -14,12 +15,23 @@ final class ServerMessagePart implements DataStorePart { ServerMessagePart(this._kernel); - Future update( - {required Snowflake id, - required Snowflake channelId, - required Map payload}) async { + Future update({ + required Snowflake id, + required Snowflake channelId, + String? content, + List? embeds, + List? components, + }) async { final response = await _kernel.dataStore.client - .patch('/channels/$channelId/messages/$id', body: payload); + .patch('/channels/$channelId/messages/$id', body: { + 'content': content, + 'embeds': await Helper.createOrNullAsync( + field: embeds, + fn: () async => embeds + ?.map(_kernel.marshaller.serializers.embed.deserialize) + .toList()), + 'components': components?.map((c) => c.toJson()).toList() + }); final ServerMessage serverMessage = await _kernel .marshaller.serializers.message diff --git a/lib/src/api/server/server_message.dart b/lib/src/api/server/server_message.dart index 742c9999a..50f902fd4 100644 --- a/lib/src/api/server/server_message.dart +++ b/lib/src/api/server/server_message.dart @@ -1,9 +1,3 @@ -import 'package:mineral/src/api/common/embed/message_embed.dart'; -import 'package:mineral/src/api/common/message.dart'; -import 'package:mineral/src/api/common/message_properties.dart'; -import 'package:mineral/src/api/common/snowflake.dart'; -import 'package:mineral/src/api/server/channels/server_channel.dart'; -import 'package:mineral/src/api/server/member.dart'; import 'package:mineral/api/common/embed/message_embed.dart'; import 'package:mineral/api/common/message.dart'; import 'package:mineral/api/common/message_properties.dart'; @@ -46,8 +40,8 @@ final class ServerMessage extends Message { required this.author, }); - Future edit(String content) async { - _dataStoreServerMessage.update(id: id, channelId: channelId, payload: {'content': content}); + Future edit(String? content, List? embeds, List? components) async { + _dataStoreServerMessage.update(id: id, channelId: channelId, content: content, embeds: embeds, components: components); } Future reply({String? content, List? embeds}) async { From 11530103050bea9849654a8a0fb43e3192bdd6b8 Mon Sep 17 00:00:00 2001 From: DakioCode Date: Thu, 8 Aug 2024 17:25:43 +0200 Subject: [PATCH 17/20] feat(ServerMessage) (ServerMessagePart) removed possibilities of of in reply method --- .../datastore/parts/server_message_part.dart | 29 +++++++++---------- 1 file changed, 13 insertions(+), 16 deletions(-) diff --git a/lib/infrastructure/internals/datastore/parts/server_message_part.dart b/lib/infrastructure/internals/datastore/parts/server_message_part.dart index 27874836e..b8ad0da9d 100644 --- a/lib/infrastructure/internals/datastore/parts/server_message_part.dart +++ b/lib/infrastructure/internals/datastore/parts/server_message_part.dart @@ -30,7 +30,7 @@ final class ServerMessagePart implements DataStorePart { fn: () async => embeds ?.map(_kernel.marshaller.serializers.embed.deserialize) .toList()), - 'components': components?.map((c) => c.toJson()).toList() + 'components': components?.map((c) => c.toJson()).toList() }); final ServerMessage serverMessage = await _kernel @@ -47,7 +47,7 @@ final class ServerMessagePart implements DataStorePart { return serverMessage; } - Future reply( + Future reply( {required Snowflake id, required Snowflake channelId, required Snowflake serverId, @@ -66,29 +66,26 @@ final class ServerMessagePart implements DataStorePart { final server = await _kernel.dataStore.server.getServer(serverId); final channel = await _kernel.dataStore.channel - .getChannel(channelId, serverId: serverId); - final ServerMessage? serverMessage = + .getChannel(channelId, serverId: serverId); + final ServerMessage serverMessage = await _kernel.marshaller.serializers.message.serializeRemote({ ...response.body, 'guild_id': serverId, }); - if (channel is ServerChannel) { + if (channel != null) { channel.server = server; - serverMessage?.channel = channel; + serverMessage.channel = channel; } - if (serverMessage != null) { - final messageKey = _kernel.marshaller.cacheKey.serverMessage( - serverId: serverMessage.channel.server.id, - messageId: serverMessage.id); - final message = await _kernel.marshaller.serializers.message - .serializeRemote({...response.body, 'guild_id': serverId}); - final rawServerMessage = - await _kernel.marshaller.serializers.message.deserialize(message); + final messageKey = _kernel.marshaller.cacheKey.serverMessage( + serverId: serverMessage.channel.server.id, messageId: serverMessage.id); + final message = await _kernel.marshaller.serializers.message + .serializeRemote({...response.body, 'guild_id': serverId}); + final rawServerMessage = + await _kernel.marshaller.serializers.message.deserialize(message); - await _kernel.marshaller.cache.put(messageKey, rawServerMessage); - } + await _kernel.marshaller.cache.put(messageKey, rawServerMessage); return serverMessage; } From 782ab9a81eb6d8b3750c8fdbb6adfe16d379f8a2 Mon Sep 17 00:00:00 2001 From: DakioCode Date: Thu, 8 Aug 2024 17:27:45 +0200 Subject: [PATCH 18/20] feat(ServerMessage) (ServerMessagePart) removed two times message serialization --- .../internals/datastore/parts/server_message_part.dart | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/lib/infrastructure/internals/datastore/parts/server_message_part.dart b/lib/infrastructure/internals/datastore/parts/server_message_part.dart index b8ad0da9d..a4eb78f05 100644 --- a/lib/infrastructure/internals/datastore/parts/server_message_part.dart +++ b/lib/infrastructure/internals/datastore/parts/server_message_part.dart @@ -80,10 +80,8 @@ final class ServerMessagePart implements DataStorePart { final messageKey = _kernel.marshaller.cacheKey.serverMessage( serverId: serverMessage.channel.server.id, messageId: serverMessage.id); - final message = await _kernel.marshaller.serializers.message - .serializeRemote({...response.body, 'guild_id': serverId}); final rawServerMessage = - await _kernel.marshaller.serializers.message.deserialize(message); + await _kernel.marshaller.serializers.message.deserialize(serverMessage); await _kernel.marshaller.cache.put(messageKey, rawServerMessage); From 8f079e065a8504ca1eb76bd0ebf109187c6dca6d Mon Sep 17 00:00:00 2001 From: DakioCode Date: Fri, 9 Aug 2024 09:59:42 +0200 Subject: [PATCH 19/20] feat(ServerMessage) add components to reply method --- .../datastore/parts/server_message_part.dart | 4 ++- lib/src/api/server/server_message.dart | 25 +++++++++++++++---- 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/lib/infrastructure/internals/datastore/parts/server_message_part.dart b/lib/infrastructure/internals/datastore/parts/server_message_part.dart index a4eb78f05..25cbd0284 100644 --- a/lib/infrastructure/internals/datastore/parts/server_message_part.dart +++ b/lib/infrastructure/internals/datastore/parts/server_message_part.dart @@ -52,7 +52,8 @@ final class ServerMessagePart implements DataStorePart { required Snowflake channelId, required Snowflake serverId, String? content, - List? embeds}) async { + List? embeds, + List? components}) async { final response = await _kernel.dataStore.client .post('/channels/$channelId/messages', body: { 'content': content, @@ -61,6 +62,7 @@ final class ServerMessagePart implements DataStorePart { fn: () async => embeds ?.map(_kernel.marshaller.serializers.embed.deserialize) .toList()), + 'components': components?.map((c) => c.toJson()).toList(), 'message_reference': {'message_id': id, 'channel_id': channelId} }); diff --git a/lib/src/api/server/server_message.dart b/lib/src/api/server/server_message.dart index 50f902fd4..3ce8ae18d 100644 --- a/lib/src/api/server/server_message.dart +++ b/lib/src/api/server/server_message.dart @@ -12,7 +12,8 @@ import 'package:mineral/infrastructure/internals/datastore/data_store.dart'; import 'package:mineral/infrastructure/internals/datastore/parts/server_message_part.dart'; final class ServerMessage extends Message { - ServerMessagePart get _dataStoreServerMessage => ioc.resolve().serverMessage; + ServerMessagePart get _dataStoreServerMessage => + ioc.resolve().serverMessage; final MessageProperties _properties; @override @@ -40,13 +41,27 @@ final class ServerMessage extends Message { required this.author, }); - Future edit(String? content, List? embeds, List? components) async { - _dataStoreServerMessage.update(id: id, channelId: channelId, content: content, embeds: embeds, components: components); + Future edit(String? content, List? embeds, + List? components) async { + _dataStoreServerMessage.update( + id: id, + channelId: channelId, + content: content, + embeds: embeds, + components: components); } - Future reply({String? content, List? embeds}) async { + Future reply( + {String? content, + List? embeds, + List? components}) async { _dataStoreServerMessage.reply( - id: id, channelId: channelId, serverId: channel.guildId, content: content, embeds: embeds); + id: id, + channelId: channelId, + serverId: channel.guildId, + content: content, + embeds: embeds, + components: components); } Future pin() async { From 362b5077d96a75b3e22a0141beafd733170a1a41 Mon Sep 17 00:00:00 2001 From: Baptiste Parmantier Date: Mon, 9 Sep 2024 20:43:44 +0200 Subject: [PATCH 20/20] feat: rebase and fix bad color serialization --- lib/api.dart | 1 - .../internals/datastore/data_store.dart | 60 ----- .../datastore/parts/server_message_part.dart | 113 --------- .../messages/private_message_factory.dart | 0 .../marshaller/serializer_bucket.dart | 0 .../serializers/message_serializer.dart | 44 ---- .../marshaller/types/message_factory.dart | 0 .../listeners/channel_pins_update_packet.dart | 56 ----- .../common/embed/message_embed_assets.dart | 11 +- .../common/embed/message_embed_builder.dart | 10 +- .../api/common/embed/message_embed_color.dart | 221 ------------------ lib/src/api/server/server_message.dart | 19 +- .../internals/datastore/data_store.dart | 7 + .../datastore/parts/server_message_part.dart | 101 ++++++++ .../serializers/embed_serializer.dart | 11 +- .../server_message_serializer.dart | 6 +- .../listeners/message_create_packet.dart | 20 +- 17 files changed, 132 insertions(+), 548 deletions(-) delete mode 100644 lib/infrastructure/internals/datastore/data_store.dart delete mode 100644 lib/infrastructure/internals/datastore/parts/server_message_part.dart delete mode 100644 lib/infrastructure/internals/marshaller/factories/messages/private_message_factory.dart delete mode 100644 lib/infrastructure/internals/marshaller/serializer_bucket.dart delete mode 100644 lib/infrastructure/internals/marshaller/serializers/message_serializer.dart delete mode 100644 lib/infrastructure/internals/marshaller/types/message_factory.dart delete mode 100644 lib/infrastructure/internals/packets/listeners/channel_pins_update_packet.dart delete mode 100644 lib/src/api/common/embed/message_embed_color.dart create mode 100644 lib/src/infrastructure/internals/datastore/parts/server_message_part.dart diff --git a/lib/api.dart b/lib/api.dart index e2bdd67d4..85b29fa71 100644 --- a/lib/api.dart +++ b/lib/api.dart @@ -38,7 +38,6 @@ export 'package:mineral/src/api/common/embed/message_embed.dart'; export 'package:mineral/src/api/common/embed/message_embed_assets.dart'; export 'package:mineral/src/api/common/embed/message_embed_author.dart'; export 'package:mineral/src/api/common/embed/message_embed_builder.dart'; -export 'package:mineral/src/api/common/embed/message_embed_color.dart'; export 'package:mineral/src/api/common/embed/message_embed_field.dart'; export 'package:mineral/src/api/common/embed/message_embed_footer.dart'; export 'package:mineral/src/api/common/embed/message_embed_image.dart'; diff --git a/lib/infrastructure/internals/datastore/data_store.dart b/lib/infrastructure/internals/datastore/data_store.dart deleted file mode 100644 index c866c3e78..000000000 --- a/lib/infrastructure/internals/datastore/data_store.dart +++ /dev/null @@ -1,60 +0,0 @@ -import 'package:mineral/infrastructure/internals/datastore/parts/channel_part.dart'; -import 'package:mineral/infrastructure/internals/datastore/parts/interaction_part.dart'; -import 'package:mineral/infrastructure/internals/datastore/parts/member_part.dart'; -import 'package:mineral/infrastructure/internals/datastore/parts/role_part.dart'; -import 'package:mineral/infrastructure/internals/datastore/parts/server_message_part.dart'; -import 'package:mineral/infrastructure/internals/datastore/parts/server_part.dart'; -import 'package:mineral/infrastructure/kernel/kernel.dart'; -import 'package:mineral/infrastructure/services/http/http_client.dart'; - -abstract class DataStoreContract { - HttpClient get client; - - ChannelPart get channel; - - ServerPart get server; - - MemberPart get member; - - RolePart get role; - - ServerMessagePart get serverMessage; - - InteractionPart get interaction; -} - -final class DataStore implements DataStoreContract { - @override - final HttpClient client; - - late final KernelContract kernel; - - @override - late final ChannelPart channel; - - @override - late final ServerPart server; - - @override - late final MemberPart member; - - @override - late final RolePart role; - - @override - late final ServerMessagePart serverMessage; - - @override - late final InteractionPart interaction; - - DataStore(this.client); - - void init() { - channel = ChannelPart(kernel); - server = ServerPart(kernel); - member = MemberPart(kernel); - role = RolePart(kernel); - interaction = InteractionPart(kernel); - serverMessage = ServerMessagePart(kernel); - } -} diff --git a/lib/infrastructure/internals/datastore/parts/server_message_part.dart b/lib/infrastructure/internals/datastore/parts/server_message_part.dart deleted file mode 100644 index 25cbd0284..000000000 --- a/lib/infrastructure/internals/datastore/parts/server_message_part.dart +++ /dev/null @@ -1,113 +0,0 @@ -import 'package:mineral/api/common/components/message_component.dart'; -import 'package:mineral/api/common/embed/message_embed.dart'; -import 'package:mineral/api/common/snowflake.dart'; -import 'package:mineral/api/server/channels/server_channel.dart'; -import 'package:mineral/api/server/server_message.dart'; -import 'package:mineral/infrastructure/commons/helper.dart'; -import 'package:mineral/infrastructure/internals/datastore/data_store_part.dart'; -import 'package:mineral/infrastructure/kernel/kernel.dart'; -import 'package:mineral/infrastructure/services/http/http_client_status.dart'; - -final class ServerMessagePart implements DataStorePart { - final KernelContract _kernel; - - HttpClientStatus get status => _kernel.dataStore.client.status; - - ServerMessagePart(this._kernel); - - Future update({ - required Snowflake id, - required Snowflake channelId, - String? content, - List? embeds, - List? components, - }) async { - 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()), - 'components': components?.map((c) => c.toJson()).toList() - }); - - final ServerMessage serverMessage = await _kernel - .marshaller.serializers.message - .serializeRemote(response.body); - - final serverKey = _kernel.marshaller.cacheKey.serverMessage( - serverId: serverMessage.channel.server.id, messageId: serverMessage.id); - final rawServerMessage = - _kernel.marshaller.serializers.message.deserialize(response.body); - - await _kernel.marshaller.cache.put(serverKey, rawServerMessage); - - return serverMessage; - } - - Future reply( - {required Snowflake id, - required Snowflake channelId, - required Snowflake serverId, - String? content, - List? embeds, - List? components}) async { - final response = await _kernel.dataStore.client - .post('/channels/$channelId/messages', body: { - 'content': content, - 'embeds': await Helper.createOrNullAsync( - field: embeds, - fn: () async => 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, serverId: serverId); - final ServerMessage serverMessage = - await _kernel.marshaller.serializers.message.serializeRemote({ - ...response.body, - 'guild_id': serverId, - }); - - if (channel != null) { - channel.server = server; - serverMessage.channel = channel; - } - - final messageKey = _kernel.marshaller.cacheKey.serverMessage( - serverId: serverMessage.channel.server.id, messageId: serverMessage.id); - final rawServerMessage = - await _kernel.marshaller.serializers.message.deserialize(serverMessage); - - await _kernel.marshaller.cache.put(messageKey, rawServerMessage); - - return serverMessage; - } - - 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 { - 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 delete( - {required Snowflake id, required Snowflake channelId}) async { - await _kernel.dataStore.client.delete('/channels/$channelId/messages/$id'); - } -} diff --git a/lib/infrastructure/internals/marshaller/factories/messages/private_message_factory.dart b/lib/infrastructure/internals/marshaller/factories/messages/private_message_factory.dart deleted file mode 100644 index e69de29bb..000000000 diff --git a/lib/infrastructure/internals/marshaller/serializer_bucket.dart b/lib/infrastructure/internals/marshaller/serializer_bucket.dart deleted file mode 100644 index e69de29bb..000000000 diff --git a/lib/infrastructure/internals/marshaller/serializers/message_serializer.dart b/lib/infrastructure/internals/marshaller/serializers/message_serializer.dart deleted file mode 100644 index 10ed3a639..000000000 --- a/lib/infrastructure/internals/marshaller/serializers/message_serializer.dart +++ /dev/null @@ -1,44 +0,0 @@ -import 'package:mineral/api/common/snowflake.dart'; -import 'package:mineral/api/private/channels/private_channel.dart'; -import 'package:mineral/api/server/channels/server_channel.dart'; -import 'package:mineral/infrastructure/internals/marshaller/factories/messages/private_message_factory.dart'; -import 'package:mineral/infrastructure/internals/marshaller/factories/messages/server_message_factory.dart'; -import 'package:mineral/infrastructure/internals/marshaller/marshaller.dart'; -import 'package:mineral/infrastructure/internals/marshaller/types/message_factory.dart'; -import 'package:mineral/infrastructure/internals/marshaller/types/serializer.dart'; - -final class MessageSerializer implements SerializerContract { - final MarshallerContract marshaller; - - final _serverMessageFactory = ServerMessageFactory(); - final _privateMessageFactory = PrivateMessageFactory(); - - MessageSerializer(this.marshaller); - - @override - Future serializeRemote(Map json) async { - final serverId = json['message_reference']?['guild_id']; - final channel = await marshaller.dataStore.channel.getChannel(Snowflake(json['channel_id']), - serverId: serverId != null ? Snowflake(serverId) : null); - - final factory = switch (channel) { - ServerChannel() => _serverMessageFactory, - PrivateChannel() => _privateMessageFactory, - _ => throw Exception('Channel type not found ${channel.runtimeType}'), - } as MessageFactory; - - return factory.serializeRemote(marshaller, json); - } - - @override - Future serializeCache(Map json) { - throw UnimplementedError(); - } - - @override - Map deserialize(dynamic object) { - return { - 'id': object.id, - }; - } -} diff --git a/lib/infrastructure/internals/marshaller/types/message_factory.dart b/lib/infrastructure/internals/marshaller/types/message_factory.dart deleted file mode 100644 index e69de29bb..000000000 diff --git a/lib/infrastructure/internals/packets/listeners/channel_pins_update_packet.dart b/lib/infrastructure/internals/packets/listeners/channel_pins_update_packet.dart deleted file mode 100644 index ff037b935..000000000 --- a/lib/infrastructure/internals/packets/listeners/channel_pins_update_packet.dart +++ /dev/null @@ -1,56 +0,0 @@ -import 'package:mineral/api/private/channels/private_channel.dart'; -import 'package:mineral/api/server/channels/server_channel.dart'; -import 'package:mineral/domains/events/event.dart'; -import 'package:mineral/infrastructure/internals/marshaller/marshaller.dart'; -import 'package:mineral/infrastructure/internals/packets/listenable_packet.dart'; -import 'package:mineral/infrastructure/internals/packets/packet_type.dart'; -import 'package:mineral/infrastructure/internals/wss/shard_message.dart'; -import 'package:mineral/infrastructure/services/logger/logger.dart'; - -final class ChannelPinsUpdatePacket implements ListenablePacket { - @override - PacketType get packetType => PacketType.channelPinsUpdate; - - final LoggerContract logger; - final MarshallerContract marshaller; - - const ChannelPinsUpdatePacket(this.logger, this.marshaller); - - @override - Future listen(ShardMessage message, DispatchEvent dispatch) async { - final channel = await marshaller.serializers.channels.serializeRemote(message.payload); - - return switch (channel) { - ServerChannel() => registerServerChannelPins(channel, dispatch), - PrivateChannel() => registerPrivateChannelPins(channel, dispatch), - _ => logger.warn("Unknown channel type: $channel contact Mineral's core team.") - }; - } - - Future registerServerChannelPins(ServerChannel channel, DispatchEvent dispatch) async { - final server = await marshaller.dataStore.server.getServer(channel.guildId); - final serverCacheKey = marshaller.cacheKey.server(server.id); - final channelCacheKey = marshaller.cacheKey.serverChannel(serverId: server.id, channelId: channel.id); - - server.channels.list.update(channel.id, (_) => channel); - - final rawServer = await marshaller.serializers.server.deserialize(server); - final rawChannel = await marshaller.serializers.channels.deserialize(channel); - - await Future.wait([ - marshaller.cache.put(serverCacheKey, rawServer), - marshaller.cache.put(channelCacheKey, rawChannel) - ]); - - dispatch(event: Event.serverChannelPinsUpdate, params: [server, channel]); - } - - Future registerPrivateChannelPins(PrivateChannel channel, DispatchEvent dispatch) async { - final channelCacheKey = marshaller.cacheKey.privateChannel(channel.id); - - final rawChannel = await marshaller.serializers.channels.deserialize(channel); - await marshaller.cache.put(channelCacheKey, rawChannel); - - dispatch(event: Event.privateChannelPinsUpdate, params: [channel]); - } -} diff --git a/lib/src/api/common/embed/message_embed_assets.dart b/lib/src/api/common/embed/message_embed_assets.dart index 1d9aaf708..c605eddc0 100644 --- a/lib/src/api/common/embed/message_embed_assets.dart +++ b/lib/src/api/common/embed/message_embed_assets.dart @@ -1,7 +1,4 @@ -import 'package:mineral/src/api/common/embed/message_embed_author.dart'; -import 'package:mineral/src/api/common/embed/message_embed_color.dart'; -import 'package:mineral/src/api/common/embed/message_embed_footer.dart'; -import 'package:mineral/src/api/common/embed/message_embed_image.dart'; +import 'package:mineral/api.dart'; import 'package:mineral/src/infrastructure/commons/helper.dart'; final class MessageEmbedAssets { @@ -10,7 +7,6 @@ final class MessageEmbedAssets { final MessageEmbedImage? video; final MessageEmbedFooter? footer; final MessageEmbedAuthor? author; - final MessageEmbedColor color; const MessageEmbedAssets({ required this.image, @@ -18,17 +14,15 @@ final class MessageEmbedAssets { required this.video, required this.footer, required this.author, - required this.color, }); - Object toJson() { + Map toJson() { return { 'image': image?.toJson(), 'thumbnail': thumbnail?.toJson(), 'video': video?.toJson(), 'footer': footer?.toJson(), 'author': author?.toJson(), - 'color': color, }; } @@ -49,7 +43,6 @@ final class MessageEmbedAssets { author: Helper.createOrNull( field: json['author'], fn: () => MessageEmbedAuthor.fromJson(json['author'])), - color: json['color'], ); } } diff --git a/lib/src/api/common/embed/message_embed_builder.dart b/lib/src/api/common/embed/message_embed_builder.dart index 5bab1e091..1b68e6ac4 100644 --- a/lib/src/api/common/embed/message_embed_builder.dart +++ b/lib/src/api/common/embed/message_embed_builder.dart @@ -1,7 +1,7 @@ +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_color.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'; @@ -11,7 +11,7 @@ final class MessageEmbedBuilder { String? description; String? url; DateTime? timestamp; - MessageEmbedColor? color; + Color? color; MessageEmbedFooter? footer; MessageEmbedAuthor? author; MessageEmbedImage? image; @@ -34,7 +34,7 @@ final class MessageEmbedBuilder { return this; } - MessageEmbedBuilder setColor(MessageEmbedColor color) { + MessageEmbedBuilder setColor(Color color) { this.color = color; return this; } @@ -94,13 +94,13 @@ final class MessageEmbedBuilder { description: description, url: url, timestamp: timestamp, + color: color ?? Color('#000000'), assets: MessageEmbedAssets( image: image, thumbnail: image, video: image, footer: footer, - author: author, - color: color ?? MessageEmbedColor.of('#000000')), + author: author), fields: fields); } } diff --git a/lib/src/api/common/embed/message_embed_color.dart b/lib/src/api/common/embed/message_embed_color.dart deleted file mode 100644 index 599d490ad..000000000 --- a/lib/src/api/common/embed/message_embed_color.dart +++ /dev/null @@ -1,221 +0,0 @@ -final class MessageEmbedColor { - static MessageEmbedColor amber_50 = MessageEmbedColor._('#fff7ed'); - static MessageEmbedColor amber_100 = MessageEmbedColor._('#ffedd5'); - static MessageEmbedColor amber_200 = MessageEmbedColor._('#fed7aa'); - static MessageEmbedColor amber_300 = MessageEmbedColor._('#fdba74'); - static MessageEmbedColor amber_400 = MessageEmbedColor._('#fb923c'); - static MessageEmbedColor amber_500 = MessageEmbedColor._('#f97316'); - static MessageEmbedColor amber_600 = MessageEmbedColor._('#ea580c'); - static MessageEmbedColor amber_700 = MessageEmbedColor._('#c2410c'); - static MessageEmbedColor amber_800 = MessageEmbedColor._('#9a3412'); - static MessageEmbedColor amber_900 = MessageEmbedColor._('#7c2d12'); - static MessageEmbedColor blue_50 = MessageEmbedColor._('#eff6ff'); - static MessageEmbedColor blue_100 = MessageEmbedColor._('#dbeafe'); - static MessageEmbedColor blue_200 = MessageEmbedColor._('#bfdbfe'); - static MessageEmbedColor blue_300 = MessageEmbedColor._('#93c5fd'); - static MessageEmbedColor blue_400 = MessageEmbedColor._('#60a5fa'); - static MessageEmbedColor blue_500 = MessageEmbedColor._('#3b82f6'); - static MessageEmbedColor blue_600 = MessageEmbedColor._('#2563eb'); - static MessageEmbedColor blue_700 = MessageEmbedColor._('#1d4ed8'); - static MessageEmbedColor blue_800 = MessageEmbedColor._('#1e40af'); - static MessageEmbedColor blue_900 = MessageEmbedColor._('#1e3a8a'); - static MessageEmbedColor cyan_50 = MessageEmbedColor._('#ecfeff'); - static MessageEmbedColor cyan_100 = MessageEmbedColor._('#cffafe'); - static MessageEmbedColor cyan_200 = MessageEmbedColor._('#a5f3fc'); - static MessageEmbedColor cyan_300 = MessageEmbedColor._('#67e8f9'); - static MessageEmbedColor cyan_400 = MessageEmbedColor._('#22d3ee'); - static MessageEmbedColor cyan_500 = MessageEmbedColor._('#06b6d4'); - static MessageEmbedColor cyan_600 = MessageEmbedColor._('#0891b2'); - static MessageEmbedColor cyan_700 = MessageEmbedColor._('#0e7490'); - static MessageEmbedColor cyan_800 = MessageEmbedColor._('#155e75'); - static MessageEmbedColor cyan_900 = MessageEmbedColor._('#164e63'); - static MessageEmbedColor emerald_50 = MessageEmbedColor._('#ecfdf5'); - static MessageEmbedColor emerald_100 = MessageEmbedColor._('#d1fae5'); - static MessageEmbedColor emerald_200 = MessageEmbedColor._('#a7f3d0'); - static MessageEmbedColor emerald_300 = MessageEmbedColor._('#6ee7b7'); - static MessageEmbedColor emerald_400 = MessageEmbedColor._('#34d399'); - static MessageEmbedColor emerald_500 = MessageEmbedColor._('#10b981'); - static MessageEmbedColor emerald_600 = MessageEmbedColor._('#059669'); - static MessageEmbedColor emerald_700 = MessageEmbedColor._('#047857'); - static MessageEmbedColor emerald_800 = MessageEmbedColor._('#065f46'); - static MessageEmbedColor emerald_900 = MessageEmbedColor._('#064e3b'); - static MessageEmbedColor fuchsia_50 = MessageEmbedColor._('#fdf4ff'); - static MessageEmbedColor fuchsia_100 = MessageEmbedColor._('#fae8ff'); - static MessageEmbedColor fuchsia_200 = MessageEmbedColor._('#f5d0fe'); - static MessageEmbedColor fuchsia_300 = MessageEmbedColor._('#f0abfc'); - static MessageEmbedColor fuchsia_400 = MessageEmbedColor._('#e879f9'); - static MessageEmbedColor fuchsia_500 = MessageEmbedColor._('#d946ef'); - static MessageEmbedColor fuchsia_600 = MessageEmbedColor._('#c026d3'); - static MessageEmbedColor fuchsia_700 = MessageEmbedColor._('#a21caf'); - static MessageEmbedColor fuchsia_800 = MessageEmbedColor._('#86198f'); - static MessageEmbedColor fuchsia_900 = MessageEmbedColor._('#701a75'); - static MessageEmbedColor gray_50 = MessageEmbedColor._('#f9fafb'); - static MessageEmbedColor gray_100 = MessageEmbedColor._('#f3f4f6'); - static MessageEmbedColor gray_200 = MessageEmbedColor._('#e5e7eb'); - static MessageEmbedColor gray_300 = MessageEmbedColor._('#d1d5db'); - static MessageEmbedColor gray_400 = MessageEmbedColor._('#9ca3af'); - static MessageEmbedColor gray_500 = MessageEmbedColor._('#6b7280'); - static MessageEmbedColor gray_600 = MessageEmbedColor._('#4b5563'); - static MessageEmbedColor gray_700 = MessageEmbedColor._('#374151'); - static MessageEmbedColor gray_800 = MessageEmbedColor._('#1f2937'); - static MessageEmbedColor gray_900 = MessageEmbedColor._('#111827'); - static MessageEmbedColor green_50 = MessageEmbedColor._('#f0fdf4'); - static MessageEmbedColor green_100 = MessageEmbedColor._('#dcfce7'); - static MessageEmbedColor green_200 = MessageEmbedColor._('#bbf7d0'); - static MessageEmbedColor green_300 = MessageEmbedColor._('#86efac'); - static MessageEmbedColor green_400 = MessageEmbedColor._('#4ade80'); - static MessageEmbedColor green_500 = MessageEmbedColor._('#22c55e'); - static MessageEmbedColor green_600 = MessageEmbedColor._('#16a34a'); - static MessageEmbedColor green_700 = MessageEmbedColor._('#15803d'); - static MessageEmbedColor green_800 = MessageEmbedColor._('#166534'); - static MessageEmbedColor green_900 = MessageEmbedColor._('#14532d'); - static MessageEmbedColor indigo_50 = MessageEmbedColor._('#eef2ff'); - static MessageEmbedColor indigo_100 = MessageEmbedColor._('#e0e7ff'); - static MessageEmbedColor indigo_200 = MessageEmbedColor._('#c7d2fe'); - static MessageEmbedColor indigo_300 = MessageEmbedColor._('#a5b4fc'); - static MessageEmbedColor indigo_400 = MessageEmbedColor._('#818cf8'); - static MessageEmbedColor indigo_500 = MessageEmbedColor._('#6366f1'); - static MessageEmbedColor indigo_600 = MessageEmbedColor._('#4f46e5'); - static MessageEmbedColor indigo_700 = MessageEmbedColor._('#4338ca'); - static MessageEmbedColor indigo_800 = MessageEmbedColor._('#3730a3'); - static MessageEmbedColor indigo_900 = MessageEmbedColor._('#312e81'); - static MessageEmbedColor lime_50 = MessageEmbedColor._('#f7fee7'); - static MessageEmbedColor lime_100 = MessageEmbedColor._('#ecfccb'); - static MessageEmbedColor lime_200 = MessageEmbedColor._('#d9f99d'); - static MessageEmbedColor lime_300 = MessageEmbedColor._('#bef264'); - static MessageEmbedColor lime_400 = MessageEmbedColor._('#a3e635'); - static MessageEmbedColor lime_500 = MessageEmbedColor._('#84cc16'); - static MessageEmbedColor lime_600 = MessageEmbedColor._('#65a30d'); - static MessageEmbedColor lime_700 = MessageEmbedColor._('#4d7c0f'); - static MessageEmbedColor lime_800 = MessageEmbedColor._('#3f6212'); - static MessageEmbedColor lime_900 = MessageEmbedColor._('#365314'); - static MessageEmbedColor neutral_50 = MessageEmbedColor._('#fafafa'); - static MessageEmbedColor neutral_100 = MessageEmbedColor._('#f5f5f5'); - static MessageEmbedColor neutral_200 = MessageEmbedColor._('#e5e5e5'); - static MessageEmbedColor neutral_300 = MessageEmbedColor._('#d4d4d4'); - static MessageEmbedColor neutral_400 = MessageEmbedColor._('#a3a3a3'); - static MessageEmbedColor neutral_500 = MessageEmbedColor._('#737373'); - static MessageEmbedColor neutral_600 = MessageEmbedColor._('#525252'); - static MessageEmbedColor neutral_700 = MessageEmbedColor._('#404040'); - static MessageEmbedColor neutral_800 = MessageEmbedColor._('#262626'); - static MessageEmbedColor neutral_900 = MessageEmbedColor._('#171717'); - static MessageEmbedColor orange_50 = MessageEmbedColor._('#fff7ed'); - static MessageEmbedColor orange_100 = MessageEmbedColor._('#ffedd5'); - static MessageEmbedColor orange_200 = MessageEmbedColor._('#fed7aa'); - static MessageEmbedColor orange_300 = MessageEmbedColor._('#fdba74'); - static MessageEmbedColor orange_400 = MessageEmbedColor._('#fb923c'); - static MessageEmbedColor orange_500 = MessageEmbedColor._('#f97316'); - static MessageEmbedColor orange_600 = MessageEmbedColor._('#ea580c'); - static MessageEmbedColor orange_700 = MessageEmbedColor._('#c2410c'); - static MessageEmbedColor orange_800 = MessageEmbedColor._('#9a3412'); - static MessageEmbedColor orange_900 = MessageEmbedColor._('#7c2d12'); - static MessageEmbedColor pink_50 = MessageEmbedColor._('#fdf2f8'); - static MessageEmbedColor pink_100 = MessageEmbedColor._('#fce7f3'); - static MessageEmbedColor pink_200 = MessageEmbedColor._('#f9a8d4'); - static MessageEmbedColor pink_300 = MessageEmbedColor._('#93c5fd'); - static MessageEmbedColor pink_400 = MessageEmbedColor._('#f472b6'); - static MessageEmbedColor pink_500 = MessageEmbedColor._('#ec4899'); - static MessageEmbedColor pink_600 = MessageEmbedColor._('#db2777'); - static MessageEmbedColor pink_700 = MessageEmbedColor._('#be185d'); - static MessageEmbedColor pink_800 = MessageEmbedColor._('#9d174d'); - static MessageEmbedColor pink_900 = MessageEmbedColor._('#831843'); - static MessageEmbedColor purple_50 = MessageEmbedColor._('#faf5ff'); - static MessageEmbedColor purple_100 = MessageEmbedColor._('#f3e8ff'); - static MessageEmbedColor purple_200 = MessageEmbedColor._('#e9d5ff'); - static MessageEmbedColor purple_300 = MessageEmbedColor._('#d8b4fe'); - static MessageEmbedColor purple_400 = MessageEmbedColor._('#c084fc'); - static MessageEmbedColor purple_500 = MessageEmbedColor._('#a855f7'); - static MessageEmbedColor purple_600 = MessageEmbedColor._('#9333ea'); - static MessageEmbedColor purple_700 = MessageEmbedColor._('#7e22ce'); - static MessageEmbedColor purple_800 = MessageEmbedColor._('#6b21a8'); - static MessageEmbedColor purple_900 = MessageEmbedColor._('#581c87'); - static MessageEmbedColor red_50 = MessageEmbedColor._('#fef2f2'); - static MessageEmbedColor red_100 = MessageEmbedColor._('#fee2e2'); - static MessageEmbedColor red_200 = MessageEmbedColor._('#fecaca'); - static MessageEmbedColor red_300 = MessageEmbedColor._('#fca5a5'); - static MessageEmbedColor red_400 = MessageEmbedColor._('#f87171'); - static MessageEmbedColor red_500 = MessageEmbedColor._('#ef4444'); - static MessageEmbedColor red_600 = MessageEmbedColor._('#dc2626'); - static MessageEmbedColor red_700 = MessageEmbedColor._('#b91c1c'); - static MessageEmbedColor red_800 = MessageEmbedColor._('#991b1b'); - static MessageEmbedColor red_900 = MessageEmbedColor._('#7f1d1d'); - static MessageEmbedColor rose_50 = MessageEmbedColor._('#fff1f2'); - static MessageEmbedColor rose_100 = MessageEmbedColor._('#ffe4e6'); - static MessageEmbedColor rose_200 = MessageEmbedColor._('#fecdd3'); - static MessageEmbedColor rose_300 = MessageEmbedColor._('#fda4af'); - static MessageEmbedColor rose_400 = MessageEmbedColor._('#fb7185'); - static MessageEmbedColor rose_500 = MessageEmbedColor._('#f43f5e'); - static MessageEmbedColor rose_600 = MessageEmbedColor._('#e11d48'); - static MessageEmbedColor rose_700 = MessageEmbedColor._('#be123c'); - static MessageEmbedColor rose_800 = MessageEmbedColor._('#9f1239'); - static MessageEmbedColor rose_900 = MessageEmbedColor._('#881337'); - static MessageEmbedColor sky_50 = MessageEmbedColor._('#f0f9ff'); - static MessageEmbedColor sky_100 = MessageEmbedColor._('#e0f2fe'); - static MessageEmbedColor sky_200 = MessageEmbedColor._('#bae6fd'); - static MessageEmbedColor sky_300 = MessageEmbedColor._('#7dd3fc'); - static MessageEmbedColor sky_400 = MessageEmbedColor._('#38bdf8'); - static MessageEmbedColor sky_500 = MessageEmbedColor._('#0ea5e9'); - static MessageEmbedColor sky_600 = MessageEmbedColor._('#0284c7'); - static MessageEmbedColor sky_700 = MessageEmbedColor._('#0369a1'); - static MessageEmbedColor sky_800 = MessageEmbedColor._('#075985'); - static MessageEmbedColor sky_900 = MessageEmbedColor._('#0c4a6e'); - static MessageEmbedColor slate_50 = MessageEmbedColor._('#f8fafc'); - static MessageEmbedColor slate_100 = MessageEmbedColor._('#f1f5f9'); - static MessageEmbedColor slate_200 = MessageEmbedColor._('#e2e8f0'); - static MessageEmbedColor slate_300 = MessageEmbedColor._('#cbd5e1'); - static MessageEmbedColor slate_400 = MessageEmbedColor._('#94a3b8'); - static MessageEmbedColor slate_500 = MessageEmbedColor._('#64748b'); - static MessageEmbedColor slate_600 = MessageEmbedColor._('#475569'); - static MessageEmbedColor slate_700 = MessageEmbedColor._('#334155'); - static MessageEmbedColor slate_800 = MessageEmbedColor._('#1e293b'); - static MessageEmbedColor slate_900 = MessageEmbedColor._('#0f172a'); - static MessageEmbedColor invisible = MessageEmbedColor._('#2f3136'); - static MessageEmbedColor stone_50 = MessageEmbedColor._('#fafafa'); - static MessageEmbedColor stone_100 = MessageEmbedColor._('#f5f5f5'); - static MessageEmbedColor stone_200 = MessageEmbedColor._('#e5e5e5'); - static MessageEmbedColor stone_300 = MessageEmbedColor._('#d4d4d4'); - static MessageEmbedColor stone_400 = MessageEmbedColor._('#a3a3a3'); - static MessageEmbedColor stone_500 = MessageEmbedColor._('#737373'); - static MessageEmbedColor stone_600 = MessageEmbedColor._('#525252'); - static MessageEmbedColor stone_700 = MessageEmbedColor._('#404040'); - static MessageEmbedColor stone_800 = MessageEmbedColor._('#262626'); - static MessageEmbedColor stone_900 = MessageEmbedColor._('#171717'); - static MessageEmbedColor teal_50 = MessageEmbedColor._('#f0fdfa'); - static MessageEmbedColor teal_100 = MessageEmbedColor._('#ccfbf1'); - static MessageEmbedColor teal_200 = MessageEmbedColor._('#99f6e4'); - static MessageEmbedColor teal_300 = MessageEmbedColor._('#5eead4'); - static MessageEmbedColor teal_400 = MessageEmbedColor._('#2dd4bf'); - static MessageEmbedColor teal_500 = MessageEmbedColor._('#14b8a6'); - static MessageEmbedColor teal_600 = MessageEmbedColor._('#0d9488'); - static MessageEmbedColor teal_700 = MessageEmbedColor._('#0f766e'); - static MessageEmbedColor teal_800 = MessageEmbedColor._('#115e59'); - static MessageEmbedColor teal_900 = MessageEmbedColor._('#134e4a'); - static MessageEmbedColor violet_50 = MessageEmbedColor._('#f5f3ff'); - static MessageEmbedColor violet_100 = MessageEmbedColor._('#ede9fe'); - static MessageEmbedColor violet_200 = MessageEmbedColor._('#ddd6fe'); - static MessageEmbedColor violet_300 = MessageEmbedColor._('#c4b5fd'); - static MessageEmbedColor violet_400 = MessageEmbedColor._('#a78bfa'); - static MessageEmbedColor violet_500 = MessageEmbedColor._('#8b5cf6'); - static MessageEmbedColor violet_600 = MessageEmbedColor._('#7c3aed'); - static MessageEmbedColor violet_700 = MessageEmbedColor._('#6d28d9'); - static MessageEmbedColor violet_800 = MessageEmbedColor._('#5b21b6'); - static MessageEmbedColor violet_900 = MessageEmbedColor._('#4c1d95'); - static MessageEmbedColor yellow_50 = MessageEmbedColor._('#fefce8'); - static MessageEmbedColor yellow_100 = MessageEmbedColor._('#fef9c3'); - static MessageEmbedColor yellow_200 = MessageEmbedColor._('#fef08a'); - static MessageEmbedColor yellow_300 = MessageEmbedColor._('#fde047'); - static MessageEmbedColor yellow_400 = MessageEmbedColor._('#facc15'); - static MessageEmbedColor yellow_500 = MessageEmbedColor._('#eab308'); - static MessageEmbedColor yellow_600 = MessageEmbedColor._('#ca8a04'); - static MessageEmbedColor yellow_700 = MessageEmbedColor._('#a16207'); - static MessageEmbedColor yellow_800 = MessageEmbedColor._('#854d0e'); - static MessageEmbedColor yellow_900 = MessageEmbedColor._('#713f12'); - factory MessageEmbedColor.of(String color) => MessageEmbedColor._(color); - - final String color; - - const MessageEmbedColor._(this.color); - - @override - String toString() => color; -} diff --git a/lib/src/api/server/server_message.dart b/lib/src/api/server/server_message.dart index 3ce8ae18d..9d057585a 100644 --- a/lib/src/api/server/server_message.dart +++ b/lib/src/api/server/server_message.dart @@ -1,15 +1,7 @@ -import 'package:mineral/api/common/embed/message_embed.dart'; -import 'package:mineral/api/common/message.dart'; -import 'package:mineral/api/common/message_properties.dart'; -import 'package:mineral/api/common/snowflake.dart'; -import 'package:mineral/api/common/types/channel_type.dart'; -import 'package:mineral/api/server/channels/server_channel.dart'; -import 'package:mineral/api/server/channels/server_text_channel.dart'; -import 'package:mineral/api/server/member.dart'; -import 'package:mineral/api/server/server.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_message_part.dart'; +import 'package:mineral/api.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_message_part.dart'; final class ServerMessage extends Message { ServerMessagePart get _dataStoreServerMessage => @@ -46,6 +38,7 @@ final class ServerMessage extends Message { _dataStoreServerMessage.update( id: id, channelId: channelId, + serverId: channel.serverId, content: content, embeds: embeds, components: components); @@ -58,7 +51,7 @@ final class ServerMessage extends Message { _dataStoreServerMessage.reply( id: id, channelId: channelId, - serverId: channel.guildId, + serverId: channel.serverId, content: content, embeds: embeds, components: components); diff --git a/lib/src/infrastructure/internals/datastore/data_store.dart b/lib/src/infrastructure/internals/datastore/data_store.dart index 54ef9891a..02ae35392 100644 --- a/lib/src/infrastructure/internals/datastore/data_store.dart +++ b/lib/src/infrastructure/internals/datastore/data_store.dart @@ -3,6 +3,7 @@ import 'package:mineral/src/infrastructure/internals/datastore/parts/interaction import 'package:mineral/src/infrastructure/internals/datastore/parts/member_part.dart'; import 'package:mineral/src/infrastructure/internals/datastore/parts/message_part.dart'; import 'package:mineral/src/infrastructure/internals/datastore/parts/role_part.dart'; +import 'package:mineral/src/infrastructure/internals/datastore/parts/server_message_part.dart'; import 'package:mineral/src/infrastructure/internals/datastore/parts/server_part.dart'; import 'package:mineral/src/infrastructure/internals/datastore/parts/sticker_part.dart'; import 'package:mineral/src/infrastructure/internals/datastore/parts/user_part.dart'; @@ -27,6 +28,8 @@ abstract class DataStoreContract { InteractionPart get interaction; StickerPart get sticker; + + ServerMessagePart get serverMessage; } final class DataStore implements DataStoreContract { @@ -59,6 +62,9 @@ final class DataStore implements DataStoreContract { @override late final StickerPart sticker; + @override + late final ServerMessagePart serverMessage; + DataStore(this.client); void init() { @@ -70,5 +76,6 @@ final class DataStore implements DataStoreContract { message = MessagePart(kernel); interaction = InteractionPart(kernel); sticker = StickerPart(kernel); + serverMessage = ServerMessagePart(kernel); } } diff --git a/lib/src/infrastructure/internals/datastore/parts/server_message_part.dart b/lib/src/infrastructure/internals/datastore/parts/server_message_part.dart new file mode 100644 index 000000000..e93c0760b --- /dev/null +++ b/lib/src/infrastructure/internals/datastore/parts/server_message_part.dart @@ -0,0 +1,101 @@ +import 'package:mineral/src/api/common/components/message_component.dart'; +import 'package:mineral/src/api/common/embed/message_embed.dart'; +import 'package:mineral/src/api/common/snowflake.dart'; +import 'package:mineral/src/api/server/channels/server_channel.dart'; +import 'package:mineral/src/api/server/server_message.dart'; +import 'package:mineral/src/infrastructure/commons/helper.dart'; +import 'package:mineral/src/infrastructure/internals/datastore/data_store_part.dart'; +import 'package:mineral/src/infrastructure/kernel/kernel.dart'; +import 'package:mineral/src/infrastructure/services/http/http_client_status.dart'; + +final class ServerMessagePart implements DataStorePart { + final KernelContract _kernel; + + HttpClientStatus get status => _kernel.dataStore.client.status; + + ServerMessagePart(this._kernel); + + Future update({ + required Snowflake id, + required Snowflake channelId, + required Snowflake serverId, + String? content, + List? embeds, + List? components, + }) async { + 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()), + 'components': components?.map((c) => c.toJson()).toList() + }); + + final server = await _kernel.dataStore.server.getServer(serverId); + 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); + + if (channel != null) { + channel.server = server; + message.channel = channel; + } + + return message; + } + + Future reply( + {required Snowflake id, + required Snowflake channelId, + required Snowflake serverId, + String? content, + List? embeds, + List? components}) async { + 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(), + '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 Map rawMessage = + await _kernel.marshaller.serializers.serverMessage.normalize({ + ...response.body, + 'server_id': serverId, + }); + + final ServerMessage message = + await _kernel.marshaller.serializers.serverMessage.serialize(rawMessage); + + if (channel != null) { + channel.server = server; + message.channel = channel; + } + + return message; + } + + 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 { + 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 delete({required Snowflake id, required Snowflake channelId}) async { + await _kernel.dataStore.client.delete('/channels/$channelId/messages/$id'); + } +} diff --git a/lib/src/infrastructure/internals/marshaller/serializers/embed_serializer.dart b/lib/src/infrastructure/internals/marshaller/serializers/embed_serializer.dart index 3cc596ef9..c6d1b0304 100644 --- a/lib/src/infrastructure/internals/marshaller/serializers/embed_serializer.dart +++ b/lib/src/infrastructure/internals/marshaller/serializers/embed_serializer.dart @@ -1,4 +1,5 @@ import 'dart:async'; +import 'dart:convert'; import 'package:mineral/src/api/common/color.dart'; import 'package:mineral/src/api/common/embed/message_embed.dart'; @@ -37,7 +38,7 @@ final class EmbedSerializer implements SerializerContract { } @override - Future serialize(Map json) async { + MessageEmbed serialize(Map json) { return MessageEmbed( title: json['title'], description: json['description'], @@ -56,7 +57,7 @@ final class EmbedSerializer implements SerializerContract { fn: () => MessageEmbedProvider.fromJson(json['provider'])), fields: Helper.createOrNull( field: json['fields'], - fn: () => json['fields'].map(MessageEmbedField.fromJson).toList()), + fn: () => List.from(json['fields']).map((element) => MessageEmbedField.fromJson(element)).toList()), color: Helper.createOrNull( field: json['color'], fn: () => Color.of(json['color'])), ); @@ -64,15 +65,17 @@ final class EmbedSerializer implements SerializerContract { @override Map deserialize(MessageEmbed embed) { + final assets = embed.assets?.toJson(); + return { 'title': embed.title, 'description': embed.description, 'type': embed.type, 'url': embed.url, 'timestamp': embed.timestamp?.toIso8601String(), - 'assets': embed.assets?.toJson(), 'fields': embed.fields?.map((field) => field.toJson()).toList(), - 'color': embed.color?.toInt() + 'color': embed.color?.toInt(), + ...assets!, }; } } diff --git a/lib/src/infrastructure/internals/marshaller/serializers/server_message_serializer.dart b/lib/src/infrastructure/internals/marshaller/serializers/server_message_serializer.dart index c1de14422..ecfa225b7 100644 --- a/lib/src/infrastructure/internals/marshaller/serializers/server_message_serializer.dart +++ b/lib/src/infrastructure/internals/marshaller/serializers/server_message_serializer.dart @@ -50,14 +50,14 @@ final class ServerMessageSerializer @override Future> deserialize(ServerMessage object) async { - final embeds = await Future.wait(object.embeds.map((message) async { + final embeds = object.embeds.map((message) { return marshaller.serializers.embed.deserialize(message); - })); + }); return { 'id': object.id, 'content': object.content, - 'embeds': embeds, + 'embeds': embeds.toList(), 'author_id': object.author.id.value, 'channel_id': object.channel.id.value, 'server_id': object.channel.serverId.value, diff --git a/lib/src/infrastructure/internals/packets/listeners/message_create_packet.dart b/lib/src/infrastructure/internals/packets/listeners/message_create_packet.dart index 1da99dce1..8fcb1f362 100644 --- a/lib/src/infrastructure/internals/packets/listeners/message_create_packet.dart +++ b/lib/src/infrastructure/internals/packets/listeners/message_create_packet.dart @@ -73,6 +73,7 @@ final class MessageCreatePacket implements ListenablePacket { final message = await marshaller.serializers.serverMessage.serialize(payload); + channel.server = server; message.channel = channel; switch (channel) { @@ -84,15 +85,6 @@ final class MessageCreatePacket implements ListenablePacket { channel.messages.list.putIfAbsent(message.id, () => message); } - final rawServer = await marshaller.serializers.server.deserialize(server); - final rawMessage = - await marshaller.serializers.serverMessage.deserialize(message); - - await marshaller.cache.putMany({ - marshaller.cacheKey.server(server.id): rawServer, - marshaller.cacheKey.message(channel.id, message.id): rawMessage, - }); - dispatch(event: Event.serverMessageCreate, params: [message]); } @@ -106,16 +98,6 @@ final class MessageCreatePacket implements ListenablePacket { message.channel = channel; channel.messages.list.putIfAbsent(message.id, () => message); - final rawChannel = - await marshaller.serializers.channels.deserialize(channel); - final rawMessage = - await marshaller.serializers.privateMessage.deserialize(message); - - await marshaller.cache.putMany({ - marshaller.cacheKey.channel(channel.id): rawChannel, - marshaller.cacheKey.message(channel.id, message.id): rawMessage, - }); - dispatch(event: Event.privateMessageCreate, params: [message]); } }