From f71583b382dfbd93517c2943dfed15121f558554 Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Tue, 24 Feb 2026 09:24:27 +0000 Subject: [PATCH] fix: compute tids for standard.site rkeys --- app/pages/blog/[article].vue | 5 ++-- modules/markdown.ts | 7 ++++- modules/shared/tid.ts | 19 +++++++++++++ modules/sync/providers/standard-site.ts | 36 ++++++++++++++++++++++--- package.json | 1 + pnpm-lock.yaml | 3 +++ 6 files changed, 64 insertions(+), 7 deletions(-) create mode 100644 modules/shared/tid.ts diff --git a/app/pages/blog/[article].vue b/app/pages/blog/[article].vue index 7b246fd7..53584797 100644 --- a/app/pages/blog/[article].vue +++ b/app/pages/blog/[article].vue @@ -83,6 +83,7 @@ const { data: page } = await useAsyncData( return { title: entry.title, date: entry.date, + tid: entry.tid, description: entry.description, tags: entry.tags, bluesky: entry.bluesky, @@ -145,8 +146,8 @@ if (import.meta.server) { }, ], link: [ - ...standardSiteDid - ? [{ rel: 'site.standard.document', href: `at://${standardSiteDid}/site.standard.document/${slug}` }] + ...standardSiteDid && page.value.tid + ? [{ rel: 'site.standard.document', href: `at://${standardSiteDid}/site.standard.document/${page.value.tid}` }] : [], ], }) diff --git a/modules/markdown.ts b/modules/markdown.ts index 342d140a..d5312760 100644 --- a/modules/markdown.ts +++ b/modules/markdown.ts @@ -10,6 +10,7 @@ import { convert as htmlToText } from 'html-to-text' import { serialize } from './shared/serialisers' import { mdCleanHtml, mdInternalLinks } from './shared/md-transforms' +import { tidFromDate } from './shared/tid' interface BlogFrontmatter { title: string @@ -24,6 +25,7 @@ interface ParsedBlogPost { slug: string title: string date: string + tid: string tags: string[] description: string path: string @@ -53,11 +55,13 @@ export default defineNuxtModule({ const { data, content } = grayMatter(raw) const slug = filename(filePath)! const fm = data as BlogFrontmatter + const date = typeof fm.date === 'object' ? (fm.date as Date).toISOString() : fm.date blogPosts.push({ slug, title: fm.title, - date: typeof fm.date === 'object' ? (fm.date as Date).toISOString() : fm.date, + date, + tid: tidFromDate(date), tags: fm.tags || [], description: fm.description || '', path: `/blog/${slug}`, @@ -226,6 +230,7 @@ declare module '#build/markdown/blog-entries.mjs' { interface BlogEntry { title: string date: string + tid: string tags: string[] description: string path: string diff --git a/modules/shared/tid.ts b/modules/shared/tid.ts new file mode 100644 index 00000000..7a00070f --- /dev/null +++ b/modules/shared/tid.ts @@ -0,0 +1,19 @@ +import { TID } from '@atproto/common-web' + +/** Fixed clock ID for deterministic TID generation from dates. */ +const CLOCK_ID = 3 +const MS_TO_MICROSECONDS = 1000 + +/** + * Generate a deterministic TID from a date string. + * + * Uses the date's millisecond timestamp converted to microseconds, + * combined with a fixed clock ID to ensure the same date always + * produces the same TID. + * + * @see https://atproto.com/specs/tid + */ +export function tidFromDate (date: string | Date): string { + const timestamp = (typeof date === 'string' ? new Date(date) : date).getTime() * MS_TO_MICROSECONDS + return TID.fromTime(timestamp, CLOCK_ID).toString() +} diff --git a/modules/sync/providers/standard-site.ts b/modules/sync/providers/standard-site.ts index 3bfe066d..444b5599 100644 --- a/modules/sync/providers/standard-site.ts +++ b/modules/sync/providers/standard-site.ts @@ -1,6 +1,7 @@ import { AtpAgent } from '@atproto/api' import type { SyncItem, SyncOptions, SyncProvider } from './index' +import { tidFromDate } from '../../shared/tid' export class StandardSiteProvider implements SyncProvider { name = 'standard-site' @@ -13,7 +14,8 @@ export class StandardSiteProvider implements SyncProvider { console.info(`[sync:standard-site] Would sync ${blogItems.length} blog posts as AT Protocol documents`) for (const item of blogItems) { const slug = item.canonical_url.replace('https://roe.dev/blog/', '').replace(/\/$/, '') - console.info(`[sync:standard-site] /blog/${slug}: ${item.title}`) + const rkey = item.date ? tidFromDate(item.date) : '(no date)' + console.info(`[sync:standard-site] /blog/${slug} (rkey: ${rkey}): ${item.title}`) } return } @@ -43,17 +45,43 @@ export class StandardSiteProvider implements SyncProvider { }, }) + // Delete any existing records with non-TID rkeys (slug-based) + const tidPattern = /^[234567abcdefghij][234567abcdefghijklmnopqrstuvwxyz]{12}$/ + try { + const existing = await agent.com.atproto.repo.listRecords({ + repo: did, + collection: 'site.standard.document', + limit: 100, + }) + for (const record of existing.data.records) { + const rkey = record.uri.split('/').pop()! + if (!tidPattern.test(rkey)) { + console.info(`[sync:standard-site] Deleting legacy record with rkey: ${rkey}`) + await agent.com.atproto.repo.deleteRecord({ + repo: did, + collection: 'site.standard.document', + rkey, + }) + } + } + } + catch (error) { + console.warn('[sync:standard-site] Failed to clean up legacy records:', error instanceof Error ? error.message : error) + } + let updated = 0 for (const item of blogItems) { const slug = item.canonical_url.replace('https://roe.dev/blog/', '').replace(/\/$/, '') - if (!slug) continue + if (!slug || !item.date) continue + + const rkey = tidFromDate(item.date) const record: Record = { $type: 'site.standard.document', site: `at://${did}/site.standard.publication/self`, path: `/blog/${slug}`, title: item.title, - publishedAt: item.date ? new Date(item.date).toISOString() : new Date().toISOString(), + publishedAt: new Date(item.date).toISOString(), } if (item.description) record.description = item.description @@ -63,7 +91,7 @@ export class StandardSiteProvider implements SyncProvider { await agent.com.atproto.repo.putRecord({ repo: did, collection: 'site.standard.document', - rkey: slug, + rkey, record, }) diff --git a/package.json b/package.json index 14626366..f99fa86a 100644 --- a/package.json +++ b/package.json @@ -85,6 +85,7 @@ "vue-router": "5.0.3" }, "devDependencies": { + "@atproto/common-web": "0.4.17", "@commitlint/cli": "20.4.2", "@commitlint/config-conventional": "20.4.2", "@nuxt/test-utils": "4.0.0", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index cb463aa5..476f49d8 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -171,6 +171,9 @@ importers: specifier: 5.0.3 version: 5.0.3(@vue/compiler-sfc@3.5.28)(vue@3.5.28(typescript@5.9.3)) devDependencies: + '@atproto/common-web': + specifier: 0.4.17 + version: 0.4.17 '@commitlint/cli': specifier: 20.4.2 version: 20.4.2(@types/node@25.3.0)(typescript@5.9.3) -- knotmirror 0.0.1