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 bc685e1 commit 2b1c3ac
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 21 deletions.
12 changes: 12 additions & 0 deletions lib/domains/components/dialog/contexts/private_dialog_context.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'package:mineral/api/private/user.dart';
import 'package:mineral/domains/components/dialog/dialog_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 PrivateDialogContext implements DialogContext {
@override
Expand Down Expand Up @@ -34,4 +35,15 @@ final class PrivateDialogContext implements DialogContext {
}) {
interaction = Interaction(token, id);
}

static Future<PrivateDialogContext> fromMap(MarshallerContract marshaller, Map<String, dynamic> payload) async{
return PrivateDialogContext(
customId: payload['data']['custom_id'],
id: Snowflake(payload['id']),
applicationId: Snowflake(payload['application_id']),
token: payload['token'],
version: payload['version'],
user: await marshaller.serializers.user.serializeRemote(payload['user']),
);
}
}
15 changes: 15 additions & 0 deletions lib/domains/components/dialog/contexts/server_dialog_context.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'package:mineral/api/server/member.dart';
import 'package:mineral/domains/components/dialog/dialog_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 ServerDialogContext implements DialogContext {
@override
Expand Down Expand Up @@ -34,4 +35,18 @@ final class ServerDialogContext implements DialogContext {
}) {
interaction = Interaction(token, id);
}

static Future<ServerDialogContext> fromMap(MarshallerContract marshaller, Map<String, dynamic> payload) async{
return ServerDialogContext(
customId: payload['data']['custom_id'],
id: Snowflake(payload['id']),
applicationId: Snowflake(payload['application_id']),
token: payload['token'],
version: payload['version'],
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
@@ -1,5 +1,4 @@
import 'package:collection/collection.dart';
import 'package:mineral/api/common/snowflake.dart';
import 'package:mineral/api/common/types/interaction_type.dart';
import 'package:mineral/domains/components/dialog/contexts/private_dialog_context.dart';
import 'package:mineral/domains/components/dialog/contexts/server_dialog_context.dart';
Expand Down Expand Up @@ -40,26 +39,11 @@ final class DialogInteractionCreatePacket implements ListenablePacket {
_ => null
};

final ctx = switch (interactionContext) {
InteractionContextType.server => ServerDialogContext(
customId: message.payload['data']['custom_id'],
id: Snowflake(message.payload['id']),
applicationId: Snowflake(message.payload['application_id']),
token: message.payload['token'],
version: message.payload['version'],
member: await marshaller.dataStore.member.getMember(
guildId: Snowflake(message.payload['guild_id']),
memberId: Snowflake(message.payload['member']['user']['id']),
),
),
InteractionContextType.privateChannel => PrivateDialogContext(
customId: message.payload['data']['custom_id'],
id: Snowflake(message.payload['id']),
applicationId: Snowflake(message.payload['application_id']),
token: message.payload['token'],
version: message.payload['version'],
user: await marshaller.serializers.user.serializeRemote(message.payload['user']),
),
final ctx = await switch (interactionContext) {
InteractionContextType.server =>
ServerDialogContext.fromMap(marshaller, message.payload['data']),
InteractionContextType.privateChannel =>
PrivateDialogContext.fromMap(marshaller, message.payload['data']),
_ => null
};

Expand Down

0 comments on commit 2b1c3ac

Please sign in to comment.