Skip to content
This repository has been archived by the owner on Jul 15, 2024. It is now read-only.

Commit

Permalink
feat: delete intellegently (#386)
Browse files Browse the repository at this point in the history
  • Loading branch information
Hazmi35 authored Feb 24, 2024
1 parent 738080c commit ffe4a46
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 23 deletions.
23 changes: 20 additions & 3 deletions packages/core/src/Structures/Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import type { ChannelWrapper } from "amqp-connection-manager";
import type { Channel } from "amqplib";
import type { APIChannel, APIGuild, APIGuildMember, APIMessage, APIUser, RESTPostAPIChannelMessageJSONBody } from "discord-api-types/v10";
import { ChannelType, Routes } from "discord-api-types/v10";
import { and, eq, sql } from "drizzle-orm";
import { and, eq, inArray, not, sql } from "drizzle-orm";
import type { NodePgDatabase } from "drizzle-orm/node-postgres";
import { drizzle } from "drizzle-orm/node-postgres";
import pg from "pg";
Expand Down Expand Up @@ -408,8 +408,25 @@ export class Client extends EventEmitter {
});

if ("permission_overwrites" in channel && channel.permission_overwrites !== undefined) {
// TODO [2024-03-01]: Avoid delete all, intelligently delete only the ones that are not in the new payload
await this.store.delete(schema.channelsOverwrite).where(eq(schema.channelsOverwrite.channelId, channel.id));
const toBeDeleted = await this.store
.select({ id: schema.channelsOverwrite.channelId })
.from(schema.channelsOverwrite)
.where(
and(
eq(schema.channelsOverwrite.channelId, channel.id),
not(inArray(schema.channelsOverwrite.userOrRole, channel.permission_overwrites.map(x => x.id)))
)
);

if (toBeDeleted.length > 0) {
await this.store.delete(schema.channelsOverwrite).where(
and(
eq(schema.channelsOverwrite.channelId, channel.id),
inArray(schema.channelsOverwrite.userOrRole, toBeDeleted.map(x => x.id) as string[])
)
);
}

for (const overwrite of channel.permission_overwrites) {
// @ts-expect-error Intended to avoid .map
overwrite.channelId = channel.id;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { channels, channelsOverwrite } from "@nezuchan/kanao-schema";
import type { GatewayChannelUpdateDispatch } from "discord-api-types/v10";
import { GatewayDispatchEvents } from "discord-api-types/v10";
import { eq, sql } from "drizzle-orm";
import { and, eq, inArray, not, sql } from "drizzle-orm";
import type { ListenerContext } from "../../../Stores/Listener.js";
import { Listener } from "../../../Stores/Listener.js";
import { stateChannels } from "../../../config.js";
Expand Down Expand Up @@ -35,27 +35,43 @@ export class ChannelUpdateListener extends Listener {
nsfw: sql`EXCLUDED.nsfw`,
lastMessageId: sql`EXCLUDED.last_message_id`
}
})
.returning({ id: channels.id })
.then(c => c[0]);
});

// TODO [2024-03-01]: Avoid delete all, intelligently delete only the ones that are not in the new payload
await this.container.client.drizzle.delete(channelsOverwrite).where(eq(channelsOverwrite.channelId, payload.data.d.id));
if ("permission_overwrites" in payload.data.d && payload.data.d.permission_overwrites !== undefined) {
const toBeDeleted = await this.container.client.drizzle
.select({ id: channelsOverwrite.channelId })
.from(channelsOverwrite)
.where(
and(
eq(channelsOverwrite.channelId, payload.data.d.id),
not(inArray(channelsOverwrite.userOrRole, payload.data.d.permission_overwrites.map(x => x.id)))
)
);

if ("permission_overwrites" in payload.data.d && payload.data.d.permission_overwrites !== undefined && payload.data.d.permission_overwrites.length > 0) {
for (const overwrite of payload.data.d.permission_overwrites) {
// @ts-expect-error Intended to avoid .map
overwrite.channelId = payload.data.d.id;
if (toBeDeleted.length > 0) {
await this.container.client.drizzle.delete(channelsOverwrite).where(
and(
eq(channelsOverwrite.channelId, payload.data.d.id),
inArray(channelsOverwrite.userOrRole, toBeDeleted.map(x => x.id) as string[])
)
);
}

if (payload.data.d.permission_overwrites.length > 0) {
for (const overwrite of payload.data.d.permission_overwrites) {
// @ts-expect-error Intended to avoid .map
overwrite.userOrRole = overwrite.id;
}
overwrite.channelId = payload.data.d.id;

// @ts-expect-error Intended to avoid .map
overwrite.userOrRole = overwrite.id;
}

await this.container.client.drizzle.insert(channelsOverwrite)
.values(payload.data.d.permission_overwrites)
.onConflictDoNothing({
target: [channelsOverwrite.userOrRole, channelsOverwrite.channelId]
});
await this.container.client.drizzle.insert(channelsOverwrite)
.values(payload.data.d.permission_overwrites)
.onConflictDoNothing({
target: [channelsOverwrite.userOrRole, channelsOverwrite.channelId]
});
}
}

await DispatchListener.dispatch(payload);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { memberRoles, members, users } from "@nezuchan/kanao-schema";
import type { GatewayGuildMemberUpdateDispatch } from "discord-api-types/v10";
import { GatewayDispatchEvents } from "discord-api-types/v10";
import { and, eq, sql } from "drizzle-orm";
import { and, eq, inArray, not, sql } from "drizzle-orm";
import type { ListenerContext } from "../../../Stores/Listener.js";
import { Listener } from "../../../Stores/Listener.js";
import { stateMembers, stateUsers } from "../../../config.js";
Expand Down Expand Up @@ -68,8 +68,24 @@ export class GuildMemberUpdateListener extends Listener {
}
});

// TODO [2024-03-01]: Avoid delete all, intelligently delete only the ones that are not in the new payload
await this.container.client.drizzle.delete(memberRoles).where(and(eq(memberRoles.memberId, payload.data.d.user.id), eq(memberRoles.guildId, payload.data.d.guild_id)));
const toBeDeleted = await this.container.client.drizzle
.select({ id: memberRoles.roleId })
.from(memberRoles)
.where(
and(
eq(memberRoles.memberId, payload.data.d.user.id),
not(inArray(memberRoles.roleId, payload.data.d.roles))
)
);

if (toBeDeleted.length > 0) {
await this.container.client.drizzle.delete(memberRoles).where(
and(
eq(memberRoles.memberId, payload.data.d.user.id),
inArray(memberRoles.roleId, toBeDeleted.map(x => x.id) as string[])
)
);
}

if (payload.data.d.roles.length > 0) {
await this.container.client.drizzle.insert(memberRoles).values(payload.data.d.roles.map(role => ({
Expand Down

0 comments on commit ffe4a46

Please sign in to comment.