feat: handle reaction events

This commit is contained in:
Paul Makles
2022-07-16 14:42:04 +01:00
parent ee5e77bf2c
commit 811f94b9a8
4 changed files with 105 additions and 5 deletions
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "revolt.js",
"version": "6.0.10",
"version": "6.0.11",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
"module": "esm/index.js",
+52 -3
View File
@@ -9,7 +9,14 @@ import type {
} from "revolt-api";
import type { File } from "revolt-api";
import { makeAutoObservable, runInAction, action, computed } from "mobx";
import {
makeAutoObservable,
runInAction,
action,
computed,
ObservableMap,
ObservableSet,
} from "mobx";
import isEqual from "lodash.isequal";
import { Nullable, toNullable, toNullableDate } from "../util/null";
@@ -33,7 +40,7 @@ export class Message {
mention_ids: Nullable<string[]>;
reply_ids: Nullable<string[]>;
masquerade: Nullable<Masquerade>;
reactions: Record<string, string[]>;
reactions: ObservableMap<string, ObservableSet<string>>;
interactions: Nullable<Interactions>;
get channel() {
@@ -132,9 +139,16 @@ export class Message {
this.mention_ids = toNullable(data.mentions);
this.reply_ids = toNullable(data.replies);
this.masquerade = toNullable(data.masquerade);
this.reactions = data.reactions ?? {};
this.interactions = toNullable(data.interactions);
this.reactions = new ObservableMap();
for (const reaction of Object.keys(data.reactions ?? {})) {
this.reactions.set(
reaction,
new ObservableSet(data.reactions![reaction]),
);
}
makeAutoObservable(this, {
_id: false,
client: false,
@@ -223,6 +237,41 @@ export class Message {
replies: [{ id: this._id, mention }],
});
}
/**
* Clear all reactions from this message
*/
async clearReactions() {
return await this.client.api.delete(
`/channels/${this.channel_id as ""}/messages/${
this._id as ""
}/reactions`,
);
}
/**
* React to a message
* @param emoji Unicode or emoji ID
*/
async react(emoji: string) {
return await this.client.api.put(
`/channels/${this.channel_id as ""}/messages/${
this._id as ""
}/reactions/${emoji as ""}`,
);
}
/**
* Unreact from a message
* @param emoji Unicode or emoji ID
*/
async unreact(emoji: string) {
return await this.client.api.delete(
`/channels/${this.channel_id as ""}/messages/${
this._id as ""
}/reactions/${emoji as ""}`,
);
}
}
export default class Messages extends Collection<string, Message> {
+32 -1
View File
@@ -1,6 +1,6 @@
import { backOff } from "@insertish/exponential-backoff";
import WebSocket from "@insertish/isomorphic-ws";
import { runInAction } from "mobx";
import { ObservableSet, runInAction } from "mobx";
import { Role } from "revolt-api";
import { Client } from "..";
@@ -315,6 +315,37 @@ export class WebSocketClient {
break;
}
case "MessageReact": {
const msg = this.client.messages.get(packet.id);
if (msg) {
if (msg.reactions.has(packet.emoji_id)) {
msg.reactions
.get(packet.emoji_id)!
.add(packet.user_id);
} else {
msg.reactions.set(
packet.emoji_id,
new ObservableSet([packet.user_id]),
);
}
}
break;
}
case "MessageUnreact": {
const msg = this.client.messages.get(packet.id);
msg?.reactions
.get(packet.emoji_id)
?.delete(packet.user_id);
break;
}
case "MessageRemoveReaction": {
const msg = this.client.messages.get(packet.id);
msg?.reactions.delete(packet.emoji_id);
break;
}
case "BulkMessageDelete": {
runInAction(() => {
for (const id of packet.ids) {
+20
View File
@@ -55,6 +55,26 @@ export type ClientboundNotification =
append: Pick<Partial<Message>, "embeds">;
}
| { type: "MessageDelete"; id: string; channel: string }
| {
type: "MessageReact";
id: string;
channel_id: string;
user_id: string;
emoji_id: string;
}
| {
type: "MessageUnreact";
id: string;
channel_id: string;
user_id: string;
emoji_id: string;
}
| {
type: "MessageRemoveReaction";
id: string;
channel_id: string;
emoji_id: string;
}
| { type: "BulkMessageDelete"; channel: string; ids: string[] }
| ({ type: "ChannelCreate" } & Channel)
| {