feat(core): API support helpers: token list, suggestions, attachment links, connection runs, device auth, file-mode sync

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
Lucas Winther
2026-10-30 21:10:39 +01:00
co-authored by Claude Opus 5.5
parent 7f3d0d5c44
commit 7b0a6738dd
5 changed files with 378 additions and 12 deletions
+64 -4
View File
@@ -3,7 +3,7 @@ import { dirname, join } from "node:path";
import type { Db } from "@sttracker/db/db";
import { newId, nowIso } from "@sttracker/db/ids";
import type { AttachmentEntity, AttachmentsTable } from "@sttracker/db/schema";
import { ValidationError } from "./errors.ts";
import { NotFoundError, ValidationError } from "./errors.ts";
export type Attachment = AttachmentsTable;
@@ -127,15 +127,15 @@ export async function readAttachment(
dataDir: string,
userId: string,
id: string,
): Promise<{ file: ReturnType<typeof Bun.file>; mime: string } | null> {
): Promise<{ file: ReturnType<typeof Bun.file>; mime: string; filename: string } | null> {
const row = await db.k
.selectFrom("attachments")
.select(["sha256", "mime"])
.select(["sha256", "mime", "filename"])
.where("id", "=", id)
.where("user_id", "=", userId)
.executeTakeFirst();
if (!row) return null;
return { file: Bun.file(pathFor(dataDir, row.sha256)), mime: row.mime };
return { file: Bun.file(pathFor(dataDir, row.sha256)), mime: row.mime, filename: row.filename };
}
/** Attachments of `userId` linked to the given entity, oldest first. */
@@ -201,3 +201,63 @@ export async function unlinkAttachment(
if (orphan) await rm(pathFor(dataDir, att.sha256), { force: true });
});
}
const LINK_ENTITIES: readonly AttachmentEntity[] = ["entry", "engagement", "copy", "list", "work"];
/**
* Throw `NotFoundError` unless `entityId` is a live `entity` the user may attach to: their own entry,
* engagement, copy or list, or a work that is shared or theirs. Call before `storeAttachment`.
*/
export async function requireLinkTarget(db: Db, userId: string, entity: AttachmentEntity, entityId: string): Promise<void> {
if (!LINK_ENTITIES.includes(entity)) throw new ValidationError("entity", "invalid");
const k = db.k;
let found: unknown;
switch (entity) {
case "entry":
found = await k.selectFrom("entries").select("id").where("id", "=", entityId).where("user_id", "=", userId).where("deleted_at", "is", null).executeTakeFirst();
break;
case "engagement":
found = await k
.selectFrom("engagements as g")
.innerJoin("entries as e", "e.id", "g.entry_id")
.select("g.id")
.where("g.id", "=", entityId)
.where("e.user_id", "=", userId)
.where("g.deleted_at", "is", null)
.where("e.deleted_at", "is", null)
.executeTakeFirst();
break;
case "copy":
found = await k.selectFrom("copies").select("id").where("id", "=", entityId).where("user_id", "=", userId).where("deleted_at", "is", null).executeTakeFirst();
break;
case "list":
found = await k.selectFrom("lists").select("id").where("id", "=", entityId).where("user_id", "=", userId).where("deleted_at", "is", null).executeTakeFirst();
break;
case "work":
found = await k
.selectFrom("works")
.select("id")
.where("id", "=", entityId)
.where((eb) => eb.or([eb("owner_user_id", "is", null), eb("owner_user_id", "=", userId)]))
.executeTakeFirst();
break;
}
if (!found) throw new NotFoundError(entity);
}
/** The links of one of the user's attachments (empty for another user's or a missing attachment). */
export async function listAttachmentLinks(
db: Db,
userId: string,
attachmentId: string,
): Promise<{ id: string; entity: AttachmentEntity; entityId: string }[]> {
const rows = await db.k
.selectFrom("attachment_links as l")
.innerJoin("attachments as a", "a.id", "l.attachment_id")
.select(["l.id", "l.entity", "l.entity_id"])
.where("a.id", "=", attachmentId)
.where("a.user_id", "=", userId)
.orderBy("l.id")
.execute();
return rows.map((r) => ({ id: r.id, entity: r.entity, entityId: r.entity_id }));
}
+28
View File
@@ -198,3 +198,31 @@ export async function revokeAppToken(db: Db, userId: string, id: string): Promis
.where("revoked_at", "is", null)
.execute();
}
export interface AppTokenInfo {
id: string;
name: string;
scopes: TokenScope[];
lastUsedAt: string | null;
revokedAt: string | null;
createdAt: string;
}
/** The user's app tokens, newest first. Metadata only: the token and its hash are never returned. */
export async function listAppTokens(db: Db, userId: string): Promise<AppTokenInfo[]> {
const rows = await db.k
.selectFrom("app_tokens")
.select(["id", "name", "scopes", "last_used_at", "revoked_at", "created_at"])
.where("user_id", "=", userId)
.orderBy("created_at", "desc")
.orderBy("id", "desc")
.execute();
return rows.map((r) => ({
id: r.id,
name: r.name,
scopes: fromJson<TokenScope[]>(r.scopes, []),
lastUsedAt: r.last_used_at,
revokedAt: r.revoked_at,
createdAt: r.created_at,
}));
}
+115 -8
View File
@@ -1,14 +1,14 @@
import type { Db } from "@sttracker/db/db";
import { newId, nowIso } from "@sttracker/db/ids";
import { fromJson, toJson } from "@sttracker/db/json";
import type { ConnectionStatus } from "@sttracker/db/schema";
import type { ConnectionStatus, RunMode, RunStatus } from "@sttracker/db/schema";
import { createHttpClient, type HttpClientOptions } from "@sttracker/plugin-sdk/http";
import type { HttpClient, PluginContext, PluginManifest } from "@sttracker/plugin-sdk/types";
import { decryptJson, encryptJson } from "./crypto.ts";
import { CryptoError, NotFoundError, ValidationError } from "./errors.ts";
import { DropJobError, NonRetryableError, type JobContext } from "./jobs.ts";
import type { PluginRegistry } from "./registry.ts";
import { runSync, type Connection, type RunSummary } from "./sync/run.ts";
import { runSync, type Connection, type RunSummary, type SyncMode } from "./sync/run.ts";
const WEEK_MS = 7 * 24 * 60 * 60 * 1000;
@@ -143,6 +143,104 @@ export async function listConnections(db: Db, userId: string): Promise<Connectio
}));
}
/** A deleted connection is paused with its credentials wiped (`deleteConnection`). */
export function isDeletedConnection(c: Pick<ConnectionView, "status" | "hasCredentials">): boolean {
return c.status === "paused" && !c.hasCredentials;
}
async function ownedRow(db: Db, userId: string, id: string) {
const r = await db.k.selectFrom("source_connections").selectAll().where("id", "=", id).where("user_id", "=", userId).executeTakeFirst();
if (!r || (r.status === "paused" && r.credentials_enc === null)) throw new NotFoundError("connection");
return r;
}
/** One of the user's live connections, without secrets. Deleted or foreign connections throw `NotFoundError`. */
export async function getConnection(db: Db, userId: string, id: string): Promise<ConnectionView> {
const r = await ownedRow(db, userId, id);
return {
id: r.id,
pluginId: r.plugin_id,
name: r.name,
config: fromJson<Record<string, unknown>>(r.config, {}),
scheduleMinutes: r.schedule_minutes,
status: r.status,
lastRunAt: r.last_run_at,
createdAt: r.created_at,
hasCredentials: r.credentials_enc !== null,
};
}
export interface RunView {
id: string;
mode: RunMode;
status: RunStatus;
startedAt: string;
finishedAt: string | null;
counts: Record<string, number>;
log: string;
error: string | null;
}
/** Runs of one of the user's connections, newest first (at most `limit`, default 50). */
export async function listRuns(db: Db, userId: string, connectionId: string, limit = 50): Promise<RunView[]> {
await ownedRow(db, userId, connectionId);
const rows = await db.k
.selectFrom("source_runs")
.selectAll()
.where("connection_id", "=", connectionId)
.orderBy("started_at", "desc")
.orderBy("id", "desc")
.limit(Math.min(Math.max(limit, 1), 200))
.execute();
return rows.map((r) => ({
id: r.id,
mode: r.mode,
status: r.status,
startedAt: r.started_at,
finishedAt: r.finished_at,
counts: fromJson<Record<string, number>>(r.counts, {}),
log: r.log,
error: r.error,
}));
}
async function deviceContext(db: Db, secret: string, registry: PluginRegistry, userId: string, id: string) {
const r = await ownedRow(db, userId, id);
const plugin = registry.get(r.plugin_id);
if (!plugin) throw new ValidationError("pluginId", "not_installed");
if (!plugin.deviceAuth) throw new ValidationError("pluginId", "no_device_auth");
const conn: Connection = {
id: r.id,
userId: r.user_id,
pluginId: r.plugin_id,
name: r.name,
config: fromJson<Record<string, unknown>>(r.config, {}),
credentialsEnc: r.credentials_enc,
scheduleMinutes: r.schedule_minutes,
cursor: r.cursor,
status: r.status,
lastRunAt: r.last_run_at,
createdAt: r.created_at,
};
const ctx = await makePluginContext(db, secret, conn, "device", plugin.manifest);
return { deviceAuth: plugin.deviceAuth, ctx };
}
/** Start a device-code sign-in for one of the user's connections. */
export async function deviceStart(db: Db, secret: string, registry: PluginRegistry, userId: string, id: string) {
const { deviceAuth, ctx } = await deviceContext(db, secret, registry, userId, id);
return deviceAuth.start(ctx);
}
/** Poll a device-code sign-in. On success the returned credentials are merged into the encrypted blob. */
export async function devicePoll(db: Db, secret: string, registry: PluginRegistry, userId: string, id: string, deviceCode: string): Promise<"pending" | "connected"> {
const { deviceAuth, ctx } = await deviceContext(db, secret, registry, userId, id);
const res = await deviceAuth.poll(ctx, deviceCode);
if (res === "pending") return "pending";
await ctx.credentials.save(res);
return "connected";
}
export const USER_AGENT = "STTracker/0.1 (+self-hosted)";
export interface ContextOpts {
@@ -215,7 +313,7 @@ export interface SyncHandlerDeps {
const MSG_CRYPTO = "Cannot decrypt this source's credentials. Was STTRACKER_SECRET changed? Reconnect the source to fix it.";
/** Record a run that failed before `runSync` could start, flag the connection, and stop the job without retry. */
async function failBeforeRun(db: Db, connectionId: string, mode: "full" | "changes", message: string): Promise<never> {
async function failBeforeRun(db: Db, connectionId: string, mode: RunMode, message: string): Promise<never> {
const now = nowIso();
await db.k
.insertInto("source_runs")
@@ -232,16 +330,25 @@ async function failBeforeRun(db: Db, connectionId: string, mode: "full" | "chang
* run, mark the connection `error` and fail the job without retry. A failed run is rethrown so the worker retries.
* `ctx.log` output is appended to the run's `source_runs.log`.
*/
export function syncHandler(deps: SyncHandlerDeps): (payload: { connectionId: string; mode?: "full" | "changes" }, jc?: JobContext) => Promise<RunSummary | void> {
export interface SyncPayload {
connectionId: string;
mode?: "full" | "changes";
/** File mode: run the plugin's `importFile` over these bytes instead of pulling. */
file?: { name: string; base64: string };
}
export function syncHandler(deps: SyncHandlerDeps): (payload: SyncPayload, jc?: JobContext) => Promise<RunSummary | void> {
return async (payload, jc) => {
const { db } = deps;
const full = await db.k.selectFrom("source_connections").selectAll().where("id", "=", payload.connectionId).executeTakeFirst();
if (!full) throw new DropJobError("connection not found");
if (full.status === "paused") throw new DropJobError("connection is paused");
const plugin = deps.registry.get(full.plugin_id);
if (!plugin) return failBeforeRun(db, full.id, payload.mode ?? "full", `Plugin '${full.plugin_id}' is not installed or failed to load.`);
let mode: "full" | "changes" = "full";
if (plugin.pullChanges && payload.mode !== "full") {
if (!plugin) return failBeforeRun(db, full.id, payload.file ? "file" : (payload.mode ?? "full"), `Plugin '${full.plugin_id}' is not installed or failed to load.`);
let mode: SyncMode = "full";
if (payload.file) {
mode = { file: { name: payload.file.name, bytes: new Uint8Array(Buffer.from(payload.file.base64, "base64")) } };
} else if (plugin.pullChanges && payload.mode !== "full") {
const cutoff = new Date(Date.now() - WEEK_MS).toISOString();
const recentFull = await db.k
.selectFrom("source_runs")
@@ -270,7 +377,7 @@ export function syncHandler(deps: SyncHandlerDeps): (payload: { connectionId: st
try {
creds = await readCreds(deps.secret, conn.credentialsEnc);
} catch (e) {
if (e instanceof CryptoError) return failBeforeRun(db, full.id, mode, MSG_CRYPTO);
if (e instanceof CryptoError) return failBeforeRun(db, full.id, typeof mode === "string" ? mode : "file", MSG_CRYPTO);
throw e;
}
const logs: string[] = [];
+31
View File
@@ -975,3 +975,34 @@ export async function rejectSuggestion(db: Db, userId: string, suggestionId: str
if (s.status !== "pending") throw new ValidationError("status", "not_pending");
await db.k.updateTable("match_suggestions").set({ status: "rejected" }).where("id", "=", s.id).execute();
}
export interface MatchSuggestion {
id: string;
workId: string;
candidateWorkId: string;
score: number;
status: "pending" | "accepted" | "rejected";
createdAt: string;
}
/** The user's pending match suggestions for a work, best first. Throws `NotFoundError` if the work is not visible. */
export async function listSuggestions(db: Db, userId: string, workId: string): Promise<MatchSuggestion[]> {
await resolveVisibleWork(db.k, userId, workId);
const rows = await db.k
.selectFrom("match_suggestions")
.selectAll()
.where("user_id", "=", userId)
.where("work_id", "=", workId)
.where("status", "=", "pending")
.orderBy("score", "desc")
.orderBy("id")
.execute();
return rows.map((r) => ({
id: r.id,
workId: r.work_id,
candidateWorkId: r.candidate_work_id,
score: r.score,
status: r.status,
createdAt: r.created_at,
}));
}