@senseihq/emaki-posting 0.1.0 → 0.1.2

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/poster/post.py CHANGED
@@ -19,36 +19,19 @@ Environment (CI secrets; see the package README):
19
19
  IG_USER_ID, IG_ACCESS_TOKEN, IG_GRAPH_VERSION Instagram Graph API (instagram_content_publish)
20
20
  X_API_KEY, X_API_SECRET, X_ACCESS_TOKEN, X_ACCESS_SECRET X API, OAuth 1.0a user context (or X_USER_TOKEN: OAuth 2.0)
21
21
  """
22
- import base64, hashlib, hmac, json, os, secrets, sys, time, urllib.parse, urllib.request
22
+ import hashlib, json, os, sys, time, urllib.parse
23
23
  from pathlib import Path
24
24
 
25
25
  INDEX = Path(os.environ.get('STORIES_INDEX', 'content/stories/index.json'))
26
26
  API = os.environ.get('ADMIN_API', '/api/admin').rstrip('/')
27
27
  sys.path.insert(0, str(Path(__file__).absolute().parent))
28
+ from net import Failed, http, jhttp # noqa: E402
29
+ from x import x_post # noqa: E402
28
30
  from youtube import yt_upload_caption, yt_set_thumbnail # noqa: E402
29
31
  E = os.environ
30
32
  RUN = f"{E.get('GITHUB_SERVER_URL', '')}/{E.get('GITHUB_REPOSITORY', '')}/actions/runs/{E.get('GITHUB_RUN_ID', '')}" if E.get('GITHUB_RUN_ID') else 'local'
31
33
 
32
34
 
33
- class Failed(Exception):
34
- pass
35
-
36
-
37
- def http(method, url, body=None, headers=None, timeout=600):
38
- req = urllib.request.Request(url, data=body, method=method, headers=headers or {})
39
- try:
40
- with urllib.request.urlopen(req, timeout=timeout) as r:
41
- return r.status, dict(r.headers), r.read()
42
- except urllib.error.HTTPError as err:
43
- raise Failed(f'{method} {url.split("?")[0]} → {err.code}: {err.read()[:400].decode(errors="replace")}')
44
-
45
-
46
- def jhttp(method, url, data=None, headers=None):
47
- body = json.dumps(data).encode() if data is not None else None
48
- _, _, b = http(method, url, body, {**({'content-type': 'application/json'} if body else {}), **(headers or {})})
49
- return json.loads(b or b'{}')
50
-
51
-
52
35
  def admin(method, path, data=None):
53
36
  h = {'CF-Access-Client-Id': E['POSTER_CLIENT_ID'], 'CF-Access-Client-Secret': E['POSTER_CLIENT_SECRET']}
54
37
  return jhttp(method, E['ADMIN_ORIGIN'].rstrip('/') + path, data, h)
@@ -149,56 +132,7 @@ def instagram_carousel(item, media, live):
149
132
  return publish(c)
150
133
 
151
134
 
152
- # ── X ────────────────────────────────────────────────────────────────────────────────────────────────────
153
-
154
- def x_auth(method, url, params=None):
155
- """Authorization header: OAuth 2.0 user token if given, else OAuth 1.0a (JSON / multipart bodies are not signed)."""
156
- if E.get('X_USER_TOKEN'):
157
- return {'authorization': f"Bearer {E['X_USER_TOKEN']}"}
158
- pct = lambda s: urllib.parse.quote(str(s), safe='~')
159
- o = {'oauth_consumer_key': E['X_API_KEY'], 'oauth_nonce': secrets.token_hex(16), 'oauth_signature_method': 'HMAC-SHA1',
160
- 'oauth_timestamp': str(int(time.time())), 'oauth_token': E['X_ACCESS_TOKEN'], 'oauth_version': '1.0'}
161
- base_url, _, query = url.partition('?')
162
- allp = {**o, **(params or {}), **dict(urllib.parse.parse_qsl(query))}
163
- base = '&'.join([method, pct(base_url), pct('&'.join(f'{pct(k)}={pct(allp[k])}' for k in sorted(allp)))])
164
- key = f"{pct(E['X_API_SECRET'])}&{pct(E['X_ACCESS_SECRET'])}"
165
- o['oauth_signature'] = base64.b64encode(hmac.new(key.encode(), base.encode(), hashlib.sha1).digest()).decode()
166
- return {'authorization': 'OAuth ' + ', '.join(f'{pct(k)}="{pct(v)}"' for k, v in sorted(o.items()))}
167
-
168
-
169
- def x_upload(data, mime):
170
- init = 'https://api.x.com/2/media/upload/initialize'
171
- mid = jhttp('POST', init, {'media_type': mime, 'total_bytes': len(data), 'media_category': 'tweet_video' if mime.startswith('video') else 'tweet_image'}, x_auth('POST', init))['data']['id']
172
- chunk = 4 * 1024 * 1024
173
- for i in range(0, len(data), chunk):
174
- url = f'https://api.x.com/2/media/upload/{mid}/append'
175
- boundary = secrets.token_hex(12)
176
- body = (f'--{boundary}\r\nContent-Disposition: form-data; name="segment_index"\r\n\r\n{i // chunk}\r\n'
177
- f'--{boundary}\r\nContent-Disposition: form-data; name="media"; filename="chunk"\r\nContent-Type: application/octet-stream\r\n\r\n').encode() + data[i:i + chunk] + f'\r\n--{boundary}--\r\n'.encode()
178
- http('POST', url, body, {**x_auth('POST', url), 'content-type': f'multipart/form-data; boundary={boundary}'})
179
- fin = f'https://api.x.com/2/media/upload/{mid}/finalize'
180
- info = jhttp('POST', fin, None, x_auth('POST', fin)).get('data', {}).get('processing_info')
181
- while info and info.get('state') in ('pending', 'in_progress'):
182
- time.sleep(info.get('check_after_secs', 2))
183
- st = f'https://api.x.com/2/media/upload?command=STATUS&media_id={mid}'
184
- info = jhttp('GET', st, None, x_auth('GET', st)).get('data', {}).get('processing_info')
185
- if info and info.get('state') == 'failed':
186
- raise Failed(f'X could not process the media: {info}')
187
- return mid
188
-
189
-
190
- def x_post(item, media, live):
191
- p = item['post']
192
- if not live:
193
- return None, f"would post with {len(media)} file(s) and a {len(p.get('thread', []))}-reply thread: {p['text'][:80]}"
194
- ids = [x_upload(m['data'], m['type']) for m in media[:1]]
195
- tweets = 'https://api.x.com/2/tweets'
196
- first = jhttp('POST', tweets, {'text': p['text'], **({'media': {'media_ids': ids}} if ids else {})}, x_auth('POST', tweets))['data']['id']
197
- prev = first
198
- for t in p.get('thread', []):
199
- prev = jhttp('POST', tweets, {'text': t, 'reply': {'in_reply_to_tweet_id': prev}}, x_auth('POST', tweets))['data']['id']
200
- return f'https://x.com/i/web/status/{first}', first
201
-
135
+ # ── The run ────────────────────────────────────────────────────────────────────────────────────────────
202
136
 
203
137
  POSTERS = {'youtube': youtube, 'instagram_reel': instagram_reel, 'instagram_carousel': instagram_carousel, 'x': x_post}
204
138
  SOCIAL = {'youtube': 'youtube', 'instagram_reel': 'instagram', 'x': 'x'}
@@ -215,29 +149,45 @@ def record_link(slug, channel, url):
215
149
  INDEX.write_text(json.dumps(index, indent=2, ensure_ascii=False) + '\n')
216
150
 
217
151
 
152
+ def parse_args(argv):
153
+ """(live, only): --live posts for real; --only youtube,x limits the run to those channels."""
154
+ only = argv[argv.index('--only') + 1].split(',') if '--only' in argv else None
155
+ return '--live' in argv, only
156
+
157
+
158
+ def report(item, status, **fields):
159
+ """Tell the desk how one post went (its timeline and audit log)."""
160
+ admin('POST', API + '/posted', {'slug': item['slug'], 'channel': item['channel'], 'content_sha': item['content_sha'], 'status': status, **fields, 'run': RUN})
161
+
162
+
163
+ def post_one(item, live):
164
+ """Download and check the media, post it (or describe it on a dry run), report it, and link a real post."""
165
+ ch = item['channel']
166
+ url, ext = POSTERS[ch](item, fetch_media(item), live)
167
+ status = 'posted' if live else 'dry-run'
168
+ print(f" {item['slug']} · {ch}: {status} {url or ext}")
169
+ report(item, status, url=url or '', external_id=ext if live else '', detail='' if live else ext)
170
+ if live:
171
+ record_link(item['slug'], ch, url)
172
+
173
+
174
+ def try_post(item, live):
175
+ """post_one, with a failure printed and reported instead of raised. True when it went through."""
176
+ try:
177
+ post_one(item, live)
178
+ return True
179
+ except (Failed, KeyError) as err:
180
+ print(f" {item['slug']} · {item['channel']}: FAILED {err}")
181
+ report(item, 'failed', detail=str(err)[:1500])
182
+ return False
183
+
184
+
218
185
  def main():
219
- live = '--live' in sys.argv
220
- only = sys.argv[sys.argv.index('--only') + 1].split(',') if '--only' in sys.argv else None
186
+ live, only = parse_args(sys.argv)
221
187
  q = admin('GET', API + '/queue')
222
188
  print(f"{q['today']}: {'paused' if q['paused'] else 'on'}, {len(q['items'])} approved post(s) due · {'LIVE' if live else 'dry run'}")
223
- failures = 0
224
- for item in q['items']:
225
- ch = item['channel']
226
- if only and ch not in only:
227
- continue
228
- try:
229
- media = fetch_media(item)
230
- url, ext = POSTERS[ch](item, media, live)
231
- status = 'posted' if live else 'dry-run'
232
- print(f" {item['slug']} · {ch}: {status} {url or ext}")
233
- admin('POST', API + '/posted', {'slug': item['slug'], 'channel': ch, 'content_sha': item['content_sha'], 'status': status,
234
- 'url': url or '', 'external_id': ext if live else '', 'detail': '' if live else ext, 'run': RUN})
235
- if live:
236
- record_link(item['slug'], ch, url)
237
- except (Failed, KeyError) as err:
238
- failures += 1
239
- print(f" {item['slug']} · {ch}: FAILED {err}")
240
- admin('POST', API + '/posted', {'slug': item['slug'], 'channel': ch, 'content_sha': item['content_sha'], 'status': 'failed', 'detail': str(err)[:1500], 'run': RUN})
189
+ chosen = (item for item in q['items'] if not only or item['channel'] in only)
190
+ failures = sum(not try_post(item, live) for item in chosen)
241
191
  sys.exit(1 if failures else 0)
242
192
 
243
193
 
package/poster/x.py ADDED
@@ -0,0 +1,55 @@
1
+ """X for the poster: request signing, chunked media upload, and the post with its reply thread. Stdlib only."""
2
+ import base64, hashlib, hmac, os, secrets, time, urllib.parse
3
+
4
+ from net import Failed, http, jhttp
5
+
6
+ E = os.environ
7
+
8
+
9
+ def x_auth(method, url, params=None):
10
+ """Authorization header: OAuth 2.0 user token if given, else OAuth 1.0a (JSON / multipart bodies are not signed)."""
11
+ if E.get('X_USER_TOKEN'):
12
+ return {'authorization': f"Bearer {E['X_USER_TOKEN']}"}
13
+ pct = lambda s: urllib.parse.quote(str(s), safe='~')
14
+ o = {'oauth_consumer_key': E['X_API_KEY'], 'oauth_nonce': secrets.token_hex(16), 'oauth_signature_method': 'HMAC-SHA1',
15
+ 'oauth_timestamp': str(int(time.time())), 'oauth_token': E['X_ACCESS_TOKEN'], 'oauth_version': '1.0'}
16
+ base_url, _, query = url.partition('?')
17
+ allp = {**o, **(params or {}), **dict(urllib.parse.parse_qsl(query))}
18
+ base = '&'.join([method, pct(base_url), pct('&'.join(f'{pct(k)}={pct(allp[k])}' for k in sorted(allp)))])
19
+ key = f"{pct(E['X_API_SECRET'])}&{pct(E['X_ACCESS_SECRET'])}"
20
+ o['oauth_signature'] = base64.b64encode(hmac.new(key.encode(), base.encode(), hashlib.sha1).digest()).decode()
21
+ return {'authorization': 'OAuth ' + ', '.join(f'{pct(k)}="{pct(v)}"' for k, v in sorted(o.items()))}
22
+
23
+
24
+ def x_upload(data, mime):
25
+ init = 'https://api.x.com/2/media/upload/initialize'
26
+ mid = jhttp('POST', init, {'media_type': mime, 'total_bytes': len(data), 'media_category': 'tweet_video' if mime.startswith('video') else 'tweet_image'}, x_auth('POST', init))['data']['id']
27
+ chunk = 4 * 1024 * 1024
28
+ for i in range(0, len(data), chunk):
29
+ url = f'https://api.x.com/2/media/upload/{mid}/append'
30
+ boundary = secrets.token_hex(12)
31
+ body = (f'--{boundary}\r\nContent-Disposition: form-data; name="segment_index"\r\n\r\n{i // chunk}\r\n'
32
+ f'--{boundary}\r\nContent-Disposition: form-data; name="media"; filename="chunk"\r\nContent-Type: application/octet-stream\r\n\r\n').encode() + data[i:i + chunk] + f'\r\n--{boundary}--\r\n'.encode()
33
+ http('POST', url, body, {**x_auth('POST', url), 'content-type': f'multipart/form-data; boundary={boundary}'})
34
+ fin = f'https://api.x.com/2/media/upload/{mid}/finalize'
35
+ info = jhttp('POST', fin, None, x_auth('POST', fin)).get('data', {}).get('processing_info')
36
+ while info and info.get('state') in ('pending', 'in_progress'):
37
+ time.sleep(info.get('check_after_secs', 2))
38
+ st = f'https://api.x.com/2/media/upload?command=STATUS&media_id={mid}'
39
+ info = jhttp('GET', st, None, x_auth('GET', st)).get('data', {}).get('processing_info')
40
+ if info and info.get('state') == 'failed':
41
+ raise Failed(f'X could not process the media: {info}')
42
+ return mid
43
+
44
+
45
+ def x_post(item, media, live):
46
+ p = item['post']
47
+ if not live:
48
+ return None, f"would post with {len(media)} file(s) and a {len(p.get('thread', []))}-reply thread: {p['text'][:80]}"
49
+ ids = [x_upload(m['data'], m['type']) for m in media[:1]]
50
+ tweets = 'https://api.x.com/2/tweets'
51
+ first = jhttp('POST', tweets, {'text': p['text'], **({'media': {'media_ids': ids}} if ids else {})}, x_auth('POST', tweets))['data']['id']
52
+ prev = first
53
+ for t in p.get('thread', []):
54
+ prev = jhttp('POST', tweets, {'text': t, 'reply': {'in_reply_to_tweet_id': prev}}, x_auth('POST', tweets))['data']['id']
55
+ return f'https://x.com/i/web/status/{first}', first
package/src/access.ts CHANGED
@@ -24,8 +24,11 @@ export function configurePaths(p: Partial<typeof paths>) { Object.assign(paths,
24
24
  export const isAdminPath = (path: string) => path === paths.admin || path.startsWith(paths.admin + '/') || path.startsWith(paths.api + '/');
25
25
  export const accessConfigured = (c: AccessConfig) => Boolean(c.teamDomain && c.aud);
26
26
 
27
+ /** A host without its scheme or trailing slash, so a team domain and an issuer compare however they are written. */
28
+ const bare = (host: string) => host.replace(/^https?:\/\//, '').replace(/\/$/, '');
29
+
27
30
  async function keys(teamDomain: string, fetchFn: typeof fetch, now: number): Promise<Jwk[]> {
28
- const url = `https://${teamDomain.replace(/^https?:\/\//, '').replace(/\/$/, '')}/cdn-cgi/access/certs`;
31
+ const url = `https://${bare(teamDomain)}/cdn-cgi/access/certs`;
29
32
  if (certs && certs.url === url && now - certs.at < 3_600_000) return certs.keys;
30
33
  const r = await fetchFn(url);
31
34
  if (!r.ok) throw new Error(`Access certs ${r.status}`);
@@ -33,47 +36,80 @@ async function keys(teamDomain: string, fetchFn: typeof fetch, now: number): Pro
33
36
  return certs.keys;
34
37
  }
35
38
 
39
+ type Claims = Record<string, unknown>;
40
+ /** A token split into its parts, header and claims decoded. */
41
+ interface Token { h: string; p: string; s: string; head: Claims; claims: Claims }
42
+ /** What a token is checked against. */
43
+ interface Check { c: AccessConfig; fetchFn: typeof fetch; now: number }
44
+
45
+ /** Why a token is refused before anyone is looked up, in the order they are checked; the first that holds refuses. */
46
+ const REFUSALS: ((t: Token, k: Check) => boolean)[] = [
47
+ // not RS256
48
+ (t) => t.head.alg !== 'RS256',
49
+ // not for this app's audience
50
+ (t, k) => !(Array.isArray(t.claims.aud) ? t.claims.aud : [t.claims.aud]).some((a) => list(k.c.aud).includes(String(a).toLowerCase())),
51
+ // no expiry, or expired
52
+ (t, k) => typeof t.claims.exp !== 'number' || t.claims.exp * 1000 < k.now,
53
+ // issued by another team
54
+ (t, k) => Boolean(t.claims.iss) && bare(String(t.claims.iss)) !== bare(k.c.teamDomain!),
55
+ ];
56
+
57
+ /** The service-token lists, in the order a client id is looked up in them. */
58
+ const MACHINES: [Role, 'stagers' | 'posters'][] = [['stager', 'stagers'], ['poster', 'posters']];
59
+
60
+ /** Who the claims name: a listed person by email, else (only without an email) a listed machine by client id. */
61
+ function named(claims: Claims, c: AccessConfig): Caller | null {
62
+ const email = String(claims.email ?? '').toLowerCase();
63
+ if (email) return list(c.emails).includes(email) ? { role: 'admin', id: email } : null;
64
+ const cn = String(claims.common_name ?? '').toLowerCase();
65
+ const machine = cn ? MACHINES.find(([, ids]) => list(c[ids]).includes(cn)) : undefined;
66
+ return machine ? { role: machine[0], id: cn } : null;
67
+ }
68
+
69
+ /** Whether the token is signed by the team key it names. */
70
+ async function signed(t: Token, k: Check): Promise<boolean> {
71
+ const jwk = (await keys(k.c.teamDomain!, k.fetchFn, k.now)).find((x) => x.kid === t.head.kid);
72
+ if (!jwk) return false;
73
+ const key = await crypto.subtle.importKey('jwk', jwk, { name: 'RSASSA-PKCS1-v1_5', hash: 'SHA-256' }, false, ['verify']);
74
+ return crypto.subtle.verify('RSASSA-PKCS1-v1_5', key, b64(t.s), new TextEncoder().encode(`${t.h}.${t.p}`));
75
+ }
76
+
77
+ /** The caller a token names, once its claims hold and its signature checks out. Throws on a badly encoded token. */
78
+ async function callerOfToken(h: string, p: string, s: string, k: Check): Promise<Caller | null> {
79
+ const t: Token = { h, p, s, head: part(h), claims: part(p) };
80
+ const caller = REFUSALS.some((refuse) => refuse(t, k)) ? null : named(t.claims, k.c);
81
+ // The keys are fetched only for a token that names someone.
82
+ return caller && (await signed(t, k)) ? caller : null;
83
+ }
84
+
36
85
  /** The caller behind a valid Access token for this app, or null. */
37
86
  export async function verifyCaller(token: string | null, c: AccessConfig, fetchFn: typeof fetch = fetch, now = Date.now()): Promise<Caller | null> {
38
87
  if (!token || !accessConfigured(c)) return null;
39
88
  const [h, p, s] = token.split('.');
40
89
  if (!h || !p || !s) return null;
41
- try {
42
- const head = part(h), claims = part(p);
43
- if (head.alg !== 'RS256') return null;
44
- const aud: string[] = Array.isArray(claims.aud) ? claims.aud : [claims.aud];
45
- if (!aud.some((a) => list(c.aud).includes(String(a).toLowerCase()))) return null;
46
- if (typeof claims.exp !== 'number' || claims.exp * 1000 < now) return null;
47
- const team = c.teamDomain!.replace(/^https?:\/\//, '').replace(/\/$/, '');
48
- if (claims.iss && String(claims.iss).replace(/^https?:\/\//, '').replace(/\/$/, '') !== team) return null;
49
- let caller: Caller | null = null;
50
- const email = String(claims.email ?? '').toLowerCase();
51
- const cn = String(claims.common_name ?? '').toLowerCase();
52
- if (email && list(c.emails).includes(email)) caller = { role: 'admin', id: email };
53
- else if (!email && cn && list(c.stagers).includes(cn)) caller = { role: 'stager', id: cn };
54
- else if (!email && cn && list(c.posters).includes(cn)) caller = { role: 'poster', id: cn };
55
- if (!caller) return null;
56
- const jwk = (await keys(c.teamDomain!, fetchFn, now)).find((k) => k.kid === head.kid);
57
- if (!jwk) return null;
58
- const key = await crypto.subtle.importKey('jwk', jwk, { name: 'RSASSA-PKCS1-v1_5', hash: 'SHA-256' }, false, ['verify']);
59
- const ok = await crypto.subtle.verify('RSASSA-PKCS1-v1_5', key, b64(s), new TextEncoder().encode(`${h}.${p}`));
60
- return ok ? caller : null;
61
- } catch {
62
- return null;
63
- }
90
+ return callerOfToken(h, p, s, { c, fetchFn, now }).catch(() => null);
64
91
  }
65
92
 
93
+ /** The endpoints each machine may use, as 'METHOD path' under the API prefix; a path ending in '/' is a prefix. */
94
+ const GRANTS: Record<Exclude<Role, 'admin'>, string[]> = {
95
+ // The producing machine: upload media, stage posts, report what is produced, read and answer motion requests.
96
+ stager: ['PUT media/', 'POST stage', 'POST production', 'GET requests', 'POST requests'],
97
+ // The CI poster: read the approved queue and the posting order, download media, report outcomes.
98
+ poster: ['GET queue', 'POST posted', 'GET media/', 'GET schedule'],
99
+ };
100
+
101
+ const covers = (grant: string, method: string, sub: string) => {
102
+ const [m, path] = grant.split(' ');
103
+ return m === method && (path.endsWith('/') ? sub.startsWith(path) : sub === path);
104
+ };
105
+
66
106
  /** Which roles may use a path (and method). People: everything. Machines: only their own endpoints. */
67
107
  export function allowed(role: Role, path: string, method: string): boolean {
68
108
  if (role === 'admin') return true;
69
- const a = paths.api;
70
- // The producing machine: upload media, stage posts, report what is produced, read and answer motion requests.
71
- if (role === 'stager') return (path.startsWith(`${a}/media/`) && method === 'PUT') || (path === `${a}/stage` && method === 'POST') ||
72
- (path === `${a}/production` && method === 'POST') || (path === `${a}/requests` && (method === 'GET' || method === 'POST'));
73
- // The CI poster: read the approved queue and the posting order, download media, report outcomes.
74
- if (role === 'poster') return (path === `${a}/queue` && method === 'GET') || (path === `${a}/posted` && method === 'POST') ||
75
- (path.startsWith(`${a}/media/`) && method === 'GET') || (path === `${a}/schedule` && method === 'GET');
76
- return false;
109
+ const prefix = `${paths.api}/`;
110
+ if (!Object.hasOwn(GRANTS, role) || !path.startsWith(prefix)) return false;
111
+ const sub = path.slice(prefix.length);
112
+ return GRANTS[role as keyof typeof GRANTS].some((g) => covers(g, method, sub));
77
113
  }
78
114
 
79
115
  /** for tests: forget the cached keys */
package/src/api.ts CHANGED
@@ -1,10 +1,16 @@
1
1
  // The desk's HTTP API as plain Web Request → Response, so it runs in any server that speaks fetch (SvelteKit, a
2
2
  // Cloudflare Worker, Hono, Next.js route handlers, Bun, Deno). Machines use it: the producing machine stages posts and
3
3
  // uploads media; the CI poster reads the approved queue and reports outcomes. People use the desk page, which calls
4
- // the `desk` actions below from its own form handlers.
5
- import { channelState, CHANNELS, checkSignature, due, effectiveDates, isChannel, makeNext, sha256, signMedia, validKey, type Channel, type Production, type StagedPost } from './rules.js';
4
+ // the `desk` actions below from its own form handlers. The handlers live in ./desk/machine.ts and ./desk/page.ts.
5
+ import { CHANNELS, type Channel } from './rules.js';
6
6
  import type { Caller } from './access.js';
7
7
  import type { MediaStore, PostingStore } from './store.js';
8
+ import type { DeskContext } from './desk/context.js';
9
+ import { HttpError, json, refusal } from './desk/http.js';
10
+ import * as machine from './desk/machine.js';
11
+ import * as page from './desk/page.js';
12
+
13
+ export { HttpError };
8
14
 
9
15
  /** One piece the desk knows about: its slug, its date as built, and (optionally) its kind. */
10
16
  export interface Piece { slug: string; date: string; title?: string; video?: 'short' | 'wide' | string }
@@ -28,198 +34,70 @@ export interface DeskOptions {
28
34
  channelsFor?: (p: Piece) => Channel[];
29
35
  }
30
36
 
31
- const TYPES: Record<string, string> = { mp4: 'video/mp4', jpg: 'image/jpeg', jpeg: 'image/jpeg', png: 'image/png', webp: 'image/webp', srt: 'text/plain; charset=utf-8', vtt: 'text/vtt' };
32
- const MAX = 95 * 1024 * 1024; // under the Workers request-body limit
33
-
34
- export class HttpError extends Error { constructor(public status: number, message: string) { super(message); } }
35
- const json = (data: unknown, status = 200) => new Response(JSON.stringify(data), { status, headers: { 'content-type': 'application/json' } });
36
- const fail = (status: number, reason: string): never => { throw new HttpError(status, reason); };
37
-
38
- export function createDesk(o: DeskOptions) {
39
- const { store, media } = o;
40
- const expected = o.channelsFor ?? ((p: Piece) => (p.video === 'short' ? [...CHANNELS] : (['youtube', 'instagram_reel', 'x'] as Channel[])));
41
- const slugs = async () => new Set((await o.pieces()).map((p) => p.slug));
42
-
43
- /** The API under the admin prefix: `sub` is the path after it ('queue', 'media/<slug>/<file>', ...). */
44
- async function api(request: Request, caller: Caller, sub: string): Promise<Response> {
45
- try {
46
- const m = request.method;
47
- if (sub === 'queue' && m === 'GET') return json(await queue(new URL(request.url).origin));
48
- if (sub === 'posted' && m === 'POST') return json(await posted(caller, await request.json()));
49
- if (sub === 'stage' && m === 'POST') return json(await stage(caller, await request.json()));
50
- if (sub === 'production' && m === 'POST') return json(await production(await request.json()));
51
- if (sub === 'schedule' && m === 'GET') return json(await schedule());
52
- if (sub === 'requests' && m === 'GET') return json(await openRequests());
53
- if (sub === 'requests' && m === 'POST') return json(await answerRequest(caller, await request.json()));
54
- if (sub.startsWith('media/')) {
55
- const key = sub.slice(6);
56
- if (m === 'GET') return await getMedia(key);
57
- if (m === 'PUT') return json(await putMedia(caller, key, request));
58
- }
59
- return json({ ok: false, reason: 'Not found' }, 404);
60
- } catch (e) {
61
- if (e instanceof HttpError) return json({ ok: false, reason: e.message }, e.status);
62
- throw e;
63
- }
64
- }
65
-
66
- /** What is approved and due now, with short-lived media links. Empty while posting is paused. */
67
- async function queue(origin: string) {
68
- const all = await store.readAll(), t = o.today();
69
- const planned = new Map(all.schedule.map((s) => [s.slug, s.publish_date]));
70
- const pieces = await o.pieces();
71
- const rows = pieces.map((a) => ({
72
- slug: a.slug, date: a.date, planned: planned.get(a.slug),
73
- staged: all.staged.filter((s) => s.slug === a.slug), approvals: all.approvals.filter((x) => x.slug === a.slug), posts: all.posts.filter((p) => p.slug === a.slug),
74
- }));
75
- const base = o.mediaOrigin || origin;
76
- return {
77
- paused: all.paused, today: t,
78
- items: await Promise.all(due(rows, t, all.paused).map(async (s) => ({
79
- slug: s.slug, channel: s.channel, content_sha: s.content_sha, post: s.post, article: o.pieceUrl(s.slug),
80
- video: pieces.find((a) => a.slug === s.slug)?.video ?? 'short',
81
- media: await Promise.all(s.post.media.map(async (x) => ({ ...x, url: o.mediaSecret ? await signMedia(x.key, o.mediaSecret, base, 3600, Date.now(), o.mediaPath ?? '/m') : null }))),
82
- }))),
83
- };
84
- }
85
-
86
- async function posted(caller: Caller, b: Record<string, string>) {
87
- if (!b.slug || !isChannel(b.channel) || !b.content_sha || !['posted', 'dry-run', 'failed'].includes(b.status)) fail(400, 'slug, channel, content_sha and status are required');
88
- await store.recordPost(caller.id, { slug: b.slug, channel: b.channel as Channel, content_sha: b.content_sha, status: b.status as 'posted', url: b.url, external_id: b.external_id, detail: b.detail?.slice(0, 2000), run: b.run });
89
- return { ok: true };
90
- }
91
-
92
- /** The post for one piece and channel, as it will go out. Every media file must already be uploaded, same sha256. */
93
- async function stage(caller: Caller, b: { slug: string; channel: string; post: StagedPost }) {
94
- if (!(await slugs()).has(b.slug)) fail(400, `No piece "${b.slug}" in the index`);
95
- if (!isChannel(b.channel) || !b.post || !Array.isArray(b.post.media)) fail(400, 'channel and post.media are required');
96
- for (const x of b.post.media) {
97
- if (!validKey(x.key) || !x.key.startsWith(`${b.slug}/`)) fail(400, `Bad media key ${x.key}`);
98
- const head = await media.head(x.key);
99
- if (!head || head.sha256 !== x.sha256) fail(400, `${x.key} is not uploaded with that sha256`);
100
- }
101
- return { ok: true, content_sha: await store.stage(caller.id, b.slug, b.channel as Channel, b.post) };
102
- }
103
-
104
- async function production(b: { items: { slug: string; script?: string; status: Production }[] }) {
105
- const known = await slugs();
106
- let saved = 0;
107
- for (const it of b.items ?? []) {
108
- if (!known.has(it.slug)) continue;
109
- await store.saveProduction(it.slug, (it.script ?? '').slice(0, 60000) || null, it.status ?? {});
110
- saved++;
111
- }
112
- return { ok: true, saved };
113
- }
114
-
115
- /** Dates set on the desk that differ from the build: the schedule job writes them into index.json. */
116
- async function schedule() {
117
- const built = new Map((await o.pieces()).map((a) => [a.slug, a.date]));
118
- return { changes: (await store.readSchedule()).filter((s) => built.has(s.slug) && built.get(s.slug) !== s.publish_date) };
119
- }
120
-
121
- async function openRequests() {
122
- const all = await store.readRequests();
123
- return { requests: all.filter((r) => !r.parent && r.status !== 'done').map((r) => ({ ...r, thread: all.filter((x) => x.parent === r.id) })) };
124
- }
125
-
126
- async function answerRequest(caller: Caller, b: { id: number; reply?: string; status?: string }) {
127
- const parent = (await store.readRequests()).find((r) => r.id === Number(b.id) && !r.parent);
128
- if (!parent) fail(404, 'No such request');
129
- if (b.reply) await store.addRequest(caller.id, parent!.slug, 'note', b.reply, parent!.id);
130
- if (b.status && ['open', 'working', 'done'].includes(b.status)) await store.setRequestStatus(caller.id, parent!.id, b.status);
131
- return { ok: true };
132
- }
133
-
134
- async function getMedia(key: string) {
135
- if (!validKey(key)) fail(400, 'Bad key');
136
- const obj = await media.get(key);
137
- if (!obj) fail(404, 'Not found');
138
- return new Response(obj!.body, { headers: { 'content-type': obj!.contentType ?? 'application/octet-stream', 'content-length': String(obj!.size), 'x-content-sha256': obj!.sha256 ?? '' } });
139
- }
37
+ /** A route of the API under the admin prefix. `rest` is the path after the route's prefix (for media: the key). */
38
+ interface Route {
39
+ method: string;
40
+ /** The exact path after the admin prefix ('queue'), or a prefix when it ends in '/' ('media/'). */
41
+ path: string;
42
+ handle: (d: DeskContext, request: Request, caller: Caller, rest: string) => Promise<Response>;
43
+ }
140
44
 
141
- async function putMedia(caller: Caller, key: string, request: Request) {
142
- if (!validKey(key)) fail(400, 'Bad key: <slug>/<file>, lower-case');
143
- const want = (request.headers.get('x-content-sha256') ?? '').toLowerCase();
144
- const body = await request.arrayBuffer();
145
- if (body.byteLength > MAX) fail(413, 'Too large');
146
- const got = await sha256(body);
147
- if (!want || got !== want) fail(400, `sha256 mismatch (got ${got})`);
148
- await media.put(key, body, { contentType: TYPES[key.split('.').pop() ?? ''] ?? 'application/octet-stream', sha256: got });
149
- await store.audit(caller.id, 'upload', key.split('/')[0], null, `${key} ${got.slice(0, 12)}`);
150
- return { ok: true, key, sha256: got, bytes: body.byteLength };
45
+ const answer = async (data: Promise<unknown>) => json(await data);
46
+ // The body is parsed only once the route is known, so an unknown route never reads it.
47
+ const body = <T>(request: Request) => request.json() as Promise<T>;
48
+
49
+ const ROUTES: Route[] = [
50
+ { method: 'GET', path: 'queue', handle: (d, r) => answer(machine.queue(d, new URL(r.url).origin)) },
51
+ { method: 'POST', path: 'posted', handle: async (d, r, c) => answer(machine.posted(d, c, await body(r))) },
52
+ { method: 'POST', path: 'stage', handle: async (d, r, c) => answer(machine.stage(d, c, await body(r))) },
53
+ { method: 'POST', path: 'production', handle: async (d, r) => answer(machine.production(d, await body(r))) },
54
+ { method: 'GET', path: 'schedule', handle: (d) => answer(machine.schedule(d)) },
55
+ { method: 'GET', path: 'requests', handle: (d) => answer(machine.openRequests(d)) },
56
+ { method: 'POST', path: 'requests', handle: async (d, r, c) => answer(machine.answerRequest(d, c, await body(r))) },
57
+ { method: 'GET', path: 'media/', handle: (d, _r, _c, key) => machine.getMedia(d, key) },
58
+ { method: 'PUT', path: 'media/', handle: (d, r, c, key) => answer(machine.putMedia(d, c, key, r)) },
59
+ ];
60
+
61
+ const isPrefix = (route: Route) => route.path.endsWith('/');
62
+ const matches = (route: Route, method: string, sub: string) =>
63
+ route.method === method && (isPrefix(route) ? sub.startsWith(route.path) : sub === route.path);
64
+ const restOf = (route: Route, sub: string) => (isPrefix(route) ? sub.slice(route.path.length) : '');
65
+
66
+ /** The API under the admin prefix: `sub` is the path after it ('queue', 'media/<slug>/<file>', ...). */
67
+ async function api(d: DeskContext, request: Request, caller: Caller, sub: string): Promise<Response> {
68
+ const route = ROUTES.find((r) => matches(r, request.method, sub));
69
+ try {
70
+ return route ? await route.handle(d, request, caller, restOf(route, sub)) : refusal(404, 'Not found');
71
+ } catch (e) {
72
+ if (e instanceof HttpError) return refusal(e.status, e.message);
73
+ throw e;
151
74
  }
75
+ }
152
76
 
153
- /** /m/<key>?exp=…&sig=…: one media file, public only with a valid, unexpired signature. The bucket stays private. */
154
- async function signedMedia(key: string, url: URL): Promise<Response> {
155
- const notFound = new Response('Not found', { status: 404 });
156
- if (!o.mediaSecret || !validKey(key)) return notFound;
157
- if (!(await checkSignature(key, url.searchParams.get('exp') ?? '', url.searchParams.get('sig') ?? '', o.mediaSecret))) return notFound;
158
- const obj = await media.get(key);
159
- if (!obj) return notFound;
160
- return new Response(obj.body, { headers: { 'content-type': obj.contentType ?? 'application/octet-stream', 'content-length': String(obj.size), 'cache-control': 'private, no-store', 'x-robots-tag': 'noindex' } });
161
- }
77
+ const defaultChannels = (p: Piece): Channel[] => (p.video === 'short' ? [...CHANNELS] : ['youtube', 'instagram_reel', 'x']);
162
78
 
163
- /** The desk page's data: every piece in date order with each channel's state, production and requests. */
164
- async function plan() {
165
- const t = o.today(), all = await store.readAll(), prod = await store.readProduction(), requests = await store.readRequests();
166
- const pieces = await o.pieces(), dates = effectiveDates(pieces, all.schedule);
167
- const rows = pieces.map((a) => {
168
- const date = dates.get(a.slug)!;
169
- const channels = expected(a).map((c) => {
170
- const staged = all.staged.find((s) => s.slug === a.slug && s.channel === c);
171
- const posts = all.posts.filter((p) => p.slug === a.slug && p.channel === c);
172
- return {
173
- channel: c, state: channelState(staged, all.approvals.filter((x) => x.slug === a.slug && x.channel === c), posts),
174
- staged: staged ?? null, url: posts.filter((p) => p.status === 'posted').at(-1)?.url ?? null, last: posts.at(-1) ?? null,
175
- };
176
- });
177
- const p = prod.find((x) => x.slug === a.slug);
178
- return {
179
- ...a, date, built: a.date, moved: date !== a.date, live: a.date <= t, channels,
180
- script: p?.script ?? null, status: p?.status ?? {}, reported: p?.updated_at ?? null,
181
- requests: requests.filter((r) => r.slug === a.slug && !r.parent).map((r) => ({ ...r, thread: requests.filter((x) => x.parent === r.id) })),
182
- };
183
- }).sort((x, y) => x.date.localeCompare(y.date) || x.slug.localeCompare(y.slug));
184
- return { today: t, paused: all.paused, plan: rows, next: rows.find((r) => r.date >= t && r.channels.some((c) => c.state !== 'posted')) ?? null, audit: await store.recentAudit() };
185
- }
79
+ export function createDesk(o: DeskOptions) {
80
+ const d: DeskContext = {
81
+ o, store: o.store, media: o.media, expected: o.channelsFor ?? defaultChannels,
82
+ slugs: async () => new Set((await o.pieces()).map((p) => p.slug)),
83
+ };
186
84
 
187
85
  /** What a person does on the desk page. Each returns { ok } or { error } with a status. */
188
- const desk = {
189
- plan,
190
- async approve(who: string, slug: string, items: string[]) {
191
- let n = 0;
192
- for (const item of items) { const [c, sha] = item.split(':'); if (isChannel(c) && (await store.approve(who, slug, c, sha))) n++; }
193
- return n === items.length ? { ok: `Approved ${n}` } : { status: 409, error: `Approved ${n} of ${items.length}: the rest changed since this page loaded. Reload and check again.` };
194
- },
195
- async revoke(who: string, slug: string, channel: string) {
196
- if (!isChannel(channel)) return { status: 400, error: 'Bad channel' };
197
- await store.revoke(who, slug, channel);
198
- return { ok: 'Approval withdrawn' };
199
- },
200
- async makeNext(who: string, slug: string) {
201
- const all = await store.readAll();
202
- const moved = makeNext(effectiveDates(await o.pieces(), all.schedule), slug, o.today());
203
- if (!moved.size) return { status: 400, error: 'That piece is already next, or already published.' };
204
- await store.setDates(who, moved, `make next: ${slug}`);
205
- return { ok: `Moved ${slug} to ${moved.get(slug)}; ${moved.size - 1} other piece(s) moved down one slot.` };
206
- },
207
- async request(who: string, slug: string, kind: string, body: string) {
208
- if (!(await slugs()).has(slug)) return { status: 400, error: 'No such piece' };
209
- if (!body.trim() && kind !== 'generate') return { status: 400, error: 'Say what to change' };
210
- await store.addRequest(who, slug, ['generate', 'edit', 'note'].includes(kind) ? kind : 'edit', body.trim() || 'Generate the motion for this piece from its script.');
211
- return { ok: 'Sent. Replies appear in the thread.' };
212
- },
213
- async reply(who: string, id: number, body: string, status?: string) {
214
- const parent = (await store.readRequests()).find((r) => r.id === id && !r.parent);
215
- if (!parent) return { status: 404, error: 'No such request' };
216
- if (body.trim()) await store.addRequest(who, parent.slug, 'note', body.trim(), id);
217
- if (status) await store.setRequestStatus(who, id, status);
218
- return { ok: 'Saved' };
219
- },
220
- async pause(who: string) { await store.setPaused(who, true); return { ok: 'Posting paused' }; },
221
- async resume(who: string) { await store.setPaused(who, false); return { ok: 'Posting resumed' }; },
86
+ const desk: page.DeskActions = {
87
+ plan: () => page.plan(d),
88
+ approve: (who: string, slug: string, items: string[]) => page.approve(d, who, slug, items),
89
+ revoke: (who: string, slug: string, channel: string) => page.revoke(d, who, slug, channel),
90
+ makeNext: (who: string, slug: string) => page.makeNext(d, who, slug),
91
+ request: (who: string, slug: string, kind: string, body: string) => page.request(d, who, slug, kind, body),
92
+ reply: (who: string, id: number, body: string, status?: string) => page.reply(d, who, id, body, status),
93
+ pause: (who: string) => page.pause(d, who),
94
+ resume: (who: string) => page.resume(d, who),
222
95
  };
223
96
 
224
- return { api, signedMedia, desk, queue };
97
+ return {
98
+ api: (request: Request, caller: Caller, sub: string) => api(d, request, caller, sub),
99
+ signedMedia: (key: string, url: URL) => machine.signedMedia(d, key, url),
100
+ desk,
101
+ queue: (origin: string) => machine.queue(d, origin),
102
+ };
225
103
  }