Skip to content

Commit

Permalink
Merge pull request #253 from vxern/staging
Browse files Browse the repository at this point in the history
fix: Database sessions not being disposed of, invalid document IDs being used.
  • Loading branch information
vxern authored Nov 20, 2023
2 parents 0b1bf6f + e1a6f81 commit 8c11ca3
Show file tree
Hide file tree
Showing 32 changed files with 258 additions and 86 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"name": "logos",
"description": "A multi-purpose community bot built to cater to language-learning communities on Discord.",
"license": "ISC",
"version": "3.26.6",
"version": "3.27.0",
"type": "module",
"keywords": [
"discord",
Expand Down
7 changes: 6 additions & 1 deletion src/lib/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,7 @@ async function prefetchDataFromDatabase(client: Client): Promise<void> {
const entryRequestDocuments = await session.query<EntryRequest>({ collection: "EntryRequests" }).all();

for (const document of entryRequestDocuments) {
client.cache.documents.entryRequests.set(document.id, document);
client.cache.documents.entryRequests.set(`${document.guildId}/${document.authorId}`, document);
}

const reportDocuments = await session.query<Report>({ collection: "Reports" }).all();
Expand All @@ -472,6 +472,8 @@ async function prefetchDataFromDatabase(client: Client): Promise<void> {
for (const document of suggestionDocuments) {
client.cache.documents.suggestions.set(`${document.guildId}/${document.authorId}/${document.createdAt}`, document);
}

session.dispose();
}

export async function handleGuildCreate(
Expand All @@ -488,6 +490,9 @@ export async function handleGuildCreate(
const guildDocument =
client.cache.documents.guilds.get(guild.id.toString()) ??
(await session.load<Guild>(`guilds/${guild.id}`).then((value) => value ?? undefined));

session.dispose();

if (guildDocument === undefined) {
return;
}
Expand Down
3 changes: 3 additions & 0 deletions src/lib/commands/information/commands/information/guild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ async function handleDisplayGuildInformation(
const guildDocument =
client.cache.documents.guilds.get(guildId.toString()) ??
(await session.load<Guild>(`guilds/${guildId}`).then((value) => value ?? undefined));

session.dispose();

if (guildDocument === undefined) {
return;
}
Expand Down
9 changes: 8 additions & 1 deletion src/lib/commands/information/commands/list/warnings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ async function handleDisplayWarnings(

const isSelf = member.id === interaction.user.id;

const session = client.database.openSession();
let session = client.database.openSession();

const userDocument =
client.cache.documents.users.get(member.id.toString()) ??
Expand All @@ -82,11 +82,16 @@ async function handleDisplayWarnings(

return userDocument as User;
})());

session.dispose();

if (userDocument === undefined) {
displayError([client, bot], interaction, { locale });
return;
}

session = client.database.openSession();

const warningDocumentsCached = client.cache.documents.warningsByTarget.get(member.id.toString());
const warningDocuments =
warningDocumentsCached !== undefined
Expand All @@ -106,6 +111,8 @@ async function handleDisplayWarnings(
return documents;
});

session.dispose();

reply([client, bot], interaction, {
embeds: [getWarningPage(client, warningDocuments, isSelf, { locale })],
});
Expand Down
3 changes: 3 additions & 0 deletions src/lib/commands/language/commands/cefr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ async function handleDisplayCefrGuide(
const guildDocument =
client.cache.documents.guilds.get(guildId.toString()) ??
(await session.load<Guild>(`guilds/${guildId}`).then((value) => value ?? undefined));

session.dispose();

if (guildDocument === undefined) {
return;
}
Expand Down
3 changes: 3 additions & 0 deletions src/lib/commands/language/commands/resources.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ async function handleDisplayResources(
const guildDocument =
client.cache.documents.guilds.get(guildId.toString()) ??
(await session.load<Guild>(`guilds/${guildId}`).then((value) => value ?? undefined));

session.dispose();

if (guildDocument === undefined) {
return;
}
Expand Down
10 changes: 8 additions & 2 deletions src/lib/commands/meta/commands/settings/language/clear.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,16 @@ async function handleClearLanguage(
): Promise<void> {
const locale = interaction.locale;

const session = client.database.openSession();

await postponeReply([client, bot], interaction);

let session = client.database.openSession();

const userDocument =
client.cache.documents.users.get(interaction.user.id.toString()) ??
(await session.load<User>(`users/${interaction.user.id}`).then((value) => value ?? undefined));

session.dispose();

if (userDocument === undefined) {
return;
}
Expand All @@ -46,9 +50,11 @@ async function handleClearLanguage(
return;
}

session = client.database.openSession();
userDocument.account.language = undefined;
await session.store(userDocument);
await session.saveChanges();
session.dispose();

{
const strings = {
Expand Down
5 changes: 4 additions & 1 deletion src/lib/commands/meta/commands/settings/language/set.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,19 +68,22 @@ async function handleSetLanguage([client, bot]: [Client, Discord.Bot], interacti

const language = languageOrUndefined;

await postponeReply([client, bot], interaction);

const session = client.database.openSession();

await postponeReply([client, bot], interaction);
const userDocument =
client.cache.documents.users.get(interaction.user.id.toString()) ??
(await session.load<User>(`users/${interaction.user.id}`).then((value) => value ?? undefined));

if (userDocument === undefined) {
return;
}

userDocument.account.language = language;
await session.store(userDocument);
await session.saveChanges();
session.dispose();

const locale = getLocaleByLocalisationLanguage(language);

Expand Down
3 changes: 3 additions & 0 deletions src/lib/commands/meta/commands/settings/view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ async function handleDisplaySettings(
const userDocument =
client.cache.documents.users.get(interaction.user.id.toString()) ??
(await session.load<User>(`users/${interaction.user.id}`).then((value) => value ?? undefined));

session.dispose();

if (userDocument === undefined) {
return;
}
Expand Down
21 changes: 19 additions & 2 deletions src/lib/commands/moderation/commands/pardon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ async function handlePardonUserAutocomplete(
const guildDocument =
client.cache.documents.guilds.get(guildId.toString()) ??
(await session.load<Guild>(`guilds/${guildId}`).then((value) => value ?? undefined));

session.dispose();

if (guildDocument === undefined) {
return;
}
Expand Down Expand Up @@ -119,11 +122,14 @@ async function handlePardonUser([client, bot]: [Client, Discord.Bot], interactio
return;
}

const session = client.database.openSession();
let session = client.database.openSession();

const guildDocument =
client.cache.documents.guilds.get(guildId.toString()) ??
(await session.load<Guild>(`guilds/${guildId}`).then((value) => value ?? undefined));

session.dispose();

if (guildDocument === undefined) {
return;
}
Expand Down Expand Up @@ -166,9 +172,13 @@ async function handlePardonUser([client, bot]: [Client, Discord.Bot], interactio
return;
}

session = client.database.openSession();

await session.delete(warning.id);
await session.saveChanges();

session.dispose();

client.cache.documents.warningsByTarget
.get(member.id.toString())
?.delete(`${warning.targetId}/${warning.authorId}/${warning.createdAt}`);
Expand Down Expand Up @@ -211,15 +221,20 @@ async function getRelevantWarnings(
member: Logos.Member,
expirationMilliseconds: number,
): Promise<Warning[] | undefined> {
const session = client.database.openSession();
let session = client.database.openSession();

const userDocument =
client.cache.documents.users.get(member.id.toString()) ??
(await session.load<User>(`users/${member.id}`).then((value) => value ?? undefined));

session.dispose();

if (userDocument === undefined) {
return undefined;
}

session = client.database.openSession();

const warningDocumentsCached = client.cache.documents.warningsByTarget.get(member.id.toString());
const warningDocuments =
warningDocumentsCached !== undefined
Expand All @@ -239,6 +254,8 @@ async function getRelevantWarnings(
return documents;
});

session.dispose();

const relevantWarnings = getActiveWarnings(warningDocuments, expirationMilliseconds).reverse();
return relevantWarnings;
}
Expand Down
3 changes: 3 additions & 0 deletions src/lib/commands/moderation/commands/policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ async function handleDisplayModerationPolicy(
const guildDocument =
client.cache.documents.guilds.get(guildId.toString()) ??
(await session.load<Guild>(`guilds/${guildId}`).then((value) => value ?? undefined));

session.dispose();

if (guildDocument === undefined) {
return;
}
Expand Down
3 changes: 3 additions & 0 deletions src/lib/commands/moderation/commands/purge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ async function handlePurgeMessages(
const guildDocument =
client.cache.documents.guilds.get(guildId.toString()) ??
(await session.load<Guild>(`guilds/${guildId}`).then((value) => value ?? undefined));

session.dispose();

if (guildDocument === undefined) {
return;
}
Expand Down
13 changes: 12 additions & 1 deletion src/lib/commands/moderation/commands/report.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,14 @@ async function handleMakeReport([client, bot]: [Client, Discord.Bot], interactio
return;
}

const session = client.database.openSession();
let session = client.database.openSession();

const guildDocument =
client.cache.documents.guilds.get(guildId.toString()) ??
(await session.load<Guild>(`guilds/${guildId}`).then((value) => value ?? undefined));

session.dispose();

if (guildDocument === undefined) {
return;
}
Expand All @@ -62,6 +65,8 @@ async function handleMakeReport([client, bot]: [Client, Discord.Bot], interactio
return;
}

session = client.database.openSession();

const userDocument =
client.cache.documents.users.get(interaction.user.id.toString()) ??
(await session.load<User>(`users/${interaction.user.id}`).then((value) => value ?? undefined)) ??
Expand All @@ -80,6 +85,8 @@ async function handleMakeReport([client, bot]: [Client, Discord.Bot], interactio
return userDocument as User;
})());

session.dispose();

if (userDocument === undefined) {
return;
}
Expand Down Expand Up @@ -123,6 +130,8 @@ async function handleMakeReport([client, bot]: [Client, Discord.Bot], interactio
onSubmit: async (submission, answers) => {
await postponeReply([client, bot], submission);

const session = client.database.openSession();

const createdAt = Date.now();
const reportDocument = {
...({
Expand All @@ -138,6 +147,8 @@ async function handleMakeReport([client, bot]: [Client, Discord.Bot], interactio
await session.store(reportDocument);
await session.saveChanges();

session.dispose();

if (configuration.journaling) {
const journallingService = client.services.journalling.get(guild.id);
journallingService?.log("reportSubmit", { args: [member, reportDocument] });
Expand Down
3 changes: 3 additions & 0 deletions src/lib/commands/moderation/commands/rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ async function handleCiteRuleAutocomplete(
const guildDocument =
client.cache.documents.guilds.get(guildId.toString()) ??
(await session.load<Guild>(`guilds/${guildId}`).then((value) => value ?? undefined));

session.dispose();

if (guildDocument === undefined) {
return;
}
Expand Down
3 changes: 3 additions & 0 deletions src/lib/commands/moderation/commands/slowmode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ async function handleToggleSlowmode(
const guildDocument =
client.cache.documents.guilds.get(guildId.toString()) ??
(await session.load<Guild>(`guilds/${guildId}`).then((value) => value ?? undefined));

session.dispose();

if (guildDocument === undefined) {
return;
}
Expand Down
3 changes: 3 additions & 0 deletions src/lib/commands/moderation/commands/timeout/clear.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ async function handleClearTimeout([client, bot]: [Client, Discord.Bot], interact
const guildDocument =
client.cache.documents.guilds.get(guildId.toString()) ??
(await session.load<Guild>(`guilds/${guildId}`).then((value) => value ?? undefined));

session.dispose();

if (guildDocument === undefined) {
return;
}
Expand Down
3 changes: 3 additions & 0 deletions src/lib/commands/moderation/commands/timeout/set.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ async function handleSetTimeout([client, bot]: [Client, Discord.Bot], interactio
const guildDocument =
client.cache.documents.guilds.get(guildId.toString()) ??
(await session.load<Guild>(`guilds/${guildId}`).then((value) => value ?? undefined));

session.dispose();

if (guildDocument === undefined) {
return;
}
Expand Down
Loading

0 comments on commit 8c11ca3

Please sign in to comment.