Files
RedditBot/packages/api-contracts/src/session.ts
T
Jamie BruntonandClaude Opus 5 a9cea7ed18 Retire removed accounts instead of deleting them
Removing an account had no path at all, so a dead session could only be
left running or hand-edited out of the database. The first cut deleted the
account and everything it owned, which threw away conversations and media
that are worth keeping.

Retire instead: keep the row and every row it produced, destroy the stored
cookies and Matrix token. Wiping the credentials is what makes sending and
syncing impossible rather than merely disabled — isEnabledAccount requires
a verified status, so no runtime ever starts, and no session survives for
anything to use. setActive, markLoginRequired and the background credential
refresh now skip retired accounts so none of them quietly revives one;
re-importing a session deliberately does.

Keeping the content visible needed more than keeping the rows. Conversations
and media are only served from running runtimes, so a retired account's
history would have disappeared from the dashboard regardless. Media already
reads the account content table for every listed account. Conversations now
have an archive path that reads the stored ActiveChat payloads directly.

Every write path refuses a retired conversation with a 409: send-reply,
both suggest endpoints, share-media, shareable-media and all read-state
writes. Lazy-loading detail is skipped too — the fallback chat service
belongs to a different account, so it would sync the wrong session and
write the wrong cache. Archived detail derives conversation state from the
transcript alone for the same reason.

Migration 32 rebuilds reddit_accounts to admit the new status, with foreign
keys off and a foreign_key_check before they are restored, as migration 11
does. Two migration tests rewound user_version by counting back from the
head, which silently breaks whenever a migration is appended; they now
target absolute versions.

Conversations never opened before removal keep only their summary. Message
history is lazy-loaded on first open and there is no session left to fetch
the rest with.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-12 08:58:22 +01:00

107 lines
4.3 KiB
TypeScript

import { z } from 'zod';
export const RedditSessionStateSchema = z.union([
z.literal('unknown'),
z.literal('authenticated'),
z.literal('login-required'),
z.literal('unreachable'),
]);
export const AccountEstablishmentStateSchema = z.union([
z.literal('unknown'),
z.literal('established'),
z.literal('unestablished'),
]);
export const AccountEstablishmentSummarySchema = z.strictObject({
state: AccountEstablishmentStateSchema,
/** True only when Reddit rejected an invitation; profile shortfalls do not hold sending. */
sendingHeld: z.boolean(),
reasons: z.array(z.string()),
accountAgeDays: z.number().nullable(),
linkKarma: z.number().nullable(),
commentKarma: z.number().nullable(),
totalKarma: z.number().nullable(),
hasVerifiedEmail: z.boolean().nullable(),
profileCheckedAt: z.number().nullable(),
lastProbeAt: z.number().nullable(),
lastProbeFailure: z.string().nullable(),
retryAt: z.number().nullable(),
});
export const RedditAccountSummarySchema = z.strictObject({
accountId: z.string().min(1),
redditUsername: z.string().min(1),
matrixUserId: z.string().min(1),
isRunning: z.boolean(),
status: z.union([z.literal('authenticated'), z.literal('login-required'), z.literal('removed')]),
createdAt: z.number(),
updatedAt: z.number(),
lastImportedAt: z.number(),
/** Set when the operator retired the account; its content is kept read-only. */
removedAt: z.number().nullable().optional(),
/** Absent on responses from a build that predates establishment tracking. */
establishment: AccountEstablishmentSummarySchema.nullable().optional(),
});
export const RedditAccountOwnerSchema = RedditAccountSummarySchema.pick({
accountId: true,
redditUsername: true,
});
export const RedditAccountsResponseSchema = z.array(RedditAccountSummarySchema);
export const LoginStatusResponseSchema = z.union([
z.strictObject({ ready: z.literal(false), error: z.string() }),
z.strictObject({
ready: z.literal(true),
loggedIn: z.boolean(),
status: RedditSessionStateSchema,
accountId: z.string().min(1).nullable().optional(),
username: z.string().min(1).nullable().optional(),
}),
z.strictObject({
ready: z.literal(true),
loggedIn: z.literal(false),
error: z.string(),
}),
]);
export const RedditCookieImportRequestSchema = z.strictObject({
cookies: z.array(z.strictObject({
name: z.string().min(1).max(256),
value: z.string().max(16_384),
domain: z.string().min(1).max(253),
path: z.string().min(1).max(4_096).default('/'),
expires: z.number().finite().optional(),
httpOnly: z.boolean().optional(),
secure: z.boolean().optional(),
sameSite: z.enum(['strict', 'lax', 'none', 'no_restriction', 'unspecified']).optional(),
})).min(1).max(100),
matrix: z.strictObject({
homeserver: z.string().url(),
accessToken: z.string().min(1).max(16_384),
userId: z.string().min(1).max(512),
capturedAt: z.number().finite(),
}),
});
export const RedditCookieImportResponseSchema = z.union([
z.strictObject({
ok: z.literal(true),
status: z.literal('authenticated'),
account: RedditAccountSummarySchema,
}),
z.strictObject({ ok: z.literal(false), error: z.string() }),
]);
export const RedditAccountActivationResponseSchema = z.union([
z.strictObject({ ok: z.literal(true), account: RedditAccountSummarySchema }),
z.strictObject({ ok: z.literal(false), error: z.string() }),
]);
/** The removed account is echoed back so the caller can name what it deleted. */
export const RedditAccountRemovalResponseSchema = z.union([
z.strictObject({ ok: z.literal(true), account: RedditAccountSummarySchema }),
z.strictObject({ ok: z.literal(false), error: z.string() }),
]);
export type RedditSessionState = z.infer<typeof RedditSessionStateSchema>;
export type LoginStatusResponse = z.infer<typeof LoginStatusResponseSchema>;
export type RedditCookieImportRequest = z.infer<typeof RedditCookieImportRequestSchema>;
export type RedditAccountSummary = z.infer<typeof RedditAccountSummarySchema>;
export type AccountEstablishmentState = z.infer<typeof AccountEstablishmentStateSchema>;
export type AccountEstablishmentSummary = z.infer<typeof AccountEstablishmentSummarySchema>;
export type RedditAccountOwner = z.infer<typeof RedditAccountOwnerSchema>;
export type RedditAccountRemovalResponse = z.infer<typeof RedditAccountRemovalResponseSchema>;