Skip to content

Commit

Permalink
feat(component): write factories
Browse files Browse the repository at this point in the history
  • Loading branch information
LeadcodeDev authored and PandaGuerrier committed Aug 15, 2024
1 parent 2b1c3ac commit 50698db
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -38,4 +39,19 @@ final class PrivateSelectContext implements SelectContext {
}) {
interaction = Interaction(token, id);
}

static Future<PrivateSelectContext> fromMap(MarshallerContract marshaller, Map<String, dynamic> 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']),
);
}
}
19 changes: 19 additions & 0 deletions lib/domains/components/selects/contexts/server_select_context.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -39,4 +40,22 @@ final class ServerSelectContext implements SelectContext {
}) {
interaction = Interaction(token, id);
}

static Future<ServerSelectContext> fromMap(MarshallerContract marshaller, Map<String, dynamic> 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']),
),
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 50698db

Please sign in to comment.