@lupinum/ginko-server 0.0.0-stage → 0.18.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +13 -0
- package/LICENSE +21 -0
- package/README.md +176 -2
- package/convex/_generated/api.d.ts +185 -0
- package/convex/_generated/api.js +23 -0
- package/convex/_generated/dataModel.d.ts +60 -0
- package/convex/_generated/server.d.ts +162 -0
- package/convex/_generated/server.js +101 -0
- package/convex/account.ts +110 -0
- package/convex/agent.ts +814 -0
- package/convex/auth.config.ts +4 -0
- package/convex/auth.ts +62 -0
- package/convex/backup.ts +94 -0
- package/convex/betterAuth/_generated/api.ts +56 -0
- package/convex/betterAuth/_generated/component.ts +431 -0
- package/convex/betterAuth/_generated/dataModel.ts +60 -0
- package/convex/betterAuth/_generated/server.ts +169 -0
- package/convex/betterAuth/adapter.ts +7 -0
- package/convex/betterAuth/convex.config.ts +3 -0
- package/convex/betterAuth/schema.ts +102 -0
- package/convex/betterAuth/schemaMetadata.ts +697 -0
- package/convex/betterAuth/schemaOptions.ts +13 -0
- package/convex/betterAuth/schemaPlugins.ts +13 -0
- package/convex/bundle.ts +183 -0
- package/convex/changes.ts +136 -0
- package/convex/cleanup.ts +44 -0
- package/convex/connect.ts +143 -0
- package/convex/convex.config.ts +7 -0
- package/convex/crons.ts +14 -0
- package/convex/demo.ts +72 -0
- package/convex/entries.ts +1770 -0
- package/convex/facts.ts +129 -0
- package/convex/files.ts +631 -0
- package/convex/http.ts +21 -0
- package/convex/image.ts +45 -0
- package/convex/images.ts +54 -0
- package/convex/invites.ts +227 -0
- package/convex/lib/access.ts +180 -0
- package/convex/lib/agentTools.ts +331 -0
- package/convex/lib/content.ts +317 -0
- package/convex/lib/corpus.ts +83 -0
- package/convex/lib/diff.ts +50 -0
- package/convex/lib/fail.ts +27 -0
- package/convex/lib/files.ts +70 -0
- package/convex/lib/history.ts +103 -0
- package/convex/lib/i18n.ts +52 -0
- package/convex/lib/input.ts +57 -0
- package/convex/lib/library.ts +28 -0
- package/convex/lib/lifecycle.ts +100 -0
- package/convex/lib/list.ts +112 -0
- package/convex/lib/migrations.ts +126 -0
- package/convex/lib/names.ts +56 -0
- package/convex/lib/pending.ts +84 -0
- package/convex/lib/policy.ts +155 -0
- package/convex/lib/publicness.ts +87 -0
- package/convex/lib/reminders.ts +27 -0
- package/convex/lib/search.ts +132 -0
- package/convex/lib/siteUrl.ts +95 -0
- package/convex/lib/status.ts +109 -0
- package/convex/lib/translation.ts +56 -0
- package/convex/lib/trees.ts +43 -0
- package/convex/lib/write.ts +296 -0
- package/convex/mail.ts +136 -0
- package/convex/members.ts +129 -0
- package/convex/migrations.ts +105 -0
- package/convex/navigation.ts +276 -0
- package/convex/ops.ts +50 -0
- package/convex/proposals.ts +753 -0
- package/convex/refresh.ts +163 -0
- package/convex/release.ts +435 -0
- package/convex/reminders.ts +116 -0
- package/convex/retention.ts +121 -0
- package/convex/routes/bundle.ts +62 -0
- package/convex/routes/connect.ts +80 -0
- package/convex/routes/export.ts +23 -0
- package/convex/routes/file.ts +34 -0
- package/convex/routes/key.ts +53 -0
- package/convex/routes/mcp.ts +300 -0
- package/convex/routes/release.ts +66 -0
- package/convex/routes/upload.ts +94 -0
- package/convex/schema.ts +443 -0
- package/convex/search.ts +58 -0
- package/convex/seed.ts +624 -0
- package/convex/siteDeletion.ts +78 -0
- package/convex/sites.ts +393 -0
- package/convex/tasks.ts +51 -0
- package/convex/tsconfig.json +27 -0
- package/convex/uploads.ts +429 -0
- package/convex/uploadsNode.ts +86 -0
- package/convex/views.ts +111 -0
- package/i18n/de.json +364 -0
- package/i18n/en.json +364 -0
- package/i18n/es.json +364 -0
- package/i18n/fr.json +364 -0
- package/package.json +45 -3
package/convex/files.ts
ADDED
|
@@ -0,0 +1,631 @@
|
|
|
1
|
+
// The asset library, backend (SPEC 3, 4.3, 12): folders, the views of the "Finder" (all, recent,
|
|
2
|
+
// unused, missing alt text, duplicates, trash), "used in", rename, move, focus, keep, trash, restore
|
|
3
|
+
// and replace. Everything is scoped to one site; an id of another site is NOT_FOUND, never a hint.
|
|
4
|
+
// Rights: all members read and upload; everything that changes the library is for owner and admin.
|
|
5
|
+
// Library actions write no `changes` rows (those are the content history, SPEC 3).
|
|
6
|
+
import type { Json, Label } from '@lupinum/ginko'
|
|
7
|
+
import { ALT_INVISIBLE, ALT_MAX, effectiveAlt, type Schema } from '@lupinum/ginko/internal'
|
|
8
|
+
import { v } from 'convex/values'
|
|
9
|
+
import type { Doc, Id } from './_generated/dataModel'
|
|
10
|
+
import { internalMutation, mutation, query, type QueryCtx } from './_generated/server'
|
|
11
|
+
import { labeler } from './lib/names'
|
|
12
|
+
import { labelOf } from './entries'
|
|
13
|
+
import { actorFor, fail, type Actor } from './lib/access'
|
|
14
|
+
import { fieldLabelAt, usesOf, valueAt, type Data } from './lib/content'
|
|
15
|
+
import { cleanName } from './lib/files'
|
|
16
|
+
import { entryUses, same, uploadIdsOf } from './lib/write'
|
|
17
|
+
|
|
18
|
+
const PAGE = 50
|
|
19
|
+
/** Rows read per step and steps per call: a view that filters a lot returns early with a cursor instead of reading the whole table. */
|
|
20
|
+
const SCAN = 100
|
|
21
|
+
const MAX_SCANS = 5
|
|
22
|
+
/** References the sidebar count reads at most. */
|
|
23
|
+
const COUNT_MAX = 1000
|
|
24
|
+
/** Files or ids one call may touch. */
|
|
25
|
+
const MAX_IDS = 200
|
|
26
|
+
const FOLDER_NAME_MAX = 80
|
|
27
|
+
|
|
28
|
+
const ids = v.array(v.string())
|
|
29
|
+
|
|
30
|
+
// ---- Access helpers ----
|
|
31
|
+
|
|
32
|
+
/** Owner and admin may change the library; staff only reads and uploads. */
|
|
33
|
+
async function manager(ctx: QueryCtx, site: string): Promise<Actor> {
|
|
34
|
+
const actor = await actorFor(ctx, site)
|
|
35
|
+
if (actor.role === 'staff') fail('FORBIDDEN', 'errors.fileLibraryOwnersOnly')
|
|
36
|
+
return actor
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
async function uploadOf(ctx: QueryCtx, actor: Actor, id: string): Promise<Doc<'uploads'>> {
|
|
40
|
+
const normalized = ctx.db.normalizeId('uploads', id)
|
|
41
|
+
const row = normalized ? await ctx.db.get(normalized) : null
|
|
42
|
+
if (!row || row.site !== actor.site._id) fail('NOT_FOUND', 'errors.fileNotFound')
|
|
43
|
+
return row
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
async function uploadsOf(ctx: QueryCtx, actor: Actor, list: string[]): Promise<Doc<'uploads'>[]> {
|
|
47
|
+
if (list.length > MAX_IDS) fail('INVALID', 'errors.tooManyFiles')
|
|
48
|
+
const out: Doc<'uploads'>[] = []
|
|
49
|
+
for (const id of new Set(list)) out.push(await uploadOf(ctx, actor, id))
|
|
50
|
+
return out
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
async function folderOf(ctx: QueryCtx, actor: Actor, id: string): Promise<Doc<'folders'>> {
|
|
54
|
+
const normalized = ctx.db.normalizeId('folders', id)
|
|
55
|
+
const row = normalized ? await ctx.db.get(normalized) : null
|
|
56
|
+
if (!row || row.site !== actor.site._id) fail('NOT_FOUND', 'errors.folderNotFound')
|
|
57
|
+
return row
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
const kindOfUpload = (row: Doc<'uploads'>) => (row.mime.startsWith('image/') ? 'image' : 'pdf')
|
|
61
|
+
|
|
62
|
+
// ---- Uses ----
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Entries (deleted ones too) whose content or unpublished changes name this file, one reference per entry: the
|
|
66
|
+
* entry's own when it has one (it carries the uses), else that of its unpublished changes.
|
|
67
|
+
*/
|
|
68
|
+
async function entryRefsOf(ctx: QueryCtx, file: Id<'uploads'>) {
|
|
69
|
+
const refs = await ctx.db.query('fileRefs').withIndex('by_file', (q) => q.eq('file', file)).collect()
|
|
70
|
+
const own = refs.filter((r) => r.holder === 'entry')
|
|
71
|
+
const shown = new Set(own.map((r) => r.holderId))
|
|
72
|
+
return [...own, ...refs.filter((r) => r.holder === 'pending' && !shown.has(r.holderId))]
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
async function usedInOf(ctx: QueryCtx, schema: Schema, refs: Doc<'fileRefs'>[], library?: Record<string, string>) {
|
|
76
|
+
const out: { entry: string; brick: string; title: string; paths: string[]; fieldLabels: (Label | null)[]; locale: string | null; missingAlt: boolean; deleted: boolean }[] = []
|
|
77
|
+
for (const ref of refs) {
|
|
78
|
+
const id = ctx.db.normalizeId('entries', ref.holderId)
|
|
79
|
+
const entry = id ? await ctx.db.get(id) : null
|
|
80
|
+
if (!entry) continue
|
|
81
|
+
const uses = ref.uses ?? []
|
|
82
|
+
// Images carry one file for all languages; what differs per language is the description. `locale` is the first language of the site
|
|
83
|
+
// that still lacks it in this entry (null when none does), so "Used in" can say where the work is.
|
|
84
|
+
const lacking = uses.filter((u) => u.missingAlt).map((u) => valueAt(entry.data as Data, u.path) as { alt?: Record<string, Json> } | undefined)
|
|
85
|
+
const locale = ref.missingAlt === true
|
|
86
|
+
? schema.locales.find((l) => lacking.some((value) => !effectiveAlt(value?.alt, library, l))) ?? null
|
|
87
|
+
: null
|
|
88
|
+
out.push({
|
|
89
|
+
entry: entry._id, brick: entry.brick, title: labelOf(schema, entry.brick, entry.data as Data),
|
|
90
|
+
paths: uses.map((u) => u.path), fieldLabels: uses.map((u) => fieldLabelAt(schema, entry.brick, u.path)), locale,
|
|
91
|
+
missingAlt: ref.missingAlt === true, deleted: entry.deletedAt !== undefined,
|
|
92
|
+
})
|
|
93
|
+
}
|
|
94
|
+
return out
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/** How many entries use a file (deleted ones too, like `used`), and whether one of the live entries shows it without a description. */
|
|
98
|
+
async function usageOf(ctx: QueryCtx, file: Id<'uploads'>) {
|
|
99
|
+
const refs = await entryRefsOf(ctx, file)
|
|
100
|
+
let missingAlt = false
|
|
101
|
+
for (const ref of refs) {
|
|
102
|
+
if (ref.missingAlt !== true) continue
|
|
103
|
+
const id = ctx.db.normalizeId('entries', ref.holderId)
|
|
104
|
+
const entry = id ? await ctx.db.get(id) : null
|
|
105
|
+
// Like the "Missing alt text" view: an image that only a deleted entry shows is nothing to describe.
|
|
106
|
+
if (entry && !entry.deletedAt) {
|
|
107
|
+
missingAlt = true
|
|
108
|
+
break
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
return { usedCount: refs.length, missingAlt }
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
async function view(ctx: QueryCtx, row: Doc<'uploads'>) {
|
|
115
|
+
const url = row.status === 'ready' && row.processedId ? await ctx.storage.getUrl(row.processedId) : null
|
|
116
|
+
const usage = await usageOf(ctx, row._id)
|
|
117
|
+
return {
|
|
118
|
+
id: row._id, name: row.name, mime: row.mime, kind: kindOfUpload(row), size: row.size, width: row.width ?? null, height: row.height ?? null,
|
|
119
|
+
url, folder: row.folder ?? null, kept: row.kept === true, focus: row.focus ?? null, sha256: row.sha256 ?? null,
|
|
120
|
+
status: row.status, error: row.error ?? null, trashedAt: row.trashedAt ?? null, createdAt: row._creationTime, used: usage.usedCount > 0, usedCount: usage.usedCount, missingAlt: usage.missingAlt,
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
// ---- Views ----
|
|
125
|
+
|
|
126
|
+
type Read<T> = (after: string | null) => Promise<{ rows: T[]; full: boolean }>
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Reads steps of at most SCAN rows from `start` until a page of resolved files is full. The cursor
|
|
130
|
+
* names the last row looked at, so a call that filters a lot may return few files and `isDone: false`.
|
|
131
|
+
*/
|
|
132
|
+
async function walk<T>(start: string | null, read: Read<T>, cursorOf: (row: T) => string, resolve: (row: T) => Promise<Doc<'uploads'> | null>) {
|
|
133
|
+
const out: Doc<'uploads'>[] = []
|
|
134
|
+
const seen = new Set<string>()
|
|
135
|
+
let after = start
|
|
136
|
+
for (let scan = 0; scan < MAX_SCANS; scan++) {
|
|
137
|
+
const { rows, full } = await read(after)
|
|
138
|
+
for (const row of rows) {
|
|
139
|
+
after = cursorOf(row)
|
|
140
|
+
const file = await resolve(row)
|
|
141
|
+
if (!file || seen.has(file._id)) continue
|
|
142
|
+
seen.add(file._id)
|
|
143
|
+
out.push(file)
|
|
144
|
+
if (out.length >= PAGE) return { rows: out, cursor: after, isDone: false }
|
|
145
|
+
}
|
|
146
|
+
if (!full) return { rows: out, cursor: null, isDone: true }
|
|
147
|
+
}
|
|
148
|
+
return { rows: out, cursor: after, isDone: false }
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/** Search text as compared: any case, and letters without their accents (`über` finds `uber` and back). */
|
|
152
|
+
const fold = (text: string) => text.normalize('NFKD').replace(/\p{M}/gu, '').toLowerCase()
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* The files of a site in one view, newest first, 50 per call. `folder`: an id for one folder, null
|
|
156
|
+
* for the top level, absent for every folder. `search`: part of the file name or of its description in any
|
|
157
|
+
* language, any case. Pass the returned `cursor` for the next call; `isDone` says nothing is left.
|
|
158
|
+
*/
|
|
159
|
+
export const list = query({
|
|
160
|
+
args: {
|
|
161
|
+
site: v.string(),
|
|
162
|
+
view: v.union(v.literal('all'), v.literal('recent'), v.literal('unused'), v.literal('missingAlt'), v.literal('duplicates'), v.literal('problems'), v.literal('trash')),
|
|
163
|
+
folder: v.optional(v.union(v.string(), v.null())),
|
|
164
|
+
search: v.optional(v.string()),
|
|
165
|
+
cursor: v.optional(v.union(v.string(), v.null())),
|
|
166
|
+
},
|
|
167
|
+
handler: async (ctx, args) => {
|
|
168
|
+
const actor = await actorFor(ctx, args.site)
|
|
169
|
+
const site = actor.site._id
|
|
170
|
+
const folder = args.folder === undefined ? undefined : args.folder === null ? null : (await folderOf(ctx, actor, args.folder))._id
|
|
171
|
+
const needle = args.search?.trim() ? fold(args.search.trim()) : undefined
|
|
172
|
+
const start = args.cursor ?? null
|
|
173
|
+
// People search what a photo shows ("Wald"), not its camera name (IMG_4032.JPG): the descriptions count too.
|
|
174
|
+
const named = (row: Doc<'uploads'>) => !needle || [row.name, ...Object.values(row.alt ?? {})].some((text) => fold(text).includes(needle))
|
|
175
|
+
const live = (row: Doc<'uploads'>) => row.status === 'ready' && !row.trashedAt
|
|
176
|
+
const inFolder = (row: Doc<'uploads'>) => folder === undefined || (row.folder ?? null) === folder
|
|
177
|
+
const byTime = (row: Doc<'uploads'> | Doc<'fileRefs'>) => String(row._creationTime)
|
|
178
|
+
const before = (after: string | null) => (after === null ? undefined : Number(after))
|
|
179
|
+
|
|
180
|
+
let result: { rows: Doc<'uploads'>[]; cursor: string | null; isDone: boolean }
|
|
181
|
+
if (args.view === 'duplicates') {
|
|
182
|
+
// Groups of equal hash stay whole, so the cursor is the last hash looked at.
|
|
183
|
+
const rows: Doc<'uploads'>[] = []
|
|
184
|
+
let after = start ?? ''
|
|
185
|
+
let done = false
|
|
186
|
+
for (let i = 0; i < MAX_SCANS * SCAN && rows.length < PAGE; i++) {
|
|
187
|
+
const first = await ctx.db.query('uploads').withIndex('by_site_sha256', (q) => q.eq('site', site).gt('sha256', after)).first()
|
|
188
|
+
if (!first) {
|
|
189
|
+
done = true
|
|
190
|
+
break
|
|
191
|
+
}
|
|
192
|
+
after = first.sha256!
|
|
193
|
+
const group = (await ctx.db.query('uploads').withIndex('by_site_sha256', (q) => q.eq('site', site).eq('sha256', after)).collect()).filter(live)
|
|
194
|
+
if (group.length > 1) rows.push(...group.filter((r) => named(r) && inFolder(r)))
|
|
195
|
+
}
|
|
196
|
+
result = { rows, cursor: done ? null : after, isDone: done }
|
|
197
|
+
} else if (args.view === 'problems') {
|
|
198
|
+
// Failed uploads, newest first. A stuck `processing` one is queued again by uploads.requeueStale and ends here
|
|
199
|
+
// once it runs out of attempts. Unused failed uploads go with the daily sweep, so the list stays short.
|
|
200
|
+
const rows: Doc<'uploads'>[] = []
|
|
201
|
+
for await (const row of ctx.db.query('uploads').withIndex('by_status', (q) => q.eq('status', 'failed'))) {
|
|
202
|
+
if (row.site === site && !row.trashedAt && named(row)) rows.push(row)
|
|
203
|
+
}
|
|
204
|
+
rows.sort((a, b) => b._creationTime - a._creationTime)
|
|
205
|
+
result = { rows: rows.slice(0, PAGE), cursor: null, isDone: true }
|
|
206
|
+
} else if (args.view === 'trash') {
|
|
207
|
+
// Newest in the trash first; the cursor is "trashedAt:creation time" because many files can be trashed in one millisecond.
|
|
208
|
+
const [t, c] = (start ?? '').split(':').map(Number)
|
|
209
|
+
const read: Read<Doc<'uploads'>> = async () => {
|
|
210
|
+
const rows = await ctx.db.query('uploads').withIndex('by_site_trashed', (q) => (start === null ? q.eq('site', site).gt('trashedAt', 0) : q.eq('site', site).gt('trashedAt', 0).lte('trashedAt', t!))).order('desc').take(SCAN)
|
|
211
|
+
return { rows: rows.filter((r) => start === null || r.trashedAt! < t! || r._creationTime < c!), full: rows.length === SCAN }
|
|
212
|
+
}
|
|
213
|
+
result = await walk(start, read, (r) => `${r.trashedAt}:${r._creationTime}`, async (r) => (named(r) && inFolder(r) ? r : null))
|
|
214
|
+
} else if (args.view === 'missingAlt') {
|
|
215
|
+
const read: Read<Doc<'fileRefs'>> = async (after) => {
|
|
216
|
+
const rows = await ctx.db.query('fileRefs').withIndex('by_site_missingAlt', (q) => {
|
|
217
|
+
const base = q.eq('site', site).eq('missingAlt', true)
|
|
218
|
+
return after === null ? base : base.lt('_creationTime', before(after)!)
|
|
219
|
+
}).order('desc').take(SCAN)
|
|
220
|
+
return { rows, full: rows.length === SCAN }
|
|
221
|
+
}
|
|
222
|
+
result = await walk(start, read, byTime, async (ref) => {
|
|
223
|
+
const entryId = ctx.db.normalizeId('entries', ref.holderId)
|
|
224
|
+
const entry = entryId ? await ctx.db.get(entryId) : null
|
|
225
|
+
const file = await ctx.db.get(ref.file)
|
|
226
|
+
// An image that only a deleted entry shows is nothing to describe.
|
|
227
|
+
return entry && !entry.deletedAt && file && live(file) && named(file) && inFolder(file) ? file : null
|
|
228
|
+
})
|
|
229
|
+
} else {
|
|
230
|
+
// all, recent, unused: newest first. "all" follows the folder, the others look at every folder (unused respects `folder` when given).
|
|
231
|
+
const byFolder = args.view === 'all' && folder !== undefined
|
|
232
|
+
const read: Read<Doc<'uploads'>> = async (after) => {
|
|
233
|
+
const rows = byFolder
|
|
234
|
+
? await ctx.db.query('uploads').withIndex('by_site_folder', (q) => {
|
|
235
|
+
const base = q.eq('site', site).eq('folder', folder ?? undefined)
|
|
236
|
+
return after === null ? base : base.lt('_creationTime', before(after)!)
|
|
237
|
+
}).order('desc').take(SCAN)
|
|
238
|
+
: await ctx.db.query('uploads').withIndex('by_site', (q) => {
|
|
239
|
+
const base = q.eq('site', site)
|
|
240
|
+
return after === null ? base : base.lt('_creationTime', before(after)!)
|
|
241
|
+
}).order('desc').take(SCAN)
|
|
242
|
+
return { rows, full: rows.length === SCAN }
|
|
243
|
+
}
|
|
244
|
+
result = await walk(start, read, byTime, async (row) => {
|
|
245
|
+
if (!live(row) || !named(row)) return null
|
|
246
|
+
if (args.view === 'unused') return inFolder(row) && !(await entryRefsOf(ctx, row._id)).length ? row : null
|
|
247
|
+
return args.view === 'recent' || inFolder(row) ? row : null
|
|
248
|
+
})
|
|
249
|
+
}
|
|
250
|
+
return { files: await Promise.all(result.rows.map((row) => view(ctx, row))), cursor: result.cursor, isDone: result.isDone }
|
|
251
|
+
},
|
|
252
|
+
})
|
|
253
|
+
|
|
254
|
+
/** Where a file is used, for the agent door (SPEC 16): the same `usedIn` as `info`, for an actor already known. */
|
|
255
|
+
export async function usageAs(ctx: QueryCtx, actor: Actor, file: string) {
|
|
256
|
+
const row = await uploadOf(ctx, actor, file)
|
|
257
|
+
const usedIn = await usedInOf(ctx, actor.site.schema as Schema, await entryRefsOf(ctx, row._id))
|
|
258
|
+
return { file: row._id, name: row.name, usedIn }
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
/**
|
|
262
|
+
* One file in full: its data, URL, focus point, a suggestion for an alt text (the one most recently
|
|
263
|
+
* saved by an entry that shows this file, per language) and every entry that uses it. Deleted entries
|
|
264
|
+
* are listed too, marked `deleted`: they still block the trash until the retention removes them.
|
|
265
|
+
*/
|
|
266
|
+
export const info = query({
|
|
267
|
+
args: { site: v.string(), file: v.string() },
|
|
268
|
+
handler: async (ctx, args) => {
|
|
269
|
+
const actor = await actorFor(ctx, args.site)
|
|
270
|
+
const row = await uploadOf(ctx, actor, args.file)
|
|
271
|
+
const schema = actor.site.schema as Schema
|
|
272
|
+
const refs = await entryRefsOf(ctx, row._id)
|
|
273
|
+
const usedIn = await usedInOf(ctx, schema, refs, row.alt)
|
|
274
|
+
|
|
275
|
+
let suggestion: { at: number; alt: Record<string, Json> } | null = null
|
|
276
|
+
for (const ref of refs) {
|
|
277
|
+
const id = ctx.db.normalizeId('entries', ref.holderId)
|
|
278
|
+
const entry = id ? await ctx.db.get(id) : null
|
|
279
|
+
if (!entry || entry.deletedAt) continue
|
|
280
|
+
const latest = await ctx.db.query('changes').withIndex('by_entry', (q) => q.eq('entry', entry._id)).order('desc').first()
|
|
281
|
+
const at = latest?.at ?? entry._creationTime
|
|
282
|
+
for (const use of ref.uses ?? []) {
|
|
283
|
+
const alt = (valueAt(entry.data as Data, use.path) as { alt?: Record<string, Json> } | undefined)?.alt
|
|
284
|
+
const filled = Object.fromEntries(Object.entries(alt && typeof alt === 'object' ? alt : {}).filter(([, text]) => typeof text === 'string' && text.trim()))
|
|
285
|
+
if (Object.keys(filled).length && (!suggestion || at > suggestion.at)) suggestion = { at, alt: filled }
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
return {
|
|
289
|
+
...(await view(ctx, row)),
|
|
290
|
+
uploader: row.uploader,
|
|
291
|
+
uploadedBy: await (await labeler(ctx, actor)).who(row.uploader),
|
|
292
|
+
previous: (row.previous ?? []).map((p) => p.replacedAt),
|
|
293
|
+
alt: row.alt ?? {},
|
|
294
|
+
altSuggestion: suggestion?.alt ?? null,
|
|
295
|
+
usedIn,
|
|
296
|
+
}
|
|
297
|
+
},
|
|
298
|
+
})
|
|
299
|
+
|
|
300
|
+
/**
|
|
301
|
+
* The sidebar badge counts of the library: files that a live entry shows without a description in some
|
|
302
|
+
* language of the site (the "Missing alt text" view, counted once per file). Any member may read.
|
|
303
|
+
*/
|
|
304
|
+
export const counts = query({
|
|
305
|
+
args: { site: v.string() },
|
|
306
|
+
handler: async (ctx, args) => {
|
|
307
|
+
const actor = await actorFor(ctx, args.site)
|
|
308
|
+
const site = actor.site._id
|
|
309
|
+
const files = new Set<string>()
|
|
310
|
+
// At most COUNT_MAX references are read; a site with more is far beyond what a badge can tell, so the count stops there.
|
|
311
|
+
const refs = await ctx.db.query('fileRefs').withIndex('by_site_missingAlt', (q) => q.eq('site', site).eq('missingAlt', true)).take(COUNT_MAX)
|
|
312
|
+
for (const ref of refs) {
|
|
313
|
+
if (files.has(ref.file)) continue
|
|
314
|
+
const entryId = ctx.db.normalizeId('entries', ref.holderId)
|
|
315
|
+
const entry = entryId ? await ctx.db.get(entryId) : null
|
|
316
|
+
const file = entry && !entry.deletedAt ? await ctx.db.get(ref.file) : null
|
|
317
|
+
if (file && file.status === 'ready' && !file.trashedAt) files.add(file._id)
|
|
318
|
+
}
|
|
319
|
+
return { missingAlt: files.size }
|
|
320
|
+
},
|
|
321
|
+
})
|
|
322
|
+
|
|
323
|
+
/** All folders of the site, flat, with their parent (null at the top level), sorted by name in the site's main language ("Folder 2" before "Folder 10"). */
|
|
324
|
+
export const folders = query({
|
|
325
|
+
args: { site: v.string() },
|
|
326
|
+
handler: async (ctx, args) => {
|
|
327
|
+
const actor = await actorFor(ctx, args.site)
|
|
328
|
+
const rows = await ctx.db.query('folders').withIndex('by_site_parent', (q) => q.eq('site', actor.site._id)).collect()
|
|
329
|
+
return rows.map((f) => ({ id: f._id, name: f.name, parent: f.parent ?? null })).sort((a, b) => a.name.localeCompare(b.name, (actor.site.schema as Schema).locales[0], { numeric: true }))
|
|
330
|
+
},
|
|
331
|
+
})
|
|
332
|
+
|
|
333
|
+
// ---- Folders ----
|
|
334
|
+
|
|
335
|
+
/** A folder name: like a file name (no path, no control characters), 1 to 80 characters, unique among its siblings. */
|
|
336
|
+
async function checkFolderName(ctx: QueryCtx, site: Id<'sites'>, parent: Id<'folders'> | undefined, raw: string, self?: Id<'folders'>) {
|
|
337
|
+
const given = raw.trim()
|
|
338
|
+
if (!given) fail('INVALID', 'errors.folderNameRequired')
|
|
339
|
+
const name = cleanName(given)
|
|
340
|
+
if (name.length > FOLDER_NAME_MAX) fail('INVALID', 'errors.folderNameTooLong', { folderNameMax: FOLDER_NAME_MAX })
|
|
341
|
+
const siblings = await ctx.db.query('folders').withIndex('by_site_parent', (q) => q.eq('site', site).eq('parent', parent)).collect()
|
|
342
|
+
if (siblings.some((f) => f._id !== self && f.name.toLowerCase() === name.toLowerCase())) fail('INVALID', 'errors.folderNameTaken', { name })
|
|
343
|
+
return name
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
export const createFolder = mutation({
|
|
347
|
+
args: { site: v.string(), name: v.string(), parent: v.optional(v.union(v.string(), v.null())) },
|
|
348
|
+
handler: async (ctx, args) => {
|
|
349
|
+
const actor = await manager(ctx, args.site)
|
|
350
|
+
const parent = args.parent ? (await folderOf(ctx, actor, args.parent))._id : undefined
|
|
351
|
+
const name = await checkFolderName(ctx, actor.site._id, parent, args.name)
|
|
352
|
+
return ctx.db.insert('folders', { site: actor.site._id, name, ...(parent ? { parent } : {}) })
|
|
353
|
+
},
|
|
354
|
+
})
|
|
355
|
+
|
|
356
|
+
export const renameFolder = mutation({
|
|
357
|
+
args: { site: v.string(), folder: v.string(), name: v.string() },
|
|
358
|
+
handler: async (ctx, args) => {
|
|
359
|
+
const actor = await manager(ctx, args.site)
|
|
360
|
+
const folder = await folderOf(ctx, actor, args.folder)
|
|
361
|
+
await ctx.db.patch(folder._id, { name: await checkFolderName(ctx, actor.site._id, folder.parent, args.name, folder._id) })
|
|
362
|
+
return null
|
|
363
|
+
},
|
|
364
|
+
})
|
|
365
|
+
|
|
366
|
+
/** Moves a folder under another one (null: to the top level). A folder cannot go into itself or into one of its own subfolders. */
|
|
367
|
+
export const moveFolder = mutation({
|
|
368
|
+
args: { site: v.string(), folder: v.string(), parent: v.union(v.string(), v.null()) },
|
|
369
|
+
handler: async (ctx, args) => {
|
|
370
|
+
const actor = await manager(ctx, args.site)
|
|
371
|
+
const folder = await folderOf(ctx, actor, args.folder)
|
|
372
|
+
const parent = args.parent === null ? undefined : await folderOf(ctx, actor, args.parent)
|
|
373
|
+
for (let up: Doc<'folders'> | null | undefined = parent; up; up = up.parent ? await ctx.db.get(up.parent) : null) {
|
|
374
|
+
if (up._id === folder._id) fail('INVALID', 'errors.folderCannotNestInSelf')
|
|
375
|
+
}
|
|
376
|
+
await checkFolderName(ctx, actor.site._id, parent?._id, folder.name, folder._id)
|
|
377
|
+
await ctx.db.patch(folder._id, { parent: parent?._id })
|
|
378
|
+
return null
|
|
379
|
+
},
|
|
380
|
+
})
|
|
381
|
+
|
|
382
|
+
/** Deletes a folder only when it is empty: no subfolders and no files, trashed files included. */
|
|
383
|
+
export const deleteFolder = mutation({
|
|
384
|
+
args: { site: v.string(), folder: v.string() },
|
|
385
|
+
handler: async (ctx, args) => {
|
|
386
|
+
const actor = await manager(ctx, args.site)
|
|
387
|
+
const folder = await folderOf(ctx, actor, args.folder)
|
|
388
|
+
const child = await ctx.db.query('folders').withIndex('by_site_parent', (q) => q.eq('site', actor.site._id).eq('parent', folder._id)).first()
|
|
389
|
+
const inside = await ctx.db.query('uploads').withIndex('by_site_folder', (q) => q.eq('site', actor.site._id).eq('folder', folder._id)).collect()
|
|
390
|
+
// A file in the Trash is not content: the person cannot see it in the folder. It loses the folder, so a restore puts it in All files.
|
|
391
|
+
if (child || inside.some((row) => !row.trashedAt)) fail('INVALID', 'errors.folderNotEmpty')
|
|
392
|
+
for (const row of inside) await ctx.db.patch(row._id, { folder: undefined })
|
|
393
|
+
await ctx.db.delete(folder._id)
|
|
394
|
+
return null
|
|
395
|
+
},
|
|
396
|
+
})
|
|
397
|
+
|
|
398
|
+
// ---- Files ----
|
|
399
|
+
|
|
400
|
+
/** Moves files into a folder (null: out of all folders). A file in a folder is kept. */
|
|
401
|
+
export const move = mutation({
|
|
402
|
+
args: { site: v.string(), files: ids, folder: v.union(v.string(), v.null()) },
|
|
403
|
+
handler: async (ctx, args) => {
|
|
404
|
+
const actor = await manager(ctx, args.site)
|
|
405
|
+
const rows = await uploadsOf(ctx, actor, args.files)
|
|
406
|
+
const folder = args.folder === null ? undefined : await folderOf(ctx, actor, args.folder)
|
|
407
|
+
for (const row of rows) await ctx.db.patch(row._id, { folder: folder?._id, ...(folder ? { kept: true } : {}) })
|
|
408
|
+
return null
|
|
409
|
+
},
|
|
410
|
+
})
|
|
411
|
+
|
|
412
|
+
/** Renames a file. The name goes through the same cleaning as an upload name. */
|
|
413
|
+
export const rename = mutation({
|
|
414
|
+
args: { site: v.string(), file: v.string(), name: v.string() },
|
|
415
|
+
handler: async (ctx, args) => {
|
|
416
|
+
const actor = await manager(ctx, args.site)
|
|
417
|
+
const row = await uploadOf(ctx, actor, args.file)
|
|
418
|
+
const given = args.name.trim()
|
|
419
|
+
if (!given || given.length > 200) fail('INVALID', 'errors.fileNameInvalid')
|
|
420
|
+
await ctx.db.patch(row._id, { name: cleanName(given) })
|
|
421
|
+
return null
|
|
422
|
+
},
|
|
423
|
+
})
|
|
424
|
+
|
|
425
|
+
/**
|
|
426
|
+
* Sets (or with null, clears) the focus point of an image: x and y from 0 to 1. The bundle carries the point (and the
|
|
427
|
+
* text trees, cached per revision, carry it as `focal`): a changed point of a used file raises contentRev like a save.
|
|
428
|
+
*/
|
|
429
|
+
export const setFocus = mutation({
|
|
430
|
+
args: { site: v.string(), file: v.string(), focus: v.union(v.object({ x: v.number(), y: v.number() }), v.null()) },
|
|
431
|
+
handler: async (ctx, args) => {
|
|
432
|
+
const actor = await manager(ctx, args.site)
|
|
433
|
+
const row = await uploadOf(ctx, actor, args.file)
|
|
434
|
+
if (kindOfUpload(row) !== 'image') fail('INVALID', 'errors.onlyImageHasFocus')
|
|
435
|
+
const { focus } = args
|
|
436
|
+
if (focus && !(focus.x >= 0 && focus.x <= 1 && focus.y >= 0 && focus.y <= 1)) fail('INVALID', 'errors.focusPointOutsideImage')
|
|
437
|
+
if (focus?.x === row.focus?.x && focus?.y === row.focus?.y) return null
|
|
438
|
+
await ctx.db.patch(row._id, { focus: focus ?? undefined })
|
|
439
|
+
if ((await entryRefsOf(ctx, row._id)).length) await ctx.db.patch(actor.site._id, { contentRev: actor.site.contentRev + 1 })
|
|
440
|
+
return null
|
|
441
|
+
},
|
|
442
|
+
})
|
|
443
|
+
|
|
444
|
+
/**
|
|
445
|
+
* Writes the description (alt text) of a photo in one language of the site: the default for every use
|
|
446
|
+
* of the photo that has no text of its own. Empty text removes that language. The "missing alt text"
|
|
447
|
+
* marks of all entries that show the photo are recomputed in the same transaction. Text trees, cached per revision,
|
|
448
|
+
* carry the description of a picture without its own: a changed text of a used file raises contentRev like a save.
|
|
449
|
+
*/
|
|
450
|
+
export const setAlt = mutation({
|
|
451
|
+
args: { site: v.string(), file: v.string(), locale: v.string(), text: v.string() },
|
|
452
|
+
handler: async (ctx, args) => {
|
|
453
|
+
const actor = await manager(ctx, args.site)
|
|
454
|
+
const row = await uploadOf(ctx, actor, args.file)
|
|
455
|
+
if (kindOfUpload(row) !== 'image') fail('INVALID', 'errors.onlyImageHasAlt')
|
|
456
|
+
const schema = actor.site.schema as Schema
|
|
457
|
+
if (!schema.locales.includes(args.locale)) fail('INVALID', 'errors.languageNotOnSite', { locale: args.locale })
|
|
458
|
+
const text = args.text.trim()
|
|
459
|
+
if (text.length > ALT_MAX) fail('INVALID', 'errors.altTooLong')
|
|
460
|
+
if (ALT_INVISIBLE.test(text)) fail('INVALID', 'errors.altControlChars')
|
|
461
|
+
const alt = { ...(row.alt ?? {}) }
|
|
462
|
+
const changed = (alt[args.locale] ?? '') !== text
|
|
463
|
+
if (text) alt[args.locale] = text
|
|
464
|
+
else delete alt[args.locale]
|
|
465
|
+
await ctx.db.patch(row._id, { alt: Object.keys(alt).length ? alt : undefined })
|
|
466
|
+
|
|
467
|
+
const refs = await entryRefsOf(ctx, row._id)
|
|
468
|
+
if (changed && refs.length) await ctx.db.patch(actor.site._id, { contentRev: actor.site.contentRev + 1 })
|
|
469
|
+
// Only the entry's own references carry uses; 'change' ones are history and 'pending' ones hold no uses.
|
|
470
|
+
for (const ref of refs) {
|
|
471
|
+
if (ref.holder !== 'entry') continue
|
|
472
|
+
const id = ctx.db.normalizeId('entries', ref.holderId)
|
|
473
|
+
const entry = id ? await ctx.db.get(id) : null
|
|
474
|
+
const brick = entry && Object.hasOwn(schema.bricks, entry.brick) ? schema.bricks[entry.brick] : undefined
|
|
475
|
+
if (!entry || !brick) continue
|
|
476
|
+
const uses = usesOf(brick.fields, entry.data as Data, schema.locales, uploadIdsOf(ctx), (file) => (file === row._id ? alt : undefined)).get(row._id)
|
|
477
|
+
if (!uses) continue
|
|
478
|
+
const missingAlt = uses.some((u) => u.missingAlt)
|
|
479
|
+
if (!same(ref.uses, uses) || ref.missingAlt !== missingAlt) await ctx.db.patch(ref._id, { uses, missingAlt })
|
|
480
|
+
}
|
|
481
|
+
return null
|
|
482
|
+
},
|
|
483
|
+
})
|
|
484
|
+
|
|
485
|
+
/** Keeps files on purpose: the 7-day sweep of unused uploads leaves them alone. */
|
|
486
|
+
export const keep = mutation({
|
|
487
|
+
args: { site: v.string(), files: ids },
|
|
488
|
+
handler: async (ctx, args) => {
|
|
489
|
+
const actor = await manager(ctx, args.site)
|
|
490
|
+
for (const row of await uploadsOf(ctx, actor, args.files)) await ctx.db.patch(row._id, { kept: true })
|
|
491
|
+
return null
|
|
492
|
+
},
|
|
493
|
+
})
|
|
494
|
+
|
|
495
|
+
/**
|
|
496
|
+
* Moves unused files to the trash. A file that any entry uses, a deleted entry included, is not
|
|
497
|
+
* trashed: it comes back in `refused` with the entries that use it. Already trashed files are skipped.
|
|
498
|
+
*/
|
|
499
|
+
export const trash = mutation({
|
|
500
|
+
args: { site: v.string(), files: ids },
|
|
501
|
+
handler: async (ctx, args) => {
|
|
502
|
+
const actor = await manager(ctx, args.site)
|
|
503
|
+
const schema = actor.site.schema as Schema
|
|
504
|
+
const now = Date.now()
|
|
505
|
+
const trashed: string[] = []
|
|
506
|
+
const refused: { file: string; name: string; usedIn: Awaited<ReturnType<typeof usedInOf>> }[] = []
|
|
507
|
+
for (const row of await uploadsOf(ctx, actor, args.files)) {
|
|
508
|
+
if (row.trashedAt) continue
|
|
509
|
+
const refs = await entryRefsOf(ctx, row._id)
|
|
510
|
+
if (refs.length) {
|
|
511
|
+
refused.push({ file: row._id, name: row.name, usedIn: await usedInOf(ctx, schema, refs) })
|
|
512
|
+
continue
|
|
513
|
+
}
|
|
514
|
+
await ctx.db.patch(row._id, { trashedAt: now })
|
|
515
|
+
trashed.push(row._id)
|
|
516
|
+
}
|
|
517
|
+
return { trashed, refused }
|
|
518
|
+
},
|
|
519
|
+
})
|
|
520
|
+
|
|
521
|
+
/** Takes files out of the trash. */
|
|
522
|
+
export const restore = mutation({
|
|
523
|
+
args: { site: v.string(), files: ids },
|
|
524
|
+
handler: async (ctx, args) => {
|
|
525
|
+
const actor = await manager(ctx, args.site)
|
|
526
|
+
const restored: string[] = []
|
|
527
|
+
for (const row of await uploadsOf(ctx, actor, args.files)) {
|
|
528
|
+
if (!row.trashedAt) continue
|
|
529
|
+
await ctx.db.patch(row._id, { trashedAt: undefined })
|
|
530
|
+
restored.push(row._id)
|
|
531
|
+
}
|
|
532
|
+
return { restored }
|
|
533
|
+
},
|
|
534
|
+
})
|
|
535
|
+
|
|
536
|
+
/**
|
|
537
|
+
* Puts a new file in the place of `file`. The new file is first uploaded the usual way (ticket,
|
|
538
|
+
* POST /upload, processing) and arrives here as `upload`, ready and not yet used. It must be the
|
|
539
|
+
* same kind (image for image, PDF for PDF). `file` keeps its ID, so every entry keeps pointing at
|
|
540
|
+
* it; it takes over the new content, name and focus stay as they are (focus is cleared: the picture
|
|
541
|
+
* changed), and the old stored file moves to `previous` for 30 days. The temporary upload row goes.
|
|
542
|
+
* If an entry uses the file, contentRev rises like after a save.
|
|
543
|
+
*/
|
|
544
|
+
export const replace = mutation({
|
|
545
|
+
args: { site: v.string(), file: v.string(), upload: v.string() },
|
|
546
|
+
handler: async (ctx, args) => {
|
|
547
|
+
const actor = await manager(ctx, args.site)
|
|
548
|
+
const row = await uploadOf(ctx, actor, args.file)
|
|
549
|
+
const fresh = await uploadOf(ctx, actor, args.upload)
|
|
550
|
+
if (row._id === fresh._id) fail('INVALID', 'errors.replacementMustDiffer')
|
|
551
|
+
if (row.trashedAt) fail('INVALID', 'errors.fileInTrash')
|
|
552
|
+
if (row.status !== 'ready') fail('INVALID', 'errors.fileNotReady')
|
|
553
|
+
if (fresh.status !== 'ready' || !fresh.processedId) fail('INVALID', 'errors.newFileNotReady')
|
|
554
|
+
if (kindOfUpload(row) !== kindOfUpload(fresh)) fail('INVALID', row.mime.startsWith('image/') ? 'errors.imageOnlyReplacedByImage' : 'errors.pdfOnlyReplacedByPdf')
|
|
555
|
+
if ((await ctx.db.query('fileRefs').withIndex('by_file', (q) => q.eq('file', fresh._id)).first())) fail('INVALID', 'errors.newFileAlreadyInUse')
|
|
556
|
+
await ctx.db.patch(row._id, {
|
|
557
|
+
storageId: fresh.storageId, processedId: fresh.processedId, variants: fresh.variants, size: fresh.size, mime: fresh.mime,
|
|
558
|
+
width: fresh.width, height: fresh.height, sha256: fresh.sha256, focus: undefined,
|
|
559
|
+
previous: [...(row.previous ?? []), { storageId: row.storageId, ...(row.processedId ? { processedId: row.processedId } : {}), ...(row.variants?.length ? { variants: row.variants } : {}), replacedAt: Date.now() }],
|
|
560
|
+
hasPrevious: true,
|
|
561
|
+
})
|
|
562
|
+
await ctx.db.delete(fresh._id)
|
|
563
|
+
// Bundles carry the file URL: a used file means new content, so sites fetch the new URL before the old file is purged.
|
|
564
|
+
if ((await entryRefsOf(ctx, row._id)).length) await ctx.db.patch(actor.site._id, { contentRev: actor.site.contentRev + 1 })
|
|
565
|
+
return null
|
|
566
|
+
},
|
|
567
|
+
})
|
|
568
|
+
|
|
569
|
+
// ---- Check of the index ----
|
|
570
|
+
|
|
571
|
+
type Expected = Map<string, { entry: Id<'entries'>; file: string; uses: { path: string; missingAlt: boolean }[] }>
|
|
572
|
+
|
|
573
|
+
/**
|
|
574
|
+
* Recomputes the file references of every entry of the site from the entries and compares them
|
|
575
|
+
* with the stored rows (the index behind "used in" and the views). Returns the differences:
|
|
576
|
+
* `missing` (an entry uses a file, no row), `extra` (a row without a use), `stale` (uses or
|
|
577
|
+
* missingAlt differ). With `fix: true` it also corrects them. Entries of a brick the schema no
|
|
578
|
+
* longer has are left alone.
|
|
579
|
+
*/
|
|
580
|
+
export const rebuildRefs = internalMutation({
|
|
581
|
+
args: { site: v.id('sites'), fix: v.optional(v.boolean()) },
|
|
582
|
+
handler: async (ctx, args) => {
|
|
583
|
+
const site = await ctx.db.get(args.site)
|
|
584
|
+
if (!site) fail('SITE_NOT_FOUND', 'errors.siteNotFound')
|
|
585
|
+
const schema = site.schema as Schema
|
|
586
|
+
const entries = await ctx.db.query('entries').withIndex('by_site_brick_order', (q) => q.eq('site', args.site)).collect()
|
|
587
|
+
const skip = new Set<string>()
|
|
588
|
+
const expected: Expected = new Map()
|
|
589
|
+
for (const entry of entries) {
|
|
590
|
+
const brick = Object.hasOwn(schema.bricks, entry.brick) ? schema.bricks[entry.brick] : undefined
|
|
591
|
+
if (!brick) {
|
|
592
|
+
skip.add(entry._id)
|
|
593
|
+
continue
|
|
594
|
+
}
|
|
595
|
+
for (const [file, uses] of await entryUses(ctx, brick.fields, entry.data as Data, schema.locales)) expected.set(`${entry._id}\n${file}`, { entry: entry._id, file, uses })
|
|
596
|
+
}
|
|
597
|
+
const rows = await ctx.db.query('fileRefs').withIndex('by_site_holder', (q) => q.eq('site', args.site).eq('holder', 'entry')).collect()
|
|
598
|
+
const found = new Set<string>()
|
|
599
|
+
const extra: { entry: string; file: string }[] = []
|
|
600
|
+
const stale: { entry: string; file: string }[] = []
|
|
601
|
+
for (const row of rows) {
|
|
602
|
+
if (skip.has(row.holderId)) continue
|
|
603
|
+
const key = `${row.holderId}\n${row.file}`
|
|
604
|
+
const want = expected.get(key)
|
|
605
|
+
if (!want || found.has(key)) {
|
|
606
|
+
extra.push({ entry: row.holderId, file: row.file })
|
|
607
|
+
if (args.fix) await ctx.db.delete(row._id)
|
|
608
|
+
continue
|
|
609
|
+
}
|
|
610
|
+
found.add(key)
|
|
611
|
+
const missingAlt = want.uses.some((u) => u.missingAlt)
|
|
612
|
+
if (!same(row.uses, want.uses) || row.missingAlt !== missingAlt) {
|
|
613
|
+
stale.push({ entry: row.holderId, file: row.file })
|
|
614
|
+
if (args.fix) await ctx.db.patch(row._id, { uses: want.uses, missingAlt })
|
|
615
|
+
}
|
|
616
|
+
}
|
|
617
|
+
const missing: { entry: string; file: string }[] = []
|
|
618
|
+
for (const [key, want] of expected) {
|
|
619
|
+
if (found.has(key)) continue
|
|
620
|
+
// An id that is no upload of this site (a leftover in the data) cannot have a row; that is a data problem, not an index one.
|
|
621
|
+
const upload = ctx.db.normalizeId('uploads', want.file)
|
|
622
|
+
if (!upload || (await ctx.db.get(upload))?.site !== args.site) continue
|
|
623
|
+
missing.push({ entry: want.entry, file: want.file })
|
|
624
|
+
if (args.fix) {
|
|
625
|
+
await ctx.db.insert('fileRefs', { site: args.site, file: upload, holder: 'entry', holderId: want.entry, uses: want.uses, missingAlt: want.uses.some((u) => u.missingAlt) })
|
|
626
|
+
}
|
|
627
|
+
}
|
|
628
|
+
return { missing, extra, stale, fixed: args.fix === true }
|
|
629
|
+
},
|
|
630
|
+
})
|
|
631
|
+
|
package/convex/http.ts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
// The platform's HTTP surface. Each area registers its own routes; this file only composes.
|
|
2
|
+
import { httpRouter } from 'convex/server'
|
|
3
|
+
import { auth } from './auth'
|
|
4
|
+
import { register as bundle } from './routes/bundle'
|
|
5
|
+
import { register as connect } from './routes/connect'
|
|
6
|
+
import { register as exportSite } from './routes/export'
|
|
7
|
+
import { register as file } from './routes/file'
|
|
8
|
+
import { register as mcp } from './routes/mcp'
|
|
9
|
+
import { register as release } from './routes/release'
|
|
10
|
+
import { register as upload } from './routes/upload'
|
|
11
|
+
|
|
12
|
+
const http = httpRouter()
|
|
13
|
+
auth.registerRoutes(http) // /api/auth/* (SPEC 10, Better Auth)
|
|
14
|
+
bundle(http) // GET /bundle/<site> (SPEC 6)
|
|
15
|
+
release(http) // GET /release/state, POST /release (SPEC 5)
|
|
16
|
+
file(http) // GET /file/<site>/<upload>/<storage>, a stable address for a site's files
|
|
17
|
+
exportSite(http) // GET /export (SPEC 9, 12)
|
|
18
|
+
upload(http) // POST /upload with a one-time ticket (SPEC 4.3)
|
|
19
|
+
mcp(http) // POST /mcp, the agent door (SPEC 16)
|
|
20
|
+
connect(http) // POST /connect/start, /connect/poll: `ginko connect` (connect.ts)
|
|
21
|
+
export default http
|