Repository navigation
feat(retention): delete expired sessions and ended moderation records #71
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
a3d3313
feat(retention): delete expired sessions and ended moderation records
lorenzocorallo b5d1b26
feat(retention): keep temporary moderation records 24 months after ex…
lorenzocorallo 94bae89
Merge origin/main into lorenzocorallo/data-retention
lorenzocorallo 0b9bcec
Merge branch 'main' into lorenzocorallo/data-retention
toto04 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| import { and, eq, inArray, isNotNull, isNull, lt, notExists, or, type SQL, sql } from "drizzle-orm" | ||
| import { type AnyPgColumn, alias, QueryBuilder } from "drizzle-orm/pg-core" | ||
| import { ENDED_MODERATION_RETENTION_DAYS, MODERATION_RETENTION_MONTHS } from "@/constants" | ||
| import { AUDIT_TYPE, auditLog, type TAuditType } from "@/db/schema/tg/audit-log" | ||
|
|
||
| // Moderation records are kept for 24 months after expiry if temporary, until revocation if | ||
| // permanent (privacy policy, data retention). A kick is a temporary measure that ends at once. | ||
|
|
||
| /** The measures each revocation lifts; `allGroups` when it lifts them in every group. */ | ||
| const REVOKES: Partial<Record<TAuditType, { types: TAuditType[]; allGroups: boolean }>> = { | ||
| [AUDIT_TYPE.UNBAN]: { types: [AUDIT_TYPE.BAN], allGroups: false }, | ||
| [AUDIT_TYPE.UNMUTE]: { types: [AUDIT_TYPE.MUTE], allGroups: false }, | ||
| // The bot's unban_all unbans the target in every group, which also lifts single-group bans. | ||
| [AUDIT_TYPE.UNBAN_ALL]: { types: [AUDIT_TYPE.BAN_ALL, AUDIT_TYPE.BAN], allGroups: true }, | ||
| } | ||
|
|
||
| const MEASURES: TAuditType[] = [AUDIT_TYPE.BAN, AUDIT_TYPE.MUTE, AUDIT_TYPE.BAN_ALL] | ||
| const REVOCATIONS: TAuditType[] = [AUDIT_TYPE.UNBAN, AUDIT_TYPE.UNMUTE, AUDIT_TYPE.UNBAN_ALL] | ||
|
|
||
| /** Telegram treats a restriction ending more than 366 days after it is applied as permanent. */ | ||
| const TELEGRAM_FOREVER_AFTER = sql.raw(`interval '366 days'`) | ||
|
|
||
| type MeasureRecord = { until: AnyPgColumn; createdAt: AnyPgColumn } | ||
|
|
||
| /** A measure with an end date Telegram honours. */ | ||
| function temporary(record: MeasureRecord) { | ||
| return and(isNotNull(record.until), sql`${record.until} <= ${record.createdAt} + ${TELEGRAM_FOREVER_AFTER}`) | ||
| } | ||
|
|
||
| /** A measure without an end date, or with one Telegram ignores. */ | ||
| function permanent(record: MeasureRecord) { | ||
| return or(isNull(record.until), sql`${record.until} > ${record.createdAt} + ${TELEGRAM_FOREVER_AFTER}`) | ||
| } | ||
|
|
||
| /** | ||
| * The records `revocation` deletes, or undefined when it is not a revocation: the permanent | ||
| * measures it lifts. Lifted temporary measures stay until their retention ends. Only records | ||
| * stored before the revocation match, so it never lifts a later measure. | ||
| */ | ||
| export function revokedMeasures(revocation: { | ||
| id: number | ||
| type: TAuditType | ||
| targetId: number | ||
| groupId: number | null | ||
| }): SQL | undefined { | ||
| const revokes = REVOKES[revocation.type] | ||
| if (!revokes) return undefined | ||
|
|
||
| const group = revokes.allGroups | ||
| ? undefined | ||
| : revocation.groupId === null | ||
| ? isNull(auditLog.groupId) | ||
| : eq(auditLog.groupId, revocation.groupId) | ||
| return and( | ||
| eq(auditLog.targetId, revocation.targetId), | ||
| inArray(auditLog.type, revokes.types), | ||
| lt(auditLog.id, revocation.id), | ||
| group, | ||
| permanent(auditLog) | ||
| ) | ||
| } | ||
|
|
||
| /** Whether the revocation in the outer `auditLog` row lifted a temporary measure still in force. */ | ||
| function liftsTemporaryMeasure() { | ||
| const lifted = alias(auditLog, "lifted") | ||
| const revokes = Object.entries(REVOKES).map(([type, { types, allGroups }]) => | ||
| and( | ||
| eq(auditLog.type, type as TAuditType), | ||
| inArray(lifted.type, types), | ||
| allGroups ? undefined : sql`${lifted.groupId} is not distinct from ${auditLog.groupId}` | ||
| ) | ||
| ) | ||
| return new QueryBuilder() | ||
| .select({ one: sql`1` }) | ||
| .from(lifted) | ||
| .where( | ||
| and( | ||
| eq(lifted.targetId, auditLog.targetId), | ||
| lt(lifted.id, auditLog.id), | ||
| or(...revokes), | ||
| temporary(lifted), | ||
| sql`${lifted.until} > ${auditLog.createdAt}` | ||
| ) | ||
| ) | ||
| } | ||
|
|
||
| /** | ||
| * Records past their retention at `now`: | ||
| * - temporary measures {@link MODERATION_RETENTION_MONTHS} after they end, kicks the same time | ||
| * after they are made; | ||
| * - revocations of a temporary measure as long as a temporary measure, from when they are made; | ||
| * - other revocations after {@link ENDED_MODERATION_RETENTION_DAYS}, long enough for the bot's | ||
| * retries of a revocation to hit its idempotency key instead of lifting a later measure. | ||
| * | ||
| * Permanent measures are deleted only by their revocation, see {@link revokedMeasures}. | ||
| */ | ||
| export function endedMeasures(now: Date): SQL | undefined { | ||
| const retainedSince = new Date(now) | ||
| retainedSince.setUTCMonth(retainedSince.getUTCMonth() - MODERATION_RETENTION_MONTHS) | ||
| const retriesSince = new Date(now) | ||
| retriesSince.setDate(retriesSince.getDate() - ENDED_MODERATION_RETENTION_DAYS) | ||
|
|
||
| return or( | ||
| and(inArray(auditLog.type, MEASURES), temporary(auditLog), lt(auditLog.until, retainedSince)), | ||
| and(inArray(auditLog.type, [AUDIT_TYPE.KICK, ...REVOCATIONS]), lt(auditLog.createdAt, retainedSince)), | ||
| and(inArray(auditLog.type, REVOCATIONS), lt(auditLog.createdAt, retriesSince), notExists(liftsTemporaryMeasure())) | ||
| ) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| import { PgDialect } from "drizzle-orm/pg-core" | ||
| import { describe, expect, it } from "vitest" | ||
| import { endedMeasures, revokedMeasures } from "@/utils/audit-log" | ||
|
|
||
| const dialect = new PgDialect() | ||
| function render(condition: Parameters<PgDialect["sqlToQuery"]>[0] | undefined) { | ||
| if (!condition) return undefined | ||
| const { sql, params } = dialect.sqlToQuery(condition) | ||
| return { sql, params } | ||
| } | ||
|
|
||
| /** No end date, or one Telegram treats as forever. */ | ||
| const PERMANENT = | ||
| '("tg_audit_log"."until" is null or "tg_audit_log"."until" > "tg_audit_log"."created_at" + interval \'366 days\')' | ||
| /** An end date Telegram honours. */ | ||
| const TEMPORARY = (table: string) => | ||
| `("${table}"."until" is not null and "${table}"."until" <= "${table}"."created_at" + interval '366 days')` | ||
|
|
||
| describe("revokedMeasures", () => { | ||
| it("matches the earlier permanent bans of the target in the same group on unban", () => { | ||
| expect(render(revokedMeasures({ id: 9, type: "unban", targetId: 777, groupId: -100 }))).toEqual({ | ||
| sql: `("tg_audit_log"."target_id" = $1 and "tg_audit_log"."type" in ($2) and "tg_audit_log"."id" < $3 and "tg_audit_log"."group_id" = $4 and ${PERMANENT})`, | ||
| params: [777, "ban", 9, -100], | ||
| }) | ||
| }) | ||
|
|
||
| it("matches the earlier mutes of the target in the same group on unmute", () => { | ||
| expect(render(revokedMeasures({ id: 9, type: "unmute", targetId: 777, groupId: -100 }))?.params).toEqual([ | ||
| 777, | ||
| "mute", | ||
| 9, | ||
| -100, | ||
| ]) | ||
| }) | ||
|
|
||
| it("matches the permanent ban_all and bans in every group on unban_all", () => { | ||
| expect(render(revokedMeasures({ id: 9, type: "unban_all", targetId: 777, groupId: null }))).toEqual({ | ||
| sql: `("tg_audit_log"."target_id" = $1 and "tg_audit_log"."type" in ($2, $3) and "tg_audit_log"."id" < $4 and ${PERMANENT})`, | ||
| params: [777, "ban_all", "ban", 9], | ||
| }) | ||
| }) | ||
|
|
||
| it("matches nothing for a measure", () => { | ||
| for (const type of ["ban", "mute", "kick", "ban_all"] as const) { | ||
| expect(revokedMeasures({ id: 9, type, targetId: 777, groupId: -100 })).toBeUndefined() | ||
| } | ||
| }) | ||
| }) | ||
|
|
||
| describe("endedMeasures", () => { | ||
| const now = new Date("2026-10-14T12:00:00Z") | ||
| const twoYearsAgo = new Date("2024-10-14T12:00:00Z").toISOString() | ||
| const yesterday = new Date("2026-10-13T12:00:00Z").toISOString() | ||
| const ended = render(endedMeasures(now)) | ||
|
|
||
| it("matches temporary measures 24 months after they end", () => { | ||
| expect(ended?.sql).toContain( | ||
| `("tg_audit_log"."type" in ($1, $2, $3) and ${TEMPORARY("tg_audit_log")} and "tg_audit_log"."until" < $4)` | ||
| ) | ||
| expect(ended?.params.slice(0, 4)).toEqual(["ban", "mute", "ban_all", twoYearsAgo]) | ||
| }) | ||
|
|
||
| it("matches kicks and revocations 24 months after they are made", () => { | ||
| expect(ended?.sql).toContain('("tg_audit_log"."type" in ($5, $6, $7, $8) and "tg_audit_log"."created_at" < $9)') | ||
| expect(ended?.params.slice(4, 9)).toEqual(["kick", "unban", "unmute", "unban_all", twoYearsAgo]) | ||
| }) | ||
|
|
||
| it("matches revocations older than a day that lifted no temporary measure in force", () => { | ||
| expect(ended?.sql).toContain( | ||
| '("tg_audit_log"."type" in ($10, $11, $12) and "tg_audit_log"."created_at" < $13 and not exists (select 1 from "tg_audit_log" "lifted" where (' | ||
| ) | ||
| expect(ended?.sql).toContain( | ||
| '"lifted"."target_id" = "tg_audit_log"."target_id" and "lifted"."id" < "tg_audit_log"."id" and ' + | ||
| '(("tg_audit_log"."type" = $14 and "lifted"."type" in ($15) and "lifted"."group_id" is not distinct from "tg_audit_log"."group_id") or ' + | ||
| '("tg_audit_log"."type" = $16 and "lifted"."type" in ($17) and "lifted"."group_id" is not distinct from "tg_audit_log"."group_id") or ' + | ||
| '("tg_audit_log"."type" = $18 and "lifted"."type" in ($19, $20))) and ' + | ||
| `${TEMPORARY("lifted")} and "lifted"."until" > "tg_audit_log"."created_at"` | ||
| ) | ||
| expect(ended?.params.slice(9)).toEqual([ | ||
| "unban", | ||
| "unmute", | ||
| "unban_all", | ||
| yesterday, | ||
| "unban", | ||
| "ban", | ||
| "unmute", | ||
| "mute", | ||
| "unban_all", | ||
| "ban_all", | ||
| "ban", | ||
| ]) | ||
| }) | ||
|
|
||
| it("never matches a permanent measure", () => { | ||
| expect(ended?.sql).not.toContain('"tg_audit_log"."until" is null') | ||
| }) | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| import { beforeEach, describe, expect, it, vi } from "vitest" | ||
|
|
||
| const state = vi.hoisted(() => { | ||
| const session = { id: Symbol("auth_sessions.id"), expiresAt: Symbol("auth_sessions.expires_at") } | ||
| const auditLog = { id: Symbol("tg_audit_log.id") } | ||
| const condition = Symbol("expiry-condition") | ||
| const ended = Symbol("ended-measures") | ||
| const returning = vi.fn(async () => [{ id: 1 }]) | ||
| const where = vi.fn(() => ({ returning })) | ||
| const deleteFrom = vi.fn(() => ({ where })) | ||
| const lessThan = vi.fn(() => condition) | ||
| const endedMeasures = vi.fn(() => ended) | ||
|
|
||
| return { auditLog, condition, deleteFrom, ended, endedMeasures, lessThan, returning, session, where } | ||
| }) | ||
|
|
||
| vi.mock("croner", () => ({ Cron: vi.fn() })) | ||
| vi.mock("drizzle-orm", () => ({ lt: state.lessThan })) | ||
| vi.mock("@/db", () => ({ | ||
| DB: { delete: state.deleteFrom }, | ||
| SCHEMA: { AUTH: { session: state.session }, TG: { auditLog: state.auditLog } }, | ||
| })) | ||
| vi.mock("@/env", () => ({ env: { NODE_ENV: "test" } })) | ||
| vi.mock("@/logger", () => ({ logger: { error: vi.fn(), info: vi.fn() } })) | ||
| vi.mock("@/utils/audit-log", () => ({ endedMeasures: state.endedMeasures })) | ||
|
|
||
| const { cleanAuditLog, cleanSessions } = await import("@/cron") | ||
|
|
||
| const DAY = 86_400_000 | ||
|
|
||
| beforeEach(() => { | ||
| vi.clearAllMocks() | ||
| }) | ||
|
|
||
| describe("cleanSessions", () => { | ||
| it("deletes sessions that expired more than 30 days ago", async () => { | ||
| const before = Date.now() | ||
| await cleanSessions() | ||
|
|
||
| expect(state.deleteFrom).toHaveBeenCalledWith(state.session) | ||
| expect(state.lessThan).toHaveBeenCalledWith(state.session.expiresAt, expect.any(Date)) | ||
| const cutoff = (state.lessThan.mock.calls[0] as unknown[])[1] as Date | ||
| // setDate keeps the wall-clock time, so a DST change can shift the cutoff by an hour. | ||
| expect(Math.abs(before - cutoff.getTime() - 30 * DAY)).toBeLessThanOrEqual(3_600_000 + 1_000) | ||
| expect(state.where).toHaveBeenCalledWith(state.condition) | ||
| }) | ||
| }) | ||
|
|
||
| describe("cleanAuditLog", () => { | ||
| it("deletes the moderation records past their retention", async () => { | ||
| const before = Date.now() | ||
| await cleanAuditLog() | ||
|
|
||
| expect(state.endedMeasures).toHaveBeenCalledWith(expect.any(Date)) | ||
| const now = (state.endedMeasures.mock.calls[0] as unknown[])[0] as Date | ||
| expect(now.getTime()).toBeGreaterThanOrEqual(before) | ||
| expect(now.getTime()).toBeLessThanOrEqual(Date.now()) | ||
| expect(state.deleteFrom).toHaveBeenCalledWith(state.auditLog) | ||
| expect(state.where).toHaveBeenCalledWith(state.ended) | ||
| }) | ||
| }) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.