linkedin-relay-mcp 1.1.0 → 1.2.1
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/.claude/skills/linkedin-relay/SKILL.md +8 -4
- package/README.md +10 -6
- package/dist/cli.js +540 -299
- package/dist/cli.js.map +1 -1
- package/dist/index.js +11 -2
- package/dist/index.js.map +1 -1
- package/dist/mcp-shim.js +22 -6
- package/dist/mcp-shim.js.map +1 -1
- package/package.json +1 -1
package/dist/cli.js
CHANGED
|
@@ -1,13 +1,53 @@
|
|
|
1
1
|
#!/usr/bin/env bun
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
};
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
2
|
+
// src/args.ts
|
|
3
|
+
function parseArgs(argv) {
|
|
4
|
+
const [command, ...rest] = argv;
|
|
5
|
+
const positionals = [];
|
|
6
|
+
const flags = {};
|
|
7
|
+
for (let i = 0; i < rest.length; i++) {
|
|
8
|
+
const token = rest[i];
|
|
9
|
+
if (token === void 0) continue;
|
|
10
|
+
if (token.startsWith("--")) {
|
|
11
|
+
const key = token.slice(2);
|
|
12
|
+
const next = rest[i + 1];
|
|
13
|
+
if (next !== void 0 && !next.startsWith("--")) {
|
|
14
|
+
flags[key] = next;
|
|
15
|
+
i++;
|
|
16
|
+
} else {
|
|
17
|
+
flags[key] = true;
|
|
18
|
+
}
|
|
19
|
+
} else if (token.startsWith("-") && token.length > 1) {
|
|
20
|
+
const key = token.slice(1);
|
|
21
|
+
const next = rest[i + 1];
|
|
22
|
+
if (next !== void 0 && !next.startsWith("-")) {
|
|
23
|
+
flags[key] = next;
|
|
24
|
+
i++;
|
|
25
|
+
} else {
|
|
26
|
+
flags[key] = true;
|
|
27
|
+
}
|
|
28
|
+
} else {
|
|
29
|
+
positionals.push(token);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
return { command, positionals, flags };
|
|
33
|
+
}
|
|
34
|
+
function bool(args, name) {
|
|
35
|
+
return args.flags[name] === true || args.flags[name] === "true";
|
|
36
|
+
}
|
|
37
|
+
function str(args, name) {
|
|
38
|
+
const v = args.flags[name];
|
|
39
|
+
return typeof v === "string" ? v : void 0;
|
|
40
|
+
}
|
|
41
|
+
function num(args, name) {
|
|
42
|
+
const v = str(args, name);
|
|
43
|
+
if (v === void 0) return void 0;
|
|
44
|
+
const n = Number(v);
|
|
45
|
+
return Number.isFinite(n) ? n : void 0;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// src/cache/db.ts
|
|
49
|
+
import { Database } from "bun:sqlite";
|
|
50
|
+
import { mkdirSync as mkdirSync2, renameSync as renameSync2 } from "fs";
|
|
11
51
|
|
|
12
52
|
// src/cache/store.ts
|
|
13
53
|
import { existsSync, mkdirSync, readFileSync, renameSync, writeFileSync } from "fs";
|
|
@@ -49,34 +89,46 @@ function saveJson(path, value) {
|
|
|
49
89
|
writeFileSync(tmp, JSON.stringify(value, null, 2), { encoding: "utf-8", mode: 384 });
|
|
50
90
|
renameSync(tmp, path);
|
|
51
91
|
}
|
|
52
|
-
var init_store = __esm({
|
|
53
|
-
"src/cache/store.ts"() {
|
|
54
|
-
"use strict";
|
|
55
|
-
}
|
|
56
|
-
});
|
|
57
92
|
|
|
58
93
|
// src/cache/db.ts
|
|
59
|
-
var
|
|
60
|
-
|
|
61
|
-
CacheCorrupt: () => CacheCorrupt,
|
|
62
|
-
THIRD_PARTY_TTL_MS: () => THIRD_PARTY_TTL_MS,
|
|
63
|
-
count: () => count,
|
|
64
|
-
fold: () => fold,
|
|
65
|
-
getCheckpoint: () => getCheckpoint,
|
|
66
|
-
openDb: () => openDb,
|
|
67
|
-
purge: () => purge,
|
|
68
|
-
removeUrns: () => removeUrns,
|
|
69
|
-
search: () => search,
|
|
70
|
-
setCheckpoint: () => setCheckpoint,
|
|
71
|
-
sweepExpired: () => sweepExpired,
|
|
72
|
-
upsert: () => upsert,
|
|
73
|
-
urnsFor: () => urnsFor
|
|
74
|
-
});
|
|
75
|
-
import { Database } from "bun:sqlite";
|
|
76
|
-
import { mkdirSync as mkdirSync2, renameSync as renameSync2 } from "fs";
|
|
94
|
+
var DB_FILE = "cache.db";
|
|
95
|
+
var THIRD_PARTY_TTL_MS = 30 * 864e5;
|
|
77
96
|
function fold(value) {
|
|
78
97
|
return value.normalize("NFD").replace(new RegExp("\\p{Diacritic}", "gu"), "").toLowerCase();
|
|
79
98
|
}
|
|
99
|
+
var TABLES = [
|
|
100
|
+
`CREATE TABLE IF NOT EXISTS records (
|
|
101
|
+
urn TEXT NOT NULL,
|
|
102
|
+
source TEXT NOT NULL,
|
|
103
|
+
body TEXT,
|
|
104
|
+
text TEXT NOT NULL DEFAULT '',
|
|
105
|
+
textFold TEXT NOT NULL DEFAULT '',
|
|
106
|
+
firstSeenAt INTEGER NOT NULL,
|
|
107
|
+
updatedAt INTEGER NOT NULL,
|
|
108
|
+
expiresAt INTEGER,
|
|
109
|
+
PRIMARY KEY (urn, source)
|
|
110
|
+
)`,
|
|
111
|
+
`CREATE TABLE IF NOT EXISTS checkpoints (
|
|
112
|
+
source TEXT PRIMARY KEY,
|
|
113
|
+
lastSuccessfulAt INTEGER NOT NULL,
|
|
114
|
+
newestCreatedAt INTEGER,
|
|
115
|
+
headIds TEXT NOT NULL DEFAULT '[]',
|
|
116
|
+
state TEXT NOT NULL DEFAULT 'ok'
|
|
117
|
+
)`
|
|
118
|
+
];
|
|
119
|
+
var INDEXES = [
|
|
120
|
+
"CREATE INDEX IF NOT EXISTS records_source ON records(source)",
|
|
121
|
+
"CREATE INDEX IF NOT EXISTS records_fold ON records(textFold)",
|
|
122
|
+
"CREATE INDEX IF NOT EXISTS records_expires ON records(expiresAt)"
|
|
123
|
+
];
|
|
124
|
+
var CacheCorrupt = class extends Error {
|
|
125
|
+
constructor(message, quarantinedTo) {
|
|
126
|
+
super(message);
|
|
127
|
+
this.quarantinedTo = quarantinedTo;
|
|
128
|
+
this.name = "CacheCorrupt";
|
|
129
|
+
}
|
|
130
|
+
quarantinedTo;
|
|
131
|
+
};
|
|
80
132
|
function openDb(now = Date.now()) {
|
|
81
133
|
mkdirSync2(cacheDir(), { recursive: true });
|
|
82
134
|
const path = cachePath(DB_FILE);
|
|
@@ -216,97 +268,6 @@ function purge(db, scope) {
|
|
|
216
268
|
if (scope === "all") db.run("DELETE FROM checkpoints");
|
|
217
269
|
return result.changes;
|
|
218
270
|
}
|
|
219
|
-
var DB_FILE, THIRD_PARTY_TTL_MS, TABLES, INDEXES, CacheCorrupt;
|
|
220
|
-
var init_db = __esm({
|
|
221
|
-
"src/cache/db.ts"() {
|
|
222
|
-
"use strict";
|
|
223
|
-
init_store();
|
|
224
|
-
DB_FILE = "cache.db";
|
|
225
|
-
THIRD_PARTY_TTL_MS = 30 * 864e5;
|
|
226
|
-
TABLES = [
|
|
227
|
-
`CREATE TABLE IF NOT EXISTS records (
|
|
228
|
-
urn TEXT NOT NULL,
|
|
229
|
-
source TEXT NOT NULL,
|
|
230
|
-
body TEXT,
|
|
231
|
-
text TEXT NOT NULL DEFAULT '',
|
|
232
|
-
textFold TEXT NOT NULL DEFAULT '',
|
|
233
|
-
firstSeenAt INTEGER NOT NULL,
|
|
234
|
-
updatedAt INTEGER NOT NULL,
|
|
235
|
-
expiresAt INTEGER,
|
|
236
|
-
PRIMARY KEY (urn, source)
|
|
237
|
-
)`,
|
|
238
|
-
`CREATE TABLE IF NOT EXISTS checkpoints (
|
|
239
|
-
source TEXT PRIMARY KEY,
|
|
240
|
-
lastSuccessfulAt INTEGER NOT NULL,
|
|
241
|
-
newestCreatedAt INTEGER,
|
|
242
|
-
headIds TEXT NOT NULL DEFAULT '[]',
|
|
243
|
-
state TEXT NOT NULL DEFAULT 'ok'
|
|
244
|
-
)`
|
|
245
|
-
];
|
|
246
|
-
INDEXES = [
|
|
247
|
-
"CREATE INDEX IF NOT EXISTS records_source ON records(source)",
|
|
248
|
-
"CREATE INDEX IF NOT EXISTS records_fold ON records(textFold)",
|
|
249
|
-
"CREATE INDEX IF NOT EXISTS records_expires ON records(expiresAt)"
|
|
250
|
-
];
|
|
251
|
-
CacheCorrupt = class extends Error {
|
|
252
|
-
constructor(message, quarantinedTo) {
|
|
253
|
-
super(message);
|
|
254
|
-
this.quarantinedTo = quarantinedTo;
|
|
255
|
-
this.name = "CacheCorrupt";
|
|
256
|
-
}
|
|
257
|
-
quarantinedTo;
|
|
258
|
-
};
|
|
259
|
-
}
|
|
260
|
-
});
|
|
261
|
-
|
|
262
|
-
// src/args.ts
|
|
263
|
-
function parseArgs(argv) {
|
|
264
|
-
const [command, ...rest] = argv;
|
|
265
|
-
const positionals = [];
|
|
266
|
-
const flags = {};
|
|
267
|
-
for (let i = 0; i < rest.length; i++) {
|
|
268
|
-
const token = rest[i];
|
|
269
|
-
if (token === void 0) continue;
|
|
270
|
-
if (token.startsWith("--")) {
|
|
271
|
-
const key = token.slice(2);
|
|
272
|
-
const next = rest[i + 1];
|
|
273
|
-
if (next !== void 0 && !next.startsWith("--")) {
|
|
274
|
-
flags[key] = next;
|
|
275
|
-
i++;
|
|
276
|
-
} else {
|
|
277
|
-
flags[key] = true;
|
|
278
|
-
}
|
|
279
|
-
} else if (token.startsWith("-") && token.length > 1) {
|
|
280
|
-
const key = token.slice(1);
|
|
281
|
-
const next = rest[i + 1];
|
|
282
|
-
if (next !== void 0 && !next.startsWith("-")) {
|
|
283
|
-
flags[key] = next;
|
|
284
|
-
i++;
|
|
285
|
-
} else {
|
|
286
|
-
flags[key] = true;
|
|
287
|
-
}
|
|
288
|
-
} else {
|
|
289
|
-
positionals.push(token);
|
|
290
|
-
}
|
|
291
|
-
}
|
|
292
|
-
return { command, positionals, flags };
|
|
293
|
-
}
|
|
294
|
-
function bool(args, name) {
|
|
295
|
-
return args.flags[name] === true || args.flags[name] === "true";
|
|
296
|
-
}
|
|
297
|
-
function str(args, name) {
|
|
298
|
-
const v = args.flags[name];
|
|
299
|
-
return typeof v === "string" ? v : void 0;
|
|
300
|
-
}
|
|
301
|
-
function num(args, name) {
|
|
302
|
-
const v = str(args, name);
|
|
303
|
-
if (v === void 0) return void 0;
|
|
304
|
-
const n = Number(v);
|
|
305
|
-
return Number.isFinite(n) ? n : void 0;
|
|
306
|
-
}
|
|
307
|
-
|
|
308
|
-
// src/commands/cache.ts
|
|
309
|
-
init_db();
|
|
310
271
|
|
|
311
272
|
// src/engine/restli.ts
|
|
312
273
|
var ENCODED = {
|
|
@@ -728,9 +689,6 @@ function retain(source, items) {
|
|
|
728
689
|
}
|
|
729
690
|
}
|
|
730
691
|
|
|
731
|
-
// src/engine/index.ts
|
|
732
|
-
init_store();
|
|
733
|
-
|
|
734
692
|
// src/progress.ts
|
|
735
693
|
function progressReporter(quiet, write = (s) => {
|
|
736
694
|
process.stderr.write(s);
|
|
@@ -938,9 +896,10 @@ function classify(res) {
|
|
|
938
896
|
if (res.status === 404) {
|
|
939
897
|
return fail("NOT_FOUND", "no such resource");
|
|
940
898
|
}
|
|
941
|
-
if (res.status
|
|
899
|
+
if (res.status < 200 || res.status >= 300) {
|
|
942
900
|
return fail("FETCH_FAILED", `unexpected status ${res.status}`);
|
|
943
901
|
}
|
|
902
|
+
if (res.body.trim() === "") return { outcome: "ok", retry: false, json: null };
|
|
944
903
|
return classifyBody(res.body);
|
|
945
904
|
}
|
|
946
905
|
function referencesUrns(data) {
|
|
@@ -1021,10 +980,13 @@ function createClient(session, deps) {
|
|
|
1021
980
|
return { request };
|
|
1022
981
|
}
|
|
1023
982
|
async function fetchRaw(spec, session, deps) {
|
|
983
|
+
const requestHeaders = buildHeaders(session);
|
|
984
|
+
if (spec.body !== void 0) requestHeaders["content-type"] = "application/json; charset=UTF-8";
|
|
1024
985
|
try {
|
|
1025
986
|
const res = await deps.fetch(spec.url, {
|
|
1026
987
|
method: spec.method ?? "GET",
|
|
1027
|
-
headers:
|
|
988
|
+
headers: requestHeaders,
|
|
989
|
+
...spec.body === void 0 ? {} : { body: JSON.stringify(spec.body) },
|
|
1028
990
|
// Manual, so a login or checkpoint redirect is classified rather than
|
|
1029
991
|
// silently followed into an HTML page that parses as "no data".
|
|
1030
992
|
redirect: "manual"
|
|
@@ -1369,8 +1331,8 @@ function missingContract(name) {
|
|
|
1369
1331
|
hint: "Only endpoints that returned 200 on this machine may back a command. Capture it with `bun run scripts/observe.ts`, verify it, then mark it verified in src/engine/contracts.ts."
|
|
1370
1332
|
};
|
|
1371
1333
|
}
|
|
1372
|
-
function
|
|
1373
|
-
|
|
1334
|
+
function createLiveClient(session, deps, quiet = false) {
|
|
1335
|
+
return createClient(session, {
|
|
1374
1336
|
fetch: globalThis.fetch,
|
|
1375
1337
|
// stdout stays JSON-only; progress is human chatter and goes to stderr.
|
|
1376
1338
|
progress: progressReporter(quiet),
|
|
@@ -1389,6 +1351,9 @@ function createEngine(session, deps, quiet = false) {
|
|
|
1389
1351
|
saveCooldown: (c) => saveJson(cachePath("cooldown.json"), c),
|
|
1390
1352
|
...deps
|
|
1391
1353
|
});
|
|
1354
|
+
}
|
|
1355
|
+
function createEngine(session, deps, quiet = false) {
|
|
1356
|
+
const client = createLiveClient(session, deps, quiet);
|
|
1392
1357
|
return {
|
|
1393
1358
|
async whoami() {
|
|
1394
1359
|
const contract = contractFor("me");
|
|
@@ -1568,7 +1533,6 @@ function totalResultCount(json) {
|
|
|
1568
1533
|
}
|
|
1569
1534
|
|
|
1570
1535
|
// src/engine/session.ts
|
|
1571
|
-
init_store();
|
|
1572
1536
|
var SESSION_FILE = "session.json";
|
|
1573
1537
|
function loadSession() {
|
|
1574
1538
|
const result = loadJson(cachePath(SESSION_FILE));
|
|
@@ -1654,6 +1618,247 @@ function getAllCookies(wsUrl) {
|
|
|
1654
1618
|
});
|
|
1655
1619
|
}
|
|
1656
1620
|
|
|
1621
|
+
// src/engine/voyager-write.ts
|
|
1622
|
+
var VOYAGER2 = "https://www.linkedin.com/voyager/api";
|
|
1623
|
+
var SHARE_URL = `${VOYAGER2}/contentcreation/normShares`;
|
|
1624
|
+
function sharePayload(text2, visibility) {
|
|
1625
|
+
return {
|
|
1626
|
+
visibleToConnectionsOnly: visibility === "CONNECTIONS",
|
|
1627
|
+
externalAudienceProviders: [],
|
|
1628
|
+
commentaryV2: { text: text2, attributes: [] },
|
|
1629
|
+
origin: "FEED",
|
|
1630
|
+
allowedCommentersScope: "ALL",
|
|
1631
|
+
postState: "PUBLISHED",
|
|
1632
|
+
media: []
|
|
1633
|
+
};
|
|
1634
|
+
}
|
|
1635
|
+
var POST_URN = /urn:li:(share|activity|ugcPost):\d+/;
|
|
1636
|
+
function isDeletableUrn(urn) {
|
|
1637
|
+
return new RegExp(`^${POST_URN.source}$`).test(urn);
|
|
1638
|
+
}
|
|
1639
|
+
function deleteUrl(urn) {
|
|
1640
|
+
return `${SHARE_URL}/${encodeURIComponent(urn)}`;
|
|
1641
|
+
}
|
|
1642
|
+
async function deletePost(confirmed, client) {
|
|
1643
|
+
const { urn } = confirmed.payload;
|
|
1644
|
+
const result = await client.request({
|
|
1645
|
+
url: deleteUrl(urn),
|
|
1646
|
+
method: "DELETE",
|
|
1647
|
+
spendClass: "write",
|
|
1648
|
+
operation: "delete"
|
|
1649
|
+
});
|
|
1650
|
+
if (result.ok) return { ok: true, id: urn };
|
|
1651
|
+
const notFound = result.code === "NOT_FOUND";
|
|
1652
|
+
const out = {
|
|
1653
|
+
ok: false,
|
|
1654
|
+
code: result.code,
|
|
1655
|
+
message: result.message
|
|
1656
|
+
};
|
|
1657
|
+
if (notFound) {
|
|
1658
|
+
out.hint = "Either the post is already gone \u2014 in which case nothing more is needed \u2014 or this is the wrong urn namespace for this endpoint. It is documented against `urn:li:share:<id>`; if you passed an activity or ugcPost urn, the same numeric id under `urn:li:share:` is the thing to try next.";
|
|
1659
|
+
} else if (result.hint !== void 0) {
|
|
1660
|
+
out.hint = result.hint;
|
|
1661
|
+
}
|
|
1662
|
+
return out;
|
|
1663
|
+
}
|
|
1664
|
+
function extractShareUrn(json) {
|
|
1665
|
+
return JSON.stringify(json ?? null).match(POST_URN)?.[0] ?? null;
|
|
1666
|
+
}
|
|
1667
|
+
async function share(confirmed, client) {
|
|
1668
|
+
const body = sharePayload(confirmed.payload.text, confirmed.payload.visibility);
|
|
1669
|
+
const result = await client.request({
|
|
1670
|
+
url: SHARE_URL,
|
|
1671
|
+
method: "POST",
|
|
1672
|
+
body,
|
|
1673
|
+
spendClass: "write",
|
|
1674
|
+
operation: "share"
|
|
1675
|
+
});
|
|
1676
|
+
if (!result.ok) {
|
|
1677
|
+
const out = { ok: false, code: result.code, message: result.message };
|
|
1678
|
+
if (result.hint !== void 0) out.hint = result.hint;
|
|
1679
|
+
return out;
|
|
1680
|
+
}
|
|
1681
|
+
const id = extractShareUrn(result.json);
|
|
1682
|
+
return id === null ? {
|
|
1683
|
+
ok: true,
|
|
1684
|
+
id: null,
|
|
1685
|
+
note: "LinkedIn accepted the post but the response carried no recognisable urn. Run `lnrelay sync my-posts && lnrelay my-posts` to confirm what actually landed \u2014 and do NOT re-run this command on the assumption it failed, or you will post twice."
|
|
1686
|
+
} : { ok: true, id };
|
|
1687
|
+
}
|
|
1688
|
+
|
|
1689
|
+
// src/commands/gate.ts
|
|
1690
|
+
import { createInterface } from "readline/promises";
|
|
1691
|
+
|
|
1692
|
+
// src/commands/confirm.ts
|
|
1693
|
+
import { createHash } from "crypto";
|
|
1694
|
+
function confirmToken(action, payload) {
|
|
1695
|
+
return createHash("sha256").update(`${action}:${JSON.stringify(payload)}`).digest("hex").slice(0, 4);
|
|
1696
|
+
}
|
|
1697
|
+
function riskLines(transport) {
|
|
1698
|
+
return transport === "oauth" ? [
|
|
1699
|
+
" This write itself is sanctioned \u2014 it uses LinkedIn's own",
|
|
1700
|
+
" w_member_social scope. The reads this tool performs are not."
|
|
1701
|
+
] : [
|
|
1702
|
+
" This goes over the PRIVATE API, not a sanctioned one. It breaches",
|
|
1703
|
+
" LinkedIn User Agreement \xA78.2 and can permanently restrict this account."
|
|
1704
|
+
];
|
|
1705
|
+
}
|
|
1706
|
+
function renderPlan(plan, budgetLine2) {
|
|
1707
|
+
const token = confirmToken(plan.action, plan.payload);
|
|
1708
|
+
const lines = [
|
|
1709
|
+
"",
|
|
1710
|
+
` ABOUT TO ${plan.action.toUpperCase()} ON LINKEDIN`,
|
|
1711
|
+
` ${"\u2500".repeat(58)}`,
|
|
1712
|
+
...plan.summary.map((l) => ` ${l}`),
|
|
1713
|
+
` via ${plan.transport === "oauth" ? "official OAuth (w_member_social)" : "Voyager (private API)"}`,
|
|
1714
|
+
` undo ${plan.reversibility}`,
|
|
1715
|
+
"",
|
|
1716
|
+
...riskLines(plan.transport),
|
|
1717
|
+
` ${budgetLine2}`,
|
|
1718
|
+
"",
|
|
1719
|
+
` Type ${token} to confirm, anything else to abort: `
|
|
1720
|
+
];
|
|
1721
|
+
return lines.join("\n");
|
|
1722
|
+
}
|
|
1723
|
+
async function confirmWrite(plan, budgetLine2, deps) {
|
|
1724
|
+
if (!deps.isTty) {
|
|
1725
|
+
return {
|
|
1726
|
+
ok: false,
|
|
1727
|
+
code: "CONFIRMATION_REQUIRED",
|
|
1728
|
+
message: `'${plan.action}' needs a human to confirm it at an interactive terminal`,
|
|
1729
|
+
hint: "No terminal is attached, so nothing was sent and no network call was made. Run this command yourself in a shell. There is deliberately no --yes flag: an agent that could set one would not be confirming anything."
|
|
1730
|
+
};
|
|
1731
|
+
}
|
|
1732
|
+
const token = confirmToken(plan.action, plan.payload);
|
|
1733
|
+
const answer = (await deps.prompt(renderPlan(plan, budgetLine2))).trim();
|
|
1734
|
+
if (answer !== token) {
|
|
1735
|
+
return {
|
|
1736
|
+
ok: false,
|
|
1737
|
+
code: "CONFIRMATION_REQUIRED",
|
|
1738
|
+
message: "aborted \u2014 the confirmation token did not match",
|
|
1739
|
+
hint: "Nothing was sent. Re-run and type the token shown in the prompt."
|
|
1740
|
+
};
|
|
1741
|
+
}
|
|
1742
|
+
return {
|
|
1743
|
+
ok: true,
|
|
1744
|
+
confirmed: { action: plan.action, payload: plan.payload, token }
|
|
1745
|
+
};
|
|
1746
|
+
}
|
|
1747
|
+
|
|
1748
|
+
// src/commands/gate.ts
|
|
1749
|
+
function ledger() {
|
|
1750
|
+
const result = loadJson(cachePath("budget.json"));
|
|
1751
|
+
return result.state === "ok" ? result.value : emptyLedger();
|
|
1752
|
+
}
|
|
1753
|
+
function budgetLine(now) {
|
|
1754
|
+
const s = summarise(ledger(), "write", now);
|
|
1755
|
+
return `${s.remaining} of ${s.cap} writes left today.`;
|
|
1756
|
+
}
|
|
1757
|
+
function terminalDeps() {
|
|
1758
|
+
return {
|
|
1759
|
+
isTty: process.stdin.isTTY === true && process.stdout.isTTY === true,
|
|
1760
|
+
prompt: async (question) => {
|
|
1761
|
+
const rl = createInterface({ input: process.stdin, output: process.stderr });
|
|
1762
|
+
const answer = await rl.question(question);
|
|
1763
|
+
rl.close();
|
|
1764
|
+
return answer;
|
|
1765
|
+
},
|
|
1766
|
+
write: (s) => process.stderr.write(s)
|
|
1767
|
+
};
|
|
1768
|
+
}
|
|
1769
|
+
function reserve(command, now) {
|
|
1770
|
+
const attempt = spend(ledger(), "write", now);
|
|
1771
|
+
return "error" in attempt ? err(command, attempt.error.code, attempt.error.message, attempt.error.hint) : null;
|
|
1772
|
+
}
|
|
1773
|
+
async function gateWrite(command, plan, now, deps) {
|
|
1774
|
+
const outcome = await confirmWrite(plan, budgetLine(now), deps);
|
|
1775
|
+
if (!outcome.ok) return err(command, outcome.code, outcome.message, outcome.hint);
|
|
1776
|
+
const attempt = spend(ledger(), "write", now);
|
|
1777
|
+
if ("permit" in attempt) saveJson(cachePath("budget.json"), attempt.ledger);
|
|
1778
|
+
return { confirmed: outcome.confirmed, payload: plan.payload };
|
|
1779
|
+
}
|
|
1780
|
+
|
|
1781
|
+
// src/commands/delete.ts
|
|
1782
|
+
var LOOKUP_LIMIT = 50;
|
|
1783
|
+
var PREVIEW_CHARS = 200;
|
|
1784
|
+
function postId(urn) {
|
|
1785
|
+
return /:(\d+)$/.exec(urn)?.[1];
|
|
1786
|
+
}
|
|
1787
|
+
function findOwnPost(posts, urn) {
|
|
1788
|
+
const id = postId(urn);
|
|
1789
|
+
if (id === void 0) return void 0;
|
|
1790
|
+
return posts.find((p) => typeof p.urn === "string" && postId(p.urn) === id);
|
|
1791
|
+
}
|
|
1792
|
+
function previewLines(post) {
|
|
1793
|
+
if (post === void 0) {
|
|
1794
|
+
return [
|
|
1795
|
+
"content COULD NOT READ THIS POST.",
|
|
1796
|
+
" It is not among your recent posts. That may only mean it is older",
|
|
1797
|
+
" than the page fetched \u2014 but nothing here has verified what you are",
|
|
1798
|
+
" about to delete, or that it is yours."
|
|
1799
|
+
];
|
|
1800
|
+
}
|
|
1801
|
+
const text2 = typeof post.text === "string" ? post.text : "(no text \u2014 media-only post?)";
|
|
1802
|
+
const shown = text2.length > PREVIEW_CHARS ? `${text2.slice(0, PREVIEW_CHARS)}\u2026` : text2;
|
|
1803
|
+
return [`content "${shown.replace(/\n/g, " ")}"`];
|
|
1804
|
+
}
|
|
1805
|
+
async function lookup(urn, quiet) {
|
|
1806
|
+
const session = loadSession();
|
|
1807
|
+
if (session.state !== "ok") return void 0;
|
|
1808
|
+
const engine = createEngine(session.session, void 0, quiet);
|
|
1809
|
+
const me = await engine.whoami();
|
|
1810
|
+
if (!me.ok) return void 0;
|
|
1811
|
+
const profileUrn = typeof me.value?.entityUrn === "string" ? me.value.entityUrn : void 0;
|
|
1812
|
+
if (profileUrn === void 0) return void 0;
|
|
1813
|
+
const posts = await engine.myPosts(profileUrn, LOOKUP_LIMIT);
|
|
1814
|
+
if (!posts.ok) return void 0;
|
|
1815
|
+
return findOwnPost(shapeAll(posts.value.parsed.items, posts.value.index), urn);
|
|
1816
|
+
}
|
|
1817
|
+
async function runDelete(urn, now = Date.now(), deps, quiet = false) {
|
|
1818
|
+
if (urn === void 0 || urn === "") {
|
|
1819
|
+
return err(
|
|
1820
|
+
"delete",
|
|
1821
|
+
"INVALID_INPUT",
|
|
1822
|
+
"a post urn is required",
|
|
1823
|
+
"lnrelay delete <urn:li:share:\u2026 | urn:li:activity:\u2026>"
|
|
1824
|
+
);
|
|
1825
|
+
}
|
|
1826
|
+
if (!isDeletableUrn(urn)) {
|
|
1827
|
+
return err(
|
|
1828
|
+
"delete",
|
|
1829
|
+
"INVALID_INPUT",
|
|
1830
|
+
`'${urn}' does not identify a post`,
|
|
1831
|
+
"Expected urn:li:share:<id>, urn:li:activity:<id> or urn:li:ugcPost:<id>. Refusing before encoding it, because a well-formed request naming the wrong kind of entity is a request to destroy the wrong kind of thing."
|
|
1832
|
+
);
|
|
1833
|
+
}
|
|
1834
|
+
const session = loadSession();
|
|
1835
|
+
if (session.state === "corrupt") {
|
|
1836
|
+
return err("delete", "CACHE_CORRUPT", "the stored session was unreadable", LAUNCH_HINT);
|
|
1837
|
+
}
|
|
1838
|
+
if (session.state === "missing") {
|
|
1839
|
+
return err(
|
|
1840
|
+
"delete",
|
|
1841
|
+
"AUTH_FAILED",
|
|
1842
|
+
"no LinkedIn session \u2014 deleting goes over the private API",
|
|
1843
|
+
`Run \`lnrelay login\`. ${LAUNCH_HINT}`
|
|
1844
|
+
);
|
|
1845
|
+
}
|
|
1846
|
+
const refused = reserve("delete", now);
|
|
1847
|
+
if (refused !== null) return refused;
|
|
1848
|
+
const post = await lookup(urn, quiet);
|
|
1849
|
+
const plan = {
|
|
1850
|
+
action: "delete a post",
|
|
1851
|
+
payload: { urn },
|
|
1852
|
+
summary: [`post ${urn}`, ...previewLines(post)],
|
|
1853
|
+
reversibility: "NONE. A deleted post is gone, with its comments and reactions.",
|
|
1854
|
+
transport: "voyager"
|
|
1855
|
+
};
|
|
1856
|
+
const gated = await gateWrite("delete", plan, now, deps ?? terminalDeps());
|
|
1857
|
+
if ("ok" in gated) return gated;
|
|
1858
|
+
const result = await deletePost(gated.confirmed, createLiveClient(session.session));
|
|
1859
|
+
return result.ok ? ok("delete", { deleted: urn, verified: post !== void 0 }) : err("delete", result.code, result.message, result.hint);
|
|
1860
|
+
}
|
|
1861
|
+
|
|
1657
1862
|
// src/commands/live.ts
|
|
1658
1863
|
function withSession(command, quiet = false) {
|
|
1659
1864
|
const loaded = loadSession();
|
|
@@ -1806,7 +2011,6 @@ function collection(result, opts = {}) {
|
|
|
1806
2011
|
}
|
|
1807
2012
|
|
|
1808
2013
|
// src/commands/local.ts
|
|
1809
|
-
init_store();
|
|
1810
2014
|
import { existsSync as existsSync2 } from "fs";
|
|
1811
2015
|
import { version as nodeVersion } from "process";
|
|
1812
2016
|
var LEDGER_FILE = "budget.json";
|
|
@@ -1941,7 +2145,7 @@ function runDoctor(now, offline) {
|
|
|
1941
2145
|
// src/commands/oauth.ts
|
|
1942
2146
|
import { spawn } from "child_process";
|
|
1943
2147
|
import { createServer } from "http";
|
|
1944
|
-
import { createInterface } from "readline/promises";
|
|
2148
|
+
import { createInterface as createInterface2 } from "readline/promises";
|
|
1945
2149
|
|
|
1946
2150
|
// src/engine/oauth.ts
|
|
1947
2151
|
var AUTHORIZE = "https://www.linkedin.com/oauth/v2/authorization";
|
|
@@ -2042,7 +2246,6 @@ function parseCallback(rawUrl, expectedState) {
|
|
|
2042
2246
|
}
|
|
2043
2247
|
|
|
2044
2248
|
// src/commands/token.ts
|
|
2045
|
-
init_store();
|
|
2046
2249
|
import { existsSync as existsSync3, rmSync } from "fs";
|
|
2047
2250
|
var TOKEN_FILE = "oauth.json";
|
|
2048
2251
|
var OAUTH_SETUP = [
|
|
@@ -2134,7 +2337,7 @@ function loginDeps() {
|
|
|
2134
2337
|
write: (s) => process.stderr.write(s),
|
|
2135
2338
|
isTty: process.stdin.isTTY === true && process.stderr.isTTY === true,
|
|
2136
2339
|
prompt: async (question) => {
|
|
2137
|
-
const rl =
|
|
2340
|
+
const rl = createInterface2({ input: process.stdin, output: process.stderr });
|
|
2138
2341
|
const answer = await rl.question(question);
|
|
2139
2342
|
rl.close();
|
|
2140
2343
|
return answer;
|
|
@@ -2404,8 +2607,8 @@ var COMMANDS = [
|
|
|
2404
2607
|
{
|
|
2405
2608
|
name: "share",
|
|
2406
2609
|
cost: "1 call \u2014 write",
|
|
2407
|
-
summary: "Post to your own feed
|
|
2408
|
-
usage: 'lnrelay share "<text>" [--visibility public|connections]\n Stops and asks at an interactive terminal. No TTY = no write, and no network call.',
|
|
2610
|
+
summary: "Post to your own feed. Prefers LinkedIn's official OAuth scope, falls back to the private API when no token exists \u2014 the prompt always names which.",
|
|
2611
|
+
usage: 'lnrelay share "<text>" [--visibility public|connections] [--via oauth|voyager]\n Stops and asks at an interactive terminal. No TTY = no write, and no network call.',
|
|
2409
2612
|
audience: ["cli"],
|
|
2410
2613
|
risk: "write",
|
|
2411
2614
|
implemented: true
|
|
@@ -2419,6 +2622,15 @@ var COMMANDS = [
|
|
|
2419
2622
|
risk: "write",
|
|
2420
2623
|
implemented: true
|
|
2421
2624
|
},
|
|
2625
|
+
{
|
|
2626
|
+
name: "delete",
|
|
2627
|
+
cost: "1 read + 1 write",
|
|
2628
|
+
summary: "Delete one of your own posts. Reads it first and shows you its text \u2014 this is the one action that cannot be undone.",
|
|
2629
|
+
usage: "lnrelay delete <urn:li:share:\u2026 | urn:li:activity:\u2026>",
|
|
2630
|
+
audience: ["cli"],
|
|
2631
|
+
risk: "write",
|
|
2632
|
+
implemented: true
|
|
2633
|
+
},
|
|
2422
2634
|
{
|
|
2423
2635
|
name: "react",
|
|
2424
2636
|
cost: "1 call \u2014 write",
|
|
@@ -2446,7 +2658,6 @@ function helpText() {
|
|
|
2446
2658
|
}
|
|
2447
2659
|
|
|
2448
2660
|
// src/commands/sync.ts
|
|
2449
|
-
init_db();
|
|
2450
2661
|
var CONNECTIONS_MIN_INTERVAL_MS = 7 * 864e5;
|
|
2451
2662
|
function searchText(item) {
|
|
2452
2663
|
return [item.name, item.headline, item.location, item.text, item.author, item.company, item.title].filter((v) => typeof v === "string").join(" ");
|
|
@@ -2459,6 +2670,76 @@ function diffSnapshot(before, seen, claimed) {
|
|
|
2459
2670
|
if (!complete) return { removed: [], complete: false };
|
|
2460
2671
|
return { removed: [...before].filter((urn) => !seen.has(urn)), complete: true };
|
|
2461
2672
|
}
|
|
2673
|
+
async function syncMyPosts(engine, db, limit, now) {
|
|
2674
|
+
const me = await engine.whoami();
|
|
2675
|
+
if (!me.ok) return err("sync", me.code, me.message, me.hint);
|
|
2676
|
+
const profileUrn = typeof me.value?.entityUrn === "string" ? me.value.entityUrn : void 0;
|
|
2677
|
+
if (profileUrn === void 0) {
|
|
2678
|
+
return err("sync", "SCHEMA_DRIFT", "could not determine the owner profile urn");
|
|
2679
|
+
}
|
|
2680
|
+
const result = await engine.myPosts(profileUrn, limit);
|
|
2681
|
+
if (!result.ok) return err("sync", result.code, result.message, result.hint);
|
|
2682
|
+
const rows = rowsFrom(shapeAll(result.value.parsed.items, result.value.index));
|
|
2683
|
+
const stored = upsert(db, "my-posts", rows, now);
|
|
2684
|
+
setCheckpoint(db, {
|
|
2685
|
+
source: "my-posts",
|
|
2686
|
+
lastSuccessfulAt: now,
|
|
2687
|
+
newestCreatedAt: null,
|
|
2688
|
+
headIds: rows.slice(0, 5).map((r) => r.urn),
|
|
2689
|
+
state: "ok"
|
|
2690
|
+
});
|
|
2691
|
+
return ok("sync", {
|
|
2692
|
+
source: "my-posts",
|
|
2693
|
+
...stored,
|
|
2694
|
+
meta: result.value.parsed.meta,
|
|
2695
|
+
note: rows.length === 0 ? "LinkedIn returned no posts for this account. That is a genuine empty, not a failed fetch \u2014 meta.state says which." : void 0
|
|
2696
|
+
});
|
|
2697
|
+
}
|
|
2698
|
+
function tooSoon(db, now) {
|
|
2699
|
+
const cp = getCheckpoint(db, "connections");
|
|
2700
|
+
const age = cp === void 0 ? Number.POSITIVE_INFINITY : now - cp.lastSuccessfulAt;
|
|
2701
|
+
if (age >= CONNECTIONS_MIN_INTERVAL_MS) return void 0;
|
|
2702
|
+
const days = Math.ceil((CONNECTIONS_MIN_INTERVAL_MS - age) / 864e5);
|
|
2703
|
+
return err(
|
|
2704
|
+
"sync",
|
|
2705
|
+
"BUDGET_EXHAUSTED",
|
|
2706
|
+
`connections were synced ${Math.floor(age / 864e5)} days ago`,
|
|
2707
|
+
`This is the most expensive read in the tool and connections change slowly. Wait ${days} more day(s), or pass --force.`
|
|
2708
|
+
);
|
|
2709
|
+
}
|
|
2710
|
+
async function syncConnections(engine, db, limit, force, now) {
|
|
2711
|
+
const before = urnsFor(db, "connections");
|
|
2712
|
+
if (!force && before.size > 0) {
|
|
2713
|
+
const blocked = tooSoon(db, now);
|
|
2714
|
+
if (blocked !== void 0) return blocked;
|
|
2715
|
+
}
|
|
2716
|
+
const result = await engine.connections(limit);
|
|
2717
|
+
if (!result.ok) return err("sync", result.code, result.message, result.hint);
|
|
2718
|
+
const rows = rowsFrom(shapeAll(result.value.parsed.items, result.value.index));
|
|
2719
|
+
const stored = upsert(db, "connections", rows, now);
|
|
2720
|
+
const claimed = result.value.parsed.meta.claimedCount;
|
|
2721
|
+
const seen = new Set(rows.map((r) => r.urn));
|
|
2722
|
+
const { removed, complete } = diffSnapshot(before, seen, claimed);
|
|
2723
|
+
if (removed.length > 0) removeUrns(db, "connections", removed);
|
|
2724
|
+
setCheckpoint(db, {
|
|
2725
|
+
source: "connections",
|
|
2726
|
+
lastSuccessfulAt: now,
|
|
2727
|
+
newestCreatedAt: null,
|
|
2728
|
+
headIds: [],
|
|
2729
|
+
// Honest: a snapshot, never a watermark.
|
|
2730
|
+
state: complete ? "ok" : "snapshot-partial"
|
|
2731
|
+
});
|
|
2732
|
+
return ok("sync", {
|
|
2733
|
+
source: "connections",
|
|
2734
|
+
added: stored.added,
|
|
2735
|
+
updated: stored.updated,
|
|
2736
|
+
removed: removed.length,
|
|
2737
|
+
claimed,
|
|
2738
|
+
complete,
|
|
2739
|
+
meta: result.value.parsed.meta,
|
|
2740
|
+
note: complete ? void 0 : `Only ${rows.length} of ${claimed} connections were retrieved, so removals could not be determined and none were applied. Raise --limit to complete the snapshot.`
|
|
2741
|
+
});
|
|
2742
|
+
}
|
|
2462
2743
|
async function runSync(source, limit, force, now = Date.now()) {
|
|
2463
2744
|
if (source !== "my-posts" && source !== "connections") {
|
|
2464
2745
|
return err(
|
|
@@ -2478,75 +2759,7 @@ async function runSync(source, limit, force, now = Date.now()) {
|
|
|
2478
2759
|
const engine = createEngine(session.session);
|
|
2479
2760
|
try {
|
|
2480
2761
|
const db = openDb(now);
|
|
2481
|
-
|
|
2482
|
-
const me = await engine.whoami();
|
|
2483
|
-
if (!me.ok) return err("sync", me.code, me.message, me.hint);
|
|
2484
|
-
const profileUrn = typeof me.value?.entityUrn === "string" ? me.value.entityUrn : void 0;
|
|
2485
|
-
if (profileUrn === void 0) {
|
|
2486
|
-
return err("sync", "SCHEMA_DRIFT", "could not determine the owner profile urn");
|
|
2487
|
-
}
|
|
2488
|
-
const result2 = await engine.myPosts(profileUrn, limit);
|
|
2489
|
-
if (!result2.ok) return err("sync", result2.code, result2.message, result2.hint);
|
|
2490
|
-
const items2 = shapeAll(result2.value.parsed.items, result2.value.index);
|
|
2491
|
-
const rows2 = rowsFrom(items2);
|
|
2492
|
-
const stored2 = upsert(db, "my-posts", rows2, now);
|
|
2493
|
-
setCheckpoint(db, {
|
|
2494
|
-
source: "my-posts",
|
|
2495
|
-
lastSuccessfulAt: now,
|
|
2496
|
-
newestCreatedAt: null,
|
|
2497
|
-
headIds: rows2.slice(0, 5).map((r) => r.urn),
|
|
2498
|
-
state: "ok"
|
|
2499
|
-
});
|
|
2500
|
-
return ok("sync", {
|
|
2501
|
-
source,
|
|
2502
|
-
...stored2,
|
|
2503
|
-
meta: result2.value.parsed.meta,
|
|
2504
|
-
note: rows2.length === 0 ? "LinkedIn returned no posts for this account. That is a genuine empty, not a failed fetch \u2014 meta.state says which." : void 0
|
|
2505
|
-
});
|
|
2506
|
-
}
|
|
2507
|
-
const before = urnsFor(db, "connections");
|
|
2508
|
-
const existing = before.size;
|
|
2509
|
-
if (!force && existing > 0) {
|
|
2510
|
-
const { getCheckpoint: getCheckpoint2 } = await Promise.resolve().then(() => (init_db(), db_exports));
|
|
2511
|
-
const cp = getCheckpoint2(db, "connections");
|
|
2512
|
-
const age = cp === void 0 ? Number.POSITIVE_INFINITY : now - cp.lastSuccessfulAt;
|
|
2513
|
-
if (age < CONNECTIONS_MIN_INTERVAL_MS) {
|
|
2514
|
-
const days = Math.ceil((CONNECTIONS_MIN_INTERVAL_MS - age) / 864e5);
|
|
2515
|
-
return err(
|
|
2516
|
-
"sync",
|
|
2517
|
-
"BUDGET_EXHAUSTED",
|
|
2518
|
-
`connections were synced ${Math.floor(age / 864e5)} days ago`,
|
|
2519
|
-
`This is the most expensive read in the tool and connections change slowly. Wait ${days} more day(s), or pass --force.`
|
|
2520
|
-
);
|
|
2521
|
-
}
|
|
2522
|
-
}
|
|
2523
|
-
const result = await engine.connections(limit);
|
|
2524
|
-
if (!result.ok) return err("sync", result.code, result.message, result.hint);
|
|
2525
|
-
const items = shapeAll(result.value.parsed.items, result.value.index);
|
|
2526
|
-
const rows = rowsFrom(items);
|
|
2527
|
-
const stored = upsert(db, "connections", rows, now);
|
|
2528
|
-
const claimed = result.value.parsed.meta.claimedCount;
|
|
2529
|
-
const seen = new Set(rows.map((r) => r.urn));
|
|
2530
|
-
const { removed, complete } = diffSnapshot(before, seen, claimed);
|
|
2531
|
-
if (removed.length > 0) removeUrns(db, "connections", removed);
|
|
2532
|
-
setCheckpoint(db, {
|
|
2533
|
-
source: "connections",
|
|
2534
|
-
lastSuccessfulAt: now,
|
|
2535
|
-
newestCreatedAt: null,
|
|
2536
|
-
headIds: [],
|
|
2537
|
-
// Honest: a snapshot, never a watermark.
|
|
2538
|
-
state: complete ? "ok" : "snapshot-partial"
|
|
2539
|
-
});
|
|
2540
|
-
return ok("sync", {
|
|
2541
|
-
source,
|
|
2542
|
-
added: stored.added,
|
|
2543
|
-
updated: stored.updated,
|
|
2544
|
-
removed: removed.length,
|
|
2545
|
-
claimed,
|
|
2546
|
-
complete,
|
|
2547
|
-
meta: result.value.parsed.meta,
|
|
2548
|
-
note: complete ? void 0 : `Only ${rows.length} of ${claimed} connections were retrieved, so removals could not be determined and none were applied. Raise --limit to complete the snapshot.`
|
|
2549
|
-
});
|
|
2762
|
+
return source === "my-posts" ? await syncMyPosts(engine, db, limit, now) : await syncConnections(engine, db, limit, force, now);
|
|
2550
2763
|
} catch (e) {
|
|
2551
2764
|
if (e instanceof CacheCorrupt) {
|
|
2552
2765
|
return err(
|
|
@@ -2560,10 +2773,6 @@ async function runSync(source, limit, force, now = Date.now()) {
|
|
|
2560
2773
|
}
|
|
2561
2774
|
}
|
|
2562
2775
|
|
|
2563
|
-
// src/commands/write.ts
|
|
2564
|
-
init_store();
|
|
2565
|
-
import { createInterface as createInterface2 } from "readline/promises";
|
|
2566
|
-
|
|
2567
2776
|
// src/engine/oauth-write.ts
|
|
2568
2777
|
var API = "https://api.linkedin.com";
|
|
2569
2778
|
function headers(token) {
|
|
@@ -2631,7 +2840,7 @@ async function send(url, body, token, deps) {
|
|
|
2631
2840
|
function extractId(body) {
|
|
2632
2841
|
return body.match(/"id"\s*:\s*"([^"]+)"/)?.[1] ?? null;
|
|
2633
2842
|
}
|
|
2634
|
-
function
|
|
2843
|
+
function share2(confirmed, token, deps) {
|
|
2635
2844
|
return send(
|
|
2636
2845
|
`${API}/rest/posts`,
|
|
2637
2846
|
{
|
|
@@ -2672,121 +2881,126 @@ function react(confirmed, token, deps) {
|
|
|
2672
2881
|
);
|
|
2673
2882
|
}
|
|
2674
2883
|
|
|
2675
|
-
// src/commands/
|
|
2676
|
-
|
|
2677
|
-
|
|
2678
|
-
|
|
2679
|
-
|
|
2680
|
-
|
|
2681
|
-
|
|
2682
|
-
|
|
2683
|
-
|
|
2684
|
-
|
|
2685
|
-
|
|
2686
|
-
|
|
2687
|
-
|
|
2688
|
-
|
|
2689
|
-
|
|
2690
|
-
|
|
2691
|
-
" permanently restrict this account.",
|
|
2692
|
-
` ${budgetLine2}`,
|
|
2693
|
-
"",
|
|
2694
|
-
` Type ${token} to confirm, anything else to abort: `
|
|
2695
|
-
];
|
|
2696
|
-
return lines.join("\n");
|
|
2697
|
-
}
|
|
2698
|
-
async function confirmWrite(plan, budgetLine2, deps) {
|
|
2699
|
-
if (!deps.isTty) {
|
|
2884
|
+
// src/commands/transport.ts
|
|
2885
|
+
var NO_CREDENTIAL_HINT = [
|
|
2886
|
+
"Two ways to write, and you need one of them:",
|
|
2887
|
+
"",
|
|
2888
|
+
" Sanctioned lnrelay oauth login --client-id <id>",
|
|
2889
|
+
" Needs a developer app, which needs a LinkedIn Page.",
|
|
2890
|
+
"",
|
|
2891
|
+
" Unsanctioned lnrelay login",
|
|
2892
|
+
" Uses the same cookies as reads. No Page needed. Breaches \xA78.2",
|
|
2893
|
+
" and puts the account at risk \u2014 every write still asks first."
|
|
2894
|
+
].join("\n");
|
|
2895
|
+
function chooseTransport(inputs, requested) {
|
|
2896
|
+
const { token, session, now } = inputs;
|
|
2897
|
+
const tokenLive = token !== null && token.expiresAt > now;
|
|
2898
|
+
if (requested === "oauth") {
|
|
2899
|
+
if (tokenLive) return { ok: true, transport: { kind: "oauth", token } };
|
|
2700
2900
|
return {
|
|
2701
2901
|
ok: false,
|
|
2702
|
-
|
|
2703
|
-
|
|
2704
|
-
hint: "No terminal is attached, so nothing was sent and no network call was made. Run this command yourself in a shell. There is deliberately no --yes flag: an agent that could set one would not be confirming anything."
|
|
2902
|
+
message: token === null ? "no OAuth token stored" : "the OAuth token has expired",
|
|
2903
|
+
hint: "Run `lnrelay oauth login --client-id <id>`."
|
|
2705
2904
|
};
|
|
2706
2905
|
}
|
|
2707
|
-
|
|
2708
|
-
|
|
2709
|
-
if (answer !== token) {
|
|
2906
|
+
if (requested === "voyager") {
|
|
2907
|
+
if (session !== null) return { ok: true, transport: { kind: "voyager", session } };
|
|
2710
2908
|
return {
|
|
2711
2909
|
ok: false,
|
|
2712
|
-
|
|
2713
|
-
|
|
2714
|
-
|
|
2910
|
+
message: "no LinkedIn session \u2014 a Voyager write needs one",
|
|
2911
|
+
hint: "Run `lnrelay login` to mint one from your own browser."
|
|
2912
|
+
};
|
|
2913
|
+
}
|
|
2914
|
+
if (tokenLive) return { ok: true, transport: { kind: "oauth", token } };
|
|
2915
|
+
if (token !== null && session !== null) {
|
|
2916
|
+
return {
|
|
2917
|
+
ok: false,
|
|
2918
|
+
message: "the OAuth token has expired",
|
|
2919
|
+
hint: "Run `lnrelay oauth login --client-id <id>` to renew it. You do have a browser session, so `--via voyager` would also work \u2014 but that is the private API, and swapping you onto it because a token aged out is not a decision this tool will make for you."
|
|
2920
|
+
};
|
|
2921
|
+
}
|
|
2922
|
+
if (session !== null) {
|
|
2923
|
+
return {
|
|
2924
|
+
ok: true,
|
|
2925
|
+
transport: { kind: "voyager", session },
|
|
2926
|
+
note: "No OAuth token, so this write goes over the private API. See the prompt."
|
|
2715
2927
|
};
|
|
2716
2928
|
}
|
|
2717
2929
|
return {
|
|
2718
|
-
ok:
|
|
2719
|
-
|
|
2930
|
+
ok: false,
|
|
2931
|
+
message: "no way to write \u2014 no OAuth token and no session",
|
|
2932
|
+
hint: NO_CREDENTIAL_HINT
|
|
2720
2933
|
};
|
|
2721
2934
|
}
|
|
2722
2935
|
|
|
2723
2936
|
// src/commands/write.ts
|
|
2724
|
-
function
|
|
2725
|
-
|
|
2726
|
-
return result.state === "ok" ? result.value : emptyLedger();
|
|
2727
|
-
}
|
|
2728
|
-
function budgetLine(now) {
|
|
2729
|
-
const s = summarise(ledger(), "write", now);
|
|
2730
|
-
return `${s.remaining} of ${s.cap} writes left today.`;
|
|
2731
|
-
}
|
|
2732
|
-
function terminalDeps() {
|
|
2733
|
-
return {
|
|
2734
|
-
isTty: process.stdin.isTTY === true && process.stdout.isTTY === true,
|
|
2735
|
-
prompt: async (question) => {
|
|
2736
|
-
const rl = createInterface2({ input: process.stdin, output: process.stderr });
|
|
2737
|
-
const answer = await rl.question(question);
|
|
2738
|
-
rl.close();
|
|
2739
|
-
return answer;
|
|
2740
|
-
},
|
|
2741
|
-
write: (s) => process.stderr.write(s)
|
|
2742
|
-
};
|
|
2937
|
+
function authorLine(transport) {
|
|
2938
|
+
return transport.kind === "oauth" ? `as ${transport.token.memberUrn}` : `as ${transport.session.ownerUrn ?? "you (browser session)"}`;
|
|
2743
2939
|
}
|
|
2744
|
-
function prepare(command, now) {
|
|
2745
|
-
const
|
|
2746
|
-
|
|
2747
|
-
|
|
2748
|
-
|
|
2749
|
-
|
|
2750
|
-
|
|
2751
|
-
|
|
2940
|
+
function prepare(command, now, via2) {
|
|
2941
|
+
const session = loadSession();
|
|
2942
|
+
const choice = chooseTransport(
|
|
2943
|
+
{ token: loadToken(), session: session.state === "ok" ? session.session : null, now },
|
|
2944
|
+
via2
|
|
2945
|
+
);
|
|
2946
|
+
if (!choice.ok) {
|
|
2947
|
+
return err(command, "AUTH_FAILED", choice.message, `${choice.hint}
|
|
2752
2948
|
|
|
2753
|
-
${OAUTH_SETUP}`
|
|
2754
|
-
);
|
|
2949
|
+
${OAUTH_SETUP}`);
|
|
2755
2950
|
}
|
|
2756
|
-
const
|
|
2757
|
-
if (
|
|
2758
|
-
|
|
2951
|
+
const refused = reserve(command, now);
|
|
2952
|
+
if (refused !== null) return refused;
|
|
2953
|
+
const ctx = { transport: choice.transport, now };
|
|
2954
|
+
if (choice.note !== void 0) ctx.note = choice.note;
|
|
2955
|
+
return ctx;
|
|
2956
|
+
}
|
|
2957
|
+
function oauthToken(ctx) {
|
|
2958
|
+
if (ctx.transport.kind !== "oauth") {
|
|
2959
|
+
throw new Error("oauthToken called on a non-oauth transport");
|
|
2759
2960
|
}
|
|
2760
|
-
return
|
|
2961
|
+
return ctx.transport.token;
|
|
2962
|
+
}
|
|
2963
|
+
function requireOauth(command, ctx) {
|
|
2964
|
+
if (ctx.transport.kind === "oauth") return null;
|
|
2965
|
+
return err(
|
|
2966
|
+
command,
|
|
2967
|
+
"NOT_IMPLEMENTED",
|
|
2968
|
+
`'${command}' is not implemented over the private API`,
|
|
2969
|
+
`Only 'share' is. The one available sample for ${command} disagrees with this tool's own verified read-side finding about which urn identifies the target, and a wrong urn doesn't error \u2014 it acts on the wrong post. Run \`bun run scripts/observe-write.ts\`, perform the action by hand once, and the capture settles it. See docs/research/W1-voyager-writes.md.`
|
|
2970
|
+
);
|
|
2761
2971
|
}
|
|
2762
2972
|
async function gate(command, plan, ctx, deps) {
|
|
2763
|
-
const
|
|
2764
|
-
if (
|
|
2765
|
-
|
|
2766
|
-
if ("permit" in attempt) saveJson(cachePath("budget.json"), attempt.ledger);
|
|
2767
|
-
return { confirmed: outcome.confirmed };
|
|
2973
|
+
const gated = await gateWrite(command, plan, ctx.now, deps);
|
|
2974
|
+
if ("ok" in gated) return gated;
|
|
2975
|
+
return { confirmed: gated.confirmed };
|
|
2768
2976
|
}
|
|
2769
|
-
async function runShare(text2, visibility, now = Date.now(), deps = terminalDeps()) {
|
|
2977
|
+
async function runShare(text2, visibility, now = Date.now(), deps = terminalDeps(), via2) {
|
|
2770
2978
|
if (text2 === void 0 || text2.trim() === "") {
|
|
2771
2979
|
return err("share", "INVALID_INPUT", "text is required", 'lnrelay share "<text>"');
|
|
2772
2980
|
}
|
|
2773
2981
|
const vis = visibility === "connections" ? "CONNECTIONS" : "PUBLIC";
|
|
2774
|
-
const ctx = prepare("share", now);
|
|
2982
|
+
const ctx = prepare("share", now, via2);
|
|
2775
2983
|
if ("ok" in ctx) return ctx;
|
|
2776
2984
|
const plan = {
|
|
2777
2985
|
action: "post to your feed",
|
|
2778
2986
|
payload: { text: text2, visibility: vis },
|
|
2779
|
-
summary: [
|
|
2780
|
-
reversibility: "deletable
|
|
2781
|
-
transport:
|
|
2987
|
+
summary: [authorLine(ctx.transport), `content "${text2}"`, `audience ${vis}`],
|
|
2988
|
+
reversibility: "deletable with `lnrelay delete <urn>`, but it is public under your name the moment it lands and anyone who saw it cannot un-see it",
|
|
2989
|
+
transport: ctx.transport.kind
|
|
2782
2990
|
};
|
|
2783
2991
|
const gated = await gate("share", plan, ctx, deps);
|
|
2784
2992
|
if ("ok" in gated) return gated;
|
|
2785
|
-
const result = await
|
|
2993
|
+
const result = ctx.transport.kind === "oauth" ? await share2(gated.confirmed, ctx.transport.token, {
|
|
2786
2994
|
fetch: globalThis.fetch,
|
|
2787
2995
|
now: () => now
|
|
2996
|
+
}) : await share(gated.confirmed, createLiveClient(ctx.transport.session));
|
|
2997
|
+
if (!result.ok) return err("share", result.code, result.message, result.hint);
|
|
2998
|
+
return ok("share", {
|
|
2999
|
+
id: result.id ?? null,
|
|
3000
|
+
transport: ctx.transport.kind,
|
|
3001
|
+
url: "url" in result ? result.url : void 0,
|
|
3002
|
+
note: "note" in result ? result.note : ctx.note
|
|
2788
3003
|
});
|
|
2789
|
-
return result.ok ? ok("share", { id: result.id, url: result.url }) : err("share", result.code, result.message, result.hint);
|
|
2790
3004
|
}
|
|
2791
3005
|
async function runComment(postUrn, text2, now = Date.now(), deps = terminalDeps()) {
|
|
2792
3006
|
if (postUrn === void 0 || text2 === void 0 || text2.trim() === "") {
|
|
@@ -2799,16 +3013,18 @@ async function runComment(postUrn, text2, now = Date.now(), deps = terminalDeps(
|
|
|
2799
3013
|
}
|
|
2800
3014
|
const ctx = prepare("comment", now);
|
|
2801
3015
|
if ("ok" in ctx) return ctx;
|
|
3016
|
+
const unsupported = requireOauth("comment", ctx);
|
|
3017
|
+
if (unsupported !== null) return unsupported;
|
|
2802
3018
|
const plan = {
|
|
2803
3019
|
action: "comment on a post",
|
|
2804
3020
|
payload: { postUrn, text: text2 },
|
|
2805
|
-
summary: [
|
|
3021
|
+
summary: [authorLine(ctx.transport), `on ${postUrn}`, `content "${text2}"`],
|
|
2806
3022
|
reversibility: "deletable from the LinkedIn UI; the author is notified immediately",
|
|
2807
3023
|
transport: "oauth"
|
|
2808
3024
|
};
|
|
2809
3025
|
const gated = await gate("comment", plan, ctx, deps);
|
|
2810
3026
|
if ("ok" in gated) return gated;
|
|
2811
|
-
const result = await comment(gated.confirmed, ctx
|
|
3027
|
+
const result = await comment(gated.confirmed, oauthToken(ctx), {
|
|
2812
3028
|
fetch: globalThis.fetch,
|
|
2813
3029
|
now: () => now
|
|
2814
3030
|
});
|
|
@@ -2835,16 +3051,18 @@ async function runReact(postUrn, type, now = Date.now(), deps = terminalDeps())
|
|
|
2835
3051
|
}
|
|
2836
3052
|
const ctx = prepare("react", now);
|
|
2837
3053
|
if ("ok" in ctx) return ctx;
|
|
3054
|
+
const unsupported = requireOauth("react", ctx);
|
|
3055
|
+
if (unsupported !== null) return unsupported;
|
|
2838
3056
|
const plan = {
|
|
2839
3057
|
action: "react to a post",
|
|
2840
3058
|
payload: { postUrn, type: reaction },
|
|
2841
|
-
summary: [
|
|
3059
|
+
summary: [authorLine(ctx.transport), `on ${postUrn}`, `reaction ${reaction}`],
|
|
2842
3060
|
reversibility: "removable from the LinkedIn UI; the author is notified immediately",
|
|
2843
3061
|
transport: "oauth"
|
|
2844
3062
|
};
|
|
2845
3063
|
const gated = await gate("react", plan, ctx, deps);
|
|
2846
3064
|
if ("ok" in gated) return gated;
|
|
2847
|
-
const result = await react(gated.confirmed, ctx
|
|
3065
|
+
const result = await react(gated.confirmed, oauthToken(ctx), {
|
|
2848
3066
|
fetch: globalThis.fetch,
|
|
2849
3067
|
now: () => now
|
|
2850
3068
|
});
|
|
@@ -2894,6 +3112,11 @@ function output(args) {
|
|
|
2894
3112
|
if (fields !== void 0) opts.fields = fields;
|
|
2895
3113
|
return opts;
|
|
2896
3114
|
}
|
|
3115
|
+
function via(args) {
|
|
3116
|
+
const value = str(args, "via");
|
|
3117
|
+
if (value === void 0) return void 0;
|
|
3118
|
+
return value === "oauth" || value === "voyager" ? value : "invalid";
|
|
3119
|
+
}
|
|
2897
3120
|
async function oauth(args) {
|
|
2898
3121
|
const sub = args.positionals[0] ?? "status";
|
|
2899
3122
|
if (sub === "status") return runOauthStatus();
|
|
@@ -2980,12 +3203,30 @@ async function dispatch(argv, now) {
|
|
|
2980
3203
|
return runLogin();
|
|
2981
3204
|
case "oauth":
|
|
2982
3205
|
return oauth(args);
|
|
2983
|
-
case "share":
|
|
2984
|
-
|
|
3206
|
+
case "share": {
|
|
3207
|
+
const surface = via(args);
|
|
3208
|
+
if (surface === "invalid") {
|
|
3209
|
+
return err(
|
|
3210
|
+
"share",
|
|
3211
|
+
"INVALID_INPUT",
|
|
3212
|
+
`unknown --via '${str(args, "via") ?? ""}'`,
|
|
3213
|
+
"one of: oauth, voyager. Omit it to prefer OAuth and fall back to Voyager."
|
|
3214
|
+
);
|
|
3215
|
+
}
|
|
3216
|
+
return runShare(
|
|
3217
|
+
args.positionals[0],
|
|
3218
|
+
str(args, "visibility") ?? "public",
|
|
3219
|
+
now,
|
|
3220
|
+
void 0,
|
|
3221
|
+
surface
|
|
3222
|
+
);
|
|
3223
|
+
}
|
|
2985
3224
|
case "comment":
|
|
2986
3225
|
return runComment(args.positionals[0], args.positionals[1], now);
|
|
2987
3226
|
case "react":
|
|
2988
3227
|
return runReact(args.positionals[0], str(args, "type") ?? "LIKE", now);
|
|
3228
|
+
case "delete":
|
|
3229
|
+
return runDelete(args.positionals[0], now, void 0, bool(args, "quiet"));
|
|
2989
3230
|
case "whoami":
|
|
2990
3231
|
return runWhoami(bool(args, "raw"));
|
|
2991
3232
|
case "profile":
|