Centralize periodic checks for mutes, tempbans, vcalerts, reminders, and scheduled posts

This should result in a significant performance improvement.
The new method is also more precise than the old one, allowing
the aforementioned checks to be performed with second-precision.
This commit is contained in:
Dragory 2021-09-25 21:33:59 +03:00
parent c84d1a0be1
commit c7751a9da1
No known key found for this signature in database
GPG key ID: 5F387BA66DF8AAC1
55 changed files with 883 additions and 366 deletions

View file

@ -4,7 +4,6 @@ import { GuildPluginData } from "knub";
import { userToTemplateSafeUser } from "../../../utils/templateSafeObjects";
import { CaseTypes } from "../../../data/CaseTypes";
import { Case } from "../../../data/entities/Case";
import { LogType } from "../../../data/LogType";
import { LogsPlugin } from "../../../plugins/Logs/LogsPlugin";
import { ERRORS, RecoverablePluginError } from "../../../RecoverablePluginError";
import { renderTemplate, TemplateSafeValueContainer } from "../../../templateFormatter";
@ -19,7 +18,12 @@ import {
import { muteLock } from "../../../utils/lockNameHelpers";
import { CasesPlugin } from "../../Cases/CasesPlugin";
import { MuteOptions, MutesPluginType } from "../types";
import { Mute } from "../../../data/entities/Mute";
import { registerExpiringMute } from "../../../data/loops/expiringMutesLoop";
/**
* TODO: Clean up this function
*/
export async function muteUser(
pluginData: GuildPluginData<MutesPluginType>,
userId: string,
@ -132,6 +136,7 @@ export async function muteUser(
// If the user is already muted, update the duration of their existing mute
const existingMute = await pluginData.state.mutes.findExistingMuteForUserId(user.id);
let finalMute: Mute;
let notifyResult: UserNotificationResult = { method: null, success: true };
if (existingMute) {
@ -139,10 +144,13 @@ export async function muteUser(
rolesToRestore = Array.from(new Set([...existingMute.roles_to_restore, ...rolesToRestore]));
}
await pluginData.state.mutes.updateExpiryTime(user.id, muteTime, rolesToRestore);
finalMute = (await pluginData.state.mutes.findExistingMuteForUserId(user.id))!;
} else {
await pluginData.state.mutes.addMute(user.id, muteTime, rolesToRestore);
finalMute = await pluginData.state.mutes.addMute(user.id, muteTime, rolesToRestore);
}
registerExpiringMute(finalMute);
const template = existingMute
? config.update_mute_message
: muteTime