···
import { Agent } from '@atproto/api'
import { deleteSite } from '../lib/db'
import { logger } from '../lib/logger'
7
+
import { extractSubfsUris } from '../lib/wisp-utils'
export const siteRoutes = (client: NodeOAuthClient, cookieSecret: string) =>
···
// Create agent with OAuth session
const agent = new Agent((url, init) => auth.session.fetchHandler(url, init))
34
-
// Delete the record from AT Protocol
35
+
// First, fetch the site record to find any subfs references
36
+
let subfsUris: Array<{ uri: string; path: string }> = [];
38
+
const existingRecord = await agent.com.atproto.repo.getRecord({
40
+
collection: 'place.wisp.fs',
44
+
if (existingRecord.data.value && typeof existingRecord.data.value === 'object' && 'root' in existingRecord.data.value) {
45
+
const manifest = existingRecord.data.value as any;
46
+
subfsUris = extractSubfsUris(manifest.root);
48
+
if (subfsUris.length > 0) {
49
+
console.log(`Found ${subfsUris.length} subfs records to delete`);
50
+
logger.info(`[Site] Found ${subfsUris.length} subfs records associated with ${rkey}`);
54
+
// Record might not exist, continue with deletion
55
+
console.log('Could not fetch site record for subfs cleanup, continuing...');
58
+
// Delete the main record from AT Protocol
await agent.com.atproto.repo.deleteRecord({
···
logger.error(`[Site] Failed to delete site ${rkey} from PDS`, err)
throw new Error('Failed to delete site from AT Protocol')
71
+
// Delete associated subfs records
72
+
if (subfsUris.length > 0) {
73
+
console.log(`Deleting ${subfsUris.length} associated subfs records...`);
76
+
subfsUris.map(async ({ uri }) => {
78
+
// Parse URI: at://did/collection/rkey
79
+
const parts = uri.replace('at://', '').split('/');
80
+
const subRkey = parts[2];
82
+
await agent.com.atproto.repo.deleteRecord({
84
+
collection: 'place.wisp.subfs',
88
+
console.log(` 🗑️ Deleted subfs: ${uri}`);
89
+
logger.info(`[Site] Deleted subfs record: ${uri}`);
90
+
} catch (err: any) {
91
+
// Log but don't fail if subfs deletion fails
92
+
console.warn(`Failed to delete subfs ${uri}:`, err?.message);
93
+
logger.warn(`[Site] Failed to delete subfs ${uri}`, err);
98
+
logger.info(`[Site] Deleted ${subfsUris.length} subfs records for ${rkey}`);