rvlib-shipkit 2.27.0 → 2.28.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 +16 -0
- package/package.json +1 -1
- package/src/agent-time/hooks.ts +10 -3
- package/src/agent-time/live.ts +11 -5
- package/src/agent-time/registry.ts +289 -0
- package/src/agent-time/store.ts +2 -2
- package/src/annex/born.ts +2 -2
- package/src/annex/offload.ts +12 -11
- package/src/asks/ask-after.ts +6 -2
- package/src/asks/ask-bar-fold.ts +57 -0
- package/src/asks/ask-migrate.ts +280 -0
- package/src/asks/ask-steps-fold.ts +166 -0
- package/src/asks/ask-store.ts +586 -0
- package/src/assets/kits/identity-kit.ts +106 -38
- package/src/assets/kits/poster-thumbnail.ts +282 -0
- package/src/checks/SK003-claude-md-shape.check.ts +92 -25
- package/src/checks/SK004-gitignore-policy.check.ts +8 -4
- package/src/checks/SK013-one-way-in-policy.check.ts +10 -0
- package/src/checks/SK026-asks-autoclose.check.ts +9 -23
- package/src/checks/SK038-speaks-as.check.ts +3 -3
- package/src/checks/SK042-post-kinds.check.ts +26 -24
- package/src/checks/SK049-thread-answers-hook.check.ts +81 -0
- package/src/checks/SK050-retired-commands.check.ts +127 -0
- package/src/cli/cmd_Asks.ts +204 -53
- package/src/cli/cmd_Backup.ts +151 -8
- package/src/cli/cmd_Calls.ts +1 -0
- package/src/cli/cmd_Commands.ts +140 -34
- package/src/cli/cmd_Contracts.ts +1 -0
- package/src/cli/cmd_Keys.ts +1 -0
- package/src/cli/cmd_Lanes.ts +15 -4
- package/src/cli/cmd_Messages.ts +152 -1
- package/src/cli/cmd_Needs.ts +1 -0
- package/src/cli/cmd_Review.ts +931 -229
- package/src/cli/cmd_Secret.ts +255 -34
- package/src/cli/cmd_Social.ts +36 -33
- package/src/cli/cmd_SocialDrafts.ts +235 -0
- package/src/cli/cmd_Sound.ts +22 -1
- package/src/cli/cmd_Start.ts +3 -2
- package/src/cli/cmd_ThreadHolds.ts +34 -0
- package/src/cli/cmd_ThreadKinds.ts +146 -0
- package/src/cli/cmd_ThreadReflect.ts +83 -0
- package/src/cli/cmd_ThreadSyncNeeds.ts +62 -0
- package/src/cli/cmd_Vault.ts +1 -1
- package/src/cli/cmd_When.ts +1 -0
- package/src/cli/family-help.ts +2 -2
- package/src/cli/family-pages.ts +1 -1
- package/src/cli/kit/builtins.ts +1 -0
- package/src/cli/kit/verb.ts +6 -2
- package/src/cli/registry.ts +21 -2
- package/src/cli.ts +5 -2
- package/src/collections/actions.ts +25 -74
- package/src/collections/collection-types.ts +9 -5
- package/src/collections/md-body.ts +5 -3
- package/src/collections/pings.ts +13 -6
- package/src/collections/providers.ts +53 -61
- package/src/collections/reflections.ts +9 -4
- package/src/core/discover-checks.ts +18 -3
- package/src/core/git.ts +31 -0
- package/src/core/house-layout.ts +17 -0
- package/src/core/layout.ts +10 -5
- package/src/core/locked-write.ts +4 -1
- package/src/core/machine-db.ts +5 -3
- package/src/core/migrate-layout.ts +8 -10
- package/src/home/activity.ts +8 -15
- package/src/home/daemon-jobs.ts +13 -1
- package/src/home/home-paths.ts +6 -3
- package/src/home/home-routes.ts +8 -0
- package/src/home/inbox-shape.ts +233 -100
- package/src/home/inbox.ts +26 -71
- package/src/home/machine-fold.ts +10 -15
- package/src/home/machine-host.ts +19 -1
- package/src/home/other-macs.ts +2 -2
- package/src/home/serve-web.ts +6 -5
- package/src/home/space-page.ts +1 -1
- package/src/home/vault-page.ts +51 -17
- package/src/home/vault-words.ts +2 -2
- package/src/hooks/one-way-in-guard.ts +24 -2
- package/src/hq/files-send.ts +27 -28
- package/src/hq/hq-db.ts +4 -1
- package/src/hq/hq-local.ts +37 -4
- package/src/hq/hq-routes.ts +10 -2
- package/src/hq/hq-store.ts +6 -2
- package/src/hq/kinds/ask.ts +6 -6
- package/src/hq/outbox.ts +5 -3
- package/src/hq/replica.ts +1 -2
- package/src/hq/vault-route.ts +112 -0
- package/src/identities/collection.ts +2 -2
- package/src/identities/identities.ts +9 -8
- package/src/identities/machine-route.ts +8 -3
- package/src/ledger/db.ts +8 -1
- package/src/media-store/backup/daemon.ts +48 -8
- package/src/media-store/backup/keys.ts +1 -1
- package/src/media-store/backup/manifest.ts +20 -2
- package/src/media-store/backup/prune.ts +365 -0
- package/src/media-store/backup/retention.ts +122 -0
- package/src/media-store/backup/run.ts +14 -8
- package/src/media-store/backup/sources.ts +18 -17
- package/src/media-store/backup/thread-copy.ts +340 -0
- package/src/media-store/copy/config.ts +3 -4
- package/src/media-store/copy/store.ts +2 -2
- package/src/media-store/db.ts +2 -2
- package/src/media-store/forget.ts +118 -0
- package/src/media-store/purge.ts +3 -2
- package/src/media-store/space-listing.ts +20 -4
- package/src/media-store/space.ts +6 -5
- package/src/messages/guard.ts +49 -0
- package/src/messages/model.ts +1 -1
- package/src/messages/objects.ts +201 -0
- package/src/messages/routes.ts +2 -2
- package/src/messages/score.ts +1 -1
- package/src/messages/write.ts +285 -0
- package/src/pages/frame-client.ts +181 -4
- package/src/reflections/reflections-queue.ts +1 -1
- package/src/release/gate-checkout.ts +2 -1
- package/src/release/git-facts.ts +3 -2
- package/src/release/public-clean.ts +3 -2
- package/src/repo-config.ts +40 -18
- package/src/review/anim.ts +1 -1
- package/src/review/answer-kinds.ts +131 -0
- package/src/review/band.ts +53 -0
- package/src/review/branch-hold.ts +84 -0
- package/src/review/card-db.ts +16 -6
- package/src/review/card-rows.ts +4 -3
- package/src/review/cells.ts +297 -65
- package/src/review/close.ts +92 -0
- package/src/review/draft-card.ts +8 -16
- package/src/review/draft-model.ts +2 -2
- package/src/review/events.ts +171 -0
- package/src/review/format.ts +128 -14
- package/src/review/glyphs.ts +63 -0
- package/src/review/gone.ts +1 -1
- package/src/review/holds-sync.ts +62 -0
- package/src/review/holds.ts +85 -0
- package/src/review/house-kind-defs.ts +41 -0
- package/src/review/inbox-hook.ts +22 -9
- package/src/review/inbox-read.ts +201 -0
- package/src/review/instructions.ts +199 -0
- package/src/review/kind-apply.ts +61 -0
- package/src/review/kind-catalog.ts +96 -0
- package/src/review/kinds.ts +278 -0
- package/src/review/lane-ask.ts +109 -0
- package/src/review/model.ts +396 -86
- package/src/review/move.ts +3 -3
- package/src/review/need-address.ts +15 -0
- package/src/review/need-threads.ts +93 -0
- package/src/review/object-card.ts +48 -0
- package/src/review/object-read.ts +31 -0
- package/src/review/owner-verdict.ts +36 -0
- package/src/review/parties.ts +182 -0
- package/src/review/previews.ts +3 -1
- package/src/review/projections.ts +235 -0
- package/src/review/referring.ts +54 -0
- package/src/review/reflection-object.ts +195 -0
- package/src/review/routes.ts +137 -32
- package/src/review/secret-route.ts +80 -0
- package/src/review/snooze.ts +38 -0
- package/src/review/stage-card.ts +4 -4
- package/src/review/store.ts +366 -85
- package/src/review/supersede.ts +116 -0
- package/src/review/take-back.ts +52 -0
- package/src/review/thread-fold.ts +20 -7
- package/src/review/threads.ts +236 -39
- package/src/review/traces.ts +218 -0
- package/src/review/trailer-card.ts +2 -2
- package/src/review/unit.ts +71 -5
- package/src/review/verdict-items.ts +73 -0
- package/src/review/verdict-write.ts +15 -0
- package/src/review/wait.ts +85 -0
- package/src/review/watchers.ts +22 -9
- package/src/scaffold/files/claude-settings-local.json +2 -0
- package/src/secrets/archive-mark.ts +52 -0
- package/src/secrets/archive.ts +83 -0
- package/src/secrets/copy.ts +14 -14
- package/src/secrets/keychain-write.ts +48 -0
- package/src/secrets/keychain.ts +8 -5
- package/src/secrets/lapse-mark.ts +33 -0
- package/src/secrets/lapse.ts +60 -0
- package/src/secrets/list.ts +16 -22
- package/src/secrets/manager.ts +31 -16
- package/src/secrets/read.ts +161 -33
- package/src/secrets/tail.ts +31 -0
- package/src/serve/hub-app/asks-page.tsx +2 -1
- package/src/serve/hub-app/auto-text.tsx +14 -6
- package/src/serve/hub-app/benign-errors.ts +15 -1
- package/src/serve/hub-app/card-head.tsx +49 -0
- package/src/serve/hub-app/check-view.tsx +3 -2
- package/src/serve/hub-app/code-copy.ts +35 -0
- package/src/serve/hub-app/col-card.tsx +31 -40
- package/src/serve/hub-app/col-list-page.tsx +5 -2
- package/src/serve/hub-app/col-tables-view.tsx +7 -4
- package/src/serve/hub-app/col-tables.ts +5 -5
- package/src/serve/hub-app/cols-svc.ts +4 -79
- package/src/serve/hub-app/commands/commands-page.tsx +2 -1
- package/src/serve/hub-app/controls.css +83 -0
- package/src/serve/hub-app/frames-st.ts +17 -1
- package/src/serve/hub-app/git-view.tsx +3 -2
- package/src/serve/hub-app/home-st.ts +57 -6
- package/src/serve/hub-app/home-strip.css +1 -6
- package/src/serve/hub-app/home-strip.tsx +2 -1
- package/src/serve/hub-app/home.html +3 -0
- package/src/serve/hub-app/house-wiki-view.tsx +20 -0
- package/src/serve/hub-app/hub-app.tsx +48 -32
- package/src/serve/hub-app/hub-types.ts +2 -0
- package/src/serve/hub-app/hub.css +50 -61
- package/src/serve/hub-app/icons.tsx +14 -4
- package/src/serve/hub-app/identities-view.tsx +4 -3
- package/src/serve/hub-app/inbox-st.ts +203 -42
- package/src/serve/hub-app/inbox-view.tsx +189 -101
- package/src/serve/hub-app/inbox.css +47 -44
- package/src/serve/hub-app/index.html +3 -0
- package/src/serve/hub-app/instructions.css +30 -0
- package/src/serve/hub-app/keys.ts +5 -3
- package/src/serve/hub-app/machine-st.ts +2 -2
- package/src/serve/hub-app/machines-view.tsx +4 -11
- package/src/serve/hub-app/machines.css +0 -9
- package/src/serve/hub-app/markdown.css +51 -0
- package/src/serve/hub-app/md-copy.tsx +18 -40
- package/src/serve/hub-app/md.ts +25 -9
- package/src/serve/hub-app/media-view.tsx +2 -2
- package/src/serve/hub-app/messages/messages-page.tsx +2 -1
- package/src/serve/hub-app/messages/messages-st.ts +1 -1
- package/src/serve/hub-app/messages.css +0 -1
- package/src/serve/hub-app/objects.css +51 -0
- package/src/serve/hub-app/page-keys.ts +1 -1
- package/src/serve/hub-app/pages-frames.tsx +11 -1
- package/src/serve/hub-app/palette-st.ts +4 -4
- package/src/serve/hub-app/pane-boundary.tsx +72 -0
- package/src/serve/hub-app/pane-errors.ts +54 -0
- package/src/serve/hub-app/plan-view.tsx +3 -2
- package/src/serve/hub-app/planned-view.tsx +3 -3
- package/src/serve/hub-app/planned.css +0 -4
- package/src/serve/hub-app/processes-view.tsx +3 -2
- package/src/serve/hub-app/project-list.css +5 -13
- package/src/serve/hub-app/project-list.tsx +3 -6
- package/src/serve/hub-app/projects-svc.ts +6 -2
- package/src/serve/hub-app/readme.css +9 -15
- package/src/serve/hub-app/reflections-page.tsx +54 -1
- package/src/serve/hub-app/releases/releases-page.tsx +3 -2
- package/src/serve/hub-app/releases.css +0 -1
- package/src/serve/hub-app/review/code-colors.ts +30 -0
- package/src/serve/hub-app/review/diff-view.tsx +75 -0
- package/src/serve/hub-app/review/dock.tsx +104 -0
- package/src/serve/hub-app/review/draft-card.tsx +2 -2
- package/src/serve/hub-app/review/group-block.tsx +209 -0
- package/src/serve/hub-app/review/instructions-view.tsx +116 -0
- package/src/serve/hub-app/review/item-verdict.tsx +38 -0
- package/src/serve/hub-app/review/kind-states.tsx +193 -0
- package/src/serve/hub-app/review/kind-view.tsx +1 -37
- package/src/serve/hub-app/review/kit.tsx +57 -54
- package/src/serve/hub-app/review/lane-bar.tsx +64 -0
- package/src/serve/hub-app/review/object-card.tsx +62 -0
- package/src/serve/hub-app/review/object-panel.tsx +179 -0
- package/src/serve/hub-app/review/question-views.tsx +385 -0
- package/src/serve/hub-app/review/review-cells.tsx +86 -105
- package/src/serve/hub-app/review/review-list.tsx +104 -47
- package/src/serve/hub-app/review/review-page.tsx +5 -4
- package/src/serve/hub-app/review/review-st.ts +68 -28
- package/src/serve/hub-app/review/review-thread.tsx +171 -40
- package/src/serve/hub-app/review/review-views.tsx +35 -31
- package/src/serve/hub-app/review/thread-close.tsx +124 -0
- package/src/serve/hub-app/review/thread-control.tsx +4 -4
- package/src/serve/hub-app/review/thread-st.ts +222 -22
- package/src/serve/hub-app/review/trailer-card.tsx +1 -1
- package/src/serve/hub-app/review/trailer-timeline.tsx +2 -2
- package/src/serve/hub-app/review.css +216 -57
- package/src/serve/hub-app/section-head.tsx +51 -0
- package/src/serve/hub-app/social/calendar.tsx +5 -11
- package/src/serve/hub-app/social/comms-page.tsx +23 -10
- package/src/serve/hub-app/social/kit.tsx +8 -15
- package/src/serve/hub-app/social/next-release.tsx +3 -5
- package/src/serve/hub-app/social/post-card.tsx +3 -0
- package/src/serve/hub-app/social/post-page.tsx +80 -0
- package/src/serve/hub-app/social/problems.tsx +5 -9
- package/src/serve/hub-app/social/social-model.ts +13 -5
- package/src/serve/hub-app/social/social-page.tsx +1 -1
- package/src/serve/hub-app/social/social-st.ts +42 -5
- package/src/serve/hub-app/social.css +7 -17
- package/src/serve/hub-app/space.css +1 -1
- package/src/serve/hub-app/stores/stores-page.tsx +6 -2
- package/src/serve/hub-app/thread-row.tsx +101 -0
- package/src/serve/hub-app/time-ago.tsx +3 -2
- package/src/serve/hub-app/time-view.tsx +2 -1
- package/src/serve/hub-app/time.css +2 -5
- package/src/serve/hub-app/tree-row.tsx +1 -9
- package/src/serve/hub-app/vault-view.tsx +4 -4
- package/src/serve/hub-app/veille/veille-page.tsx +2 -11
- package/src/serve/hub-app/veille.css +0 -1
- package/src/serve/hub-app/waiting-svc.ts +4 -4
- package/src/serve/hub-app/when-view.tsx +6 -3
- package/src/serve/hub-app/wiki-st.ts +30 -4
- package/src/serve/hub-app/wikis-view.tsx +4 -3
- package/src/serve/hub-runtime.ts +20 -6
- package/src/serve/project-gutter-route.ts +16 -6
- package/src/serve/project-gutter.ts +6 -53
- package/src/serve/same-origin.ts +3 -0
- package/src/serve/secret-gate.ts +21 -0
- package/src/serve/serve-hub.ts +29 -20
- package/src/serve/tailnet-caller.ts +117 -0
- package/src/shipkit.ts +2 -2
- package/src/site/desk.ts +2 -2
- package/src/smoke/run-smoke.ts +3 -4
- package/src/social/brands.ts +4 -0
- package/src/social/card-drafts.ts +11 -19
- package/src/social/desk-index.ts +34 -13
- package/src/social/drafts.ts +4 -4
- package/src/social/import-drafts.ts +244 -0
- package/src/social/kind-pages.ts +3 -3
- package/src/social/kinds.ts +1 -1
- package/src/social/load.ts +89 -64
- package/src/social/media-kind.ts +68 -161
- package/src/social/media-of.ts +5 -4
- package/src/social/open.ts +3 -2
- package/src/social/post-object.ts +120 -0
- package/src/social/post-sources.ts +136 -0
- package/src/social/post-text.ts +103 -0
- package/src/social/posts.ts +378 -0
- package/src/social/presence.ts +7 -5
- package/src/social/publish/index.ts +17 -5
- package/src/social/publish/secret.ts +2 -2
- package/src/social/release-stage.ts +58 -57
- package/src/social/routes.ts +64 -24
- package/src/social/stage.ts +42 -57
- package/src/social/text.ts +4 -3
- package/src/social/thumbnail.ts +135 -43
- package/src/social/tracker.ts +1 -1
- package/src/social/writes.ts +12 -29
- package/src/stores/desk.ts +16 -11
- package/src/stores/unit.ts +2 -2
- package/src/trailer/sound/eleven.ts +33 -3
- package/src/urls/rows.ts +1 -1
- package/src/vault/vault-http.ts +11 -3
- package/src/vault/vault-import.ts +6 -1
- package/src/veille/identity.ts +2 -2
- package/src/wiki/graph-pages.ts +13 -1
- package/src/wiki/scan-md.ts +1 -1
- package/src/asks/asks-queue.ts +0 -424
- package/src/messages/suggest.ts +0 -96
- package/src/social/migrate.ts +0 -123
|
@@ -0,0 +1,378 @@
|
|
|
1
|
+
// the posts and their groups, written (wiki tools/shipkit/social.md, Posts and their groups): every write of a post or
|
|
2
|
+
// a group is an event of the thread store, the one truth; a text (a stage, a save, the import) is loaded by the desk's own
|
|
3
|
+
// builder, then written as one group event and one event a post that moved. An agent's new group is referred to by a
|
|
4
|
+
// thread in the same call (rule 5 of the hub map). The one constraint a reader breaks first: nothing here writes a file
|
|
5
|
+
// a post depends on but the repo's copy of the group's files, keyed by their sha
|
|
6
|
+
import type { Database } from 'bun:sqlite'
|
|
7
|
+
import { copyFileSync, existsSync, mkdirSync, statSync } from 'node:fs'
|
|
8
|
+
import { dirname } from 'node:path'
|
|
9
|
+
import { cardsDb } from '../review/card-db.ts'
|
|
10
|
+
import { appendEvent, eventsRepo, type Attachment, type ThreadEvent } from '../review/events.ts'
|
|
11
|
+
import { heldThreadOf, threadsReferring as referring } from '../review/referring.ts'
|
|
12
|
+
import { groupOf, postOf, type GroupContent, type PostContent, type PostedFrom, type PostState } from '../review/projections.ts'
|
|
13
|
+
import { openThread, readAllCached, specOf, storeDir } from '../review/store.ts'
|
|
14
|
+
import { ownAddressesOf, threadsOf } from '../review/threads.ts'
|
|
15
|
+
import { shaOf, typeOf } from '../media-store/content.ts'
|
|
16
|
+
import { fileList, OPTIONS, type DraftSection, type ParsedDraft } from './drafts.ts'
|
|
17
|
+
import { accountOf, type Desk, type Draft } from './load.ts'
|
|
18
|
+
import { socialKey } from './media-facts.ts'
|
|
19
|
+
import { postTime } from './post-url.ts'
|
|
20
|
+
import { groupAddress, groupNow, postAddress, postMediaPath, REMOVED, type GroupNow } from './post-sources.ts'
|
|
21
|
+
import { checkUrl } from './writes.ts'
|
|
22
|
+
|
|
23
|
+
const repoOf = (root: string): string => eventsRepo(storeDir(root))
|
|
24
|
+
|
|
25
|
+
/** who writes: an agent's lane (`SK_AGENT`), else `owner` from the hub, `machine` for a sync */
|
|
26
|
+
export const senderOf = (fallback = 'agent'): string => process.env.SK_AGENT?.trim() || fallback
|
|
27
|
+
|
|
28
|
+
type Std = { title: string; body: string; attachments?: Attachment[] }
|
|
29
|
+
|
|
30
|
+
function putGroup(repo: string, g: GroupContent, std: Std, sender: string, db: Database, at?: number, attach?: readonly string[]): ThreadEvent {
|
|
31
|
+
return appendEvent(repo, { type: 'post-group', sender, stateKey: g.slug, ...(at ? { at } : {}), ...(attach ? { attach } : {}), content: { title: std.title, body: std.body, attachments: std.attachments ?? [], group: g } }, db)
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function putPost(repo: string, p: PostContent, std: Std, sender: string, db: Database, at?: number): ThreadEvent {
|
|
35
|
+
return appendEvent(repo, { type: 'post', sender, stateKey: `${p.group}/${p.key}`, ...(at ? { at } : {}), content: { title: std.title, body: std.body, post: p } }, db)
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// ---- files
|
|
39
|
+
|
|
40
|
+
/** a file kept as one of a group's: hashed, copied into the repo's copy under its sha, sent to the Space (a send that
|
|
41
|
+
* fails waits for the media daemon, which reads the same attachments) */
|
|
42
|
+
export async function keepPostFile(root: string, file: string, name: string): Promise<Attachment> {
|
|
43
|
+
const st = statSync(file, { throwIfNoEntry: false })
|
|
44
|
+
if (!st?.isFile()) throw new Error(`${file}: no such file`)
|
|
45
|
+
const sha = await shaOf(file)
|
|
46
|
+
const dest = postMediaPath(root, sha, name)
|
|
47
|
+
if (!existsSync(dest)) {
|
|
48
|
+
mkdirSync(dirname(dest), { recursive: true })
|
|
49
|
+
copyFileSync(file, dest)
|
|
50
|
+
}
|
|
51
|
+
const key = socialKey({ sha }, name)
|
|
52
|
+
const a: Attachment = { name, sha, size: st.size, type: typeOf(name), key }
|
|
53
|
+
await sendUp(key, st.size, dest)
|
|
54
|
+
return a
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** the file up in the Space now, when the Space answers; else its row waits for the media daemon */
|
|
58
|
+
async function sendUp(key: string, size: number, path: string): Promise<void> {
|
|
59
|
+
if (process.env['SHIPKIT_POSTS_NO_SEND'] === '1') return
|
|
60
|
+
try {
|
|
61
|
+
const { isUp } = await import('../media-store/remote.ts')
|
|
62
|
+
if (isUp(key)) return
|
|
63
|
+
const { machineSpace } = await import('../media-store/space.ts')
|
|
64
|
+
const { space } = await machineSpace()
|
|
65
|
+
if (space == null) return
|
|
66
|
+
const { uploadOne } = await import('../media-store/upload.ts')
|
|
67
|
+
await uploadOne(space, { key, size, path })
|
|
68
|
+
} catch (e) {
|
|
69
|
+
console.error(`WARN posts: ${key} not sent yet (${(e as Error).message}): the media daemon sends it`)
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// ---- rule 5: an agent's new group is never an orphan
|
|
74
|
+
|
|
75
|
+
/** a thread's key for a link: its own address, else its newest version's id */
|
|
76
|
+
function threadKey(t: ReturnType<typeof threadsOf>[number], dir: string): string {
|
|
77
|
+
return ownAddressesOf(t, specOf(dir))[0] ?? t.versions[t.versions.length - 1]?.item.id ?? t.head.item.id
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/** the thread a lane holds that it acted in last, null when it holds none in this repo */
|
|
81
|
+
export function laneThreadOf(root: string, lane: string): string | null {
|
|
82
|
+
const t = heldThreadOf(root, { lane })
|
|
83
|
+
return t === null ? null : threadKey(t, storeDir(root))
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/** the thread a group was staged from, by any item id of it */
|
|
87
|
+
export function versionThreadOf(root: string, item: string): string | null {
|
|
88
|
+
const dir = storeDir(root)
|
|
89
|
+
for (const t of threadsOf(readAllCached(dir).items, specOf(dir))) if ([...t.versions, ...t.answers, { item: t.head.item }].some((x) => x.item.id === item)) return threadKey(t, dir)
|
|
90
|
+
return null
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
const linked = (repo: string, thread: string, address: string, db: Database): boolean =>
|
|
94
|
+
db.query("select 1 from thread_events where repo = ? and type = 'link' and thread = ? and json_extract(content, '$.link.address') = ?").get(repo, thread, address) != null
|
|
95
|
+
|
|
96
|
+
/** the threads that refer to a group or one of its posts */
|
|
97
|
+
export const threadsReferring = (root: string, slug: string, db: Database = cardsDb()): string[] => referring(root, 'draft', slug, db)
|
|
98
|
+
|
|
99
|
+
/** a link to the group into a thread, once */
|
|
100
|
+
export function linkGroup(root: string, thread: string, slug: string, title: string, sender: string, db: Database = cardsDb()): boolean {
|
|
101
|
+
const repo = repoOf(root)
|
|
102
|
+
if (linked(repo, thread, groupAddress(slug), db)) return false
|
|
103
|
+
appendEvent(repo, { type: 'link', sender, thread, content: { title, link: { address: groupAddress(slug), type: 'post-group', key: slug } } }, db)
|
|
104
|
+
return true
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
/** the threads a new group is attached to in the call that creates it (rule 5): the version it was staged from, the
|
|
108
|
+
* thread its lane holds, else a thread opened on its own address. Chosen before the write, which links each */
|
|
109
|
+
export async function threadsFor(root: string, slug: string, title: string, o: { review?: string | null } = {}): Promise<string[]> {
|
|
110
|
+
const out: string[] = []
|
|
111
|
+
const from = o.review ? versionThreadOf(root, o.review) : null
|
|
112
|
+
if (from != null) out.push(from)
|
|
113
|
+
const lane = process.env.SK_AGENT?.trim()
|
|
114
|
+
const held = lane ? laneThreadOf(root, lane) : null
|
|
115
|
+
if (held != null && !out.includes(held)) out.push(held)
|
|
116
|
+
if (out.length === 0) out.push(await groupThread(root, slug, title, lane ? { lane } : {}))
|
|
117
|
+
return out
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/** the thread opened on a group's address, made when none is (a click on it in Communications, `sk social draft thread`) */
|
|
121
|
+
export async function groupThread(root: string, slug: string, title: string, o: { lane?: string } = {}): Promise<string> {
|
|
122
|
+
const dir = storeDir(root)
|
|
123
|
+
const own = threadsOf(readAllCached(dir).items, specOf(dir)).find((t) => ownAddressesOf(t, specOf(dir)).includes(groupAddress(slug)))
|
|
124
|
+
if (own != null) return own.head.item.id
|
|
125
|
+
return (await openThread(dir, groupAddress(slug), { title: title || slug, ...(o.lane ? { lane: o.lane } : { machine: true }) })).id
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
// ---- a text, written
|
|
129
|
+
|
|
130
|
+
/** the post a loaded message makes: its section's words and options, its place as the load gave it */
|
|
131
|
+
function postFromMessage(slug: string, p: Draft['posts'][number], s: DraftSection): { post: Omit<PostContent, 'version' | 'why' | 'state'>; std: Std } {
|
|
132
|
+
const options: Record<string, string> = {}
|
|
133
|
+
for (const o of OPTIONS) if (o !== 'name' && o !== 'place' && s.options[o] !== undefined) options[o] = s.options[o]!
|
|
134
|
+
return { post: { group: slug, key: p.key, platform: p.platform, place: p.place, options, format: p.format }, std: { title: s.options.title ?? '', body: s.text } }
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/** a heading that reached no account, kept as one post on its own heading word: a group's (an identity with no account
|
|
138
|
+
* in it yet) goes to each account of the group once one exists */
|
|
139
|
+
function waitingPost(slug: string, s: DraftSection): { post: Omit<PostContent, 'version' | 'why' | 'state'>; std: Std } {
|
|
140
|
+
const options: Record<string, string> = {}
|
|
141
|
+
for (const o of OPTIONS) if (o !== 'name' && o !== 'place' && s.options[o] !== undefined) options[o] = s.options[o]!
|
|
142
|
+
return { post: { group: slug, key: s.key, platform: s.platform, place: '', options, format: s.platform }, std: { title: s.options.title ?? '', body: s.text } }
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
export interface FromText {
|
|
146
|
+
/** the draft the desk's builder made of the text */
|
|
147
|
+
draft: Draft
|
|
148
|
+
/** the text form's parts, parsed with the speaker's groups */
|
|
149
|
+
parsed: ParsedDraft
|
|
150
|
+
/** every file of the group, as its attachments */
|
|
151
|
+
attachments: Attachment[]
|
|
152
|
+
article: string | null
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
export interface Written {
|
|
156
|
+
slug: string
|
|
157
|
+
group: boolean
|
|
158
|
+
posts: string[]
|
|
159
|
+
created: boolean
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
const same = (a: unknown, b: unknown): boolean => JSON.stringify(a) === JSON.stringify(b)
|
|
163
|
+
|
|
164
|
+
/** a group's text written as events: the group when its facts moved, each post whose words, options or (with `states`)
|
|
165
|
+
* state moved, a new post for a new heading. `states`: the import's, each post's state as the text says it (`moments`
|
|
166
|
+
* its posted moments); without, a post keeps its state, and a text that says another is refused */
|
|
167
|
+
export function writeText(root: string, t: FromText, o: { why: string; sender: string; at?: number; states?: boolean; moments?: ReadonlyMap<string, { at: number | null; from: PostedFrom }>; attach?: readonly string[] }, db: Database = cardsDb()): Written {
|
|
168
|
+
const repo = repoOf(root)
|
|
169
|
+
const d = t.draft
|
|
170
|
+
const now = groupNow(root, d.slug, true)
|
|
171
|
+
const meta: Record<string, string> = {}
|
|
172
|
+
for (const [k, v] of Object.entries(t.parsed.meta)) if (!['title', 'status', 'media', 'dropped'].includes(k)) meta[k] = v
|
|
173
|
+
const group: Omit<GroupContent, 'version' | 'why'> = { slug: d.slug, identity: d.identity, meta, media: fileList(t.parsed.meta.media), article: t.article, ...(d.status === 'picked' ? { picked: true as const } : {}) }
|
|
174
|
+
const std: Std = { title: d.title, body: t.parsed.angle, attachments: t.attachments }
|
|
175
|
+
const messages: { post: Omit<PostContent, 'version' | 'why' | 'state'>; std: Std; state: PostState; url?: string }[] = []
|
|
176
|
+
for (const p of d.posts) {
|
|
177
|
+
const s = t.parsed.sections.find((x) => x.key === p.from)
|
|
178
|
+
if (s == null) throw new Error(`${d.slug}: no heading ${p.from} for ${p.key}`)
|
|
179
|
+
const state: PostState = p.posted !== '' ? 'posted' : p.dropped || d.status === 'rejected' ? 'dropped' : 'draft'
|
|
180
|
+
messages.push({ ...postFromMessage(d.slug, p, s), state, ...(p.posted !== '' ? { url: p.posted } : {}) })
|
|
181
|
+
}
|
|
182
|
+
// a heading that made no message (a group with no account yet, every account skipped, an account the identity lacks)
|
|
183
|
+
// keeps its words: the load reads it again as it reads it now
|
|
184
|
+
for (const s of t.parsed.sections) if (!d.posts.some((p) => p.from === s.key)) messages.push({ ...waitingPost(d.slug, s), state: 'draft' })
|
|
185
|
+
const out: Written = { slug: d.slug, group: false, posts: [], created: now == null }
|
|
186
|
+
db.transaction(() => {
|
|
187
|
+
const was = now?.group
|
|
188
|
+
const wasStd = now == null ? null : { title: now.event.content.title ?? '', body: now.event.content.body ?? '', attachments: now.event.content.attachments ?? [] }
|
|
189
|
+
if (was == null || !same({ ...was, version: 0, why: '' }, { ...group, version: 0, why: '' }) || !same(wasStd, { ...std, attachments: std.attachments ?? [] })) {
|
|
190
|
+
putGroup(repo, { ...group, version: (was?.version ?? 0) + 1, why: o.why }, std, o.sender, db, o.at, was == null ? o.attach : undefined)
|
|
191
|
+
out.group = true
|
|
192
|
+
}
|
|
193
|
+
for (const m of messages) {
|
|
194
|
+
const cur = now?.posts.find((x) => x.post.key === m.post.key)
|
|
195
|
+
// the text form writes each post under its account's own heading: the group heading it was written under stays
|
|
196
|
+
if (cur != null && m.post.format === '') m.post = { ...m.post, format: cur.post.format }
|
|
197
|
+
const curWords = cur == null ? null : { title: cur.event.content.title ?? '', body: cur.event.content.body ?? '', options: cur.post.options, place: cur.post.place, platform: cur.post.platform, format: cur.post.format }
|
|
198
|
+
const words = { title: m.std.title, body: m.std.body, options: m.post.options, place: m.post.place, platform: m.post.platform, format: m.post.format }
|
|
199
|
+
let state: Pick<PostContent, 'state' | 'url' | 'postedAt' | 'postedFrom'>
|
|
200
|
+
if (o.states) {
|
|
201
|
+
const id = m.state === 'posted' ? postTime(m.post.platform, m.url!) : null
|
|
202
|
+
const at = m.state === 'posted' ? (o.moments?.get(m.post.key) ?? (id != null ? { at: id, from: 'id' as PostedFrom } : { at: null, from: 'unknown' as PostedFrom })) : null
|
|
203
|
+
state = { state: m.state, ...(m.url ? { url: m.url } : {}), ...(at ? { postedAt: at.at, postedFrom: at.from } : {}) }
|
|
204
|
+
} else if (cur != null && cur.post.why === REMOVED) state = { state: 'draft' }
|
|
205
|
+
else if (cur != null) {
|
|
206
|
+
if (m.state !== cur.post.state && !(m.state === 'draft' && cur.post.state === 'dropped') && !(m.state === 'dropped' && cur.post.state === 'dropped'))
|
|
207
|
+
throw new Error(`${d.slug} ${m.post.key} is ${cur.post.state}: its state moves by its verb (sk social draft posted|clear|drop|undrop), never by the text`)
|
|
208
|
+
state = { state: cur.post.state, ...(cur.post.url ? { url: cur.post.url } : {}), ...(cur.post.postedAt !== undefined ? { postedAt: cur.post.postedAt } : {}), ...(cur.post.postedFrom ? { postedFrom: cur.post.postedFrom } : {}) }
|
|
209
|
+
} else state = { state: 'draft' }
|
|
210
|
+
const curState = cur == null ? null : { state: cur.post.state, url: cur.post.url, postedAt: cur.post.postedAt, postedFrom: cur.post.postedFrom }
|
|
211
|
+
const moved = !same(curWords, words)
|
|
212
|
+
if (cur != null && !moved && same(curState, { state: state.state, url: state.url, postedAt: state.postedAt, postedFrom: state.postedFrom })) continue
|
|
213
|
+
putPost(repo, { ...m.post, ...state, version: (cur?.post.version ?? 0) + (moved ? 1 : 0), why: o.why }, m.std, o.sender, db, o.at)
|
|
214
|
+
out.posts.push(m.post.key)
|
|
215
|
+
}
|
|
216
|
+
// a post the text no longer names leaves the group; a posted one is public, so its heading stays
|
|
217
|
+
for (const cur of now?.posts ?? []) {
|
|
218
|
+
if (messages.some((m) => m.post.key === cur.post.key) || cur.post.why === REMOVED) continue
|
|
219
|
+
if (cur.post.state === 'posted') throw new Error(`${d.slug} ${cur.post.key} is posted (${cur.post.url}): its heading stays in the text`)
|
|
220
|
+
const { url: _u, postedAt: _a, postedFrom: _f, ...rest } = cur.post
|
|
221
|
+
putPost(repo, { ...rest, state: 'dropped', why: REMOVED }, { title: cur.event.content.title ?? '', body: cur.event.content.body ?? '' }, o.sender, db, o.at)
|
|
222
|
+
out.posts.push(cur.post.key)
|
|
223
|
+
}
|
|
224
|
+
}).immediate()
|
|
225
|
+
return out
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
// ---- the verbs
|
|
229
|
+
|
|
230
|
+
function current(root: string, slug: string, key: string): { now: GroupNow; post: GroupNow['posts'][number] } {
|
|
231
|
+
const now = groupNow(root, slug)
|
|
232
|
+
if (now == null) throw new Error(`no draft ${slug} in the database`)
|
|
233
|
+
const post = now.posts.find((p) => p.post.key === key)
|
|
234
|
+
if (post == null) throw new Error(`${slug} has no post ${key}: ${now.posts.map((p) => p.post.key).join(', ') || 'none'}`)
|
|
235
|
+
return { now, post }
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
/** one post's state moved, with its words as they are */
|
|
239
|
+
function moveState(root: string, slug: string, key: string, next: Pick<PostContent, 'state'> & Partial<Pick<PostContent, 'url' | 'postedAt' | 'postedFrom'>>, why: string, sender: string): boolean {
|
|
240
|
+
const { post } = current(root, slug, key)
|
|
241
|
+
const p = post.post
|
|
242
|
+
if (p.state === next.state && (p.url ?? '') === (next.url ?? '')) return false
|
|
243
|
+
const { url: _u, postedAt: _a, postedFrom: _f, ...rest } = p
|
|
244
|
+
putPost(repoOf(root), { ...rest, ...next, why }, { title: post.event.content.title ?? '', body: post.event.content.body ?? '' }, sender, cardsDb())
|
|
245
|
+
return true
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
/** his word that a post is posted at `url` (checked against the account's hosts), or none: a draft again */
|
|
249
|
+
export function setPosted(desk: Desk, slug: string, key: string, url: string | null, o: { sender?: string; why?: string } = {}): boolean {
|
|
250
|
+
const d = desk.drafts.find((x) => x.slug === slug)
|
|
251
|
+
const m = d?.posts.find((x) => x.key === key)
|
|
252
|
+
if (d != null && m != null) checkUrl(url, accountOf(desk, m.identity, m.platform))
|
|
253
|
+
const sender = o.sender ?? senderOf('owner')
|
|
254
|
+
if (url == null) return moveState(desk.root, slug, key, { state: 'draft' }, o.why ?? 'clear', sender)
|
|
255
|
+
const { post } = current(desk.root, slug, key)
|
|
256
|
+
const at = postTime(post.post.platform, url)
|
|
257
|
+
return moveState(desk.root, slug, key, { state: 'posted', url, postedAt: at ?? Date.now(), postedFrom: at != null ? 'id' : 'mark' }, o.why ?? 'posted', sender)
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
export type PostVerb = 'drop' | 'undrop' | 'discard' | 'restore'
|
|
261
|
+
export const POST_VERBS: readonly PostVerb[] = ['drop', 'undrop', 'discard', 'restore']
|
|
262
|
+
|
|
263
|
+
/** drop or take back one post; discard a group (each post in draft dropped) or restore it (each post a discard dropped) */
|
|
264
|
+
export function postVerb(root: string, slug: string, verb: PostVerb, key?: string, sender = senderOf('owner')): string[] {
|
|
265
|
+
if (verb === 'drop' || verb === 'undrop') {
|
|
266
|
+
if (key == null) throw new Error(`${verb} names a post: ${slug} <key>`)
|
|
267
|
+
const { post } = current(root, slug, key)
|
|
268
|
+
if (verb === 'drop' && post.post.state !== 'draft') throw new Error(`${slug} ${key} is ${post.post.state}: only a draft is dropped`)
|
|
269
|
+
if (verb === 'undrop' && post.post.state !== 'dropped') return []
|
|
270
|
+
return moveState(root, slug, key, { state: verb === 'drop' ? 'dropped' : 'draft' }, verb, sender) ? [key] : []
|
|
271
|
+
}
|
|
272
|
+
const now = groupNow(root, slug)
|
|
273
|
+
if (now == null) throw new Error(`no draft ${slug} in the database`)
|
|
274
|
+
const moved: string[] = []
|
|
275
|
+
cardsDb()
|
|
276
|
+
.transaction(() => {
|
|
277
|
+
for (const p of now.posts) {
|
|
278
|
+
if (verb === 'discard' && p.post.state === 'draft' && moveState(root, slug, p.post.key, { state: 'dropped' }, 'discard', sender)) moved.push(p.post.key)
|
|
279
|
+
if (verb === 'restore' && p.post.state === 'dropped' && p.post.why === 'discard' && moveState(root, slug, p.post.key, { state: 'draft' }, 'restore', sender)) moved.push(p.post.key)
|
|
280
|
+
}
|
|
281
|
+
})
|
|
282
|
+
.immediate()
|
|
283
|
+
return moved
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
/** one post's words (and its title and tags where given), a new version of it */
|
|
287
|
+
export function setPostText(root: string, slug: string, key: string, text: string, o: { title?: string; tags?: string; sender?: string } = {}): boolean {
|
|
288
|
+
if (text.trim() === '') throw new Error(`${slug} ${key}: an empty text`)
|
|
289
|
+
if (/^## /m.test(text)) throw new Error(`a text holds no "## " line: it would start a heading`)
|
|
290
|
+
const { post } = current(root, slug, key)
|
|
291
|
+
const options = { ...post.post.options }
|
|
292
|
+
for (const [k, v] of [['title', o.title], ['tags', o.tags === undefined ? undefined : o.tags.split(/\s+/).filter(Boolean).join(' ')]] as const) {
|
|
293
|
+
if (v === undefined) continue
|
|
294
|
+
if (v.trim() === '') delete options[k]
|
|
295
|
+
else options[k] = v.trim()
|
|
296
|
+
}
|
|
297
|
+
const title = options['title'] ?? ''
|
|
298
|
+
if ((post.event.content.body ?? '') === text.trim() && same(options, post.post.options)) return false
|
|
299
|
+
putPost(repoOf(root), { ...post.post, options, version: post.post.version + 1, why: 'text' }, { title, body: text.trim() }, o.sender ?? senderOf('owner'), cardsDb())
|
|
300
|
+
return true
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
/** the group's facts moved: its meta (a key to a value, empty removes it), its media list, a file added */
|
|
304
|
+
export async function setGroup(root: string, slug: string, change: { meta?: Record<string, string>; media?: string[]; files?: { file: string; name: string }[]; alt?: string; article?: string | null }, o: { why: string; sender?: string }): Promise<boolean> {
|
|
305
|
+
const now = groupNow(root, slug)
|
|
306
|
+
if (now == null) throw new Error(`no draft ${slug} in the database`)
|
|
307
|
+
const added: Attachment[] = []
|
|
308
|
+
for (const f of change.files ?? []) added.push(await keepPostFile(root, f.file, f.name))
|
|
309
|
+
const g = groupOf(now.event)
|
|
310
|
+
const meta = { ...g.meta }
|
|
311
|
+
for (const [k, v] of Object.entries({ ...change.meta, ...(change.alt !== undefined ? { alt: change.alt } : {}) })) {
|
|
312
|
+
if (v.trim() === '') delete meta[k]
|
|
313
|
+
else meta[k] = v.trim()
|
|
314
|
+
}
|
|
315
|
+
const attachments = [...(now.event.content.attachments ?? []).filter((a) => !added.some((x) => x.name === a.name)), ...added]
|
|
316
|
+
const next = { ...g, meta, media: change.media ?? g.media, ...(change.article !== undefined ? { article: change.article } : {}) }
|
|
317
|
+
if (same(next, g) && same(attachments, now.event.content.attachments ?? [])) return false
|
|
318
|
+
putGroup(repoOf(root), { ...next, version: g.version + 1, why: o.why }, { title: now.event.content.title ?? '', body: now.event.content.body ?? '', attachments }, o.sender ?? senderOf(), cardsDb())
|
|
319
|
+
return true
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
// ---- the reads Communications draws
|
|
323
|
+
|
|
324
|
+
export interface PostEntry {
|
|
325
|
+
slug: string
|
|
326
|
+
key: string
|
|
327
|
+
platform: string
|
|
328
|
+
place: string
|
|
329
|
+
state: PostState
|
|
330
|
+
url: string
|
|
331
|
+
postedAt: number | null
|
|
332
|
+
version: number
|
|
333
|
+
at: number
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
export interface GroupEntry {
|
|
337
|
+
slug: string
|
|
338
|
+
title: string
|
|
339
|
+
kind: string
|
|
340
|
+
identity: string
|
|
341
|
+
drafts: number
|
|
342
|
+
posted: number
|
|
343
|
+
dropped: number
|
|
344
|
+
version: number
|
|
345
|
+
at: number
|
|
346
|
+
posts: PostEntry[]
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
/** every group of the repo with its posts, from the projections, the newest group first */
|
|
350
|
+
export function postsIndex(root: string, db: Database = cardsDb()): GroupEntry[] {
|
|
351
|
+
const repo = repoOf(root)
|
|
352
|
+
const posts = db.query('select * from thread_posts where repo = ? order by first_seq').all(repo) as { slug: string; key: string; platform: string; place: string; state: PostState; url: string; posted_at: number | null; version: number; last_at: number }[]
|
|
353
|
+
const by = new Map<string, PostEntry[]>()
|
|
354
|
+
for (const p of posts) by.set(p.slug, [...(by.get(p.slug) ?? []), { slug: p.slug, key: p.key, platform: p.platform, place: p.place, state: p.state, url: p.url, postedAt: p.posted_at, version: p.version, at: p.last_at }])
|
|
355
|
+
return (db.query('select * from thread_post_groups where repo = ? order by slug desc').all(repo) as { slug: string; title: string; kind: string; identity: string; drafts: number; posted: number; dropped: number; version: number; last_at: number }[]).map((g) => ({
|
|
356
|
+
slug: g.slug,
|
|
357
|
+
title: g.title,
|
|
358
|
+
kind: g.kind,
|
|
359
|
+
identity: g.identity,
|
|
360
|
+
drafts: g.drafts,
|
|
361
|
+
posted: g.posted,
|
|
362
|
+
dropped: g.dropped,
|
|
363
|
+
version: g.version,
|
|
364
|
+
at: g.last_at,
|
|
365
|
+
posts: by.get(g.slug) ?? [],
|
|
366
|
+
}))
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
/** one post's versions, oldest first */
|
|
370
|
+
export function postHistory(root: string, slug: string, key: string, db: Database = cardsDb()): { id: string; at: number; sender: string; why: string; version: number; state: PostState; body: string }[] {
|
|
371
|
+
const { eventOfRow } = require('../review/events.ts') as typeof import('../review/events.ts')
|
|
372
|
+
return (db.query("select * from thread_events where repo = ? and type = 'post' and state_key = ? order by seq").all(repoOf(root), `${slug}/${key}`) as Parameters<typeof eventOfRow>[0][]).map(eventOfRow).map((e) => {
|
|
373
|
+
const p = postOf(e)
|
|
374
|
+
return { id: e.id, at: e.at, sender: e.sender, why: p.why, version: p.version, state: p.state, body: e.content.body ?? '' }
|
|
375
|
+
})
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
export { groupAddress, postAddress }
|
package/src/social/presence.ts
CHANGED
|
@@ -17,8 +17,9 @@ export type Family = 'feed' | 'shorts' | 'other'
|
|
|
17
17
|
export const FAMILIES: readonly Family[] = ['feed', 'shorts', 'other']
|
|
18
18
|
export const familyOf = (a: Pick<Account, 'group'>): Family => (a.group === 'short' ? 'feed' : a.group === 'vertical' ? 'shorts' : 'other')
|
|
19
19
|
|
|
20
|
-
/** where a moment came from: the post's id, the commit that recorded it
|
|
21
|
-
|
|
20
|
+
/** where a moment came from: the post's id, the commit that recorded it (kept by the import), his mark, a devlog
|
|
21
|
+
* post's `date`, nowhere */
|
|
22
|
+
type Source = 'id' | 'commit' | 'mark' | 'page' | 'unknown'
|
|
22
23
|
|
|
23
24
|
/** one message on one account, posted */
|
|
24
25
|
export type Sent = {
|
|
@@ -40,7 +41,7 @@ type Draft = Desk['drafts'][number]
|
|
|
40
41
|
/** what the model reads of a draft */
|
|
41
42
|
export type DraftIn = Pick<Draft, 'slug' | 'kind' | 'topics' | 'status'> & {
|
|
42
43
|
article: Pick<NonNullable<Draft['article']>, 'draft' | 'date' | 'url'> | null
|
|
43
|
-
posts: readonly Pick<SocialPost, 'platform' | 'key' | 'posted' | 'tags'>[]
|
|
44
|
+
posts: readonly (Pick<SocialPost, 'platform' | 'key' | 'posted' | 'tags'> & Partial<Pick<SocialPost, 'postedAt' | 'postedFrom'>>)[]
|
|
44
45
|
}
|
|
45
46
|
/** what the model reads of the social folder */
|
|
46
47
|
export type SocialIn = { accounts: readonly Pick<Account, 'id' | 'group'>[]; drafts: readonly DraftIn[] }
|
|
@@ -91,7 +92,7 @@ function postedCommits(repo: string, since: string, range: string[]): Map<string
|
|
|
91
92
|
* <url> »; git walks only from the day before the oldest such draft was made, and not at all when none is. Kept per
|
|
92
93
|
* HEAD in a long lived process: a new HEAD walks only the commits it adds */
|
|
93
94
|
export function commitMoments(social: Pick<SocialIn, 'drafts'>, repo: string): Map<string, number> {
|
|
94
|
-
const made = social.drafts.filter((d) => d.posts.some((p) => p.posted !== '' && postTime(p.platform, p.posted) == null)).map((d) => d.slug.slice(0, 8))
|
|
95
|
+
const made = social.drafts.filter((d) => d.posts.some((p) => p.posted !== '' && p.postedAt == null && postTime(p.platform, p.posted) == null)).map((d) => d.slug.slice(0, 8))
|
|
95
96
|
if (made.length === 0) return new Map()
|
|
96
97
|
const first = made.sort()[0]!
|
|
97
98
|
const since = dayBefore(`${first.slice(0, 4)}-${first.slice(4, 6)}-${first.slice(6, 8)}`)
|
|
@@ -121,7 +122,8 @@ export function postedOf(social: SocialIn, moments: ReadonlyMap<string, number>)
|
|
|
121
122
|
if (p.posted === '') continue
|
|
122
123
|
const fromId = postTime(p.platform, p.posted)
|
|
123
124
|
const fromCommit = moments.get(p.posted)
|
|
124
|
-
const
|
|
125
|
+
const kept: Source | null = p.postedAt != null ? ((['id', 'commit', 'mark'] as const).find((x) => x === p.postedFrom) ?? 'mark') : null
|
|
126
|
+
const [at, source]: [number | null, Source] = fromId != null ? [fromId, 'id'] : kept != null ? [p.postedAt!, kept] : fromCommit != null ? [fromCommit, 'commit'] : [null, 'unknown']
|
|
125
127
|
out.push({ platform: p.platform, key: p.key, slug: d.slug, url: p.posted, kind: d.kind, topics: d.topics, tags: p.tags, family: family.get(p.platform) ?? 'other', at, source })
|
|
126
128
|
}
|
|
127
129
|
const a = sitePost(d)
|
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
// the accounts shipkit posts to itself (`automation: live`): one publisher a platform, the desk's « Publish », and
|
|
2
2
|
// `sk social post --publish`, every waiting message of a draft on its live accounts (`--only` one of them), `--edit` a
|
|
3
3
|
// posted one sent again at its link where the venue edits in place
|
|
4
|
+
import { existsSync } from 'node:fs'
|
|
4
5
|
import { accountOf, draftPost, type Desk } from '../load.ts'
|
|
5
|
-
import {
|
|
6
|
+
import { refuseUnimported } from '../post-sources.ts'
|
|
7
|
+
import { setPosted } from '../posts.ts'
|
|
6
8
|
import { postBluesky } from './bluesky.ts'
|
|
7
9
|
import { postDiscord } from './discord.ts'
|
|
8
10
|
import { secretsAs } from './secret.ts'
|
|
@@ -27,9 +29,13 @@ export function onlyOf<P extends Pick<SocialPost, 'key' | 'platform'>>(posts: re
|
|
|
27
29
|
/** the venues whose publisher edits a posted message at its link; any other refuses a second send */
|
|
28
30
|
const EDITS_IN_PLACE: ReadonlySet<string> = new Set(['discord'])
|
|
29
31
|
|
|
30
|
-
/** posts one message of a draft and records its link
|
|
31
|
-
|
|
32
|
+
/** posts one message of a draft and records its link as the post's state (posts.ts); `publishers` is a test's stub. A
|
|
33
|
+
* record that fails after the venue took the post names the link and the command that records it: a live link is never lost */
|
|
34
|
+
export async function publish(desk: Desk, slug: string, key: string, publishers: Publishers = PUBLISHERS): Promise<{ url: string; recorded: boolean }> {
|
|
35
|
+
refuseUnimported(desk.root)
|
|
32
36
|
const post = draftPost(desk, slug, key)
|
|
37
|
+
// the bytes the guard and the cover read are the ones sent: a file not on the disk refuses, never a send unread
|
|
38
|
+
if (post.media != null && !existsSync(post.media.path)) throw new Error(`${slug} ${key}: its media ${post.media.name} is not on the disk: fetch it back, then load the desk again (the route and sk social post do both)`)
|
|
33
39
|
const a = accountOf(desk, post.identity, post.platform)
|
|
34
40
|
if (post.refused !== '') throw new Error(`${slug} may not go out on ${a.name}: ${post.refused}`)
|
|
35
41
|
const run = publishers[post.platform]
|
|
@@ -38,7 +44,12 @@ export async function publish(desk: Desk, slug: string, key: string, publishers:
|
|
|
38
44
|
if (post.posted !== '' && !EDITS_IN_PLACE.has(post.platform)) throw new Error(`${slug} is already posted on ${a.name}: ${post.posted}`)
|
|
39
45
|
// the hub runs in the house's folder: the token is read as the draft's repo
|
|
40
46
|
const url = await secretsAs(desk.root, () => run(a, post))
|
|
41
|
-
|
|
47
|
+
if (url === post.posted) return { url, recorded: false }
|
|
48
|
+
try {
|
|
49
|
+
return { url, recorded: setPosted(desk, slug, key, url, { why: 'publish' }) }
|
|
50
|
+
} catch (e) {
|
|
51
|
+
throw new Error(`posted on ${a.name} at ${url}, but its link was not recorded (${(e as Error).message}): sk social draft posted ${slug} ${key} ${url}`)
|
|
52
|
+
}
|
|
42
53
|
}
|
|
43
54
|
|
|
44
55
|
/** each waiting message of a draft on a live account posted, the others named as waiting for his paste; `only` keeps
|
|
@@ -57,7 +68,8 @@ export async function publishDraft(
|
|
|
57
68
|
const live = a.automation === 'live'
|
|
58
69
|
if (p.posted !== '' && !o.edit) out.push({ key: p.key, line: `already posted, ${p.posted}${live && EDITS_IN_PLACE.has(p.platform) ? ' (--edit sends its text again at its link)' : ''}` })
|
|
59
70
|
else if (p.posted !== '' && !EDITS_IN_PLACE.has(p.platform)) out.push({ key: p.key, line: `already posted, ${p.posted}: ${a.name} cannot edit a post in place, nothing sent` })
|
|
60
|
-
else if (
|
|
71
|
+
else if (p.dropped && p.posted === '') out.push({ key: p.key, line: 'dropped: he will not post it here, nothing sent' })
|
|
72
|
+
else if (!live) out.push({ key: p.key, line: p.posted !== '' ?`already posted, ${p.posted}: ${a.file} is not automation: live, edit it by hand` : 'waits for his paste' })
|
|
61
73
|
else {
|
|
62
74
|
await o.before?.(p)
|
|
63
75
|
out.push({ key: p.key, line: `${p.posted !== '' ? 'edited at its link' : 'posted'}, ${(await publish(desk, slug, p.key, o.publishers)).url}` })
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
// an account's token, read through the house config
|
|
1
|
+
// an account's token, read through the house store (or the manager the house config declares): a missing one fails loud
|
|
2
|
+
// with where it is named.
|
|
2
3
|
// It reads as the repo that posts: the hub runs in the house's folder, so `secretsAs` names the draft's repo
|
|
3
4
|
import { AsyncLocalStorage } from 'node:async_hooks'
|
|
4
5
|
import { readThrough } from '../../secrets/read.ts'
|
|
@@ -16,6 +17,5 @@ export async function secret(name: string, where: string): Promise<string> {
|
|
|
16
17
|
const house = houseRootOf(cwd)
|
|
17
18
|
if (!house) throw new Error(`no house repo seen from ${cwd} to read the token ${name} (${where})`)
|
|
18
19
|
const c = await loadRepoConfig(house)
|
|
19
|
-
if (!c.credsReadCommand) throw new Error(`the house config (${house}) declares no credsReadCommand to read ${name} (${where})`)
|
|
20
20
|
return readThrough(c.credsReadCommand, name, 'get', cwd)
|
|
21
21
|
}
|