Automatically delete old trust relationships

This commit is contained in:
Kalle 2024-11-11 21:57:28 +02:00
parent 9a851b99e7
commit ea77ea361b
7 changed files with 60 additions and 3 deletions

View File

@ -679,6 +679,7 @@ export interface TournamentOrganizationSeries {
export interface TrustRelationship {
trustGiverUserId: number;
trustReceiverUserId: number;
lastUsedAt: number;
}
export interface UnvalidatedUserSubmittedImage {

View File

@ -9,10 +9,12 @@ import { isbot } from "isbot";
import cron from "node-cron";
import { renderToPipeableStream } from "react-dom/server";
import { I18nextProvider, initReactI18next } from "react-i18next";
import * as QRepository from "~/features/sendouq/QRepository.server";
import { config } from "~/modules/i18n/config"; // your i18n configuration file
import i18next from "~/modules/i18n/i18next.server";
import { resources } from "./modules/i18n/resources.server";
import { updatePatreonData } from "./modules/patreon";
import { logger } from "./utils/logger";
const ABORT_DELAY = 5000;
@ -88,6 +90,13 @@ if (!global.appStartSignal && process.env.NODE_ENV === "production") {
cron.schedule("0 */2 * * *", () =>
updatePatreonData().catch((err) => console.error(err)),
);
// every hour
cron.schedule("0 */1 * * *", async () => {
const { numDeletedRows } = await QRepository.deleteOldTrust();
logger.info(`Deleted ${numDeletedRows} old trusts`);
});
}
process.on("unhandledRejection", (reason: string, p: Promise<any>) => {

View File

@ -1,3 +1,4 @@
import { sub } from "date-fns";
import { sql } from "kysely";
import { jsonArrayFrom, jsonObjectFrom } from "kysely/helpers/sqlite";
import { nanoid } from "nanoid";
@ -8,7 +9,7 @@ import type {
TablesInsertable,
UserMapModePreferences,
} from "~/db/tables";
import { dateToDatabaseTimestamp } from "~/utils/dates";
import { databaseTimestampNow, dateToDatabaseTimestamp } from "~/utils/dates";
import { COMMON_USER_FIELDS } from "~/utils/kysely.server";
import { userIsBanned } from "../ban/core/banned.server";
import type { LookingGroupWithInviteCode } from "./q-types";
@ -333,3 +334,28 @@ export async function usersThatTrusted(userId: number) {
trusters: deduplicatedRows,
};
}
/** Update the timestamp of the trust relationship, delaying its automatic deletion */
export async function refreshTrust({
trustGiverUserId,
trustReceiverUserId,
}: {
trustGiverUserId: number;
trustReceiverUserId: number;
}) {
return db
.updateTable("TrustRelationship")
.set({ lastUsedAt: databaseTimestampNow() })
.where("trustGiverUserId", "=", trustGiverUserId)
.where("trustReceiverUserId", "=", trustReceiverUserId)
.execute();
}
export async function deleteOldTrust() {
const twoMonthsAgo = sub(new Date(), { months: 2 });
return db
.deleteFrom("TrustRelationship")
.where("lastUsedAt", "<", dateToDatabaseTimestamp(twoMonthsAgo))
.executeTakeFirst();
}

View File

@ -108,6 +108,10 @@ export const action = async ({ request }: ActionFunctionArgs) => {
userId: data.id,
role: "MANAGER",
});
await QRepository.refreshTrust({
trustGiverUserId: data.id,
trustReceiverUserId: user.id,
});
return null;
}

View File

@ -239,6 +239,10 @@ export const action: ActionFunction = async ({ request, params }) => {
userId: data.userId,
}),
});
await QRepository.refreshTrust({
trustGiverUserId: data.userId,
trustReceiverUserId: user.id,
});
ShowcaseTournaments.addToParticipationInfoMap({
tournamentId,

View File

@ -3,10 +3,12 @@ import { sql } from "~/db/sql";
const stm = sql.prepare(/*sql */ `
insert into "TrustRelationship" (
"trustGiverUserId",
"trustReceiverUserId"
"trustReceiverUserId",
"lastUsedAt"
) values (
@trustGiverUserId,
@trustReceiverUserId
@trustReceiverUserId,
strftime('%s', 'now')
) on conflict do nothing
`);

View File

@ -0,0 +1,11 @@
export function up(db) {
db.transaction(() => {
db.prepare(
/* sql */ `alter table "TrustRelationship" add "lastUsedAt" integer default 0`,
).run();
db.prepare(
/* sql */ `update "TrustRelationship" set "lastUsedAt" = strftime('%s', 'now')`,
).run();
})();
}