rvlib-shipkit 2.12.0 → 2.13.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +48 -13
- package/README.md +1 -1
- package/package.json +4 -2
- package/src/agent-time/store.ts +2 -2
- package/src/check.ts +1 -1
- package/src/checks/SK001-package-json-shape.check.ts +4 -0
- package/src/checks/SK003-claude-md-shape.check.ts +1 -1
- package/src/checks/SK004-gitignore-policy.check.ts +27 -2
- package/src/checks/SK005-rvlib-versions.check.ts +1 -1
- package/src/checks/SK008-reviewer-agent.check.ts +2 -2
- package/src/checks/SK012-config-barrel-import.check.ts +1 -1
- package/src/checks/SK015-nested-repo.check.ts +1 -1
- package/src/checks/SK035-public-clean.check.ts +46 -0
- package/src/checks/SK036-draft-media-size.check.ts +28 -0
- package/src/cli/cmd_Backup.ts +152 -0
- package/src/cli/cmd_Ci.ts +8 -11
- package/src/cli/cmd_Drill.ts +145 -0
- package/src/cli/cmd_Home.ts +50 -3
- package/src/cli/cmd_Media.ts +7 -5
- package/src/cli/cmd_Review.ts +4 -2
- package/src/cli/cmd_Shot.ts +5 -2
- package/src/cli/cmd_Smoke.ts +4 -1
- package/src/cli/cmd_Social.ts +226 -0
- package/src/cli/cmd_Start.ts +1 -1
- package/src/cli/cmd_Time.ts +1 -1
- package/src/cli/cmd_Trailer.ts +5 -2
- package/src/cli/cmd_Veille.ts +173 -0
- package/src/cli/family-pages.ts +3 -0
- package/src/cli/registry.ts +13 -2
- package/src/cli.ts +5 -2
- package/src/collections/actions.ts +1 -49
- package/src/collections/checks.ts +2 -1
- package/src/collections/collection-types.ts +11 -2
- package/src/collections/providers.ts +4 -4
- package/src/collections/review.ts +2 -1
- package/src/collections/tests.ts +19 -8
- package/src/contracts/apply-contracts.ts +1 -3
- package/src/contracts/contract-manifests.ts +2 -3
- package/src/contracts/contract-types.ts +0 -1
- package/src/contracts/contracts-engine.ts +0 -3
- package/src/core/app-icon.ts +9 -3
- package/src/core/bun-builtins.ts +2 -2
- package/src/core/discover-checks.ts +37 -5
- package/src/core/fix-scope.ts +4 -2
- package/src/core/floor-resolve.ts +68 -48
- package/src/core/floor.ts +9 -0
- package/src/core/git-root.ts +24 -0
- package/src/core/last-good.ts +3 -1
- package/src/core/layout.ts +8 -0
- package/src/core/log-stamp.ts +36 -0
- package/src/core/loop-watch.ts +93 -0
- package/src/core/machine-link.ts +134 -0
- package/src/core/memory-watch.ts +41 -0
- package/src/core/one-way-in-policy.ts +27 -19
- package/src/core/run-ci.ts +6 -8
- package/src/core/run-fixers.ts +9 -9
- package/src/core/slots.ts +21 -0
- package/src/core/tree-rev.ts +60 -0
- package/src/core/unit-files.ts +42 -0
- package/src/drill/render.ts +140 -0
- package/src/drill/run.ts +269 -0
- package/src/fleet/scan-fleet.ts +6 -5
- package/src/home/daemon-jobs.ts +201 -0
- package/src/home/global-context.ts +34 -1
- package/src/home/home-routes.ts +22 -15
- package/src/home/hubs.ts +10 -3
- package/src/home/inbox-shape.ts +53 -4
- package/src/home/inbox.ts +16 -1
- package/src/home/lib-watch.ts +10 -3
- package/src/home/machine-stores.ts +15 -29
- package/src/home/run-daemon.ts +15 -18
- package/src/home/run-pm.ts +2 -0
- package/src/home/serve-daemon.ts +4 -3
- package/src/home/serve-web.ts +104 -1
- package/src/hooks/commit-message.ts +28 -19
- package/src/hooks/leak-guard.ts +10 -3
- package/src/hooks/run-hooks.ts +2 -6
- package/src/identities/identities.ts +334 -0
- package/src/identities/machine-route.ts +126 -0
- package/src/identities/machine.ts +62 -0
- package/src/init-repo.ts +1 -1
- package/src/ledger/check-stats.ts +10 -9
- package/src/ledger/gate-checks.ts +32 -1
- package/src/ledger/record-kept.ts +112 -0
- package/src/ledger/repo-view.ts +10 -0
- package/src/ledger/run-log.ts +17 -12
- package/src/ledger/run-timeline.ts +8 -4
- package/src/ledger/store.ts +7 -0
- package/src/ledger/suite-log.ts +17 -13
- package/src/media-kinds.ts +2 -1
- package/src/media-store/backup/daemon.ts +73 -0
- package/src/media-store/backup/keys.ts +25 -0
- package/src/media-store/backup/manifest.ts +193 -0
- package/src/media-store/backup/restore.ts +192 -0
- package/src/media-store/backup/run.ts +278 -0
- package/src/media-store/backup/secret.ts +86 -0
- package/src/media-store/backup/snapshot.ts +64 -0
- package/src/media-store/backup/sources.ts +199 -0
- package/src/media-store/daemon.ts +56 -70
- package/src/media-store/put.ts +4 -2
- package/src/media-store/space.ts +23 -4
- package/src/pages/forward.ts +35 -0
- package/src/pages/frame-client.ts +118 -0
- package/src/pages/routes.ts +29 -0
- package/src/pages/unit.ts +92 -0
- package/src/plan/planner.ts +12 -0
- package/src/plan/repo-source.ts +19 -5
- package/src/reflect/transcript.ts +35 -10
- package/src/release/public-clean.ts +301 -0
- package/src/release/published-here.ts +22 -0
- package/src/release/run-bump.ts +2 -1
- package/src/release/run-release.ts +4 -1
- package/src/repo-config.ts +53 -2
- package/src/review/http.ts +16 -2
- package/src/review/model.ts +15 -4
- package/src/review/notes.ts +77 -0
- package/src/review/project.ts +6 -38
- package/src/review/routes.ts +4 -12
- package/src/review/store.ts +41 -1
- package/src/review/trailer-media.ts +7 -4
- package/src/review/trailer-shots.ts +21 -4
- package/src/review/unit.ts +1 -1
- package/src/scaffold/files/agent-handoff.md +3 -0
- package/src/scaffold/files/agent-index.md +4 -12
- package/src/scaffold/project-scaffold.ts +5 -4
- package/src/serve/embed.ts +1 -1
- package/src/serve/hub-app/col-card.tsx +3 -17
- package/src/serve/hub-app/col-tables-view.tsx +2 -1
- package/src/serve/hub-app/cols-svc.ts +0 -13
- package/src/serve/hub-app/dock-st.ts +4 -2
- package/src/serve/hub-app/dock-view.tsx +2 -0
- package/src/serve/hub-app/frames-st.ts +35 -0
- package/src/serve/hub-app/hash-route.ts +64 -1
- package/src/serve/hub-app/home-st.ts +31 -7
- package/src/serve/hub-app/home-strip.css +9 -1
- package/src/serve/hub-app/home-strip.tsx +7 -6
- package/src/serve/hub-app/home.html +5 -0
- package/src/serve/hub-app/hub-app.tsx +49 -5
- package/src/serve/hub-app/hub.css +29 -11
- package/src/serve/hub-app/icons.tsx +16 -1
- package/src/serve/hub-app/identities-st.ts +210 -0
- package/src/serve/hub-app/identities-view.tsx +352 -0
- package/src/serve/hub-app/identities.css +48 -0
- package/src/serve/hub-app/identity-address.ts +52 -0
- package/src/serve/hub-app/inbox-st.ts +59 -6
- package/src/serve/hub-app/inbox-view.tsx +48 -6
- package/src/serve/hub-app/inbox.css +12 -1
- package/src/serve/hub-app/index.html +3 -0
- package/src/serve/hub-app/keys.ts +10 -2
- package/src/serve/hub-app/list-walk.ts +89 -0
- package/src/serve/hub-app/machine-st.ts +12 -0
- package/src/serve/hub-app/media-view.tsx +2 -1
- package/src/serve/hub-app/pages-frames.tsx +134 -0
- package/src/serve/hub-app/pages-svc.ts +111 -0
- package/src/serve/hub-app/palette-st.ts +18 -3
- package/src/serve/hub-app/palette-view.tsx +9 -2
- package/src/serve/hub-app/planned-view.tsx +2 -1
- package/src/serve/hub-app/project-list.css +18 -14
- package/src/serve/hub-app/project-list.tsx +88 -49
- package/src/serve/hub-app/projects-svc.ts +33 -3
- package/src/serve/hub-app/repo-sections.ts +19 -2
- package/src/serve/hub-app/repo-st.ts +75 -19
- package/src/serve/hub-app/review/review-cells.tsx +3 -3
- package/src/serve/hub-app/review/review-kit.ts +30 -7
- package/src/serve/hub-app/review/review-page.tsx +1 -1
- package/src/serve/hub-app/review/review-st.ts +19 -8
- package/src/serve/hub-app/review/review-thread.tsx +11 -9
- package/src/serve/hub-app/review/review-views.tsx +84 -19
- package/src/serve/hub-app/review/thread-st.ts +1 -10
- package/src/serve/hub-app/review/trailer-page.tsx +2 -1
- package/src/serve/hub-app/review.css +17 -8
- package/src/serve/hub-app/site/site-part.tsx +173 -0
- package/src/serve/hub-app/site/site-st.ts +157 -0
- package/src/serve/hub-app/site.css +35 -0
- package/src/serve/hub-app/social/calendar.tsx +182 -0
- package/src/serve/hub-app/social/comms-page.tsx +306 -0
- package/src/serve/hub-app/social/kit.tsx +244 -0
- package/src/serve/hub-app/social/post-card.tsx +456 -0
- package/src/serve/hub-app/social/problems.tsx +60 -0
- package/src/serve/hub-app/social/social-model.ts +154 -0
- package/src/serve/hub-app/social/social-page.tsx +674 -0
- package/src/serve/hub-app/social/social-st.ts +986 -0
- package/src/serve/hub-app/social.css +289 -0
- package/src/serve/hub-app/stores/stores-page.tsx +311 -0
- package/src/serve/hub-app/stores/stores-st.ts +286 -0
- package/src/serve/hub-app/stores.css +54 -0
- package/src/serve/hub-app/timeline-geometry.ts +1 -1
- package/src/serve/hub-app/timeline-view.tsx +3 -2
- package/src/serve/hub-app/tree-build.ts +6 -0
- package/src/serve/hub-app/tree-row.tsx +27 -4
- package/src/serve/hub-app/tree-svc.ts +18 -49
- package/src/serve/hub-app/veille/veille-page.tsx +514 -0
- package/src/serve/hub-app/veille/veille-st.ts +443 -0
- package/src/serve/hub-app/veille.css +84 -0
- package/src/serve/hub-runtime.ts +14 -9
- package/src/serve/project-gutter-route.ts +44 -12
- package/src/serve/run-lanes.ts +7 -4
- package/src/serve/same-origin.ts +1 -1
- package/src/serve/serve-hub.ts +20 -12
- package/src/serve/tracked-watch.ts +24 -3
- package/src/serve/warm-cell.ts +5 -1
- package/src/shipkit-files.ts +15 -4
- package/src/shipkit.ts +0 -1
- package/src/shot/box.ts +162 -0
- package/src/shot/browser-slot.ts +62 -0
- package/src/shot/client.ts +43 -7
- package/src/shot/clip-encoder.ts +93 -12
- package/src/shot/film-box.ts +188 -0
- package/src/shot/shooter.ts +50 -15
- package/src/shot/shot-ask.ts +10 -2
- package/src/shot/stepped-clip.ts +5 -4
- package/src/site/desk.ts +242 -0
- package/src/site/routes.ts +66 -0
- package/src/site/unit.ts +62 -0
- package/src/social/brands.ts +22 -0
- package/src/social/compose.ts +97 -0
- package/src/social/desk-index.ts +123 -0
- package/src/social/desk-wire.ts +64 -0
- package/src/social/drafts.ts +183 -0
- package/src/social/house.ts +24 -0
- package/src/social/load.ts +319 -0
- package/src/social/md.ts +70 -0
- package/src/social/media-facts.ts +53 -0
- package/src/social/media-kind.ts +189 -0
- package/src/social/media-limit.ts +42 -0
- package/src/social/media-of.ts +30 -0
- package/src/social/migrate.ts +123 -0
- package/src/social/open.ts +17 -0
- package/src/social/post-url.ts +158 -0
- package/src/social/presence.ts +332 -0
- package/src/social/public-read.ts +53 -0
- package/src/social/publish/bluesky.ts +168 -0
- package/src/social/publish/discord.ts +76 -0
- package/src/social/publish/index.ts +24 -0
- package/src/social/publish/secret.ts +13 -0
- package/src/social/push.ts +31 -0
- package/src/social/richtext.ts +37 -0
- package/src/social/routes.ts +180 -0
- package/src/social/stats.ts +134 -0
- package/src/social/text.ts +267 -0
- package/src/social/tracker-route.ts +39 -0
- package/src/social/tracker.ts +255 -0
- package/src/social/unit.ts +53 -0
- package/src/social/writes.ts +130 -0
- package/src/start-repo.ts +14 -9
- package/src/stores/collection.ts +27 -0
- package/src/stores/desk.ts +336 -0
- package/src/stores/model.ts +187 -0
- package/src/stores/routes.ts +126 -0
- package/src/stores/unit.ts +57 -0
- package/src/suite/calibrate.ts +127 -0
- package/src/suite/core-counters.ts +91 -0
- package/src/suite/judge-suites.ts +94 -34
- package/src/suite/suite-cli.ts +44 -10
- package/src/suite/suite-cpu-preload.ts +34 -9
- package/src/trailer/adapter.ts +4 -0
- package/src/trailer/build.ts +42 -11
- package/src/trailer/ffmpeg.ts +11 -0
- package/src/trailer/footage.ts +30 -13
- package/src/trailer/grammar.ts +18 -2
- package/src/trailer/graph.ts +28 -6
- package/src/trailer/passage.ts +17 -5
- package/src/trailer/plan.ts +74 -3
- package/src/trailer/stitch.ts +67 -26
- package/src/trailer/url.ts +1 -1
- package/src/trailer/versions.ts +24 -0
- package/src/ts/gate-typecheck.ts +32 -0
- package/src/ts/typecheck-cli.ts +9 -11
- package/src/tui/tui-app.tsx +0 -1
- package/src/veille/asks.ts +155 -0
- package/src/veille/auto.ts +31 -0
- package/src/veille/batch.ts +27 -0
- package/src/veille/chrome.ts +127 -0
- package/src/veille/collection.ts +28 -0
- package/src/veille/desk.ts +242 -0
- package/src/veille/identity.ts +80 -0
- package/src/veille/import.ts +68 -0
- package/src/veille/log.ts +183 -0
- package/src/veille/platforms.ts +118 -0
- package/src/veille/reads.ts +66 -0
- package/src/veille/replied.ts +110 -0
- package/src/veille/routes.ts +185 -0
- package/src/veille/service.ts +81 -0
- package/src/veille/watch.ts +116 -0
- package/src/veille/worth.ts +46 -0
- package/src/wiki/scan-md.ts +18 -0
- package/src/collections/posts.ts +0 -142
- package/src/contracts/manifests/social.contract.ts +0 -20
- package/src/scaffold/files/agent-coding.md +0 -35
- package/src/ts/typecheck-cache.ts +0 -11
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
// the facts of a repo's draft media (wiki build/shipkit/social.md, Media): one local json, `.shipkit/social-media.json`,
|
|
2
|
+
// each file of the drafts folder by `<slug>/<name>` with its sha, size, type and the mtime it was hashed at. The media
|
|
3
|
+
// store's `social` kind writes it; a file the disk let go keeps its key here, so the desk still finds it
|
|
4
|
+
import { readFileSync } from 'node:fs'
|
|
5
|
+
import { join, relative, resolve } from 'node:path'
|
|
6
|
+
import { LAYOUT } from '../core/layout.ts'
|
|
7
|
+
import { atomicWrite } from '../core/locked-write.ts'
|
|
8
|
+
import { mediaKey } from '../media-store/content.ts'
|
|
9
|
+
import type { MediaFact } from '../media-store/fact.ts'
|
|
10
|
+
import { isUp } from '../media-store/remote.ts'
|
|
11
|
+
|
|
12
|
+
export const SOCIAL_KIND = 'social'
|
|
13
|
+
|
|
14
|
+
export type FileFact = MediaFact & { mtime: number }
|
|
15
|
+
|
|
16
|
+
export interface Facts {
|
|
17
|
+
v: 1
|
|
18
|
+
files: Record<string, FileFact>
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export const factsFile = (root: string): string => join(root, LAYOUT.socialMedia)
|
|
22
|
+
export const draftsOf = (root: string): string => join(root, LAYOUT.social, 'drafts')
|
|
23
|
+
/** the repo of a drafts folder: `<root>/.shipkit/social/drafts` */
|
|
24
|
+
export const rootOfDrafts = (store: string): string => resolve(store, '..', '..', '..')
|
|
25
|
+
|
|
26
|
+
export function readFacts(root: string): Facts {
|
|
27
|
+
try {
|
|
28
|
+
const f = JSON.parse(readFileSync(factsFile(root), 'utf8')) as Facts
|
|
29
|
+
if (f.v === 1 && f.files != null && typeof f.files === 'object') return f
|
|
30
|
+
} catch {}
|
|
31
|
+
return { v: 1, files: {} }
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export const writeFacts = (root: string, f: Facts): void => atomicWrite(factsFile(root), `${JSON.stringify(f, null, 1)}\n`)
|
|
35
|
+
|
|
36
|
+
/** the key the Space keeps a draft file under */
|
|
37
|
+
export const socialKey = (f: Pick<MediaFact, 'sha'>, name: string): string => mediaKey(SOCIAL_KIND, f, name)
|
|
38
|
+
|
|
39
|
+
/** the fact of the file at `path` inside the drafts folder, null outside it or when none was ever taken */
|
|
40
|
+
export function factAt(root: string, path: string, facts = readFacts(root)): { id: string; fact: FileFact } | null {
|
|
41
|
+
const id = relative(draftsOf(root), path)
|
|
42
|
+
if (id === '' || id.startsWith('..')) return null
|
|
43
|
+
const fact = facts.files[id]
|
|
44
|
+
return fact ? { id, fact } : null
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** a media a draft names that the disk let go and the Space holds: its place in the draft's folder, else null */
|
|
48
|
+
export function purgedAt(root: string, dir: string, name: string): string | null {
|
|
49
|
+
if (name.startsWith('~/') || name.startsWith('/')) return null
|
|
50
|
+
const path = join(dir, name)
|
|
51
|
+
const at = factAt(root, path)
|
|
52
|
+
return at && isUp(socialKey(at.fact, path)) ? path : null
|
|
53
|
+
}
|
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
// the social drafts as a kind of the media store (wiki build/shipkit/social.md, Media): every media file of every
|
|
2
|
+
// draft folder, and every file a draft names inside the drafts folder, by its facts (media-facts.ts). Every one goes up.
|
|
3
|
+
// A file git tracks, or would commit, never leaves the disk: a purge would read as a deletion in git. Only a file git
|
|
4
|
+
// ignores (a rendered trailer, a public repo's drafts) may go, and the desk then serves it from the Space
|
|
5
|
+
import { readdirSync, readFileSync, statSync, type Dirent } from 'node:fs'
|
|
6
|
+
import { extname, join, relative } from 'node:path'
|
|
7
|
+
import { gitBin } from '../core/git-bin.ts'
|
|
8
|
+
import { shaOf, shaOfKey, typeOf } from '../media-store/content.ts'
|
|
9
|
+
import { mediaDb } from '../media-store/db.ts'
|
|
10
|
+
import type { MediaKind, MediaRow } from '../media-store/kind.ts'
|
|
11
|
+
import { fetchBack, signedUrl } from '../media-store/remote.ts'
|
|
12
|
+
import { mediaNames, parseDraft, resolveMedia } from './drafts.ts'
|
|
13
|
+
import { draftsOf, factAt, readFacts, rootOfDrafts, SOCIAL_KIND, socialKey, writeFacts, type FileFact } from './media-facts.ts'
|
|
14
|
+
|
|
15
|
+
/** a file of a draft folder that is media, whether or not its draft names it */
|
|
16
|
+
const MEDIA_EXT = new Set(['.png', '.jpg', '.jpeg', '.webp', '.gif', '.avif', '.svg', '.mp4', '.webm', '.mov', '.m4v', '.wav', '.mp3', '.flac', '.ogg', '.opus', '.m4a', '.aac'])
|
|
17
|
+
|
|
18
|
+
const entries = (dir: string): Dirent[] => {
|
|
19
|
+
try {
|
|
20
|
+
return readdirSync(dir, { withFileTypes: true })
|
|
21
|
+
} catch {
|
|
22
|
+
return []
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** every media file under a draft folder, as `<name>` relative to it; dot files and the draft's text never */
|
|
27
|
+
function mediaFiles(dir: string, at = ''): string[] {
|
|
28
|
+
return entries(join(dir, at)).flatMap((e) => {
|
|
29
|
+
if (e.name.startsWith('.')) return []
|
|
30
|
+
const rel = at === '' ? e.name : `${at}/${e.name}`
|
|
31
|
+
if (e.isDirectory()) return mediaFiles(dir, rel)
|
|
32
|
+
return e.isFile() && MEDIA_EXT.has(extname(e.name).toLowerCase()) ? [rel] : []
|
|
33
|
+
})
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const slugs = (store: string): string[] => entries(store).filter((e) => e.isDirectory() && !e.name.startsWith('.')).map((e) => e.name).sort()
|
|
37
|
+
|
|
38
|
+
/** every `<slug>/<name>` the drafts folder holds or names, the paths on the disk */
|
|
39
|
+
function named(store: string): Set<string> {
|
|
40
|
+
const root = rootOfDrafts(store)
|
|
41
|
+
const out = new Set<string>()
|
|
42
|
+
for (const slug of slugs(store)) {
|
|
43
|
+
const dir = join(store, slug)
|
|
44
|
+
for (const f of mediaFiles(dir)) out.add(`${slug}/${f}`)
|
|
45
|
+
let raw: string
|
|
46
|
+
try {
|
|
47
|
+
raw = readFileSync(join(dir, 'draft.md'), 'utf8')
|
|
48
|
+
} catch {
|
|
49
|
+
continue
|
|
50
|
+
}
|
|
51
|
+
for (const n of mediaNames(parseDraft(raw, ''))) {
|
|
52
|
+
const p = resolveMedia(n, dir, root)
|
|
53
|
+
const id = p == null ? '' : relative(store, p)
|
|
54
|
+
if (id !== '' && !id.startsWith('..') && id.includes('/')) out.add(id)
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
return out
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
const split = (id: string): { item: string; name: string } => {
|
|
61
|
+
const at = id.indexOf('/')
|
|
62
|
+
return { item: id.slice(0, at), name: id.slice(at + 1) }
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** the files under the store git ignores and does not track: the only ones a purge may take. null: git did not answer */
|
|
66
|
+
function ignoredOf(store: string): Set<string> | null {
|
|
67
|
+
const r = Bun.spawnSync([gitBin(), 'ls-files', '-z', '-o', '-i', '--exclude-standard', '--', '.'], { cwd: store, stdout: 'pipe', stderr: 'ignore' })
|
|
68
|
+
if (r.exitCode !== 0) return null
|
|
69
|
+
return new Set(r.stdout.toString().split('\0').filter((l) => l !== ''))
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** the index's own rows of the store: the keys of files the record lost (a record deleted, a machine restored) */
|
|
73
|
+
function indexed(store: string): Map<string, { key: string; size: number }> {
|
|
74
|
+
const p = `${SOCIAL_KIND}/`
|
|
75
|
+
const rows = (mediaDb()?.query('select item, name, key, size from files where store = ? and substr(key, 1, ?) = ?').all(store, p.length, p) ?? []) as { item: string; name: string; key: string; size: number }[]
|
|
76
|
+
return new Map(rows.map((r) => [`${r.item}/${r.name}`, { key: r.key, size: r.size }]))
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export const socialMedia: MediaKind = {
|
|
80
|
+
kind: SOCIAL_KIND,
|
|
81
|
+
storeOf: (root) => draftsOf(root),
|
|
82
|
+
stamp(store) {
|
|
83
|
+
const parts: string[] = []
|
|
84
|
+
for (const slug of slugs(store)) {
|
|
85
|
+
const dir = join(store, slug)
|
|
86
|
+
for (const f of ['draft.md', ...mediaFiles(dir)]) {
|
|
87
|
+
const s = statSync(join(dir, f), { throwIfNoEntry: false })
|
|
88
|
+
parts.push(`${slug}/${f}:${s?.size ?? '-'}:${s?.mtimeMs ?? '-'}`)
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
return Bun.hash(parts.join('\n')).toString(36)
|
|
92
|
+
},
|
|
93
|
+
async scan(store, o) {
|
|
94
|
+
const root = rootOfDrafts(store)
|
|
95
|
+
const facts = readFacts(root)
|
|
96
|
+
const ids = named(store)
|
|
97
|
+
let budget = o.fill
|
|
98
|
+
let filled = 0
|
|
99
|
+
let left = 0
|
|
100
|
+
let moved = false
|
|
101
|
+
const rows: MediaRow[] = []
|
|
102
|
+
for (const id of ids) {
|
|
103
|
+
const path = join(store, id)
|
|
104
|
+
const s = statSync(path, { throwIfNoEntry: false })
|
|
105
|
+
const held = facts.files[id]
|
|
106
|
+
if (!s?.isFile()) continue
|
|
107
|
+
if (held && held.size === s.size && held.mtime === s.mtimeMs) {
|
|
108
|
+
rows.push({ ...split(id), fact: held, local: true })
|
|
109
|
+
continue
|
|
110
|
+
}
|
|
111
|
+
if (budget <= 0) {
|
|
112
|
+
left++
|
|
113
|
+
// its key kept while it waits for a hash, never uploaded nor purged on a fact the disk may have moved past
|
|
114
|
+
if (held) rows.push({ ...split(id), fact: held, local: false })
|
|
115
|
+
continue
|
|
116
|
+
}
|
|
117
|
+
budget--
|
|
118
|
+
filled++
|
|
119
|
+
const fact: FileFact = { sha: await shaOf(path), size: s.size, type: typeOf(path), mtime: s.mtimeMs }
|
|
120
|
+
facts.files[id] = fact
|
|
121
|
+
moved = true
|
|
122
|
+
rows.push({ ...split(id), fact, local: true })
|
|
123
|
+
}
|
|
124
|
+
// a file off the disk keeps its row while its draft folder stands: purged, or deleted by hand, its key stays
|
|
125
|
+
const off = (id: string): boolean => !statSync(join(store, id), { throwIfNoEntry: false })?.isFile()
|
|
126
|
+
const alive = new Set(slugs(store))
|
|
127
|
+
for (const [id, fact] of Object.entries(facts.files)) {
|
|
128
|
+
if (ids.has(id)) continue
|
|
129
|
+
if (!off(id)) {
|
|
130
|
+
// on the disk and no media of a draft any more: the disk holds it, the record needs it no more
|
|
131
|
+
delete facts.files[id]
|
|
132
|
+
moved = true
|
|
133
|
+
continue
|
|
134
|
+
}
|
|
135
|
+
// its draft folder gone (deleted, renamed): no row, the record keeps its key for a restore
|
|
136
|
+
if (alive.has(split(id).item)) rows.push({ ...split(id), fact, local: false })
|
|
137
|
+
}
|
|
138
|
+
// the record lost a file the index still keys: taken back into it
|
|
139
|
+
for (const [id, r] of indexed(store)) {
|
|
140
|
+
const sha = shaOfKey(r.key)
|
|
141
|
+
if (facts.files[id] || !sha || !off(id) || !alive.has(split(id).item)) continue
|
|
142
|
+
const fact: FileFact = { sha, size: r.size, type: typeOf(id), mtime: 0 }
|
|
143
|
+
facts.files[id] = fact
|
|
144
|
+
moved = true
|
|
145
|
+
rows.push({ ...split(id), fact, local: false })
|
|
146
|
+
}
|
|
147
|
+
if (moved) writeFacts(root, facts)
|
|
148
|
+
return { rows, filled, left }
|
|
149
|
+
},
|
|
150
|
+
async holds(store) {
|
|
151
|
+
const ignored = ignoredOf(store)
|
|
152
|
+
return (row) => {
|
|
153
|
+
const id = `${row.item}/${row.name}`
|
|
154
|
+
const mtime = Math.max(statSync(join(store, id), { throwIfNoEntry: false })?.mtimeMs ?? 0, statSync(join(store, row.item, 'draft.md'), { throwIfNoEntry: false })?.mtimeMs ?? 0)
|
|
155
|
+
return { pinned: ignored == null || !ignored.has(id), used: mtime || undefined }
|
|
156
|
+
}
|
|
157
|
+
},
|
|
158
|
+
async beforePurge(store, _space, row) {
|
|
159
|
+
const path = join(store, row.item, row.name)
|
|
160
|
+
// asked again at the last moment: a file git tracks, or would commit, never leaves the disk
|
|
161
|
+
const ig = Bun.spawnSync([gitBin(), 'check-ignore', '-q', '--', join(row.item, row.name)], { cwd: store, stdout: 'ignore', stderr: 'ignore' })
|
|
162
|
+
if (ig.exitCode !== 0) throw new Error(`${path} is not ignored by git: kept`)
|
|
163
|
+
if ((await shaOf(path)) !== shaOfKey(row.key)) throw new Error(`${path} does not hash to its key ${row.key}: kept`)
|
|
164
|
+
},
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/** a signed URL for a draft file the disk let go, null when it is on the disk or the Space lacks it */
|
|
168
|
+
export async function socialMediaUrl(root: string, path: string): Promise<string | null> {
|
|
169
|
+
const at = factAt(root, path)
|
|
170
|
+
return at ? signedUrl(socialKey(at.fact, path), at.fact.size) : null
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/** the file at `path`, fetched back from the Space when the disk let it go; the path itself when the record knows none */
|
|
174
|
+
export async function socialMediaBack(root: string, path: string): Promise<string> {
|
|
175
|
+
const at = factAt(root, path)
|
|
176
|
+
if (!at) return path
|
|
177
|
+
const { item, name } = split(at.id)
|
|
178
|
+
const back = await fetchBack(path, socialKey(at.fact, path), at.fact, { store: draftsOf(root), item, name })
|
|
179
|
+
if (back == null) return path
|
|
180
|
+
// the bytes are checked by their sha: the record takes the new mtime, so no scan hashes them again
|
|
181
|
+
const facts = readFacts(root)
|
|
182
|
+
const f = facts.files[at.id]
|
|
183
|
+
const mtime = statSync(back, { throwIfNoEntry: false })?.mtimeMs
|
|
184
|
+
if (f && mtime != null && f.sha === at.fact.sha) {
|
|
185
|
+
facts.files[at.id] = { ...f, mtime }
|
|
186
|
+
writeFacts(root, facts)
|
|
187
|
+
}
|
|
188
|
+
return back
|
|
189
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
// the size a committed draft media may take (wiki build/shipkit/social.md, contract v3): GitHub refuses a file over
|
|
2
|
+
// 100 MB, so the desk's background push would fail with the commit already made. The desk's load reads the disk,
|
|
3
|
+
// SK036 the index at commit
|
|
4
|
+
import { statSync } from 'node:fs'
|
|
5
|
+
import { relative } from 'node:path'
|
|
6
|
+
import { gitBin } from '../core/git-bin.ts'
|
|
7
|
+
|
|
8
|
+
export const MEDIA_MAX_BYTES = 95_000_000
|
|
9
|
+
|
|
10
|
+
export const mbOf = (n: number): string => `${Math.round(n / 1e6)} MB`
|
|
11
|
+
|
|
12
|
+
/** the files of `paths` over the limit that git would commit: inside the repo and not ignored */
|
|
13
|
+
export function heavyMedia(root: string, paths: readonly string[]): { path: string; size: number }[] {
|
|
14
|
+
const big = [...new Set(paths)].flatMap((p) => {
|
|
15
|
+
const size = statSync(p, { throwIfNoEntry: false })?.size ?? 0
|
|
16
|
+
return size > MEDIA_MAX_BYTES && !relative(root, p).startsWith('..') ? [{ path: p, size }] : []
|
|
17
|
+
})
|
|
18
|
+
if (big.length === 0) return []
|
|
19
|
+
// exit 0 names the ignored paths, 1 none; any other answer (no repo) commits nothing
|
|
20
|
+
const r = Bun.spawnSync([gitBin(), 'check-ignore', '--', ...big.map((b) => b.path)], { cwd: root, stdout: 'pipe', stderr: 'ignore' })
|
|
21
|
+
if (r.exitCode > 1) return []
|
|
22
|
+
const ignored = new Set(r.stdout.toString().split('\n').filter((l) => l !== ''))
|
|
23
|
+
return big.filter((b) => !ignored.has(b.path))
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** the index entries under `dir` over the limit: what the next commit would carry, read in two git calls */
|
|
27
|
+
export function heavyStaged(root: string, dir: string): { path: string; size: number }[] {
|
|
28
|
+
const ls = Bun.spawnSync([gitBin(), 'ls-files', '-s', '-z', '--', dir], { cwd: root, stdout: 'pipe', stderr: 'ignore' })
|
|
29
|
+
if (ls.exitCode !== 0) return []
|
|
30
|
+
const rows = ls.stdout.toString().split('\0').flatMap((l) => {
|
|
31
|
+
const m = /^\d+ ([0-9a-f]+) \d\t(.+)$/.exec(l)
|
|
32
|
+
return m ? [{ sha: m[1]!, path: m[2]! }] : []
|
|
33
|
+
})
|
|
34
|
+
if (rows.length === 0) return []
|
|
35
|
+
const cat = Bun.spawnSync([gitBin(), 'cat-file', '--batch-check=%(objectsize)'], { cwd: root, stdin: new TextEncoder().encode(rows.map((r) => r.sha).join('\n') + '\n'), stdout: 'pipe', stderr: 'ignore' })
|
|
36
|
+
if (cat.exitCode !== 0) return []
|
|
37
|
+
const sizes = cat.stdout.toString().split('\n')
|
|
38
|
+
return rows.flatMap((r, i) => {
|
|
39
|
+
const size = Number(sizes[i])
|
|
40
|
+
return size > MEDIA_MAX_BYTES ? [{ path: r.path, size }] : []
|
|
41
|
+
})
|
|
42
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
// the media a repo's drafts could lend a reply (wiki build/shipkit/veille.md, The page): each message of each draft with
|
|
2
|
+
// a media file, read by the desk's own loader (load.ts) with the repo's speaker, synchronously for the Veille's routes
|
|
3
|
+
import { readFileSync } from 'node:fs'
|
|
4
|
+
import { join } from 'node:path'
|
|
5
|
+
import { LAYOUT } from '../core/layout.ts'
|
|
6
|
+
import { houseRootOf } from '../home/home-start.ts'
|
|
7
|
+
import { readIdentities } from '../identities/identities.ts'
|
|
8
|
+
import type { DraftMedia } from '../veille/desk.ts'
|
|
9
|
+
import { speaksAsOfText } from './house.ts'
|
|
10
|
+
import { loadDesk } from './load.ts'
|
|
11
|
+
|
|
12
|
+
export const DRAFTS_DIR = join(LAYOUT.social, 'drafts')
|
|
13
|
+
|
|
14
|
+
/** every draft's messages with a media file, one a heading, the newest draft first */
|
|
15
|
+
export function draftMedia(root: string): DraftMedia[] {
|
|
16
|
+
const house = houseRootOf(root)
|
|
17
|
+
const dir = process.env['SHIPKIT_IDENTITIES_DIR'] ?? (house == null ? null : join(house, LAYOUT.identities))
|
|
18
|
+
let config = ''
|
|
19
|
+
try {
|
|
20
|
+
config = readFileSync(join(root, 'repo.config.ts'), 'utf8')
|
|
21
|
+
} catch {
|
|
22
|
+
config = ''
|
|
23
|
+
}
|
|
24
|
+
const desk = loadDesk(root, readIdentities(dir), { speaksAs: speaksAsOfText(config) })
|
|
25
|
+
return desk.drafts.flatMap((d) => {
|
|
26
|
+
const seen = new Set<string>()
|
|
27
|
+
const posts = d.posts.flatMap((p) => (p.media == null || seen.has(p.from) ? [] : (seen.add(p.from), [{ text: p.body, title: d.title, media: { path: p.media.path } }])))
|
|
28
|
+
return posts.length > 0 ? [{ slug: d.slug, posts }] : []
|
|
29
|
+
})
|
|
30
|
+
}
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
// the v2 channel files become drafts, once (wiki build/shipkit/social.md, `sk social`): every
|
|
2
|
+
// `.shipkit/social/<channel>/<slug>[.<variant>].md` of one slug is ONE draft folder, a heading a channel. The chosen
|
|
3
|
+
// variant (picked, else posted, else the first) is the heading's text; every other variant is kept word for word under
|
|
4
|
+
// `## Angle`, with each file's angle and feedback, so nothing he wrote is lost. A v2 file goes once its draft is written
|
|
5
|
+
import { existsSync, mkdirSync, readdirSync, readFileSync, rmSync, rmdirSync, statSync, writeFileSync } from 'node:fs'
|
|
6
|
+
import { join, relative } from 'node:path'
|
|
7
|
+
import { gitBin } from '../core/git-bin.ts'
|
|
8
|
+
import { LAYOUT } from '../core/layout.ts'
|
|
9
|
+
import { parseFrontmatter, section } from './md.ts'
|
|
10
|
+
|
|
11
|
+
type V2 = { channel: string; file: string; abs: string; slug: string; variant: string | null; meta: Record<string, string>; angle: string; post: string; feedback: string }
|
|
12
|
+
|
|
13
|
+
const NAME = /^([a-z0-9][a-z0-9-]*?)(?:\.([a-z]))?\.md$/
|
|
14
|
+
|
|
15
|
+
/** the `## Post` text runs to `## Feedback`: a post may hold `##` lines of its own */
|
|
16
|
+
function postOf(body: string): string {
|
|
17
|
+
const start = body.search(/^## Post[ \t]*$/m)
|
|
18
|
+
if (start === -1) return ''
|
|
19
|
+
const rest = body.slice(start).replace(/^.*\n?/, '')
|
|
20
|
+
const end = rest.search(/^## Feedback[ \t]*$/m)
|
|
21
|
+
return (end === -1 ? rest : rest.slice(0, end)).trim()
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function readV2(root: string): { files: V2[]; skipped: string[] } {
|
|
25
|
+
const at = join(root, LAYOUT.social)
|
|
26
|
+
const files: V2[] = []
|
|
27
|
+
const skipped: string[] = []
|
|
28
|
+
if (!existsSync(at)) return { files, skipped }
|
|
29
|
+
for (const channel of readdirSync(at).sort()) {
|
|
30
|
+
const dir = join(at, channel)
|
|
31
|
+
if (channel === 'drafts' || channel.startsWith('.') || !statSync(dir).isDirectory()) continue
|
|
32
|
+
for (const f of readdirSync(dir).sort()) {
|
|
33
|
+
const abs = join(dir, f)
|
|
34
|
+
const m = NAME.exec(f)
|
|
35
|
+
if (m == null) {
|
|
36
|
+
if (f !== '.DS_Store') skipped.push(relative(root, abs))
|
|
37
|
+
continue
|
|
38
|
+
}
|
|
39
|
+
const { meta, body } = parseFrontmatter(readFileSync(abs, 'utf8'))
|
|
40
|
+
files.push({ channel, file: f, abs, slug: m[1]!, variant: m[2] ?? null, meta, angle: section(body, 'Angle') ?? '', post: postOf(body), feedback: section(body, 'Feedback') ?? '' })
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
return { files, skipped }
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** the moment a v2 file was first committed, else its mtime: `YYYYMMDD-HHMMSS`, Paris */
|
|
47
|
+
function momentOf(root: string, abs: string): string {
|
|
48
|
+
const r = Bun.spawnSync([gitBin(), 'log', '--diff-filter=A', '--format=%at', '--', abs], { cwd: root, stdout: 'pipe', stderr: 'pipe' })
|
|
49
|
+
const t = Number(r.stdout.toString().trim().split('\n').at(-1)) * 1000 || statSync(abs).mtimeMs
|
|
50
|
+
const parts = Object.fromEntries(new Intl.DateTimeFormat('en-GB', { timeZone: 'Europe/Paris', year: 'numeric', month: '2-digit', day: '2-digit', hour: '2-digit', minute: '2-digit', second: '2-digit', hourCycle: 'h23' }).formatToParts(t).map((p) => [p.type, p.value]))
|
|
51
|
+
return `${parts.year}${parts.month}${parts.day}-${parts.hour}${parts.minute}${parts.second}`
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const rank = (v: V2): number => (v.meta.status === 'picked' ? 0 : v.meta.status === 'posted' ? 1 : v.meta.status === 'draft' ? 2 : 3)
|
|
55
|
+
|
|
56
|
+
/** the draft.md of one slug's v2 files */
|
|
57
|
+
export function draftOfV2(slug: string, files: readonly V2[]): string {
|
|
58
|
+
const channels = [...new Set(files.map((f) => f.channel))]
|
|
59
|
+
const chosen = new Map(channels.map((c) => [c, [...files.filter((f) => f.channel === c)].sort((a, b) => rank(a) - rank(b) || (a.variant ?? '').localeCompare(b.variant ?? ''))[0]!]))
|
|
60
|
+
const all = [...chosen.values()]
|
|
61
|
+
const status = all.some((v) => v.meta.status === 'picked' || v.meta.status === 'posted') ? 'picked' : all.every((v) => v.meta.status === 'rejected') ? 'rejected' : 'draft'
|
|
62
|
+
const first = all[0]!
|
|
63
|
+
const meta = [`title: ${slug.replace(/-/g, ' ')}`, `kind: ${first.meta.kind ?? ''}`, `status: ${status}`, ...(first.meta.tone ? [`tone: ${first.meta.tone}`] : []), ...(first.meta.rating ? [`rating: ${first.meta.rating}`] : [])]
|
|
64
|
+
const angle: string[] = []
|
|
65
|
+
for (const f of files) {
|
|
66
|
+
const tag = `${f.channel}/${f.file}`
|
|
67
|
+
const kept = chosen.get(f.channel) === f
|
|
68
|
+
const parts = [
|
|
69
|
+
...(f.angle ? [f.angle] : []),
|
|
70
|
+
...(!kept && f.post ? [`The other variant ${tag} (${f.meta.status ?? 'no status'}), word for word:`, f.post.split('\n').map((l) => `> ${l}`).join('\n')] : []),
|
|
71
|
+
...(f.feedback ? [`His feedback on ${tag}:`, '', f.feedback] : []),
|
|
72
|
+
]
|
|
73
|
+
if (parts.length) angle.push(`**${tag}${kept ? ', the text below' : ''}.** ${parts.join('\n\n')}`)
|
|
74
|
+
}
|
|
75
|
+
const headings = channels.map((c) => `## ${c}\n\n${chosen.get(c)!.post || '(empty in v2)'}`)
|
|
76
|
+
const posted = channels.flatMap((c) => {
|
|
77
|
+
const url = files.find((f) => f.channel === c && f.meta.status === 'posted' && /^https?:\/\//.test(f.meta.posted ?? ''))?.meta.posted
|
|
78
|
+
return url ? [`- ${c}: ${url}`] : []
|
|
79
|
+
})
|
|
80
|
+
return [
|
|
81
|
+
`---\n${meta.join('\n')}\n---`,
|
|
82
|
+
`## Angle\n\n${angle.join('\n\n') || 'Moved from the v2 channel files.'}`,
|
|
83
|
+
...headings,
|
|
84
|
+
...(posted.length ? [`## Posted\n\n${posted.join('\n')}`] : []),
|
|
85
|
+
].join('\n\n') + '\n'
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** every v2 slug of the repo made a draft; `dry` writes nothing. The lines say what moved */
|
|
89
|
+
export function migrateV2(root: string, o: { dry?: boolean } = {}): { lines: string[]; written: string[]; removed: string[] } {
|
|
90
|
+
const { files, skipped } = readV2(root)
|
|
91
|
+
const lines: string[] = []
|
|
92
|
+
const written: string[] = []
|
|
93
|
+
const removed: string[] = []
|
|
94
|
+
if (files.length === 0) return { lines: [`ok: no v2 channel file in ${LAYOUT.social}/`, ...skipped.map((s) => `WARN: ${s}: not a <slug>[.<variant>].md, left as it is`)], written, removed }
|
|
95
|
+
const slugs = [...new Set(files.map((f) => f.slug))].sort()
|
|
96
|
+
for (const slug of slugs) {
|
|
97
|
+
const mine = files.filter((f) => f.slug === slug)
|
|
98
|
+
const moment = mine.map((f) => momentOf(root, f.abs)).sort()[0]!
|
|
99
|
+
const dir = join(root, LAYOUT.social, 'drafts', `${moment}-${slug}`)
|
|
100
|
+
const draft = join(dir, 'draft.md')
|
|
101
|
+
lines.push(`${o.dry ? 'would write' : 'wrote'} ${relative(root, draft)} from ${mine.map((f) => `${f.channel}/${f.file}`).join(', ')}`)
|
|
102
|
+
if (o.dry) continue
|
|
103
|
+
if (existsSync(draft)) throw new Error(`${relative(root, draft)} exists: move it aside, then run again`)
|
|
104
|
+
mkdirSync(dir, { recursive: true })
|
|
105
|
+
writeFileSync(draft, draftOfV2(slug, mine))
|
|
106
|
+
written.push(draft)
|
|
107
|
+
for (const f of mine) {
|
|
108
|
+
rmSync(f.abs)
|
|
109
|
+
removed.push(f.abs)
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
if (!o.dry)
|
|
113
|
+
for (const ch of new Set(files.map((f) => join(root, LAYOUT.social, f.channel)))) {
|
|
114
|
+
const left = readdirSync(ch).filter((f) => f !== '.DS_Store')
|
|
115
|
+
if (left.length === 0) {
|
|
116
|
+
rmSync(join(ch, '.DS_Store'), { force: true })
|
|
117
|
+
rmdirSync(ch)
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
lines.push(...skipped.map((s) => `WARN: ${s}: not a <slug>[.<variant>].md, left as it is`))
|
|
121
|
+
lines.push(o.dry ? `${slugs.length} drafts from ${files.length} v2 files: run without --dry to write them` : `ok: ${slugs.length} drafts from ${files.length} v2 files; the v2 files are gone`)
|
|
122
|
+
return { lines, written, removed }
|
|
123
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
// a repo's desk as every surface opens it: its config's speaksAs, the house's identities, its social unit, then the
|
|
2
|
+
// folder read (load.ts). The one way in for the routes and `sk social`
|
|
3
|
+
import { loadRepoConfig } from '../repo-config.ts'
|
|
4
|
+
import { loadIdentities, type Identities } from '../identities/identities.ts'
|
|
5
|
+
import { loadDesk, type Desk } from './load.ts'
|
|
6
|
+
import { loadSocialUnit, type SocialUnit } from './unit.ts'
|
|
7
|
+
|
|
8
|
+
export type Opened = { desk: Desk; unit: SocialUnit | null; ids: Identities }
|
|
9
|
+
|
|
10
|
+
export async function openDesk(root: string, o: { identitiesDir?: string | null } = {}): Promise<Opened> {
|
|
11
|
+
const [config, ids, unit] = await Promise.all([
|
|
12
|
+
loadRepoConfig(root, { fresh: true }).catch(() => null),
|
|
13
|
+
loadIdentities({ ...(o.identitiesDir !== undefined ? { dir: o.identitiesDir } : {}), cwd: root }),
|
|
14
|
+
loadSocialUnit(root),
|
|
15
|
+
])
|
|
16
|
+
return { desk: loadDesk(root, ids, { speaksAs: config?.speaksAs, unit }), unit, ids }
|
|
17
|
+
}
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
import type { Account } from '../identities/identities.ts'
|
|
2
|
+
|
|
3
|
+
// what counts as a post of OUR accounts (wiki build/shipkit/social.md, The post tracker): one rule a shape, its pattern built
|
|
4
|
+
// from the account's handle in account.md, never written here. The extension tests the same patterns (it gets them
|
|
5
|
+
// from the desk), the desk matches again and writes the canonical url
|
|
6
|
+
|
|
7
|
+
/** a pattern as the extension takes it: `sure` names our account; else (no author in the url) it counts only for a
|
|
8
|
+
* platform opened moments ago */
|
|
9
|
+
export type TrackRule = { platform: string; pattern: string; sure: boolean }
|
|
10
|
+
|
|
11
|
+
export type PostMatch = { platform: string; url: string; sure: boolean }
|
|
12
|
+
|
|
13
|
+
type Rule = TrackRule & { canon: (m: RegExpExecArray) => string }
|
|
14
|
+
|
|
15
|
+
const esc = (s: string): string => s.replace(/[.*+?^${}()|[\]\\/]/g, '\\$&')
|
|
16
|
+
/** the end of a post's own path: a sub page (`/photo/1`), a query or nothing */
|
|
17
|
+
const END = '(?:[/?#]|$)'
|
|
18
|
+
|
|
19
|
+
/** the bare handle: no `@`, no `u/` */
|
|
20
|
+
const bare = (handle: string): string => handle.trim().replace(/^u\//, '').replace(/^@/, '')
|
|
21
|
+
|
|
22
|
+
function rulesOf(a: Pick<Account, 'id' | 'handle'>, guild: string): Rule[] {
|
|
23
|
+
const h = bare(a.handle)
|
|
24
|
+
if (h === '' && a.id !== 'discord') return []
|
|
25
|
+
const r = (pattern: string, sure: boolean, canon: Rule['canon']): Rule => ({ platform: a.id, pattern, sure, canon })
|
|
26
|
+
const g = (m: RegExpExecArray, i: number): string => m[i] ?? ''
|
|
27
|
+
switch (a.id) {
|
|
28
|
+
case 'x':
|
|
29
|
+
return [r(`^https?://(?:www\\.|mobile\\.)?(?:x|twitter)\\.com/${esc(h)}/status/(\\d+)${END}`, true, (m) => `https://x.com/${h}/status/${g(m, 1)}`)]
|
|
30
|
+
case 'threads':
|
|
31
|
+
return [r(`^https?://(?:www\\.)?threads\\.(?:net|com)/@${esc(h)}/post/([A-Za-z0-9_-]+)${END}`, true, (m) => `https://www.threads.com/@${h}/post/${g(m, 1)}`)]
|
|
32
|
+
case 'bluesky':
|
|
33
|
+
return [r(`^https?://bsky\\.app/profile/${esc(h)}/post/([a-z0-9]+)${END}`, true, (m) => `https://bsky.app/profile/${h}/post/${g(m, 1)}`)]
|
|
34
|
+
case 'mastodon': {
|
|
35
|
+
const [user = '', host = ''] = h.split('@')
|
|
36
|
+
if (user === '' || host === '') return []
|
|
37
|
+
return [r(`^https?://${esc(host)}/(?:deck/)?@${esc(user)}/(\\d+)${END}`, true, (m) => `https://${host}/@${user}/${g(m, 1)}`)]
|
|
38
|
+
}
|
|
39
|
+
case 'tiktok':
|
|
40
|
+
return [r(`^https?://(?:www\\.|m\\.)?tiktok\\.com/@${esc(h)}/video/(\\d+)${END}`, true, (m) => `https://www.tiktok.com/@${h}/video/${g(m, 1)}`)]
|
|
41
|
+
case 'instagram': {
|
|
42
|
+
const canon = (m: RegExpExecArray): string => `https://www.instagram.com/${g(m, 1).toLowerCase().startsWith('p') ? 'p' : 'reel'}/${g(m, 2)}/`
|
|
43
|
+
return [
|
|
44
|
+
r(`^https?://(?:www\\.)?instagram\\.com/${esc(h)}/(p|reel)/([A-Za-z0-9_-]+)${END}`, true, canon),
|
|
45
|
+
r(`^https?://(?:www\\.)?instagram\\.com/(p|reels?)/([A-Za-z0-9_-]+)${END}`, false, canon),
|
|
46
|
+
]
|
|
47
|
+
}
|
|
48
|
+
case 'youtube':
|
|
49
|
+
// the studio edits only our own videos; a short's page names no channel
|
|
50
|
+
return [
|
|
51
|
+
r(`^https?://studio\\.youtube\\.com/video/([A-Za-z0-9_-]{11})${END}`, true, (m) => `https://youtu.be/${g(m, 1)}`),
|
|
52
|
+
r(`^https?://(?:www\\.|m\\.)?youtube\\.com/shorts/([A-Za-z0-9_-]{11})${END}`, false, (m) => `https://youtu.be/${g(m, 1)}`),
|
|
53
|
+
]
|
|
54
|
+
case 'discord':
|
|
55
|
+
if (guild === '') return []
|
|
56
|
+
return [r(`^https?://(?:www\\.|canary\\.|ptb\\.)?discord\\.com/channels/${guild}/(\\d+)/(\\d+)${END}`, true, (m) => `https://discord.com/channels/${guild}/${g(m, 1)}/${g(m, 2)}`)]
|
|
57
|
+
case 'reddit':
|
|
58
|
+
return [
|
|
59
|
+
r(`^https?://(?:www\\.|old\\.|new\\.)?reddit\\.com/r/([A-Za-z0-9_]+)/comments/([a-z0-9]+)(?:/([^/?#]+))?${END}`, false, (m) =>
|
|
60
|
+
`https://www.reddit.com/r/${g(m, 1)}/comments/${g(m, 2)}/${m[3] ? `${m[3]}/` : ''}`,
|
|
61
|
+
),
|
|
62
|
+
]
|
|
63
|
+
default:
|
|
64
|
+
return []
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
const rulesFor = (accounts: readonly Pick<Account, 'id' | 'handle'>[], guild: string): Rule[] => accounts.flatMap((a) => rulesOf(a, guild))
|
|
69
|
+
|
|
70
|
+
/** the patterns the extension tests, from the accounts as loadSocial reads them */
|
|
71
|
+
export const trackRules = (accounts: readonly Pick<Account, 'id' | 'handle'>[], guild: string): TrackRule[] =>
|
|
72
|
+
rulesFor(accounts, guild).map(({ platform, pattern, sure }) => ({ platform, pattern, sure }))
|
|
73
|
+
|
|
74
|
+
/** a post of one of our accounts, its url as the drafts write it; null for anything else (another account, a profile,
|
|
75
|
+
* a search) */
|
|
76
|
+
export function matchPost(url: string, accounts: readonly Pick<Account, 'id' | 'handle'>[], guild: string): PostMatch | null {
|
|
77
|
+
for (const rule of rulesFor(accounts, guild)) {
|
|
78
|
+
const m = new RegExp(rule.pattern, 'i').exec(url.trim())
|
|
79
|
+
if (m) return { platform: rule.platform, url: rule.canon(m), sure: rule.sure }
|
|
80
|
+
}
|
|
81
|
+
return null
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/** a platform with no free read API: after a post, the extension reads OUR profile page. `compose` is its compose
|
|
85
|
+
* page, `created` the request of its own that makes a post (seen, never touched), `host` every url of its pages,
|
|
86
|
+
* `profile` the account's own page (account.md's `profile`) */
|
|
87
|
+
export type ProfileCheck = { platform: string; compose: string; created: string; host: string; profile: string }
|
|
88
|
+
|
|
89
|
+
const SHAPES: Readonly<Record<string, Omit<ProfileCheck, 'platform' | 'profile'>>> = {
|
|
90
|
+
x: {
|
|
91
|
+
compose: '^https?://(?:www\\.|mobile\\.)?(?:x|twitter)\\.com/(?:intent/(?:post|tweet)|compose/(?:post|tweet))(?:[/?#]|$)',
|
|
92
|
+
created: '^https://(?:x|twitter)\\.com/i/api/graphql/[^/]+/Create(?:Note)?Tweet(?:[/?#]|$)',
|
|
93
|
+
host: '^https://(?:www\\.)?(?:x|twitter)\\.com/',
|
|
94
|
+
},
|
|
95
|
+
threads: {
|
|
96
|
+
compose: '^https?://(?:www\\.)?threads\\.(?:net|com)/intent/post(?:[/?#]|$)',
|
|
97
|
+
created: '^https://www\\.threads\\.(?:net|com)/api/v1/media/configure',
|
|
98
|
+
host: '^https://(?:www\\.)?threads\\.(?:net|com)/',
|
|
99
|
+
},
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
export const profileChecks = (accounts: readonly Pick<Account, 'id' | 'profile'>[]): ProfileCheck[] =>
|
|
103
|
+
accounts.flatMap((a) => {
|
|
104
|
+
const shapes = SHAPES[a.id]
|
|
105
|
+
return shapes && a.profile !== '' ? [{ platform: a.id, ...shapes, profile: a.profile }] : []
|
|
106
|
+
})
|
|
107
|
+
|
|
108
|
+
const SHORTCODE = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_'
|
|
109
|
+
/** a Bluesky record key (a TID) in base 32 sortable: microseconds since 1970, shifted by 10 */
|
|
110
|
+
const TID = '234567abcdefghijklmnopqrstuvwxyz'
|
|
111
|
+
/** X's ids are snowflakes: milliseconds since this, shifted by 22 */
|
|
112
|
+
const X_EPOCH_MS = 1288834974657
|
|
113
|
+
/** Discord's ids are snowflakes: milliseconds since this, shifted by 22 */
|
|
114
|
+
const DISCORD_EPOCH_MS = 1420070400000
|
|
115
|
+
/** a Threads or Instagram code is a media id in base 64: milliseconds since this, shifted by 23 */
|
|
116
|
+
const META_EPOCH_MS = 1314220021721
|
|
117
|
+
|
|
118
|
+
const digits = (code: string, alphabet: string, base: bigint): bigint => {
|
|
119
|
+
let n = 0n
|
|
120
|
+
for (const c of code) n = n * base + BigInt(alphabet.indexOf(c))
|
|
121
|
+
return n
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/** when a post was made, read from its id: X, Discord, Mastodon and TikTok snowflakes, a Bluesky TID, a Threads or
|
|
125
|
+
* Instagram code; null for a platform whose id holds no time (YouTube, Reddit) and for any other url */
|
|
126
|
+
export function postTime(platform: string, url: string): number | null {
|
|
127
|
+
const id = (re: RegExp): string | null => re.exec(url)?.[1] ?? null
|
|
128
|
+
switch (platform) {
|
|
129
|
+
case 'x': {
|
|
130
|
+
const n = id(/\/status\/(\d+)/)
|
|
131
|
+
return n ? Number(BigInt(n) >> 22n) + X_EPOCH_MS : null
|
|
132
|
+
}
|
|
133
|
+
case 'discord': {
|
|
134
|
+
const n = id(/\/channels\/\d+\/\d+\/(\d+)/)
|
|
135
|
+
return n ? Number(BigInt(n) >> 22n) + DISCORD_EPOCH_MS : null
|
|
136
|
+
}
|
|
137
|
+
case 'mastodon': {
|
|
138
|
+
const n = id(/\/@[\w.]+\/(\d+)/)
|
|
139
|
+
return n ? Number(BigInt(n) >> 16n) : null
|
|
140
|
+
}
|
|
141
|
+
case 'tiktok': {
|
|
142
|
+
const n = id(/\/video\/(\d+)/)
|
|
143
|
+
return n ? Number(BigInt(n) >> 32n) * 1000 : null
|
|
144
|
+
}
|
|
145
|
+
case 'bluesky': {
|
|
146
|
+
const n = id(/\/post\/([2-7a-z]{13})(?:[/?#]|$)/)
|
|
147
|
+
return n ? Math.floor(Number(digits(n, TID, 32n) >> 10n) / 1000) : null
|
|
148
|
+
}
|
|
149
|
+
case 'threads':
|
|
150
|
+
case 'instagram': {
|
|
151
|
+
const code = id(/\/(?:post|p|reel)\/([A-Za-z0-9_-]+)/)
|
|
152
|
+
if (!code || code.length < 11) return null
|
|
153
|
+
return Number(digits(code.slice(0, 11), SHORTCODE, 64n) >> 23n) + META_EPOCH_MS
|
|
154
|
+
}
|
|
155
|
+
default:
|
|
156
|
+
return null
|
|
157
|
+
}
|
|
158
|
+
}
|