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 reactions #53

Merged
merged 2 commits into from
Aug 17, 2022
Merged
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/api.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export 'src/api/messages/message.dart' show Message;
export 'src/api/messages/message_embed.dart' show MessageEmbed, Footer, Image, Thumbnail, Author, Field;
export 'src/api/color.dart' show Color;

export 'src/api/emoji.dart' show PartialEmoji, Emoji;
export 'src/api/emoji.dart' show EmojiBuilder, Emoji;
export 'src/api/role.dart' show Role;

export 'src/api/components/row.dart' show Row;
Expand Down
4 changes: 2 additions & 2 deletions lib/src/api/components/button.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class Button extends Component {
String customId;
String label;
ButtonStyle style;
PartialEmoji? emoji;
EmojiBuilder? emoji;
bool disabled;

Button({ required this.customId, required this.label, required this.style, this.emoji, this.disabled = false }) : super(type: ComponentType.button);
Expand All @@ -31,7 +31,7 @@ class Button extends Component {
'custom_id': customId,
'label': label,
'style': style.value,
'emoji': emoji?.toJson(),
'emoji': emoji?.emoji.toJson(),
'disabled': disabled
};
}
Expand Down
9 changes: 9 additions & 0 deletions lib/src/api/emoji.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,15 @@ import 'package:mineral/core.dart';
import 'package:mineral/src/api/managers/emoji_manager.dart';
import 'package:mineral/src/api/managers/member_manager.dart';

class EmojiBuilder {
PartialEmoji emoji;

EmojiBuilder(this.emoji);

factory EmojiBuilder.fromUnicode(String label) => EmojiBuilder(PartialEmoji('', label, false));
factory EmojiBuilder.fromEmoji(Emoji emoji) => EmojiBuilder(emoji);
}

