Skip to content

Commit

Permalink
fix(msgs): fix old messages not getting deleted
Browse files Browse the repository at this point in the history
  • Loading branch information
dev-737 committed Jan 13, 2024
1 parent c6e202f commit ec99483
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 8 deletions.
1 change: 1 addition & 0 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ model originalMessages {
model broadcastedMessages {
messageId String @id @map("_id")
channelId String
createdAt Int
originalMsg originalMessages @relation(fields: [originalMsgId], references: [messageId])
originalMsgId String @db.String
}
Expand Down
8 changes: 2 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,10 @@ const deleteExpiredInvites = async () => {
// Delete all network messages from db that are older than 24 hours old.
const deleteOldMessages = async () => {
const olderThan24h = Date.now() - 60 * 60 * 24_000;
// the big number is Discord's epoch. ie. the first second of year 2015
const snowflakeFor24hAgo = (olderThan24h - 1420070400000) << 22;

db.broadcastedMessages
.findMany({ where: { messageId: { lte: `${snowflakeFor24hAgo}` } } })
.then(async (m) => {
await deleteMsgsFromDb(m.map(({ messageId }) => messageId));
})
.findMany({ where: { createdAt: { lte: olderThan24h } } })
.then(async (m) => deleteMsgsFromDb(m.map(({ messageId }) => messageId)))
.catch(captureException);
};

Expand Down
5 changes: 3 additions & 2 deletions src/managers/NetworkManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { Prisma, connectedList, hubs, originalMessages } from '@prisma/client';
import { LINKS, REGEX, emojis } from '../utils/Constants.js';
import { check as checkProfanity, censor } from '../utils/Profanity.js';
import { HubSettingsBitField } from '../utils/BitFields.js';
import { replaceLinks } from '../utils/Utils.js';
import { parseTimestampFromId, replaceLinks } from '../utils/Utils.js';
import HubLogsManager from './HubLogsManager.js';
import { t } from '../utils/Locale.js';

Expand Down Expand Up @@ -545,7 +545,7 @@ export default class NetworkManager extends Factory {
hubId: string,
dbReference?: originalMessages | null,
): Promise<void> {
const messageDataObj: { channelId: string; messageId: string }[] = [];
const messageDataObj: { channelId: string; messageId: string, createdAt: number }[] = [];
const invalidWebhookURLs: string[] = [];

// loop through all results and extract message data and invalid webhook urls
Expand Down Expand Up @@ -573,6 +573,7 @@ export default class NetworkManager extends Factory {
messageDataObj.push({
channelId: result.messageOrError.channelId,
messageId: result.messageOrError.id,
createdAt: parseTimestampFromId(result.messageOrError.id),
});
}
});
Expand Down

0 comments on commit ec99483

Please sign in to comment.