rvlib-shipkit 2.11.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 +56 -12
- 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 +96 -5
- package/src/cli/cmd_Review.ts +8 -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 +9 -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 +61 -6
- package/src/home/inbox.ts +75 -4
- 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 +110 -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 +9 -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/content.ts +11 -1
- package/src/media-store/daemon.ts +56 -70
- package/src/media-store/hand.ts +58 -0
- package/src/media-store/put.ts +122 -0
- package/src/media-store/space.ts +73 -11
- 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/media-kind.ts +3 -3
- package/src/review/media-read.ts +3 -3
- package/src/review/model.ts +24 -5
- package/src/review/notes.ts +77 -0
- package/src/review/project.ts +6 -38
- package/src/review/routes.ts +8 -13
- package/src/review/store.ts +46 -4
- package/src/review/trailer-media.ts +24 -3
- package/src/review/trailer-shots.ts +106 -0
- 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 +12 -14
- 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 +33 -11
- 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/test-view.tsx +9 -2
- package/src/serve/hub-app/tests-svc.ts +16 -1
- 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/resources-wire.ts +48 -0
- package/src/serve/run-lanes.ts +7 -4
- package/src/serve/same-origin.ts +1 -1
- package/src/serve/serve-hub.ts +27 -18
- 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 +21 -6
- package/src/trailer/plan.ts +74 -3
- package/src/trailer/shelf.ts +16 -0
- 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,24 @@
|
|
|
1
|
+
// the accounts shipkit posts to itself (`automation: live`): one publisher a platform, the desk's « Publish »
|
|
2
|
+
import { accountOf, draftPost, type Desk } from '../load.ts'
|
|
3
|
+
import { setPosted } from '../writes.ts'
|
|
4
|
+
import { postBluesky } from './bluesky.ts'
|
|
5
|
+
import { postDiscord } from './discord.ts'
|
|
6
|
+
import type { Account } from '../../identities/identities.ts'
|
|
7
|
+
import type { SocialPost } from '../load.ts'
|
|
8
|
+
|
|
9
|
+
const PUBLISHERS: Readonly<Record<string, (a: Account, post: SocialPost) => Promise<string>>> = {
|
|
10
|
+
discord: postDiscord,
|
|
11
|
+
bluesky: postBluesky,
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/** posts one message of a draft and records its link; the files written */
|
|
15
|
+
export async function publish(desk: Desk, slug: string, key: string): Promise<{ url: string; files: string[] }> {
|
|
16
|
+
const post = draftPost(desk, slug, key)
|
|
17
|
+
const a = accountOf(desk, post.identity, post.platform)
|
|
18
|
+
const run = PUBLISHERS[post.platform]
|
|
19
|
+
if (run == null) throw new Error(`${post.platform}: nothing posts there by itself`)
|
|
20
|
+
if (a.automation !== 'live') throw new Error(`${a.file}: its automation is not live`)
|
|
21
|
+
if (post.posted !== '' && post.platform !== 'discord') throw new Error(`${slug} is already posted on ${a.name}: ${post.posted}`)
|
|
22
|
+
const url = await run(a, post)
|
|
23
|
+
return { url, files: url === post.posted ? [] : setPosted(desk, slug, key, url) }
|
|
24
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
// an account's token, read through the house config's `credsReadCommand`: a missing one fails loud with where it is named
|
|
2
|
+
import { readSecret } from '../../smoke/run-smoke.ts'
|
|
3
|
+
|
|
4
|
+
export async function secret(name: string, where: string): Promise<string> {
|
|
5
|
+
if (name === '') throw new Error(`no token named: ${where} names no secret`)
|
|
6
|
+
const { houseRootOf } = await import('../../home/home-start.ts')
|
|
7
|
+
const { loadRepoConfig } = await import('../../repo-config.ts')
|
|
8
|
+
const house = houseRootOf(process.cwd())
|
|
9
|
+
if (!house) throw new Error(`no house repo seen from ${process.cwd()} to read the token ${name} (${where})`)
|
|
10
|
+
const c = await loadRepoConfig(house)
|
|
11
|
+
if (!c.credsReadCommand) throw new Error(`the house config (${house}) declares no credsReadCommand to read ${name} (${where})`)
|
|
12
|
+
return readSecret(c.credsReadCommand, name)
|
|
13
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
// the desk's background push (wiki build/shipkit/social.md, The desks in the hub): never a wait in the write path, and
|
|
2
|
+
// never silent. A failed push leaves its outcome in the repo's logs folder, which the desk's load turns into an error
|
|
3
|
+
// line; the next push that lands removes it
|
|
4
|
+
import { readFileSync } from 'node:fs'
|
|
5
|
+
import { join } from 'node:path'
|
|
6
|
+
import { gitBin } from '../core/git-bin.ts'
|
|
7
|
+
import { LAYOUT } from '../core/layout.ts'
|
|
8
|
+
|
|
9
|
+
/** the last failed push of a repo: `<iso time> exit <code>`, then git's output */
|
|
10
|
+
export const pushLog = (root: string): string => join(root, LAYOUT.logs, 'social-push-failed.txt')
|
|
11
|
+
|
|
12
|
+
const SCRIPT = `out=$("$1" push -q 2>&1); c=$?
|
|
13
|
+
if [ $c -eq 0 ]; then rm -f "$2"; else mkdir -p "$(dirname "$2")"; printf '%s exit %s\\n%s\\n' "$(date -u +%Y-%m-%dT%H:%M:%SZ)" "$c" "$out" > "$2.tmp" && mv "$2.tmp" "$2"; fi`
|
|
14
|
+
|
|
15
|
+
/** `git push` of the repo at `root`, detached: the caller never waits, the outcome lands in `pushLog` */
|
|
16
|
+
export function pushInBackground(root: string): void {
|
|
17
|
+
Bun.spawn(['sh', '-c', `nohup sh -c '${SCRIPT.replace(/'/g, `'\\''`)}' push "$0" "$1" >/dev/null 2>&1 &`, gitBin(), pushLog(root)], { cwd: root, stdout: 'ignore', stderr: 'ignore' })
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/** the desk's error line while the last push failed, null once one landed */
|
|
21
|
+
export function pushFailure(root: string): string | null {
|
|
22
|
+
let text: string
|
|
23
|
+
try {
|
|
24
|
+
text = readFileSync(pushLog(root), 'utf8')
|
|
25
|
+
} catch {
|
|
26
|
+
return null
|
|
27
|
+
}
|
|
28
|
+
const [head = '', ...rest] = text.split('\n')
|
|
29
|
+
const why = rest.map((l) => l.trim()).find((l) => l !== '') ?? 'no output'
|
|
30
|
+
return `git push of the desk's commits failed (${head.trim()}): ${why}. They stay on this Mac until a push lands: git push in ${root}`
|
|
31
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
// a text as Bluesky shows and counts it (wiki build/shipkit/social.md, Publishing): each link shortened the way its app does,
|
|
2
|
+
// its facet still the whole address, then the #tags; the 300 a post may take counts this text, never the draft's
|
|
3
|
+
export type Facet = { index: { byteStart: number; byteEnd: number }; features: ({ $type: string } & Record<string, string>)[] }
|
|
4
|
+
|
|
5
|
+
/** a shown link at most this long, its end cut to « … » */
|
|
6
|
+
const SHOWN = 30
|
|
7
|
+
|
|
8
|
+
const UTF8 = new TextEncoder()
|
|
9
|
+
const bytes = (s: string): number => UTF8.encode(s).length
|
|
10
|
+
|
|
11
|
+
/** the text Bluesky posts and its facets, offsets in utf8 bytes; a link's trailing punctuation stays text */
|
|
12
|
+
export function richText(draft: string): { text: string; facets: Facet[] } {
|
|
13
|
+
let text = ''
|
|
14
|
+
let last = 0
|
|
15
|
+
const links: { start: number; len: number; uri: string }[] = []
|
|
16
|
+
for (const m of draft.matchAll(/https?:\/\/[^\s]+/g)) {
|
|
17
|
+
const uri = m[0].replace(/[.,;:!?)"'»]+$/, '')
|
|
18
|
+
const plain = uri.replace(/^https?:\/\/(www\.)?/, '')
|
|
19
|
+
const shown = plain.length > SHOWN ? `${plain.slice(0, SHOWN - 1)}…` : plain
|
|
20
|
+
text += draft.slice(last, m.index)
|
|
21
|
+
links.push({ start: text.length, len: shown.length, uri })
|
|
22
|
+
text += shown
|
|
23
|
+
last = m.index + uri.length
|
|
24
|
+
}
|
|
25
|
+
text += draft.slice(last)
|
|
26
|
+
const facets: Facet[] = []
|
|
27
|
+
const at = (start: number, len: number, feature: Facet['features'][number]): void => {
|
|
28
|
+
const byteStart = bytes(text.slice(0, start))
|
|
29
|
+
facets.push({ index: { byteStart, byteEnd: byteStart + bytes(text.slice(start, start + len)) }, features: [feature] })
|
|
30
|
+
}
|
|
31
|
+
for (const l of links) at(l.start, l.len, { $type: 'app.bsky.richtext.facet#link', uri: l.uri })
|
|
32
|
+
for (const m of text.matchAll(/(^|\s)#([\p{L}\p{N}_]*[\p{L}_][\p{L}\p{N}_]*)/gu)) {
|
|
33
|
+
const tag = m[2]!
|
|
34
|
+
at(m.index + m[1]!.length, tag.length + 1, { $type: 'app.bsky.richtext.facet#tag', tag })
|
|
35
|
+
}
|
|
36
|
+
return { text, facets: facets.sort((a, b) => a.index.byteStart - b.index.byteStart) }
|
|
37
|
+
}
|
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
// the hub's social routes (wiki build/shipkit/social.md, Routes): the desk of the repo as json, the live numbers apart,
|
|
2
|
+
// a draft's media and an identity's pictures with byte ranges, his writes, « Publish », « Open all », the Finder and
|
|
3
|
+
// his notes. Paths are relative to the repo hub's base; null: not a path of these. Every answer reads the disk again,
|
|
4
|
+
// the desk through its index (desk-index.ts)
|
|
5
|
+
import { existsSync } from 'node:fs'
|
|
6
|
+
import { homedir } from 'node:os'
|
|
7
|
+
import { isAbsolute, join, relative, resolve } from 'node:path'
|
|
8
|
+
import { bad, NO_STORE, ranged } from '../review/http.ts'
|
|
9
|
+
import { noteOn, notesBy } from '../review/notes.ts'
|
|
10
|
+
import { readAllCached, storeDir } from '../review/store.ts'
|
|
11
|
+
import { DRAFT_NAME, draftsDir, type Desk } from './load.ts'
|
|
12
|
+
import { deskAnswer } from './desk-index.ts'
|
|
13
|
+
import { socialMediaBack, socialMediaUrl } from './media-kind.ts'
|
|
14
|
+
import { openDesk } from './open.ts'
|
|
15
|
+
import { openAll } from '../veille/chrome.ts'
|
|
16
|
+
import { noteOpen, searchHistory, settleSeen, trackerView } from './tracker.ts'
|
|
17
|
+
import { publish } from './publish/index.ts'
|
|
18
|
+
import { socialStats } from './stats.ts'
|
|
19
|
+
import { setLive, setPosted, setStep, setText } from './writes.ts'
|
|
20
|
+
|
|
21
|
+
const KEY = /^[a-z0-9-]+(?:\/[a-z0-9-]+)?$/
|
|
22
|
+
const ID = /^[a-z0-9-]+$/
|
|
23
|
+
|
|
24
|
+
const str = (v: unknown, what: string, re?: RegExp): string => {
|
|
25
|
+
const s = typeof v === 'string' ? v : ''
|
|
26
|
+
if (s === '' || (re != null && !re.test(s))) throw new Error(`${what}: « ${String(v)} »?`)
|
|
27
|
+
return s
|
|
28
|
+
}
|
|
29
|
+
const urlOrNull = (v: unknown): string | null => (typeof v === 'string' && v.trim() !== '' ? v.trim() : null)
|
|
30
|
+
|
|
31
|
+
/** a media a draft names, wherever it lives: a file no draft names is refused */
|
|
32
|
+
function draftMedia(desk: Desk, slug: string, name: string): string {
|
|
33
|
+
const d = desk.drafts.find((x) => x.slug === slug)
|
|
34
|
+
const m = d?.media.find((x) => x.name === name || relative(d.dir, x.path) === name)
|
|
35
|
+
if (m != null) return m.path
|
|
36
|
+
// the article's own media folder, which its post.md names by `/media/<file>`
|
|
37
|
+
const inMedia = d == null ? null : resolve(d.dir, 'media', name)
|
|
38
|
+
if (d != null && inMedia != null && relative(join(d.dir, 'media'), inMedia).split('/')[0] !== '..' && existsSync(inMedia)) return inMedia
|
|
39
|
+
throw new Error(`${slug} names no media ${name}`)
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/** a picture under an identity's `files` folder, or one its `pictures` list from a folder elsewhere; never another */
|
|
43
|
+
function picture(desk: Desk, identity: string, rel: string): string {
|
|
44
|
+
const i = desk.identities.find((x) => x.id === identity)
|
|
45
|
+
if (i?.files == null) throw new Error(`${identity} has no files folder`)
|
|
46
|
+
if (rel.startsWith('~/') || isAbsolute(rel)) {
|
|
47
|
+
if (!i.pictures.some((p) => p.file === rel)) throw new Error(`not a picture of ${identity}: ${rel}`)
|
|
48
|
+
return rel.startsWith('~/') ? join(homedir(), rel.slice(2)) : rel
|
|
49
|
+
}
|
|
50
|
+
const abs = resolve(i.files, rel)
|
|
51
|
+
const r = relative(i.files, abs)
|
|
52
|
+
if (r === '' || r.startsWith('..') || isAbsolute(r)) throw new Error(`not a file of ${identity}: ${rel}`)
|
|
53
|
+
return abs
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export async function socialRoutes(root: string, req: Request, path: string): Promise<Response | null> {
|
|
57
|
+
if (!path.startsWith('/api/social/')) return null
|
|
58
|
+
const sub = path.slice('/api/social/'.length)
|
|
59
|
+
try {
|
|
60
|
+
if (req.method === 'GET') {
|
|
61
|
+
if (sub === 'desk.json') return deskAnswer(req, root)
|
|
62
|
+
if (sub === 'stats.json') {
|
|
63
|
+
const { desk } = await openDesk(root)
|
|
64
|
+
const out: Record<string, Record<string, string>> = {}
|
|
65
|
+
for (const i of desk.identities) out[i.id] = await socialStats(i.accounts)
|
|
66
|
+
return Response.json(out, { headers: NO_STORE })
|
|
67
|
+
}
|
|
68
|
+
if (sub === 'tracker.json') {
|
|
69
|
+
const { desk } = await openDesk(root)
|
|
70
|
+
const out: Record<string, ReturnType<typeof trackerView>> = {}
|
|
71
|
+
for (const i of desk.identities) out[i.id] = trackerView(i.id)
|
|
72
|
+
return Response.json(out, { headers: NO_STORE })
|
|
73
|
+
}
|
|
74
|
+
if (sub === 'notes') {
|
|
75
|
+
const slug = str(new URL(req.url).searchParams.get('slug'), 'draft', DRAFT_NAME)
|
|
76
|
+
const dir = storeDir(root)
|
|
77
|
+
return Response.json({ notes: notesBy(dir, readAllCached(dir).items)({ kind: 'draft', slug }) }, { headers: NO_STORE })
|
|
78
|
+
}
|
|
79
|
+
const media = /^media\/([^/]+)\/(.+)$/.exec(sub)
|
|
80
|
+
if (media) {
|
|
81
|
+
const { desk } = await openDesk(root)
|
|
82
|
+
const file = draftMedia(desk, str(media[1], 'draft', DRAFT_NAME), decodeURIComponent(media[2]!))
|
|
83
|
+
// a file the disk let go plays from the Space
|
|
84
|
+
const url = existsSync(file) ? null : await socialMediaUrl(root, file)
|
|
85
|
+
return url ? new Response(null, { status: 302, headers: { Location: url, 'Cache-Control': 'private, max-age=1800' } }) : ranged(req, file, NO_STORE)
|
|
86
|
+
}
|
|
87
|
+
const pic = /^picture\/([a-z0-9-]+)\/(.+)$/.exec(sub)
|
|
88
|
+
if (pic) {
|
|
89
|
+
const { desk } = await openDesk(root)
|
|
90
|
+
return ranged(req, picture(desk, pic[1]!, decodeURIComponent(pic[2]!)))
|
|
91
|
+
}
|
|
92
|
+
const prev = /^preview\/([^/]+)$/.exec(sub)
|
|
93
|
+
if (prev) {
|
|
94
|
+
const { desk, unit } = await openDesk(root)
|
|
95
|
+
const slug = str(prev[1], 'draft', DRAFT_NAME)
|
|
96
|
+
if (unit?.preview == null) return bad('this repo names no preview in its social unit', 404)
|
|
97
|
+
const html = await unit.preview(join(draftsDir(root), slug), `api/social/media/${slug}/`)
|
|
98
|
+
if (!desk.drafts.some((d) => d.slug === slug)) return bad(`no draft ${slug}`, 404)
|
|
99
|
+
return new Response(html, { headers: { 'Content-Type': 'text/html; charset=utf-8', ...NO_STORE } })
|
|
100
|
+
}
|
|
101
|
+
return bad(`no route GET ${path}`, 404)
|
|
102
|
+
}
|
|
103
|
+
if (req.method !== 'POST') return bad(`${req.method} ${path}?`, 405)
|
|
104
|
+
const b = (await req.json().catch(() => ({}))) as Record<string, unknown>
|
|
105
|
+
if (sub === 'text') {
|
|
106
|
+
const tags = typeof b.tags === 'string' ? b.tags : undefined
|
|
107
|
+
const title = typeof b.title === 'string' ? b.title : undefined
|
|
108
|
+
return Response.json({ files: setText(root, str(b.slug, 'draft', DRAFT_NAME), str(b.key, 'heading', KEY), String(b.text ?? ''), title, tags) })
|
|
109
|
+
}
|
|
110
|
+
if (sub === 'notes') {
|
|
111
|
+
const { unit } = await openDesk(root)
|
|
112
|
+
const slug = str(b.slug, 'draft', DRAFT_NAME)
|
|
113
|
+
const text = String(b.text ?? '')
|
|
114
|
+
return Response.json(await noteOn(storeDir(root), { subject: { kind: 'draft', slug }, origin: { by: 'owner', from: 'communications' }, topic: unit?.notes ?? 'social', name: String(b.title ?? '') || slug, text }))
|
|
115
|
+
}
|
|
116
|
+
if (sub === 'reveal') {
|
|
117
|
+
const { desk } = await openDesk(root)
|
|
118
|
+
const slug = str(b.slug, 'draft', DRAFT_NAME)
|
|
119
|
+
const file = await socialMediaBack(root, draftMedia(desk, slug, str(b.name, 'media')))
|
|
120
|
+
Bun.spawn(['open', '-R', file], { stdout: 'ignore', stderr: 'ignore' })
|
|
121
|
+
return Response.json({ ok: true })
|
|
122
|
+
}
|
|
123
|
+
const { desk } = await openDesk(root)
|
|
124
|
+
if (sub === 'posted') return Response.json({ files: setPosted(desk, str(b.slug, 'draft', DRAFT_NAME), str(b.key, 'message', KEY), urlOrNull(b.url)) })
|
|
125
|
+
if (sub === 'live') return Response.json({ files: setLive(desk, str(b.identity, 'identity', ID), str(b.account, 'account', ID), urlOrNull(b.url)) })
|
|
126
|
+
if (sub === 'step') {
|
|
127
|
+
const index = Number(b.index)
|
|
128
|
+
if (!Number.isInteger(index) || index < 0) throw new Error(`step ${String(b.index)}?`)
|
|
129
|
+
return Response.json({ files: setStep(desk, str(b.identity, 'identity', ID), str(b.account, 'account', ID), index, b.done === true) })
|
|
130
|
+
}
|
|
131
|
+
if (sub === 'opened') {
|
|
132
|
+
const slug = str(b.slug, 'draft', DRAFT_NAME)
|
|
133
|
+
const key = str(b.key, 'message', KEY)
|
|
134
|
+
const d = desk.drafts.find((x) => x.slug === slug)
|
|
135
|
+
if (d == null) throw new Error(`no draft ${slug}`)
|
|
136
|
+
noteOpen(root, d.identity, key.split('/')[0]!, slug, key)
|
|
137
|
+
return Response.json({ ok: true })
|
|
138
|
+
}
|
|
139
|
+
if (sub === 'seen-take') {
|
|
140
|
+
settleSeen(desk, str(b.url, 'url'), { slug: str(b.slug, 'draft', DRAFT_NAME), key: str(b.key, 'message', KEY) })
|
|
141
|
+
return Response.json({ ok: true })
|
|
142
|
+
}
|
|
143
|
+
if (sub === 'seen-drop') {
|
|
144
|
+
settleSeen(desk, str(b.url, 'url'), null)
|
|
145
|
+
return Response.json({ ok: true })
|
|
146
|
+
}
|
|
147
|
+
if (sub === 'history') return Response.json({ ok: true, counts: await searchHistory(desk, str(b.identity, 'identity', ID), str(b.platform, 'account', ID)) })
|
|
148
|
+
if (sub === 'publish') {
|
|
149
|
+
const slug = str(b.slug, 'draft', DRAFT_NAME)
|
|
150
|
+
const key = str(b.key, 'message', KEY)
|
|
151
|
+
// the bytes a platform takes: a media the disk let go comes back first
|
|
152
|
+
const p = desk.drafts.find((x) => x.slug === slug)?.posts.find((x) => x.key === key)
|
|
153
|
+
if (p?.media) await socialMediaBack(root, p.media.path)
|
|
154
|
+
return Response.json(await publish(desk, slug, key))
|
|
155
|
+
}
|
|
156
|
+
if (sub === 'open-all') {
|
|
157
|
+
const slug = str(b.slug, 'draft', DRAFT_NAME)
|
|
158
|
+
const from = str(b.from, 'heading', KEY)
|
|
159
|
+
const d = desk.drafts.find((x) => x.slug === slug)
|
|
160
|
+
const posts = d?.posts.filter((p) => p.from === from) ?? []
|
|
161
|
+
if (d == null || posts.length === 0) return bad(`no message ${from} in ${slug}`, 404)
|
|
162
|
+
const accounts = desk.identities.find((i) => i.id === d.identity)?.accounts ?? []
|
|
163
|
+
// a live account posts with « Publish », a posted one is done: neither needs a window
|
|
164
|
+
const open = posts.flatMap((p) => {
|
|
165
|
+
const a = accounts.find((x) => x.id === p.platform)
|
|
166
|
+
const url = p.href ?? a?.profile ?? ''
|
|
167
|
+
return p.posted === '' && a?.automation !== 'live' && url !== '' ? [{ p, url }] : []
|
|
168
|
+
})
|
|
169
|
+
const email = desk.identities.find((i) => i.id === d.identity)?.rows.find((r) => r.field === 'email')?.value ?? ''
|
|
170
|
+
const shown = posts[0]?.media?.path
|
|
171
|
+
await openAll(open.map((o) => o.url), shown == null ? null : await socialMediaBack(root, shown), email)
|
|
172
|
+
// the post tracker takes a post seen next on each of these platforms for this message
|
|
173
|
+
for (const { p } of open) noteOpen(root, d.identity, p.platform, p.slug, p.key)
|
|
174
|
+
return Response.json({ ok: true, opened: open.length })
|
|
175
|
+
}
|
|
176
|
+
return bad(`no route POST ${path}`, 404)
|
|
177
|
+
} catch (e) {
|
|
178
|
+
return bad(e)
|
|
179
|
+
}
|
|
180
|
+
}
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
// the live numbers an account shows for free (wiki build/shipkit/social.md, Routes): Bluesky's and Mastodon's public
|
|
2
|
+
// reads, a Discord invite's counts. Machine level, cached in the home folder 30 minutes a profile; a refusal backs off
|
|
3
|
+
import { mkdirSync, readFileSync, renameSync, writeFileSync } from 'node:fs'
|
|
4
|
+
import { join } from 'node:path'
|
|
5
|
+
import { homeDir } from '../home/home-paths.ts'
|
|
6
|
+
import type { Account } from '../identities/identities.ts'
|
|
7
|
+
|
|
8
|
+
const n = (count: number | undefined, one: string, many: string): string => `${count ?? 0} ${(count ?? 0) > 1 ? many : one}`
|
|
9
|
+
|
|
10
|
+
/** a platform said no: its status, and how long it asks us to wait (a 429's Retry-After), 0 when it names none */
|
|
11
|
+
export class Refused extends Error {
|
|
12
|
+
constructor(
|
|
13
|
+
readonly status: number,
|
|
14
|
+
readonly waitMs: number,
|
|
15
|
+
) {
|
|
16
|
+
super(`http ${status}`)
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/** a platform's answer as json; a refusal throws with its status and how long it asks us to wait */
|
|
21
|
+
async function ask<T>(url: string): Promise<T> {
|
|
22
|
+
const r = await fetch(url, { signal: AbortSignal.timeout(3000), headers: { 'User-Agent': 'shipkit-social-desk' } })
|
|
23
|
+
if (!r.ok) throw new Refused(r.status, Number(r.headers.get('retry-after')) * 1000 || 0)
|
|
24
|
+
return (await r.json()) as T
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** a platform's public numbers by an english name (`followers`, `posts`, `members`, `online`) */
|
|
28
|
+
export type Counts = Record<string, number>
|
|
29
|
+
type Got = { line: string; counts: Counts }
|
|
30
|
+
|
|
31
|
+
/** what a platform shows for free, by its public api: a few words a card puts beside its name, and the numbers */
|
|
32
|
+
const STATS: Readonly<Record<string, (live: string) => Promise<Got | null>>> = {
|
|
33
|
+
bluesky: async (live) => {
|
|
34
|
+
const actor = /\/profile\/([^/?#]+)/.exec(live)?.[1]
|
|
35
|
+
if (actor == null) return null
|
|
36
|
+
const d = await ask<{ followersCount?: number; postsCount?: number }>(`https://public.api.bsky.app/xrpc/app.bsky.actor.getProfile?actor=${actor}`)
|
|
37
|
+
return {
|
|
38
|
+
line: `${n(d.followersCount, 'follower', 'followers')} · ${n(d.postsCount, 'post', 'posts')}`,
|
|
39
|
+
counts: { followers: d.followersCount ?? 0, posts: d.postsCount ?? 0 },
|
|
40
|
+
}
|
|
41
|
+
},
|
|
42
|
+
discord: async (live) => {
|
|
43
|
+
const code = /discord\.gg\/([\w-]+)/.exec(live)?.[1]
|
|
44
|
+
if (code == null) return null
|
|
45
|
+
const d = await ask<{ approximate_member_count?: number; approximate_presence_count?: number }>(`https://discord.com/api/v10/invites/${code}?with_counts=true`)
|
|
46
|
+
return {
|
|
47
|
+
line: `${n(d.approximate_member_count, 'member', 'members')} · ${d.approximate_presence_count ?? 0} online`,
|
|
48
|
+
counts: { members: d.approximate_member_count ?? 0, online: d.approximate_presence_count ?? 0 },
|
|
49
|
+
}
|
|
50
|
+
},
|
|
51
|
+
mastodon: async (live) => {
|
|
52
|
+
const m = /^https:\/\/([^/]+)\/@([\w.]+)/.exec(live)
|
|
53
|
+
if (m == null) return null
|
|
54
|
+
const d = await ask<{ followers_count?: number; statuses_count?: number }>(`https://${m[1]}/api/v1/accounts/lookup?acct=${m[2]}`)
|
|
55
|
+
return {
|
|
56
|
+
line: `${n(d.followers_count, 'follower', 'followers')} · ${n(d.statuses_count, 'post', 'posts')}`,
|
|
57
|
+
counts: { followers: d.followers_count ?? 0, posts: d.statuses_count ?? 0 },
|
|
58
|
+
}
|
|
59
|
+
},
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** the accounts whose numbers a free public read gives */
|
|
63
|
+
export const STATS_PLATFORMS: readonly string[] = Object.keys(STATS)
|
|
64
|
+
|
|
65
|
+
/** one platform's numbers as last asked: its line (the last good one survives a failure), its numbers, for which
|
|
66
|
+
* profile, the moment they were read, and the moment it may be asked again */
|
|
67
|
+
export type StatsEntry = { line: string; counts?: Counts; at?: number; live: string; next: number }
|
|
68
|
+
|
|
69
|
+
/** a good answer holds this long: the counts move slowly, and an open of the desk never costs a request */
|
|
70
|
+
export const STATS_FRESH_MS = 30 * 60_000
|
|
71
|
+
/** a failure waits at least this long, or what the platform asks (a 429's Retry-After) when longer */
|
|
72
|
+
export const STATS_BACKOFF_MS = 15 * 60_000
|
|
73
|
+
|
|
74
|
+
const statsFile = (): string => join(homeDir(), 'social-numbers.json')
|
|
75
|
+
|
|
76
|
+
/** asked again only once its time has come, or when the profile it was asked for changed */
|
|
77
|
+
export const statsDue = (e: StatsEntry | undefined, live: string, now: number): boolean => e == null || e.live !== live || now >= e.next
|
|
78
|
+
|
|
79
|
+
/** what one ask leaves in the cache: the new line, or the old one kept and the next ask pushed back */
|
|
80
|
+
export function statsAfter(old: StatsEntry | undefined, live: string, now: number, got: { line: string; counts?: Counts } | { error: unknown }): StatsEntry {
|
|
81
|
+
if ('line' in got) return { line: got.line, ...(got.counts ? { counts: got.counts, at: now } : {}), live, next: now + STATS_FRESH_MS }
|
|
82
|
+
const wait = Math.max(STATS_BACKOFF_MS, got.error instanceof Refused ? got.error.waitMs : 0)
|
|
83
|
+
const why = got.error instanceof Refused ? got.error.message : (got.error as Error).name
|
|
84
|
+
const same = old?.live === live
|
|
85
|
+
const kept = same ? old.line : ''
|
|
86
|
+
return { line: kept || `numbers unavailable (${why})`, ...(same && old.counts ? { counts: old.counts, at: old.at } : {}), live, next: now + wait }
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
function readStats(): Record<string, StatsEntry> {
|
|
90
|
+
try {
|
|
91
|
+
return JSON.parse(readFileSync(statsFile(), 'utf8')) as Record<string, StatsEntry>
|
|
92
|
+
} catch {
|
|
93
|
+
return {}
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/** every online account's numbers as the cache holds them, asked again first where their time has come: a platform is
|
|
98
|
+
* asked at most once per STATS_FRESH_MS, and a refusal backs off */
|
|
99
|
+
export async function socialStatsEntries(accounts: readonly Pick<Account, 'id' | 'live'>[], now = Date.now()): Promise<Record<string, StatsEntry>> {
|
|
100
|
+
const cache = readStats()
|
|
101
|
+
let asked = false
|
|
102
|
+
await Promise.all(
|
|
103
|
+
accounts.map(async (a) => {
|
|
104
|
+
const get = STATS[a.id]
|
|
105
|
+
if (get == null || a.live === '' || !statsDue(cache[a.id], a.live, now)) return
|
|
106
|
+
asked = true
|
|
107
|
+
const old = cache[a.id]
|
|
108
|
+
try {
|
|
109
|
+
const got = await get(a.live)
|
|
110
|
+
if (got == null) delete cache[a.id]
|
|
111
|
+
else cache[a.id] = statsAfter(old, a.live, now, got)
|
|
112
|
+
} catch (error) {
|
|
113
|
+
cache[a.id] = statsAfter(old, a.live, now, { error })
|
|
114
|
+
}
|
|
115
|
+
}),
|
|
116
|
+
)
|
|
117
|
+
if (asked) {
|
|
118
|
+
mkdirSync(homeDir(), { recursive: true })
|
|
119
|
+
const tmp = `${statsFile()}.tmp-${process.pid}`
|
|
120
|
+
writeFileSync(tmp, JSON.stringify(cache, null, 2))
|
|
121
|
+
renameSync(tmp, statsFile())
|
|
122
|
+
}
|
|
123
|
+
const out: Record<string, StatsEntry> = {}
|
|
124
|
+
for (const a of accounts) {
|
|
125
|
+
const e = cache[a.id]
|
|
126
|
+
if (e && e.live === a.live) out[a.id] = e
|
|
127
|
+
}
|
|
128
|
+
return out
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/** the live numbers' line of every online account that shows them (the desk's cards) */
|
|
132
|
+
export async function socialStats(accounts: readonly Pick<Account, 'id' | 'live'>[], now = Date.now()): Promise<Record<string, string>> {
|
|
133
|
+
return Object.fromEntries(Object.entries(await socialStatsEntries(accounts, now)).map(([id, e]) => [id, e.line]))
|
|
134
|
+
}
|