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>
This commit is contained in:
1 parent
3f60f7f665
commit
a9cea7ed18
16 files changed
+433
-97
No files matched your search
@@ -50,10 +50,13 @@ export interface RedditAccountSummary {
|
||||
redditUsername: string;
|
||||
matrixUserId: string;
|
||||
isRunning: boolean;
|
||||
status: 'authenticated' | 'login-required';
|
||||
/** `removed` is terminal until a fresh session is imported for the account. */
|
||||
status: 'authenticated' | 'login-required' | 'removed';
|
||||
createdAt: number;
|
||||
updatedAt: number;
|
||||
lastImportedAt: number;
|
||||
/** When the operator retired the account. Null while it is in use. */
|
||||
removedAt: number | null;
|
||||
/** Null until the establishment check has run for this account. */
|
||||
establishment: RedditAccountEstablishmentSummary | null;
|
||||
}
|
||||
@@ -78,6 +81,11 @@ export class RedditAccountsService {
|
||||
return this.list().find((account) => account.accountId === accountId) ?? null;
|
||||
}
|
||||
|
||||
/** Retired accounts, whose collected content is still served read-only. */
|
||||
listRemoved(): RedditAccountSummary[] {
|
||||
return this.list().filter((account) => account.status === 'removed');
|
||||
}
|
||||
|
||||
async activate(accountId: string): Promise<RedditAccountSummary> {
|
||||
const stored = this.accounts.get(accountId);
|
||||
if (!stored || stored.status === 'legacy-unassigned') {
|
||||
@@ -91,19 +99,23 @@ export class RedditAccountsService {
|
||||
}
|
||||
|
||||
/**
|
||||
* Stop the account's loop and drop it, along with the content it owns, from
|
||||
* the database. Downloaded media files stay on disk: the media library can
|
||||
* hand an image to another account, so the files are not ours alone to bin.
|
||||
* Retire the account: stop its loop and destroy its credentials, but keep
|
||||
* every conversation, message and media item it collected. The content stays
|
||||
* readable and attributed; with no session left there is nothing to sync and
|
||||
* nothing to send with.
|
||||
*/
|
||||
async remove(accountId: string): Promise<RedditAccountSummary> {
|
||||
const account = this.get(accountId);
|
||||
if (!account) throw new Error('Reddit account not found');
|
||||
if (account.status === 'removed') throw new Error('Reddit account was already removed');
|
||||
await this.runtimes?.stop(accountId);
|
||||
if (!this.accounts.delete(accountId)) throw new Error('Reddit account not found');
|
||||
if (!this.accounts.retire(accountId)) throw new Error('Reddit account not found');
|
||||
if (this.auth.getSnapshot().accountId === accountId) {
|
||||
this.auth.invalidate(`Reddit account u/${account.redditUsername} was removed`);
|
||||
}
|
||||
return { ...account, isRunning: false, status: 'login-required' };
|
||||
const retired = this.get(accountId);
|
||||
if (!retired) throw new Error('Removed Reddit account was not persisted');
|
||||
return retired;
|
||||
}
|
||||
|
||||
async importSession(cookies: Cookie[], credentials: MatrixCredentials): Promise<RedditAccountSummary> {
|
||||
@@ -124,18 +136,23 @@ export class RedditAccountsService {
|
||||
|
||||
private toSummary(account: RedditAccountPublicRecord): RedditAccountSummary {
|
||||
const runtime = this.runtimes?.get(account.accountId);
|
||||
const running = runtime?.snapshot().state === 'running';
|
||||
const running = account.status === 'removed' ? false : runtime?.snapshot().state === 'running';
|
||||
return {
|
||||
accountId: account.accountId,
|
||||
redditUsername: account.redditUsername,
|
||||
matrixUserId: account.matrixUserId ?? account.accountId,
|
||||
isRunning: running ?? (account.accountId === this.auth.getSnapshot().accountId),
|
||||
status: account.status === 'verified' && (running ?? account.accountId === this.auth.getSnapshot().accountId)
|
||||
? 'authenticated'
|
||||
: 'login-required',
|
||||
isRunning: account.status === 'removed'
|
||||
? false
|
||||
: running ?? (account.accountId === this.auth.getSnapshot().accountId),
|
||||
status: account.status === 'removed'
|
||||
? 'removed'
|
||||
: account.status === 'verified' && (running ?? account.accountId === this.auth.getSnapshot().accountId)
|
||||
? 'authenticated'
|
||||
: 'login-required',
|
||||
createdAt: account.createdAt,
|
||||
updatedAt: account.updatedAt,
|
||||
lastImportedAt: account.lastImportedAt ?? account.updatedAt,
|
||||
removedAt: account.removedAt,
|
||||
establishment: this.toEstablishmentSummary(account.accountId),
|
||||
};
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user