feat: handle bulk delete

This commit is contained in:
Paul Makles
2022-05-07 16:34:51 +01:00
parent ef26fe3fe6
commit 1813d41dac
5 changed files with 114 additions and 49 deletions
+2 -2
View File
@@ -1,6 +1,6 @@
{
"name": "revolt.js",
"version": "6.0.0-rc.26",
"version": "6.0.0-rc.27",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
"module": "esm/index.js",
@@ -17,7 +17,7 @@
"lodash.isequal": "^4.5.0",
"long": "^5.2.0",
"mobx": "^6.3.2",
"revolt-api": "0.5.3-rc.16",
"revolt-api": "0.5.3",
"ulid": "^2.3.0",
"ws": "^8.2.2"
},
+9
View File
@@ -565,6 +565,15 @@ export class Channel {
return { deprecated: ids };
}
async deleteMessages(ids: string[]) {
await this.client.api.post(
// @ts-expect-error .delete does not support params
`/channels/${this._id as ""}/messages/bulk`,
{ ids },
{ method: "DELETE" },
);
}
/**
* Create an invite to the channel
* @returns Newly created invite code
+77 -27
View File
@@ -150,8 +150,9 @@ export class WebSocketClient {
});
this.client.user = this.client.users.get(
packet.users.find((x) => x.relationship === "User")!
._id,
packet.users.find(
(x) => x.relationship === "User",
)!._id,
)!;
this.client.emit("ready");
@@ -191,7 +192,9 @@ export class WebSocketClient {
);
this.disconnect();
this.connect(disallowReconnect);
this.connect(
disallowReconnect,
);
}
}
}, this.client.options.pongTimeout * 1000);
@@ -231,17 +234,21 @@ export class WebSocketClient {
}
}
} else {
await this.client.users.fetch(packet.author);
await this.client.users.fetch(
packet.author,
);
}
const channel = await this.client.channels.fetch(
packet.channel,
);
const channel =
await this.client.channels.fetch(
packet.channel,
);
if (channel.channel_type === "TextChannel") {
const server = await this.client.servers.fetch(
channel.server_id!,
);
const server =
await this.client.servers.fetch(
channel.server_id!,
);
if (
packet.author !==
"00000000000000000000000000"
@@ -249,18 +256,30 @@ export class WebSocketClient {
await server.fetchMember(packet.author);
}
const message = this.client.messages.createObj(packet, true);
const message = this.client.messages.createObj(
packet,
true,
);
runInAction(() => {
if (channel.channel_type === "DirectMessage") {
if (
channel.channel_type === "DirectMessage"
) {
channel.active = true;
}
channel.last_message_id = message._id;
if (this.client.unreads &&
message.mention_ids?.includes(this.client.user!._id)) {
this.client.unreads.markMention(message.channel_id, message._id);
if (
this.client.unreads &&
message.mention_ids?.includes(
this.client.user!._id,
)
) {
this.client.unreads.markMention(
message.channel_id,
message._id,
);
}
});
}
@@ -292,6 +311,17 @@ export class WebSocketClient {
break;
}
case "MessageBulkDelete": {
runInAction(() => {
for (const id of packet.ids) {
const msg = this.client.messages.get(id);
this.client.messages.delete(id);
this.client.emit("message/delete", id, msg);
}
});
break;
}
case "ChannelCreate": {
runInAction(async () => {
if (packet.type !== "ChannelCreate") throw 0;
@@ -300,9 +330,10 @@ export class WebSocketClient {
packet.channel_type === "TextChannel" ||
packet.channel_type === "VoiceChannel"
) {
const server = await this.client.servers.fetch(
packet.server,
);
const server =
await this.client.servers.fetch(
packet.server,
);
server.channel_ids.push(packet._id);
}
@@ -323,7 +354,11 @@ export class WebSocketClient {
case "ChannelDelete": {
const channel = this.client.channels.get(packet.id);
channel?.delete(true);
this.client.emit("channel/delete", packet.id, channel);
this.client.emit(
"channel/delete",
packet.id,
channel,
);
break;
}
@@ -352,12 +387,20 @@ export class WebSocketClient {
runInAction(async () => {
const channels = [];
for (const channel of packet.channels) {
channels.push(await this.client.channels.fetch(channel._id, channel));
channels.push(
await this.client.channels.fetch(
channel._id,
channel,
),
);
}
await this.client.servers.fetch(packet.id, packet.server);
await this.client.servers.fetch(
packet.id,
packet.server,
);
});
break;
}
@@ -373,12 +416,18 @@ export class WebSocketClient {
case "ServerDelete": {
const server = this.client.servers.get(packet.id);
server?.delete(true);
this.client.emit("server/delete", packet.id, server);
this.client.emit(
"server/delete",
packet.id,
server,
);
break;
}
case "ServerMemberUpdate": {
const member = this.client.members.getKey(packet.id);
const member = this.client.members.getKey(
packet.id,
);
if (member) {
member.update(packet.data, packet.clear);
this.client.emit("member/update", member);
@@ -415,7 +464,8 @@ export class WebSocketClient {
[...this.client.members.keys()].forEach(
(key) => {
if (
JSON.parse(key).server === server_id
JSON.parse(key).server ===
server_id
) {
this.client.members.delete(key);
}
@@ -484,7 +534,7 @@ export class WebSocketClient {
if (user) {
user.update({
...packet.user,
relationship: packet.status
relationship: packet.status,
});
} else {
this.client.users.createObj(packet.user);
@@ -536,7 +586,7 @@ export class WebSocketClient {
`Warning: Unhandled packet! ${packet.type}`,
);
}
} catch(e) {
} catch (e) {
console.error(e);
}
};
+22 -16
View File
@@ -1,11 +1,12 @@
import type { FieldsChannel, FieldsMember, FieldsServer, FieldsUser, Session } from "revolt-api";
import type { Channel, Message } from "revolt-api";
import type {
Member,
MemberCompositeKey,
Role,
Server,
FieldsChannel,
FieldsMember,
FieldsServer,
FieldsUser,
Session,
} from "revolt-api";
import type { Channel, Message } from "revolt-api";
import type { Member, MemberCompositeKey, Role, Server } from "revolt-api";
import type { RelationshipStatus, User } from "revolt-api";
type WebSocketError = {
@@ -47,12 +48,13 @@ export type ClientboundNotification =
data: Partial<Message>;
}
| {
type: "MessageAppend";
id: string;
channel: string;
append: Pick<Partial<Message>, 'embeds'>;
type: "MessageAppend";
id: string;
channel: string;
append: Pick<Partial<Message>, "embeds">;
}
| { type: "MessageDelete"; id: string; channel: string }
| { type: "MessageBulkDelete"; channel: string; ids: string[] }
| ({ type: "ChannelCreate" } & Channel)
| {
type: "ChannelUpdate";
@@ -67,11 +69,11 @@ export type ClientboundNotification =
| { type: "ChannelStopTyping"; id: string; user: string }
| { type: "ChannelAck"; id: string; user: string; message_id: string }
| {
type: "ServerCreate";
id: string;
server: Server;
channels: Channel[];
}
type: "ServerCreate";
id: string;
server: Server;
channels: Channel[];
}
| {
type: "ServerUpdate";
id: string;
@@ -102,4 +104,8 @@ export type ClientboundNotification =
}
| { type: "UserRelationship"; user: User; status: RelationshipStatus }
| { type: "UserPresence"; id: string; online: boolean }
| { type: "UserSettingsUpdate"; id: string; update: { [key: string]: [number, string] } };
| {
type: "UserSettingsUpdate";
id: string;
update: { [key: string]: [number, string] };
};
+4 -4
View File
@@ -1018,10 +1018,10 @@ reusify@^1.0.4:
resolved "https://registry.yarnpkg.com/reusify/-/reusify-1.0.4.tgz#90da382b1e126efc02146e90845a88db12925d76"
integrity sha512-U9nH88a3fc/ekCF1l0/UP1IosiuIjyTh7hBvXVMHYgVcfGvt897Xguj2UOLDeI5BG2m7/uwyaLVT6fbtCwTyzw==
revolt-api@0.5.3-rc.16:
version "0.5.3-rc.16"
resolved "https://registry.yarnpkg.com/revolt-api/-/revolt-api-0.5.3-rc.16.tgz#7c6ecab99286fd54e8b19a878afafda0bea9e6f3"
integrity sha512-KMTMMaJxf9MsWFHjkcL0/I+5gaa4NDnR0yTC6BZ3eObTxLmAHUAD8OsCB/aewGR7zKDSyuNWBVXHDpLjb+6JrA==
revolt-api@0.5.3:
version "0.5.3"
resolved "https://registry.yarnpkg.com/revolt-api/-/revolt-api-0.5.3.tgz#e0ec2dcf812ea4338247b2eb77d67fc731d71b8a"
integrity sha512-hYdyStQiDZFvD+0dlf6SgQSiOk+JiEmQo0qIQHaqYRtrFN6FQBbGVNaiv7b5LzHHMPq7vks6ZVVA7hSNpcwlkA==
dependencies:
"@insertish/oapi" "0.1.15"
axios "^0.26.1"