feat: read subscription ids from the payload per event

This commit is contained in:
Leafus committed 2026-08-18 23:44:36 +02:00
1 parent 715b575e12
commit bfb7a2129b
2 files changed
+22 -15

No files matched your search

+2 -2
View File
@@ -166,12 +166,12 @@ router.delete(
const user = await User.findOneOrFail({
where: { id: req.user_id },
select: Object.fromEntries(userProjection.map((i) => [i, true])), // TODO: cleanup
relations: { relationships: { to: true } },
relations: { relationships: true },
});
const friend = await User.findOneOrFail({
where: { id: user_id },
select: Object.fromEntries(userProjection.map((i) => [i, true])), // TODO: cleanup
relations: { relationships: { to: true } },
relations: { relationships: true },
});
const relationship = user.relationships.find((x) => x.to_id === user_id);
+20 -13
View File
@@ -185,7 +185,6 @@ export async function setupListener(this: WebSocket) {
async function consume(this: WebSocket, opts: EventOpts) {
const { data, event } = opts;
const id = (opts.guild_id || opts.channel_id || opts.user_id || opts.session_id) as string;
const subscription_id = (data?.id as string) || id;
const permission = this.permissions[id] || new Permissions("ADMINISTRATOR"); // default permission for dm
const consumer = consume.bind(this);
@@ -240,12 +239,14 @@ async function consume(this: WebSocket, opts: EventOpts) {
break;
case "RELATIONSHIP_REMOVE":
case "CHANNEL_DELETE":
case "GUILD_DELETE":
this.events[subscription_id]?.();
delete this.events[subscription_id];
case "GUILD_DELETE": {
const removed_id = data.id as string;
if (!removed_id) break;
this.events[removed_id]?.();
delete this.events[removed_id];
if (event === "GUILD_DELETE" && this.ipAddress) {
const ban = await Ban.findOne({
where: { guild_id: subscription_id, user_id: this.user_id },
where: { guild_id: removed_id, user_id: this.user_id },
});
if (ban) {
@@ -254,31 +255,37 @@ async function consume(this: WebSocket, opts: EventOpts) {
}
}
break;
case "CHANNEL_CREATE":
}
case "CHANNEL_CREATE": {
if (!permission.overwriteChannel(data.permission_overwrites).has("VIEW_CHANNEL")) return;
this.events[subscription_id] = await listenEvent(subscription_id, consumer, listenOpts);
const channel_id = data.id as string;
this.events[channel_id] = await listenEvent(channel_id, consumer, listenOpts);
break;
}
case "RELATIONSHIP_ADD":
// don't let a payload without a user object stop the dispatch below
if (data.user?.id) this.events[data.user.id] = await listenEvent(data.user.id, handlePresenceUpdate.bind(this), this.listen_options);
break;
case "GUILD_CREATE":
case "GUILD_CREATE": {
const guild_id = data.id as string;
await Promise.all([
...data.channels.map(async ({ id }: { id: string }) => {
this.events[id] = await listenEvent(id, consumer, listenOpts);
}),
listenEvent(subscription_id, consumer, listenOpts).then((ret) => (this.events[subscription_id] = ret)),
listenEvent(guild_id, consumer, listenOpts).then((ret) => (this.events[guild_id] = ret)),
]);
break;
}
case "CHANNEL_UPDATE": {
const exists = this.events[subscription_id];
const channel_id = data.id as string;
const exists = this.events[channel_id];
if (permission.overwriteChannel(data.permission_overwrites).has("VIEW_CHANNEL")) {
if (exists) break;
this.events[subscription_id] = await listenEvent(subscription_id, consumer, listenOpts);
this.events[channel_id] = await listenEvent(channel_id, consumer, listenOpts);
} else {
if (!exists) return; // return -> do not send channel update events for hidden channels
opts.cancel(subscription_id);
delete this.events[subscription_id];
opts.cancel(channel_id);
delete this.events[channel_id];
}
break;
}