mirror of
https://github.com/Sendouc/sendou.ink.git
synced 2026-03-21 18:04:39 -05:00
Automatically delete old trust relationships
This commit is contained in:
parent
9a851b99e7
commit
ea77ea361b
|
|
@ -679,6 +679,7 @@ export interface TournamentOrganizationSeries {
|
|||
export interface TrustRelationship {
|
||||
trustGiverUserId: number;
|
||||
trustReceiverUserId: number;
|
||||
lastUsedAt: number;
|
||||
}
|
||||
|
||||
export interface UnvalidatedUserSubmittedImage {
|
||||
|
|
|
|||
|
|
@ -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>) => {
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
|
|
|
|||
|
|
@ -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
|
||||
`);
|
||||
|
||||
|
|
|
|||
11
migrations/073-trust-timestamp.js
Normal file
11
migrations/073-trust-timestamp.js
Normal 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();
|
||||
})();
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user