class PartialEmoji {
final Snowflake _id;
String _label;
Expand Down
29 changes: 29 additions & 0 deletions lib/src/api/guilds/guild_member_reaction.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import 'package:http/http.dart';
import 'package:mineral/api.dart';
import 'package:mineral/core.dart';
import 'package:mineral/src/api/emoji.dart';
import 'package:mineral/src/api/managers/guild_member_reaction_manager.dart';
import 'package:mineral/src/api/messages/partial_message.dart';

class GuildMemberReaction {
final GuildMemberReactionManager _manager;
final PartialEmoji _partialEmoji;
final PartialMessage _message;
final User user;

GuildMemberReaction(this._manager, this._partialEmoji, this._message, this.user);

Future<void> remove () async {
Http http = ioc.singleton(ioc.services.http);

String _emoji = _partialEmoji is Emoji
? '${_partialEmoji.label}:${_partialEmoji.id}'
: _partialEmoji.label;

print('/channels/${_message.channel.id}/messages/${_message.id}/reactions/$_emoji/${user.id}');
Response response = await http.destroy(url: '/channels/${_message.channel.id}/messages/${_message.id}/reactions/$_emoji/${user.id}');
if (response.statusCode == 200) {
_manager.reactions.remove(_emoji);
}
}
}
15 changes: 15 additions & 0 deletions lib/src/api/managers/guild_member_reaction_manager.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import 'package:mineral/api.dart';
import 'package:mineral/src/api/guilds/guild_member_reaction.dart';
import 'package:mineral/src/api/managers/cache_manager.dart';
import 'package:mineral/src/api/messages/partial_message.dart';

class GuildMemberReactionManager extends CacheManager<GuildMemberReaction> {
final Map<Snowflake, GuildMemberReaction> reactions = {};
final PartialMessage _message;
final User _user;

GuildMemberReactionManager(this._message, this._user);

PartialMessage get message => _message;
User get user => _user;
}
57 changes: 57 additions & 0 deletions lib/src/api/managers/message_reaction_manager.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import 'package:http/http.dart';
import 'package:mineral/api.dart';
import 'package:mineral/core.dart';
import 'package:mineral/src/api/channels/partial_channel.dart';
import 'package:mineral/src/api/guilds/guild_member_reaction.dart';
import 'package:mineral/src/api/managers/cache_manager.dart';
import 'package:mineral/src/api/managers/guild_member_reaction_manager.dart';
import 'package:mineral/src/api/messages/partial_message.dart';

class MessageReactionManager<C extends PartialChannel, T extends PartialMessage> extends CacheManager {
Map<Snowflake, GuildMemberReactionManager> users = {};

final C _channel;
late final T message;

MessageReactionManager(this._channel);

Future<void> add (EmojiBuilder emojiBuilder) async {
Http http = ioc.singleton(ioc.services.http);
MineralClient client = ioc.singleton(ioc.services.client);

String _emoji = emojiBuilder.emoji is Emoji
? '${emojiBuilder.emoji.label}:${emojiBuilder.emoji.id}'
: emojiBuilder.emoji.label;

Response response = await http.put(url: '/channels/${_channel.id}/messages/${message.id}/reactions/$_emoji/@me', payload: {});

if (response.statusCode == 204) {
final key = emojiBuilder.emoji.id != '' ? emojiBuilder.emoji.id : emojiBuilder.emoji.label;
GuildMemberReactionManager? memberCache = users.get(client.user.id);

if (memberCache == null) {
final reactionManager = GuildMemberReactionManager(message, client.user);
reactionManager.reactions.putIfAbsent(key, () => GuildMemberReaction(reactionManager, emojiBuilder.emoji, message, client.user));

users.putIfAbsent(client.user.id, () => reactionManager);
memberCache = reactionManager;
}

memberCache.reactions.putIfAbsent(key, () => GuildMemberReaction(
memberCache!,
emojiBuilder.emoji,
message,
client.user
));
}
}

Future<void> removeAll () async {
Http http = ioc.singleton(ioc.services.http);

Response response = await http.destroy(url: '/channels/${message.channel.id}/messages/${message.id}/reactions');
if (response.statusCode == 200) {
cache.clear();
}
}
}
19 changes: 14 additions & 5 deletions lib/src/api/messages/dm_message.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'package:mineral/api.dart';
import 'package:mineral/core.dart';
import 'package:mineral/src/api/channels/dm_channel.dart';
import 'package:mineral/src/api/components/component.dart';
import 'package:mineral/src/api/managers/message_reaction_manager.dart';
import 'package:mineral/src/api/messages/message_attachment.dart';
import 'package:mineral/src/api/messages/message_sticker_item.dart';
import 'package:mineral/src/api/messages/partial_message.dart';
Expand All @@ -24,14 +25,15 @@ class DmMessage extends PartialMessage<DmChannel> {
super.pinned,
super.channelId,
super.channel,
super.reactions,
this.author,
);

factory DmMessage.from({ required DmChannel channel, required dynamic payload }) {
MineralClient client = ioc.singleton(ioc.services.client);
User? user = client.users.cache.get(payload['author']['id']);
List<MessageEmbed> embeds = [];

List<MessageEmbed> embeds = [];
for (dynamic element in payload['embeds']) {
List<Field> fields = [];
if (element['fields'] != null) {
Expand Down Expand Up @@ -86,12 +88,14 @@ class DmMessage extends PartialMessage<DmChannel> {
}

List<Component> components = [];
for (dynamic payload in payload['components']) {
Component component = Component.from(payload: payload);
components.add(component);
if (payload['components'] != null) {
for (dynamic payload in payload['components']) {
Component component = Component.from(payload: payload);
components.add(component);
}
}

return DmMessage(
final message = DmMessage(
payload['id'],
payload['content'],
payload['tts'] ?? false,
Expand All @@ -106,7 +110,12 @@ class DmMessage extends PartialMessage<DmChannel> {
payload['pinned'],
channel.id,
channel,
MessageReactionManager<DmChannel, DmMessage>(channel),
user!,
);

message.reactions.message = message;

return message;
}
}
9 changes: 8 additions & 1 deletion lib/src/api/messages/message.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'package:mineral/api.dart';
import 'package:mineral/console.dart';
import 'package:mineral/core.dart';
import 'package:mineral/src/api/components/component.dart';
import 'package:mineral/src/api/managers/message_reaction_manager.dart';
import 'package:mineral/src/api/messages/message_attachment.dart';
import 'package:mineral/src/api/messages/message_sticker_item.dart';
import 'package:mineral/src/api/messages/partial_message.dart';
Expand All @@ -25,6 +26,7 @@ class Message extends PartialMessage<TextBasedChannel> {
super._pinned,
super._channelId,
super._channel,
super._reactions,
this._author,
);

Expand Down Expand Up @@ -146,7 +148,7 @@ class Message extends PartialMessage<TextBasedChannel> {
components.add(component);
}

return Message(
final message = Message(
payload['id'],
payload['content'],
payload['tts'] ?? false,
Expand All @@ -161,7 +163,12 @@ class Message extends PartialMessage<TextBasedChannel> {
payload['pinned'],
channel.id,
channel,
MessageReactionManager<TextBasedChannel, Message>(channel),
guildMember,
);

message.reactions.message = message;

return message;
}
}
3 changes: 3 additions & 0 deletions lib/src/api/messages/message_reaction.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class MessageReaction {

}
4 changes: 4 additions & 0 deletions lib/src/api/messages/partial_message.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'package:mineral/api.dart';
import 'package:mineral/src/api/channels/partial_channel.dart';
import 'package:mineral/src/api/components/component.dart';
import 'package:mineral/src/api/managers/message_reaction_manager.dart';
import 'package:mineral/src/api/messages/message_attachment.dart';
import 'package:mineral/src/api/messages/message_sticker_item.dart';

Expand All @@ -19,6 +20,7 @@ class PartialMessage<T extends PartialChannel> {
final bool _pinned;
final Snowflake _channelId;
final T _channel;
final MessageReactionManager _reactions;

PartialMessage(
this._id,
Expand All @@ -35,6 +37,7 @@ class PartialMessage<T extends PartialChannel> {
this._pinned,
this._channelId,
this._channel,
this._reactions,
);

Snowflake get id => _id;
Expand All @@ -48,4 +51,5 @@ class PartialMessage<T extends PartialChannel> {
bool get isPinned => _pinned;
Snowflake get channelId => _channelId;
T get channel => _channel;
MessageReactionManager get reactions => _reactions;
}