Skip to content
This repository was archived by the owner on Oct 9, 2025. It is now read-only.
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
30 changes: 4 additions & 26 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -92,15 +92,16 @@ model ServerInfraction {
// TODO: major refactor needed for mode and profFilter thing
model connectedList {
id String @id @default(auto()) @map("_id") @db.ObjectId
mode Int @default(0) // 0 = compact, 1 = embed
channelId String @unique // channel can be thread, or a normal channel
parentId String? // ID of the parent channel, if it's a thread
parentId String? // ID of the parent channel, if it's a thread @map("parentChannelId")
serverId String
connected Boolean
compact Boolean
invite String?
profFilter Boolean
embedColor String?
webhookURL String
lastActive DateTime? @default(now())
// TODO: rename to createdAt
date DateTime @default(now())
hub Hub? @relation(fields: [hubId], references: [id])
hubId String @db.ObjectId
Expand All @@ -127,7 +128,6 @@ model Hub {
connections connectedList[]
logConfig HubLogConfig[]
msgBlockList MessageBlockList[]
originalMessages originalMessages[]
userInfractions UserInfraction[]
serverInfractions ServerInfraction[]

Expand Down Expand Up @@ -169,28 +169,6 @@ model HubInvite {
@@index([code, hubId])
}

model originalMessages {
messageId String @id @map("_id") @db.String
serverId String
authorId String
reactions Json? // eg. {"👎": ["9893820930928", "39283902803982"]} "emoji": userId[] basically
createdAt DateTime
mode Int @default(0)
broadcastMsgs broadcastedMessages[] // Ids of messages that were broadcasted to other hubs
messageReference String? @db.String // id of the original message this message is replying to
hub Hub @relation(fields: [hubId], references: [id])
hubId String @db.ObjectId
}

model broadcastedMessages {
messageId String @id @map("_id")
channelId String
createdAt DateTime
mode Int @default(0)
originalMsg originalMessages @relation(fields: [originalMsgId], references: [messageId])
originalMsgId String @db.String
}

model UserData {
id String @id @map("_id") @db.String
voteCount Int @default(0)
Expand Down
8 changes: 2 additions & 6 deletions src/commands/context-menu/deleteMsg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,8 @@ export default class DeleteMessage extends BaseCommand {
`${emojis.yes} Your request has been queued. Messages will be deleted shortly...`,
);

const broadcasts = await getBroadcasts(originalMsg.messageId, originalMsg.hubId);
const { deletedCount } = await deleteMessageFromHub(
hub.id,
originalMsg.messageId,
Object.values(broadcasts),
);
const broadcasts = Object.values(await getBroadcasts(originalMsg.messageId, originalMsg.hubId));
const { deletedCount } = await deleteMessageFromHub(hub.id, originalMsg.messageId, broadcasts);

await interaction
.editReply(
Expand Down
3 changes: 2 additions & 1 deletion src/utils/moderation/deleteMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import getRedis from '#utils/Redis.js';
import { cacheData, getCachedData } from '#utils/cache/cacheUtils.js';
import { getHubConnections } from '#utils/ConnectedListUtils.js';
import { Snowflake, WebhookClient } from 'discord.js';
import { Broadcast } from '#main/utils/network/messageUtils.js';
import { Broadcast, deleteMessageCache } from '#main/utils/network/messageUtils.js';

export const setDeleteLock = async (messageId: string) => {
const key = `${RedisKeys.msgDeleteInProgress}:${messageId}` as const;
Expand Down Expand Up @@ -33,6 +33,7 @@ export const deleteMessageFromHub = async (
}

await getRedis().del(`${RedisKeys.msgDeleteInProgress}:${originalMsgId}`);
deleteMessageCache(originalMsgId);
return { deletedCount };
};

Expand Down
23 changes: 19 additions & 4 deletions src/utils/network/messageUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export const storeMessage = async (originalMsgId: string, messageData: OriginalM
const redis = getRedis();

await redis.hset(key, messageData);
await redis.expire(key, 172800); // 2 days in seconds
await redis.expire(key, 86400); // 1 day in seconds
};

export const getOriginalMessage = async (originalMsgId: string) => {
Expand Down Expand Up @@ -70,14 +70,14 @@ export const addBroadcasts = async (
await redis
.multi()
.hset(broadcastsKey, ...broadcastEntries)
.expire(broadcastsKey, 172800)
.expire(broadcastsKey, 86400)
.mset(...reverseLookups)
.exec();

reverseLookups
.filter((_, i) => i % 2 === 0)
.forEach(async (key) => {
await redis.expire(key, 172800);
await redis.expire(key, 86400);
});
};

Expand Down Expand Up @@ -110,10 +110,25 @@ export const findOriginalMessage = async (broadcastedMessageId: string) => {
const lookup = await getRedis().get(reverseLookupKey);

if (!lookup) return null;
const [originalMsgId] = lookup.split(':');

return await getOriginalMessage(lookup);
return await getOriginalMessage(originalMsgId);
};

export const storeMessageTimestamp = async (message: Message) => {
await getRedis().hset(`${RedisKeys.msgTimestamp}`, message.channelId, message.createdTimestamp);
};

export const deleteMessageCache = async (originalMsgId: Snowflake) => {
const redis = getRedis();
const original = await getOriginalMessage(originalMsgId);
if (!original) return 0;

// delete broadcats, reverse lookups and original message
const broadcats = Object.values(await getBroadcasts(originalMsgId, original.hubId));
await redis.del(`${RedisKeys.broadcasts}:${originalMsgId}:${original.hubId}`);
await redis.del(broadcats.map((b) => `${RedisKeys.messageReverse}:${b.messageId}`)); // multi delete
const count = await redis.del(`${RedisKeys.message}:${originalMsgId}`);

return count;
};
Loading