Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat/implement message poll #212

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/events.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ export 'package:mineral/src/domains/events/contracts/server/server_member_remove
export 'package:mineral/src/domains/events/contracts/server/server_member_select_event.dart';
export 'package:mineral/src/domains/events/contracts/server/server_member_update_event.dart';
export 'package:mineral/src/domains/events/contracts/server/server_message_create_event.dart';
export 'package:mineral/src/domains/events/contracts/server/server_poll_vote_add_event.dart';
export 'package:mineral/src/domains/events/contracts/server/server_presence_update_event.dart';
export 'package:mineral/src/domains/events/contracts/server/server_role_select_event.dart';
export 'package:mineral/src/domains/events/contracts/server/server_roles_create_event.dart';
export 'package:mineral/src/domains/events/contracts/server/server_roles_remove_event.dart';
export 'package:mineral/src/domains/events/contracts/server/server_roles_update_event.dart';
export 'package:mineral/src/domains/events/contracts/server/server_stickers_update_event.dart';
export 'package:mineral/src/domains/events/contracts/server/server_text_select_event.dart';
export 'package:mineral/src/domains/events/contracts/server/server_update_event.dart';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why remove server update event export ?

export 'package:mineral/src/domains/events/event.dart';
2 changes: 2 additions & 0 deletions lib/src/api/common/polls/poll.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ final class Poll {
final bool isAllowMultiple;
final Snowflake? messageId;
final PollLayout layout;
final bool isFinalized;

Poll({
required this.question,
Expand All @@ -18,5 +19,6 @@ final class Poll {
required this.isAllowMultiple,
this.messageId,
this.layout = PollLayout.initial,
this.isFinalized = false,
});
}
5 changes: 4 additions & 1 deletion lib/src/api/common/polls/poll_answer.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import 'package:mineral/api.dart';
import 'package:mineral/src/api/common/partial_emoji.dart';
import 'package:mineral/src/infrastructure/commons/helper.dart';

final class PollAnswer {
Snowflake id;
String content;
PartialEmoji? emoji;

PollAnswer({required this.content, this.emoji});
PollAnswer({required this.content, Snowflake? id, this.emoji})
: id = id ?? Snowflake('');

Map<String, dynamic> toJson() {
return {
Expand Down
4 changes: 4 additions & 0 deletions lib/src/api/private/private_message.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:mineral/api.dart';
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';
Expand Down Expand Up @@ -30,9 +31,12 @@ final class PrivateMessage implements Message<PrivateChannel> {

final User author;

final Poll? poll;

PrivateMessage(
this._properties, {
required this.userId,
required this.author,
required this.poll,
});
}
3 changes: 3 additions & 0 deletions lib/src/api/server/server_message.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,12 @@ final class ServerMessage extends Message<ServerChannel> {

final Member author;

final Poll? poll;

ServerMessage(
this._properties, {
required this.author,
required this.poll,
});

Future<void> edit(String? content, List<MessageEmbed>? embeds,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,20 @@ final class GlobalCommandContext implements CommandContext {
@override
final int version;


@override
late final InteractionContract interaction;

final User user;
final Channel? channel;


GlobalCommandContext({
required this.id,
required this.applicationId,
required this.token,
required this.version,
required this.user,
this.channel,
}): interaction = Interaction(token, id);
}) : interaction = Interaction(token, id);

static Future<GlobalCommandContext> fromMap(
MarshallerContract marshaller, Map<String, dynamic> payload) async {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ final class ServerCommandContext implements CommandContext {
final Channel? channel;
final Server server;


ServerCommandContext({
required this.id,
required this.applicationId,
Expand All @@ -36,7 +35,7 @@ final class ServerCommandContext implements CommandContext {
required this.member,
required this.server,
this.channel,
}): interaction = Interaction(token, id);
}) : interaction = Interaction(token, id);

static Future<ServerCommandContext> fromMap(
MarshallerContract marshaller, Map<String, dynamic> payload) async {
Expand Down
8 changes: 8 additions & 0 deletions lib/src/domains/events/buckets/private_bucket.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import 'package:mineral/src/domains/events/contracts/private/private_channel_cre
import 'package:mineral/src/domains/events/contracts/private/private_channel_pins_update_event.dart';
import 'package:mineral/src/domains/events/contracts/private/private_dialog_submit_event.dart';
import 'package:mineral/src/domains/events/contracts/private/private_message_create_event.dart';
import 'package:mineral/src/domains/events/contracts/private/private_poll_vote_add_event.dart';
import 'package:mineral/src/domains/events/contracts/private/private_poll_vote_remove_event.dart';
import 'package:mineral/src/domains/events/contracts/private/private_text_select_event.dart';
import 'package:mineral/src/domains/events/contracts/private/private_user_select_event.dart';
import 'package:mineral/src/domains/events/event.dart';
Expand Down Expand Up @@ -34,4 +36,10 @@ final class PrivateBucket {

void selectText(PrivateTextSelectEventHandler handle, {String? customId}) =>
_events.make(Event.privateTextSelect, handle, customId: customId);

void pollAdd(PrivatePollVoteAddEventHandler handle) =>
_events.make(Event.privatePollVoteAdd, handle);

void pollRemove(PrivatePollVoteRemoveEventHandler handle) =>
_events.make(Event.privatePollVoteRemove, handle);
}
8 changes: 8 additions & 0 deletions lib/src/domains/events/buckets/server_bucket.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import 'package:mineral/src/domains/events/contracts/server/server_member_remove
import 'package:mineral/src/domains/events/contracts/server/server_member_select_event.dart';
import 'package:mineral/src/domains/events/contracts/server/server_member_update_event.dart';
import 'package:mineral/src/domains/events/contracts/server/server_message_create_event.dart';
import 'package:mineral/src/domains/events/contracts/server/server_poll_vote_add_event.dart';
import 'package:mineral/src/domains/events/contracts/server/server_poll_vote_remove_event.dart';
import 'package:mineral/src/domains/events/contracts/server/server_presence_update_event.dart';
import 'package:mineral/src/domains/events/contracts/server/server_role_select_event.dart';
import 'package:mineral/src/domains/events/contracts/server/server_roles_create_event.dart';
Expand Down Expand Up @@ -132,4 +134,10 @@ final class ServerBucket {

void threadMemberRemove(ServerThreadMemberRemoveEventHandler handle) =>
_events.make(Event.serverThreadMemberRemove, handle);

void pollAdd(ServerPollVoteAddEventHandler handle) =>
_events.make(Event.serverPollVoteAdd, handle);

void pollRemove(ServerPollVoteRemoveEventHandler handle) =>
_events.make(Event.serverPollVoteRemove, handle);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import 'package:mineral/src/api/private/channels/private_channel.dart';
import 'package:mineral/src/domains/events/event.dart';
import 'package:mineral/src/domains/events/types/listenable_event.dart';

typedef PrivateChannelUpdateEventHandler = FutureOr<void> Function(PrivateChannel?, PrivateChannel);
typedef PrivateChannelUpdateEventHandler = FutureOr<void> Function(
PrivateChannel?, PrivateChannel);

abstract class PrivateChannelUpdateEvent implements ListenableEvent {
@override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import 'dart:async';

import 'package:mineral/api.dart';
import 'package:mineral/src/domains/events/event.dart';
import 'package:mineral/src/domains/events/types/listenable_event.dart';

typedef PrivatePollVoteAddEventHandler = FutureOr<void> Function(
PrivateMessage, Member, int);

abstract class PrivatePollVoteAddEvent implements ListenableEvent {
@override
Event get event => Event.privatePollVoteAdd;

@override
String? customId;

FutureOr<void> handle(PrivateMessage message, User user, int answer);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why don't give directly PollAnswer object instead of int ?

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import 'dart:async';

import 'package:mineral/api.dart';
import 'package:mineral/src/domains/events/event.dart';
import 'package:mineral/src/domains/events/types/listenable_event.dart';

typedef PrivatePollVoteRemoveEventHandler = FutureOr<void> Function(
PrivateMessage, Member, int);

abstract class PrivatePollVoteRemoveEvent implements ListenableEvent {
@override
Event get event => Event.privatePollVoteRemove;

@override
String? customId;

FutureOr<void> handle(PrivateMessage message, User user, int answer);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import 'dart:async';

import 'package:mineral/api.dart';
import 'package:mineral/src/domains/events/event.dart';
import 'package:mineral/src/domains/events/types/listenable_event.dart';

typedef ServerPollVoteAddEventHandler = FutureOr<void> Function(
ServerMessage, Member, int);

abstract class ServerPollVoteAddEvent implements ListenableEvent {
@override
Event get event => Event.serverPollVoteAdd;

@override
String? customId;

FutureOr<void> handle(ServerMessage message, Member member, int answer);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import 'dart:async';

import 'package:mineral/api.dart';
import 'package:mineral/src/domains/events/event.dart';
import 'package:mineral/src/domains/events/types/listenable_event.dart';

typedef ServerPollVoteRemoveEventHandler = FutureOr<void> Function(
ServerMessage, Member, int);

abstract class ServerPollVoteRemoveEvent implements ListenableEvent {
@override
Event get event => Event.serverPollVoteRemove;

@override
String? customId;

FutureOr<void> handle(ServerMessage message, Member member, int answer);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same

}
13 changes: 12 additions & 1 deletion lib/src/domains/events/event.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import 'package:mineral/src/domains/events/contracts/private/private_channel_pin
import 'package:mineral/src/domains/events/contracts/private/private_channel_update_event.dart';
import 'package:mineral/src/domains/events/contracts/private/private_dialog_submit_event.dart';
import 'package:mineral/src/domains/events/contracts/private/private_message_create_event.dart';
import 'package:mineral/src/domains/events/contracts/private/private_poll_vote_add_event.dart';
import 'package:mineral/src/domains/events/contracts/private/private_poll_vote_remove_event.dart';
import 'package:mineral/src/domains/events/contracts/private/private_text_select_event.dart';
import 'package:mineral/src/domains/events/contracts/private/private_user_select_event.dart';
import 'package:mineral/src/domains/events/contracts/server/server_ban_add_event.dart';
Expand All @@ -26,6 +28,8 @@ import 'package:mineral/src/domains/events/contracts/server/server_member_remove
import 'package:mineral/src/domains/events/contracts/server/server_member_select_event.dart';
import 'package:mineral/src/domains/events/contracts/server/server_member_update_event.dart';
import 'package:mineral/src/domains/events/contracts/server/server_message_create_event.dart';
import 'package:mineral/src/domains/events/contracts/server/server_poll_vote_add_event.dart';
import 'package:mineral/src/domains/events/contracts/server/server_poll_vote_remove_event.dart';
import 'package:mineral/src/domains/events/contracts/server/server_presence_update_event.dart';
import 'package:mineral/src/domains/events/contracts/server/server_role_select_event.dart';
import 'package:mineral/src/domains/events/contracts/server/server_roles_create_event.dart';
Expand Down Expand Up @@ -98,6 +102,10 @@ enum Event implements EnhancedEnum, EventType {
['ThreadChannel thread', 'Server server', 'Member member']),
serverThreadListSync(ServerThreadListSyncEvent,
['List<ThreadChannel> threads', 'Server server']),
serverPollVoteAdd(ServerPollVoteAddEvent,
['ServerMessage message', 'Member member', 'String answer']),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You give as a "String answer", but you give really as int

serverPollVoteRemove(ServerPollVoteRemoveEvent,
['ServerMessage message', 'Member member', 'String answer']),

// private
privateMessageCreate(PrivateMessageCreateEvent, ['PrivateMessage message']),
Expand All @@ -111,7 +119,10 @@ enum Event implements EnhancedEnum, EventType {
PrivateUserSelectEvent, ['PrivateSelectContext ctx', 'List<User> users']),
privateTextSelect(PrivateTextSelectEvent,
['PrivateSelectContext ctx', 'List<String> values']),
;
privatePollVoteAdd(PrivatePollVoteAddEvent,
['PrivateMessage message', 'User user', 'String answer']),
privatePollVoteRemove(PrivatePollVoteRemoveEvent,
['PrivateMessage message', 'User user', 'String answer']);

@override
final Type value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@ final class PrivateMessageFactory implements MessageFactory<PrivateMessage> {
MessageProperties.fromJson(channel as PrivateChannel, json);

final user = await marshaller.serializers.user.serialize(json['author']);
final poll = json['poll'] != null
? await marshaller.serializers.poll.serialize(json['poll'])
: null;

return PrivateMessage(messageProperties,
userId: json['author']['id'], author: user);
userId: json['author']['id'], author: user, poll: poll);
}

@override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ final class ServerMessageFactory implements MessageFactory<ServerMessage> {
final messageProperties =
MessageProperties.fromJson(channel as ServerChannel, json);

return ServerMessage(messageProperties, author: member!);
final poll = json['poll'] != null
? await marshaller.serializers.poll.serialize(json['poll'])
: null;

return ServerMessage(messageProperties, author: member!, poll: poll);
}

@override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:convert';

import 'package:mineral/src/api/common/partial_emoji.dart';
import 'package:mineral/src/api/common/polls/poll.dart';
import 'package:mineral/src/api/common/polls/poll_answer.dart';
Expand All @@ -15,30 +17,32 @@ final class PollSerializer implements SerializerContract<Poll> {
PollSerializer(this._marshaller);

@override
Future<Map<String, dynamic>> normalize(Map<String, dynamic> json) async {
final payload = {
Map<String, dynamic> normalize(Map<String, dynamic> json) {
print(jsonEncode(json));
return {
'message_id': json['message_id'],
'question_text': json['question']['text'],
'answers':
List.from(json['answers']).map((element) => element['text']).toList(),
'answers': List.from(json['answers'])
.map((element) => {
'text': element['poll_media']['text'],
'id': element['poll_media']['id'],
})
.toList(),
'expiry': json['expiry'],
'allow_multiselect': json['allow_multiselect'],
'layout_type': json['layout_type'],
};

final cacheKey = _marshaller.cacheKey.poll(json['message_id']);
await _marshaller.cache.put(cacheKey, payload);

return payload;
}

@override
Poll serialize(Map<String, dynamic> json) {
return Poll(
messageId: Snowflake(json['message_id']),
messageId:
json['message_id'] != null ? Snowflake(json['message_id']) : null,
question: PollQuestion(content: json['question_text']),
answers: List.from(json['answers'])
.map((element) => PollAnswer(
id: element['id'] != null ? Snowflake(element['id']) : null,
content: element['text'],
emoji: Helper.createOrNull(
field: element['emoji'],
Expand All @@ -56,6 +60,7 @@ final class PollSerializer implements SerializerContract<Poll> {
Map<String, dynamic> deserialize(Poll poll) {
final answers = poll.answers.map((element) {
return {
'id': element.id,
'emoji': element.emoji,
'text': element.content,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,12 @@ final class PrivateMessageSerializer

final user = await marshaller.dataStore.user.getUser(json['user_id']);

return PrivateMessage(properties, userId: user.id, author: user);
final poll = json['poll'] != null
? await marshaller.serializers.poll.serialize(json['poll'])
: null;

return PrivateMessage(properties,
userId: user.id, author: user, poll: poll);
}

@override
Expand Down
Loading