From 50698db7077b8ea8083d82f2c7fc390d2f0bdf50 Mon Sep 17 00:00:00 2001 From: Baptiste Parmantier Date: Tue, 13 Aug 2024 08:58:54 +0200 Subject: [PATCH] feat(component): write factories --- .../contexts/private_select_context.dart | 16 ++++++++++ .../contexts/server_select_context.dart | 19 ++++++++++++ .../select_interaction_create_packet.dart | 30 ++----------------- 3 files changed, 38 insertions(+), 27 deletions(-) diff --git a/lib/domains/components/selects/contexts/private_select_context.dart b/lib/domains/components/selects/contexts/private_select_context.dart index 51a7e4515..b954c8bba 100644 --- a/lib/domains/components/selects/contexts/private_select_context.dart +++ b/lib/domains/components/selects/contexts/private_select_context.dart @@ -4,6 +4,7 @@ import 'package:mineral/api/private/user.dart'; import 'package:mineral/domains/components/selects/button_context.dart'; import 'package:mineral/infrastructure/internals/interactions/interaction.dart'; import 'package:mineral/infrastructure/internals/interactions/types/interaction_contract.dart'; +import 'package:mineral/infrastructure/internals/marshaller/marshaller.dart'; final class PrivateSelectContext implements SelectContext { @override @@ -38,4 +39,19 @@ final class PrivateSelectContext implements SelectContext { }) { interaction = Interaction(token, id); } + + static Future fromMap(MarshallerContract marshaller, Map payload) async{ + return PrivateSelectContext( + customId: payload['data']['custom_id'], + id: Snowflake(payload['id']), + applicationId: Snowflake(payload['application_id']), + token: payload['token'], + version: payload['version'], + message: await marshaller.dataStore.message.getPrivateMessage( + messageId: Snowflake(payload['message']['id']), + channelId: Snowflake(payload['channel_id']), + ), + user: await marshaller.serializers.user.serializeRemote(payload['user']), + ); + } } diff --git a/lib/domains/components/selects/contexts/server_select_context.dart b/lib/domains/components/selects/contexts/server_select_context.dart index d2230346c..1a293e6f7 100644 --- a/lib/domains/components/selects/contexts/server_select_context.dart +++ b/lib/domains/components/selects/contexts/server_select_context.dart @@ -5,6 +5,7 @@ import 'package:mineral/domains/components/buttons/button_context.dart'; import 'package:mineral/domains/components/selects/button_context.dart'; import 'package:mineral/infrastructure/internals/interactions/interaction.dart'; import 'package:mineral/infrastructure/internals/interactions/types/interaction_contract.dart'; +import 'package:mineral/infrastructure/internals/marshaller/marshaller.dart'; final class ServerSelectContext implements SelectContext { @override @@ -39,4 +40,22 @@ final class ServerSelectContext implements SelectContext { }) { interaction = Interaction(token, id); } + + static Future fromMap(MarshallerContract marshaller, Map payload) async{ + return ServerSelectContext( + customId: payload['data']['custom_id'], + id: Snowflake(payload['id']), + applicationId: Snowflake(payload['application_id']), + token: payload['token'], + version: payload['version'], + message: await marshaller.dataStore.message.getServerMessage( + messageId: Snowflake(payload['message']['id']), + channelId: Snowflake(payload['channel_id']), + ), + member: await marshaller.dataStore.member.getMember( + guildId: Snowflake(payload['guild_id']), + memberId: Snowflake(payload['member']['user']['id']), + ), + ); + } } diff --git a/lib/infrastructure/internals/packets/listeners/interactions/select_interaction_create_packet.dart b/lib/infrastructure/internals/packets/listeners/interactions/select_interaction_create_packet.dart index d33ad3c13..a68c01f32 100644 --- a/lib/infrastructure/internals/packets/listeners/interactions/select_interaction_create_packet.dart +++ b/lib/infrastructure/internals/packets/listeners/interactions/select_interaction_create_packet.dart @@ -36,33 +36,9 @@ final class SelectInteractionCreatePacket implements ListenablePacket { final selectMenuType = ComponentType.values .firstWhereOrNull((e) => e.value == message.payload['data']['component_type']); - final ctx = switch (message.payload['guild_id']) { - String() => ServerSelectContext( - id: Snowflake(message.payload['id']), - applicationId: Snowflake(message.payload['application_id']), - token: message.payload['token'], - version: message.payload['version'], - customId: message.payload['data']['custom_id'], - message: await marshaller.dataStore.message.getServerMessage( - messageId: Snowflake(message.payload['message']['id']), - channelId: Snowflake(message.payload['channel_id']), - ), - member: await marshaller.dataStore.member.getMember( - guildId: Snowflake(message.payload['guild_id']), - memberId: Snowflake(message.payload['member']['user']['id']), - ), - ), - _ => PrivateSelectContext( - id: Snowflake(message.payload['id']), - applicationId: Snowflake(message.payload['application_id']), - token: message.payload['token'], - version: message.payload['version'], - customId: message.payload['data']['custom_id'], - message: await marshaller.dataStore.message.getPrivateMessage( - messageId: Snowflake(message.payload['message']['id']), - channelId: Snowflake(message.payload['channel_id']), - ), - user: await marshaller.serializers.user.serializeRemote(message.payload['user'])), + final ctx = await switch (message.payload['guild_id']) { + String() => ServerSelectContext.fromMap(marshaller, message.payload), + _ => PrivateSelectContext.fromMap(marshaller, message.payload), }; switch (selectMenuType) {