chore: optimisation for deleted messages storage

This commit is contained in:
M1000fr 2024-09-07 02:04:37 +02:00
parent 528b15d147
commit c918acb25f
4 changed files with 15 additions and 37 deletions

3
.gitignore vendored
View File

@ -1,4 +1,3 @@
node_modules/ node_modules/
build/ build/
messages/ messages/
messagesDeleted.json

View File

@ -2,7 +2,6 @@ import axios from "axios";
import fs from "node:fs/promises"; import fs from "node:fs/promises";
import { ChannelData } from "./interfaces/channelData"; import { ChannelData } from "./interfaces/channelData";
import { MessageData } from "./interfaces/messageData"; import { MessageData } from "./interfaces/messageData";
import { MessageDeleteData } from "./interfaces/messageDeleteData";
import moment from "moment"; import moment from "moment";
import "json-bigint-patch"; import "json-bigint-patch";
@ -51,18 +50,6 @@ async function main() {
const channelsFolder = await fs.readdir("./messages"); const channelsFolder = await fs.readdir("./messages");
// get messagesDeleted.json, if not exists, create it
var messagesDeletedFile: MessageDeleteData[] = await fs
.readFile("./messagesDeleted.json")
.catch(() => null)
.then((data) => data && JSON.parse(data.toString()));
if (!messagesDeletedFile) {
await fs.writeFile("./messagesDeleted.json", "[]");
messagesDeletedFile = [];
}
totalDeleted += messagesDeletedFile.length;
// Loop through all the folders in the messages directory to calculate the total number of messages // Loop through all the folders in the messages directory to calculate the total number of messages
for (const channelFolder of channelsFolder) { for (const channelFolder of channelsFolder) {
const channelDataFile = await fs const channelDataFile = await fs
@ -90,6 +77,10 @@ async function main() {
CHANNELS_DATA.push(channelData); CHANNELS_DATA.push(channelData);
MESSAGES_DATA[channelData.id] = JSON.parse(messagesData.toString()); MESSAGES_DATA[channelData.id] = JSON.parse(messagesData.toString());
totalDeleted += messages.filter(
(message) => message?.Deleted == true
).length;
} }
// Loop through all the folders in the messages directory to delete the messages // Loop through all the folders in the messages directory to delete the messages
@ -103,12 +94,7 @@ async function main() {
// get messagesDeleted.json and filter to get only messages not deleted // get messagesDeleted.json and filter to get only messages not deleted
const messages: MessageData[] = MESSAGES_DATA[channelData.id], const messages: MessageData[] = MESSAGES_DATA[channelData.id],
messagesNotDeleted = messages.filter((message) => { messagesNotDeleted = messages.filter((message) => {
const messageDeleted = messagesDeletedFile.find( return !message.Deleted;
(messageDeleted) =>
messageDeleted.channel_id === channelData.id &&
messageDeleted.message_id === message.ID
);
return !messageDeleted;
}); });
for (const message of messagesNotDeleted) { for (const message of messagesNotDeleted) {
@ -130,13 +116,11 @@ async function main() {
// Delete the message // Delete the message
await deleteMessage(channelData.id, message.ID) await deleteMessage(channelData.id, message.ID)
.then(async () => { .then(async () => {
messagesDeletedFile.push({ // Update the message data
channel_id: channelData.id, message.Deleted = true;
message_id: message.ID,
});
await fs.writeFile( await fs.writeFile(
"./messagesDeleted.json", `./messages/c${channelData.id}/messages.json`,
JSON.stringify(messagesDeletedFile, null, 2) JSON.stringify(messages, null, 2)
); );
}) })
.catch(async (error) => { .catch(async (error) => {
@ -151,13 +135,11 @@ async function main() {
errorMessage?.includes(msg) errorMessage?.includes(msg)
) )
) { ) {
messagesDeletedFile.push({ // Update the message data
channel_id: channelData.id, message.Deleted = true;
message_id: message.ID,
});
await fs.writeFile( await fs.writeFile(
"./messagesDeleted.json", `./messages/c${channelData.id}/messages.json`,
JSON.stringify(messagesDeletedFile, null, 2) JSON.stringify(messages, null, 1)
); );
} }
}); });

View File

@ -3,4 +3,5 @@ export interface MessageData {
Timestamp: string; Timestamp: string;
Contents: string; Contents: string;
Attachments: string; Attachments: string;
Deleted?: boolean;
} }

View File

@ -1,4 +0,0 @@
export interface MessageDeleteData {
channel_id: string;
message_id: bigint;
}