pups-client 0.7.0 → 0.9.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/README.md +7 -7
- package/dist/discovery.d.ts +5 -0
- package/dist/discovery.d.ts.map +1 -1
- package/dist/discovery.js +1 -0
- package/dist/discovery.js.map +1 -1
- package/dist/errors.js +1 -1
- package/dist/errors.js.map +1 -1
- package/dist/feed.d.ts +48 -74
- package/dist/feed.d.ts.map +1 -1
- package/dist/feed.js +138 -574
- package/dist/feed.js.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js.map +1 -1
- package/dist/mentions.d.ts.map +1 -1
- package/dist/mentions.js +2 -2
- package/dist/mentions.js.map +1 -1
- package/dist/session.d.ts +11 -0
- package/dist/session.d.ts.map +1 -1
- package/dist/session.js +13 -0
- package/dist/session.js.map +1 -1
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/package.json +1 -1
- package/src/discovery.ts +6 -0
- package/src/errors.ts +1 -1
- package/src/feed.ts +147 -607
- package/src/index.ts +0 -1
- package/src/mentions.ts +2 -3
- package/src/session.ts +15 -1
- package/src/version.ts +1 -1
package/src/feed.ts
CHANGED
|
@@ -12,7 +12,7 @@ export const FEED_REL = 'https://bigwide.net/ns/feed'
|
|
|
12
12
|
|
|
13
13
|
/**
|
|
14
14
|
* The site property in `host-meta.json` with the URL template of the pages that show single posts, e.g.
|
|
15
|
-
* `https://tim.example/p/{feed}/post/{index}` (RFC-6 "Post URLs"
|
|
15
|
+
* `https://tim.example/p/{feed}/post/{index}` (RFC-6 "Post URLs"). The server fills it into new posts.
|
|
16
16
|
*/
|
|
17
17
|
export const POST_URL_TEMPLATE = 'https://bigwide.net/ns/post-url-template'
|
|
18
18
|
|
|
@@ -26,20 +26,16 @@ const RFC1_URL = 'https://github.com/bigwide-net/RFCs/blob/main/RFC-1-Source-for
|
|
|
26
26
|
const RFC4_URL = 'https://github.com/bigwide-net/RFCs/blob/main/RFC-4-Feed-Submissions.md'
|
|
27
27
|
const JSON_FEED_VERSION = 'https://jsonfeed.org/version/1.1'
|
|
28
28
|
const FEED_CONTENT_TYPE = 'application/feed+json'
|
|
29
|
-
const maxArchiveNumber = 999_999
|
|
30
|
-
/** The feed file keeps at least this many items, also when `maxBytes` is exceeded (RFC-7 "Size in bytes"). */
|
|
31
|
-
const minFeedItems = 10
|
|
32
|
-
|
|
33
|
-
const mainComment = `This feed is maintained by apps through a Personal Upload Server (PUPS), following ${PUPS_FEED_SPEC}. It contains the newest items. If it has _archive.archive_url, all items are in the archives, starting with the one there.`
|
|
34
|
-
const archiveComment = (feedUrl: string) =>
|
|
35
|
-
`This is an archive of ${feedUrl}, maintained by apps through a Personal Upload Server (PUPS), following ${PUPS_FEED_SPEC}. New items appear in the feed at _archive.updates_url, older ones are in the archive at next_url.`
|
|
36
29
|
|
|
37
30
|
/** Feed names (RFC-7 "Named feeds"): 1 to 40 lowercase letters, digits and `-`, not starting or ending with `-`. */
|
|
38
31
|
const feedNamePattern = /^[a-z0-9](?:[a-z0-9-]{0,38}[a-z0-9])?$/
|
|
39
32
|
|
|
40
|
-
/** The main feed's media directories in `/feed
|
|
33
|
+
/** The main feed's media directories in `/feed/`. */
|
|
41
34
|
const mainMediaFolders = ['images', 'media']
|
|
42
35
|
|
|
36
|
+
/** Names in `/feed/` that belong to the main feed, its media and comment feeds, and therefore can't be feed names. */
|
|
37
|
+
const reservedNames = [...mainMediaFolders, 'comments']
|
|
38
|
+
|
|
43
39
|
/** Top-level fields that are fixed or managed by RFC-7, and can't be set through `create()` or `updateSettings()`. */
|
|
44
40
|
const reservedFields = new Set(['version', 'home_page_url', 'feed_url', 'user_comment', 'next_url', 'items', 'hubs', '_pups', '_archive', 'icon', 'favicon'])
|
|
45
41
|
|
|
@@ -88,9 +84,6 @@ export interface ItemArchive {
|
|
|
88
84
|
[field: string]: unknown
|
|
89
85
|
}
|
|
90
86
|
|
|
91
|
-
/** @deprecated The index moved from `_pups` to `_archive` (RFC-13): use ItemArchive. */
|
|
92
|
-
export type ItemMarker = ItemArchive
|
|
93
|
-
|
|
94
87
|
/** The `_pups` object of a feed file or archive: the marker of RFC-7. */
|
|
95
88
|
export interface PupsMarker {
|
|
96
89
|
about?: string
|
|
@@ -98,7 +91,7 @@ export interface PupsMarker {
|
|
|
98
91
|
[field: string]: unknown
|
|
99
92
|
}
|
|
100
93
|
|
|
101
|
-
/** The `_archive` object of a feed file or archive (RFC-13). */
|
|
94
|
+
/** The `_archive` object of a feed file or archive (RFC-13). The server writes it. */
|
|
102
95
|
export interface ArchiveFields {
|
|
103
96
|
about?: string
|
|
104
97
|
/** In the feed file: the public URL of the newest archive. */
|
|
@@ -111,10 +104,7 @@ export interface ArchiveFields {
|
|
|
111
104
|
updates_url?: string
|
|
112
105
|
/** In an archive: its number, from 0 for the oldest. */
|
|
113
106
|
number?: number
|
|
114
|
-
/**
|
|
115
|
-
* The index the next new item gets (RFC-13 "Indexes"). Valid in the newest archive, or in the feed file of a feed
|
|
116
|
-
* without archives.
|
|
117
|
-
*/
|
|
107
|
+
/** In the newest archive: the index the next new item gets (RFC-13 "Indexes"). */
|
|
118
108
|
next_index?: number
|
|
119
109
|
[field: string]: unknown
|
|
120
110
|
}
|
|
@@ -164,6 +154,8 @@ export interface CommentPolicy {
|
|
|
164
154
|
requireSocialSpec?: boolean
|
|
165
155
|
allowEdit?: boolean
|
|
166
156
|
allowDelete?: boolean
|
|
157
|
+
commentLimit?: number
|
|
158
|
+
maxDepth?: number
|
|
167
159
|
[rule: string]: unknown
|
|
168
160
|
}
|
|
169
161
|
|
|
@@ -222,13 +214,13 @@ export type PostChanges = { [K in Exclude<keyof PostInput, 'id'>]?: PostInput[K]
|
|
|
222
214
|
|
|
223
215
|
export interface PostResult {
|
|
224
216
|
id: string
|
|
225
|
-
/** The item's index (RFC-
|
|
217
|
+
/** The item's index (RFC-13 "Indexes"). */
|
|
226
218
|
index?: number
|
|
227
219
|
/** The item as stored. */
|
|
228
220
|
item: FeedItem
|
|
229
221
|
/**
|
|
230
|
-
* The URL of
|
|
231
|
-
* as the `source` of an RFC-4 webmention.
|
|
222
|
+
* The item's address: the URL of its archive plus `#<id>` (RFC-13 "Items"). Items never leave their archive, so it
|
|
223
|
+
* never changes; use it e.g. as the `source` of an RFC-4 webmention. The post's page, if it has one, is `item.url`.
|
|
232
224
|
*/
|
|
233
225
|
url: string
|
|
234
226
|
/**
|
|
@@ -250,7 +242,7 @@ export interface MediaResult {
|
|
|
250
242
|
export type FeedState =
|
|
251
243
|
/** There is no feed file yet. `create()` sets it up. */
|
|
252
244
|
| { status: 'missing' }
|
|
253
|
-
/** A feed
|
|
245
|
+
/** A feed kept by the PUPS server, ready to write. */
|
|
254
246
|
| { status: 'pups'; feed: FeedDocument; etag: string }
|
|
255
247
|
/** A JSON Feed without `_pups.pupsControlled`: maintained by other means, and never written (RFC-7). */
|
|
256
248
|
| { status: 'foreign'; feed: FeedDocument }
|
|
@@ -258,28 +250,8 @@ export type FeedState =
|
|
|
258
250
|
| { status: 'invalid' }
|
|
259
251
|
|
|
260
252
|
export interface FeedOptions {
|
|
261
|
-
/** How
|
|
262
|
-
maxItems?: number
|
|
263
|
-
/** How many items an archive holds. Default 20 (RFC-7 "Archives"); other clients expect 20. */
|
|
264
|
-
archiveSize?: number
|
|
265
|
-
/**
|
|
266
|
-
* Whether to start archives for a feed that has none, on its next post (RFC-7 "Starting the archives").
|
|
267
|
-
* Default true. With false, posts to such a feed only go into the feed file, and items beyond `maxItems` are
|
|
268
|
-
* dropped. Feeds that have archives always keep them.
|
|
269
|
-
*/
|
|
270
|
-
startArchives?: boolean
|
|
271
|
-
/**
|
|
272
|
-
* Optional: keep fewer items in the feed file when it would be larger than this many bytes, but at least 10
|
|
273
|
-
* (RFC-7 "Size in bytes").
|
|
274
|
-
*/
|
|
275
|
-
maxBytes?: number
|
|
276
|
-
/** How often an operation is repeated when another client changed the feed in between. Default 8. */
|
|
253
|
+
/** How often a change of the feed's settings is repeated when someone changed the feed in between. Default 8. */
|
|
277
254
|
maxAttempts?: number
|
|
278
|
-
/**
|
|
279
|
-
* Whether posts of a feed with archives get a `url` from the site's post URL template, if `host-meta.json`
|
|
280
|
-
* declares one and the post has no `url` (RFC-7 "Post URLs"). Default true.
|
|
281
|
-
*/
|
|
282
|
-
postUrls?: boolean
|
|
283
255
|
/**
|
|
284
256
|
* Whether to send RFC-11 mentions after `addPost()`, `editPost()` and `deletePost()`: to the domains a post names
|
|
285
257
|
* (`@tim.example.com`) and to the author of the item it answers. `true` uses `session.mentions`. Default false.
|
|
@@ -312,10 +284,10 @@ interface Loaded {
|
|
|
312
284
|
etag: string
|
|
313
285
|
}
|
|
314
286
|
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
287
|
+
/** The server's answer to a POST of an item (RFC-7 "Posting items"). */
|
|
288
|
+
interface Posted {
|
|
289
|
+
item: FeedItem
|
|
290
|
+
address: string
|
|
319
291
|
}
|
|
320
292
|
|
|
321
293
|
/** The scopes an app needs to write a feed: the feed file and the feed directory (RFC-7 "Server requirements"). */
|
|
@@ -328,12 +300,11 @@ function feedPaths(name?: string): { path: `/${string}`; directory: `/${string}`
|
|
|
328
300
|
if (name === undefined) return { path: '/feed.json', directory: '/feed/' }
|
|
329
301
|
if (!feedNamePattern.test(name))
|
|
330
302
|
throw new PupsError('invalid_request', `"${name}" is not a valid feed name: use 1 to 40 lowercase letters, digits and "-", not starting or ending with "-" (RFC-7, Named feeds).`)
|
|
331
|
-
if (
|
|
303
|
+
if (reservedNames.includes(name))
|
|
332
304
|
throw new PupsError('invalid_request', `"${name}" can't be a feed name: /feed/${name}/ and its files belong to the main feed (RFC-7, Named feeds).`)
|
|
333
305
|
return { path: `/feed/${name}.json`, directory: `/feed/${name}/` }
|
|
334
306
|
}
|
|
335
307
|
|
|
336
|
-
|
|
337
308
|
const isFeedDocument = (value: unknown): value is FeedDocument =>
|
|
338
309
|
isObject(value) && typeof value.version == 'string' && value.version.startsWith('https://jsonfeed.org/version/1')
|
|
339
310
|
&& Array.isArray(value.items) && value.items.every(i => isObject(i) && typeof i.id == 'string')
|
|
@@ -341,9 +312,7 @@ const isFeedDocument = (value: unknown): value is FeedDocument =>
|
|
|
341
312
|
const isControlled = (doc: FeedDocument) => doc._pups?.pupsControlled === true
|
|
342
313
|
|
|
343
314
|
const notControlled = (path: string) =>
|
|
344
|
-
new PupsError('not_pups_controlled', `${path} is not
|
|
345
|
-
|
|
346
|
-
const conflict = (what: string) => new PupsError('precondition_failed', `${what} changed in the meantime.`)
|
|
315
|
+
new PupsError('not_pups_controlled', `${path} is not kept by PUPS: it has no "_pups": { "pupsControlled": true }. It is never written; the owner can hand it over by adding that marker (RFC-7, The _pups object).`)
|
|
347
316
|
|
|
348
317
|
const now = () => new Date().toISOString().replace(/\.\d{3}Z$/, 'Z')
|
|
349
318
|
|
|
@@ -353,88 +322,12 @@ const serialize = (doc: FeedDocument) => JSON.stringify(doc, null, 2)
|
|
|
353
322
|
|
|
354
323
|
const byteLength = (text: string) => new TextEncoder().encode(text).length
|
|
355
324
|
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
/** Adds `incoming` items at the top of `existing`; an item whose id is already there replaces the old copy instead. */
|
|
359
|
-
function mergeItems(existing: FeedItem[], incoming: FeedItem[]): FeedItem[] {
|
|
360
|
-
const byId = new Map(incoming.map(item => [item.id, item]))
|
|
361
|
-
const present = new Set(existing.map(item => item.id))
|
|
362
|
-
return [...incoming.filter(item => !present.has(item.id)), ...existing.map(item => byId.get(item.id) ?? item)]
|
|
363
|
-
}
|
|
364
|
-
|
|
365
|
-
/** The index of an item (RFC-13 "Indexes"), also from `_pups` of earlier drafts, or undefined if it has none. */
|
|
325
|
+
/** The index of an item (RFC-13 "Indexes"), or undefined if it has none. */
|
|
366
326
|
export function itemIndex(item: FeedItem): number | undefined {
|
|
367
|
-
const index = isObject(item._archive)
|
|
327
|
+
const index = isObject(item._archive) ? item._archive.index : undefined
|
|
368
328
|
return typeof index == 'number' && Number.isSafeInteger(index) && index >= 0 ? index : undefined
|
|
369
329
|
}
|
|
370
330
|
|
|
371
|
-
const withIndex = (item: FeedItem, index: number): FeedItem => ({ ...item, _archive: { ...isObject(item._archive) ? item._archive : {}, index } })
|
|
372
|
-
|
|
373
|
-
/** The fields of RFC-13 that earlier drafts of RFC-7 kept in `_pups`. */
|
|
374
|
-
const legacyArchiveFields = ['archive_url', 'updates_url', 'next_index'] as const
|
|
375
|
-
|
|
376
|
-
/**
|
|
377
|
-
* Moves the fields that earlier drafts kept in `_pups` into `_archive` (RFC-13 "Earlier drafts"), in the file and its
|
|
378
|
-
* items, where `_archive` doesn't have them already. Changes `doc`.
|
|
379
|
-
*/
|
|
380
|
-
function upgradeArchiveFields(doc: FeedDocument): FeedDocument {
|
|
381
|
-
const move = (from: Record<string, unknown>, to: Record<string, unknown>, keys: readonly string[]) => {
|
|
382
|
-
for (const key of keys) if (key in from) {
|
|
383
|
-
if (to[key] === undefined) to[key] = from[key]
|
|
384
|
-
delete from[key]
|
|
385
|
-
}
|
|
386
|
-
}
|
|
387
|
-
if (isObject(doc._pups) && legacyArchiveFields.some(key => key in doc._pups!)) {
|
|
388
|
-
const archive: ArchiveFields = { about: ARCHIVE_SPEC, ...isObject(doc._archive) ? doc._archive : {} }
|
|
389
|
-
move(doc._pups, archive, legacyArchiveFields)
|
|
390
|
-
doc._archive = archive
|
|
391
|
-
}
|
|
392
|
-
for (const item of doc.items)
|
|
393
|
-
if (isObject(item._pups) && 'index' in item._pups) {
|
|
394
|
-
const archive: ItemArchive = isObject(item._archive) ? item._archive : {}
|
|
395
|
-
move(item._pups, archive, ['index'])
|
|
396
|
-
item._archive = archive
|
|
397
|
-
if (!Object.keys(item._pups).length) delete item._pups
|
|
398
|
-
}
|
|
399
|
-
return doc
|
|
400
|
-
}
|
|
401
|
-
|
|
402
|
-
/** A post URL template (RFC-6 "Post URLs"): an https URL with `{index}` or `{id}`. */
|
|
403
|
-
function validTemplate(template: unknown): template is string {
|
|
404
|
-
if (typeof template != 'string' || !/\{(index|id)\}/.test(template)) return false
|
|
405
|
-
try {
|
|
406
|
-
return new URL(template.replace(/\{(feed|index|id)\}/g, 'x')).protocol == 'https:'
|
|
407
|
-
} catch {
|
|
408
|
-
return false
|
|
409
|
-
}
|
|
410
|
-
}
|
|
411
|
-
|
|
412
|
-
/** The counter `_archive.next_index` of a file, if it has a valid one. */
|
|
413
|
-
function counterOf(doc: FeedDocument): number | undefined {
|
|
414
|
-
const next = doc._archive?.next_index
|
|
415
|
-
return typeof next == 'number' && Number.isSafeInteger(next) && next >= 0 ? next : undefined
|
|
416
|
-
}
|
|
417
|
-
|
|
418
|
-
/** The next free index: above every index of `items`, and at least each of `counters`. */
|
|
419
|
-
const nextFree = (items: FeedItem[], ...counters: (number | undefined)[]) =>
|
|
420
|
-
Math.max(0, ...counters.filter((c): c is number => c !== undefined), ...items.map(item => (itemIndex(item) ?? -1) + 1))
|
|
421
|
-
|
|
422
|
-
/**
|
|
423
|
-
* Gives the items without an index (newest first, as in a file) the next indexes from `next`, the oldest first
|
|
424
|
-
* (RFC-7 "Items without an index").
|
|
425
|
-
* @returns the items, and the counter after them.
|
|
426
|
-
*/
|
|
427
|
-
function indexItems(items: FeedItem[], next: number): { items: FeedItem[]; next: number; changed: boolean } {
|
|
428
|
-
const result = [...items]
|
|
429
|
-
let changed = false
|
|
430
|
-
for (let i = result.length - 1; i >= 0; i--)
|
|
431
|
-
if (itemIndex(result[i]!) === undefined) {
|
|
432
|
-
result[i] = withIndex(result[i]!, next++)
|
|
433
|
-
changed = true
|
|
434
|
-
}
|
|
435
|
-
return { items: result, next, changed }
|
|
436
|
-
}
|
|
437
|
-
|
|
438
331
|
/** Every string inside `value`, deeply. */
|
|
439
332
|
function* strings(value: unknown): Generator<string> {
|
|
440
333
|
if (typeof value == 'string') yield value
|
|
@@ -449,15 +342,26 @@ function extensionForType(type: string): string | undefined {
|
|
|
449
342
|
return entries.find(([, t]) => t.served == essence)?.[0] ?? entries.find(([, t]) => t.accepted.includes(essence))?.[0]
|
|
450
343
|
}
|
|
451
344
|
|
|
345
|
+
/** The answer to a POST of an item, checked. */
|
|
346
|
+
function posted(body: unknown, path: string): Posted {
|
|
347
|
+
if (!isObject(body) || !isObject(body.item) || typeof body.item.id != 'string' || typeof body.address != 'string')
|
|
348
|
+
throw new PupsError('server_error', `The server's answer to the POST to ${path} has no item and address (RFC-7, Posting items).`)
|
|
349
|
+
return { item: body.item as FeedItem, address: body.address }
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
/** The file part of an item's address. */
|
|
353
|
+
const fileOf = (address: string) => address.split('#')[0]!
|
|
354
|
+
|
|
452
355
|
/**
|
|
453
|
-
* A feed on the owner's domain,
|
|
454
|
-
* `/feed/<name>.json`, each with its feed directory for archives, icons and media: `/feed/`, or `/feed/<name>/`.
|
|
356
|
+
* A feed on the owner's domain, kept by the PUPS server according to RFC-7: the main feed `/feed.json`, or a named
|
|
357
|
+
* feed `/feed/<name>.json`, each with its feed directory for archives, icons and media: `/feed/`, or `/feed/<name>/`.
|
|
455
358
|
* The main feed keeps its media in `/feed/images/` and `/feed/media/`, since the rest of `/feed/` belongs to the
|
|
456
359
|
* named feeds.
|
|
457
360
|
*
|
|
458
361
|
* Get one from `session.feed()` or `session.feed('photos')`. The session needs the feed's scopes, see
|
|
459
|
-
* {@link feedScopes}.
|
|
460
|
-
* read-modify-write cycles with ETags, repeated when
|
|
362
|
+
* {@link feedScopes}. Posts are sent to the server one at a time, which gives them their index and puts them into
|
|
363
|
+
* the archives (RFC-13); changes of the feed's settings are read-modify-write cycles with ETags, repeated when
|
|
364
|
+
* another app wrote in between.
|
|
461
365
|
*/
|
|
462
366
|
export class PupsFeed {
|
|
463
367
|
/** The name of a named feed; undefined for the main feed. */
|
|
@@ -467,14 +371,8 @@ export class PupsFeed {
|
|
|
467
371
|
/** The feed directory, e.g. `/feed/` or `/feed/photos/`. */
|
|
468
372
|
readonly directory: string
|
|
469
373
|
readonly #session: PupsSession
|
|
470
|
-
readonly #maxItems: number
|
|
471
|
-
readonly #archiveSize: number
|
|
472
|
-
readonly #archivesWanted: boolean
|
|
473
|
-
readonly #maxBytes?: number
|
|
474
374
|
readonly #maxAttempts: number
|
|
475
|
-
readonly #postUrls: boolean
|
|
476
375
|
readonly #mentions?: Mentions
|
|
477
|
-
#template?: Promise<string | undefined>
|
|
478
376
|
|
|
479
377
|
/** @internal Use `session.feed()`. */
|
|
480
378
|
constructor(session: PupsSession, name?: string, options: FeedOptions = {}) {
|
|
@@ -483,15 +381,8 @@ export class PupsFeed {
|
|
|
483
381
|
this.path = path
|
|
484
382
|
this.directory = directory
|
|
485
383
|
this.#session = session
|
|
486
|
-
this.#maxItems = options.maxItems ?? 30
|
|
487
|
-
this.#archiveSize = options.archiveSize ?? 20
|
|
488
|
-
this.#archivesWanted = options.startArchives ?? true
|
|
489
|
-
this.#maxBytes = options.maxBytes
|
|
490
384
|
this.#maxAttempts = options.maxAttempts ?? 8
|
|
491
|
-
this.#postUrls = options.postUrls ?? true
|
|
492
385
|
this.#mentions = options.mentions === true ? session.mentions : options.mentions || undefined
|
|
493
|
-
if (!(this.#archiveSize >= 1 && this.#maxItems >= 1))
|
|
494
|
-
throw new PupsError('invalid_request', 'Feed options: maxItems and archiveSize must be at least 1.')
|
|
495
386
|
}
|
|
496
387
|
|
|
497
388
|
/** The public URL of the feed file, its `feed_url`. */
|
|
@@ -506,10 +397,10 @@ export class PupsFeed {
|
|
|
506
397
|
|
|
507
398
|
/** Whether the session may write the feed file and the feed directory. */
|
|
508
399
|
get writable(): boolean {
|
|
509
|
-
return this.#session.canWrite(this.path) && this.#session.canWrite(this
|
|
400
|
+
return this.#session.canWrite(this.path) && this.#session.canWrite(`${this.directory}images/x`)
|
|
510
401
|
}
|
|
511
402
|
|
|
512
|
-
/** Reads the feed file and tells whether it exists, and whether
|
|
403
|
+
/** Reads the feed file and tells whether it exists, and whether the server keeps it. */
|
|
513
404
|
async load(options: Signal = {}): Promise<FeedState> {
|
|
514
405
|
try {
|
|
515
406
|
const loaded = await this.#read(this.path, options.signal)
|
|
@@ -522,17 +413,19 @@ export class PupsFeed {
|
|
|
522
413
|
}
|
|
523
414
|
|
|
524
415
|
/**
|
|
525
|
-
* Sets up a new feed (RFC-7 "Setting up a feed"): uploads the icons, writes the feed file with the
|
|
526
|
-
* and
|
|
416
|
+
* Sets up a new feed (RFC-7 "Setting up a feed"): uploads the icons, writes the feed file with the `_pups` marker,
|
|
417
|
+
* and registers the feed in `host-meta.json` (unless `register` is false). The server sets the fixed fields.
|
|
527
418
|
* @returns `registered`: false if the feed couldn't be registered; call `register()` again later, or tell the owner
|
|
528
419
|
* to add the link to `host-meta.json` by hand.
|
|
529
|
-
* @throws {PupsError} `precondition_failed` if the feed file exists already; `insufficient_scope
|
|
420
|
+
* @throws {PupsError} `precondition_failed` if the feed file exists already; `insufficient_scope`; `unsupported` if
|
|
421
|
+
* the server doesn't keep feeds.
|
|
530
422
|
*/
|
|
531
423
|
async create(setup: FeedSetup, options: Signal & { register?: boolean } = {}): Promise<{ url: string; registered: boolean }> {
|
|
532
424
|
const { icon, favicon, title, ...settings } = setup
|
|
533
425
|
if (typeof title != 'string' || !title.trim()) throw new PupsError('invalid_request', 'A feed needs a title.')
|
|
534
426
|
this.#checkSettings(settings)
|
|
535
427
|
this.#checkWritable()
|
|
428
|
+
this.#checkKept()
|
|
536
429
|
const uploaded: MediaResult[] = []
|
|
537
430
|
try {
|
|
538
431
|
const iconFile = icon && await this.#upload(icon, 'images/icons/icon-', options.signal)
|
|
@@ -543,10 +436,7 @@ export class PupsFeed {
|
|
|
543
436
|
const doc: FeedDocument = {
|
|
544
437
|
version: JSON_FEED_VERSION,
|
|
545
438
|
title,
|
|
546
|
-
home_page_url: this.#session.me,
|
|
547
|
-
feed_url: this.url,
|
|
548
439
|
...description === undefined ? {} : { description },
|
|
549
|
-
user_comment: mainComment,
|
|
550
440
|
...iconFile ? { icon: iconFile.url } : {},
|
|
551
441
|
...faviconFile ? { favicon: faviconFile.url } : {},
|
|
552
442
|
...rest,
|
|
@@ -592,14 +482,11 @@ export class PupsFeed {
|
|
|
592
482
|
this.#checkSettings(changes)
|
|
593
483
|
if (changes.title === null || (changes.title !== undefined && !changes.title.trim()))
|
|
594
484
|
throw new PupsError('invalid_request', 'A feed needs a title.')
|
|
595
|
-
return this.#
|
|
596
|
-
const main = await this.#readControlled(this.path, options.signal)
|
|
597
|
-
const doc = { ...main.doc }
|
|
485
|
+
return this.#change(doc => {
|
|
598
486
|
for (const [key, value] of Object.entries(changes))
|
|
599
487
|
if (value === null) delete doc[key]
|
|
600
488
|
else if (value !== undefined) doc[key] = value
|
|
601
|
-
|
|
602
|
-
})
|
|
489
|
+
}, options.signal)
|
|
603
490
|
}
|
|
604
491
|
|
|
605
492
|
/**
|
|
@@ -630,21 +517,24 @@ export class PupsFeed {
|
|
|
630
517
|
|
|
631
518
|
/**
|
|
632
519
|
* Lets readers comment on the feed's posts through the PUPS server, which puts the comments into the owner's inbox
|
|
633
|
-
* (RFC-6 "Built-in receiver")
|
|
634
|
-
* the comments
|
|
635
|
-
*
|
|
636
|
-
*
|
|
520
|
+
* (RFC-6 "Built-in receiver"). The owner approves them there, and the server publishes them in a comment feed per
|
|
521
|
+
* post, which the post's `_source.comments` names (RFC-7 "Publishing comments"). With `publish: false`, the owner only
|
|
522
|
+
* reads them. Writes RFC-4 `_submissions` into the feed file, with `comment_endpoint` as `url`, the comments policy
|
|
523
|
+
* (RFC-4's defaults for `title`, `minLength` and `maxLength`, and `maxDepth: 1`, since the server accepts comments
|
|
524
|
+
* on posts only, overridden by `policy`) and `republish: "full"`, or `"no"`. Replaces the `_submissions` of another
|
|
525
|
+
* service; check `comments()` first and ask the owner. Single posts are closed with
|
|
526
|
+
* `_commenting: { allowComments: false }` (RFC-4).
|
|
637
527
|
* @throws {PupsError} `unsupported` if the server has no `comment_endpoint`.
|
|
638
528
|
*/
|
|
639
|
-
async enableComments(settings: { policy?: CommentPolicy; tos?: string } = {}, options: Signal = {}): Promise<FeedDocument> {
|
|
529
|
+
async enableComments(settings: { policy?: CommentPolicy; tos?: string; publish?: boolean } = {}, options: Signal = {}): Promise<FeedDocument> {
|
|
640
530
|
const url = this.#session.server.commentEndpoint
|
|
641
531
|
if (!url) throw new PupsError('unsupported', 'This PUPS server doesn\'t receive comments: its configuration has no comment_endpoint (RFC-6, Built-in receiver).')
|
|
642
532
|
return this.#setSubmissions({
|
|
643
533
|
about: RFC4_URL,
|
|
644
534
|
url,
|
|
645
535
|
...settings.tos ? { tos: settings.tos } : {},
|
|
646
|
-
policies: { comments: { title: 'none', minLength: 2, maxLength: 2000, ...settings.policy } },
|
|
647
|
-
republish: 'no',
|
|
536
|
+
policies: { comments: { title: 'none', minLength: 2, maxLength: 2000, maxDepth: 1, ...settings.policy } },
|
|
537
|
+
republish: settings.publish == false ? 'no' : 'full',
|
|
648
538
|
}, options.signal)
|
|
649
539
|
}
|
|
650
540
|
|
|
@@ -654,11 +544,10 @@ export class PupsFeed {
|
|
|
654
544
|
}
|
|
655
545
|
|
|
656
546
|
#setSubmissions(submissions: Record<string, unknown> | undefined, signal?: AbortSignal): Promise<FeedDocument> {
|
|
657
|
-
return this.#
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
})
|
|
547
|
+
return this.#change(doc => {
|
|
548
|
+
if (submissions) doc._submissions = submissions
|
|
549
|
+
else delete doc._submissions
|
|
550
|
+
}, signal)
|
|
662
551
|
}
|
|
663
552
|
|
|
664
553
|
/**
|
|
@@ -671,101 +560,41 @@ export class PupsFeed {
|
|
|
671
560
|
const folder = (options.folder ?? (type.startsWith('image/') ? 'images' : 'media')).replace(/^\/+|\/+$/g, '')
|
|
672
561
|
if (folder == 'images/icons' || folder.startsWith('images/icons/'))
|
|
673
562
|
throw new PupsError('invalid_request', `${this.directory}images/icons/ is reserved for the feed's icons (RFC-7, Icons).`)
|
|
563
|
+
if (folder.split('/')[0] == 'comments')
|
|
564
|
+
throw new PupsError('invalid_request', `${this.directory}comments/ is reserved for the comment feeds that the server writes (RFC-7, Publishing comments).`)
|
|
674
565
|
if (this.name === undefined && !mainMediaFolders.includes(folder.split('/')[0]!))
|
|
675
566
|
throw new PupsError('invalid_request', `The main feed's media go into /feed/images/ or /feed/media/; the rest of /feed/ belongs to named feeds (RFC-7, Items).`)
|
|
676
567
|
return this.#upload(media, `${folder}/${new Date().getUTCFullYear()}/`, options.signal)
|
|
677
568
|
}
|
|
678
569
|
|
|
679
570
|
/**
|
|
680
|
-
* Adds a post (RFC-7 "Posting
|
|
681
|
-
*
|
|
682
|
-
*
|
|
683
|
-
* media with `uploadMedia()` before posting.
|
|
684
|
-
* In a feed with archives, a post without `url` gets one from the site's post URL template, if `host-meta.json`
|
|
685
|
-
* has one and `postUrls` isn't false: the page that shows the post (RFC-7 "Post URLs").
|
|
571
|
+
* Adds a post (RFC-7 "Posting items"): sends it to the server, which gives it its index, puts it into the newest
|
|
572
|
+
* archive and at the top of the feed file, and, if the site declares a post URL template, gives it the `url` of
|
|
573
|
+
* its page. Upload media with `uploadMedia()` before posting.
|
|
686
574
|
* With the option `mentions`, the domains the post names and the author of the item it answers are notified
|
|
687
575
|
* (RFC-11) once the post is visible; see `PostResult.mentions`.
|
|
688
|
-
* @returns the post, with `url`: its
|
|
689
|
-
*
|
|
690
|
-
*
|
|
576
|
+
* @returns the post as stored, with `url`: its address, which never changes. The post's page, if it has one, is
|
|
577
|
+
* `item.url`.
|
|
578
|
+
* @throws {PupsError} `not_found` if the feed doesn't exist; `not_pups_controlled`; `unsupported` if the server
|
|
579
|
+
* doesn't keep feeds.
|
|
691
580
|
*/
|
|
692
581
|
async addPost(input: PostInput, options: WithMentions = {}): Promise<PostResult> {
|
|
693
|
-
const item = this.#newItem(input)
|
|
694
|
-
|
|
695
|
-
let archiveUrl = ''
|
|
696
|
-
let stored = item
|
|
697
|
-
let firstAttempt = true
|
|
698
|
-
await this.#transaction(async () => {
|
|
699
|
-
const main = await this.#readControlled(this.path, signal)
|
|
700
|
-
// On a retry, the item may be in the feed file already: another client repaired it from the archive.
|
|
701
|
-
if (firstAttempt && main.doc.items.some(i => i.id == item.id))
|
|
702
|
-
throw new PupsError('invalid_request', `The feed already has an item with the id "${item.id}".`)
|
|
703
|
-
firstAttempt = false
|
|
704
|
-
const doc: FeedDocument = { ...main.doc, _archive: { ...main.doc._archive } }
|
|
705
|
-
const hasArchives = this.#archiveNumber(doc._archive!.archive_url) !== undefined
|
|
706
|
-
if (!hasArchives && !this.#archivesWanted) {
|
|
707
|
-
// A feed without archives: the feed file is all there is, and holds the counter (RFC-7 "Posting").
|
|
708
|
-
archiveUrl = this.url
|
|
709
|
-
const indexed = indexItems(doc.items, nextFree(doc.items, counterOf(doc)))
|
|
710
|
-
stored = withIndex(item, indexed.next)
|
|
711
|
-
doc._archive!.next_index = indexed.next + 1
|
|
712
|
-
await this.#writeMain({ ...doc, items: this.#newestItems([stored], indexed.items, doc) }, main.etag, signal)
|
|
713
|
-
return
|
|
714
|
-
}
|
|
715
|
-
if (!hasArchives) {
|
|
716
|
-
const started = await this.#startArchives(main, signal)
|
|
717
|
-
if (started) {
|
|
718
|
-
doc._archive!.archive_url = started.url
|
|
719
|
-
doc.items = started.items
|
|
720
|
-
}
|
|
721
|
-
}
|
|
722
|
-
const template = await this.#postUrlTemplate(signal)
|
|
723
|
-
const added = await this.#addToArchive(main, doc, item, signal, template)
|
|
724
|
-
archiveUrl = added.url
|
|
725
|
-
stored = added.item
|
|
726
|
-
doc._archive!.archive_url = added.url
|
|
727
|
-
if (!hasArchives) {
|
|
728
|
-
// The old chain of a handed-over feed now continues behind the oldest archive, and the newest archive
|
|
729
|
-
// holds the counter.
|
|
730
|
-
delete doc.next_url
|
|
731
|
-
delete doc._archive!.next_index
|
|
732
|
-
}
|
|
733
|
-
doc.items = this.#newestItems(added.recent, doc.items, doc)
|
|
734
|
-
await this.#writeMain(doc, main.etag, signal)
|
|
735
|
-
})
|
|
736
|
-
return this.#withMentions(options.mentions, this.#result(stored, archiveUrl), archiveUrl, stored, undefined, item => !!item)
|
|
737
|
-
}
|
|
738
|
-
|
|
739
|
-
/**
|
|
740
|
-
* Gives every item of the feed without an index one (RFC-7 "Indexing an existing feed"), and sets the counter.
|
|
741
|
-
* `addPost()` does this by itself before the first post to a feed that hasn't been indexed yet; call it to
|
|
742
|
-
* index a feed without posting.
|
|
743
|
-
*/
|
|
744
|
-
async assignIndexes(options: Signal = {}): Promise<void> {
|
|
745
|
-
const signal = options.signal
|
|
746
|
-
await this.#transaction(async () => {
|
|
747
|
-
const main = await this.#readControlled(this.path, signal)
|
|
748
|
-
const newest = this.#archiveNumber(main.doc._archive?.archive_url)
|
|
749
|
-
if (newest === undefined) {
|
|
750
|
-
const indexed = indexItems(main.doc.items, nextFree(main.doc.items, counterOf(main.doc)))
|
|
751
|
-
if (indexed.changed || counterOf(main.doc) !== indexed.next)
|
|
752
|
-
await this.#writeMain({ ...main.doc, items: indexed.items, _archive: { ...main.doc._archive, next_index: indexed.next } }, main.etag, signal)
|
|
753
|
-
return
|
|
754
|
-
}
|
|
755
|
-
const archive = await this.#readArchive(this.#archivePath(newest), signal)
|
|
756
|
-
if (archive && archive != 'foreign' && (counterOf(archive.doc) === undefined || archive.doc.items.some(item => itemIndex(item) === undefined)))
|
|
757
|
-
await this.#indexArchives(main, signal)
|
|
758
|
-
})
|
|
582
|
+
const { item, address } = await this.#post(this.#newItem(input), options.signal)
|
|
583
|
+
return this.#withMentions(options.mentions, this.#result(item, address), fileOf(address), item, undefined, found => !!found)
|
|
759
584
|
}
|
|
760
585
|
|
|
761
586
|
/**
|
|
762
|
-
* Finds a post by id in the archives
|
|
587
|
+
* Finds a post by id in the archives.
|
|
763
588
|
* @returns undefined if there is no such post.
|
|
764
589
|
*/
|
|
765
590
|
async getPost(id: string, options: Signal = {}): Promise<PostResult | undefined> {
|
|
766
591
|
const main = await this.#read(this.path, options.signal)
|
|
767
|
-
|
|
768
|
-
|
|
592
|
+
if (!main) return
|
|
593
|
+
for await (const file of this.#chain(main, options.signal)) {
|
|
594
|
+
const item = file.doc.items.find(i => i.id == id)
|
|
595
|
+
if (item) return this.#result(item, `${this.#session.publicUrl(file.path)}#${encodeURIComponent(id)}`)
|
|
596
|
+
}
|
|
597
|
+
return
|
|
769
598
|
}
|
|
770
599
|
|
|
771
600
|
/**
|
|
@@ -785,70 +614,38 @@ export class PupsFeed {
|
|
|
785
614
|
}
|
|
786
615
|
|
|
787
616
|
/**
|
|
788
|
-
* Edits a post
|
|
789
|
-
*
|
|
790
|
-
* a function that gets a copy of the item and returns the new item, or
|
|
791
|
-
*
|
|
617
|
+
* Edits a post (RFC-7 "Posting items"): reads it from its archive, applies `changes`, and sends it to the server,
|
|
618
|
+
* which replaces it in its archive and in the feed file and sets `date_modified`. `changes` is either an object (a
|
|
619
|
+
* value sets a field, `null` removes it), or a function that gets a copy of the item and returns the new item, or
|
|
620
|
+
* `undefined` to leave it unchanged. The post keeps its id and index.
|
|
792
621
|
*
|
|
793
622
|
* Apps should edit posts only when the owner asks them to, whichever app created them. With the option
|
|
794
|
-
* `mentions`, everyone the old or the new version mentions is notified.
|
|
795
|
-
* archives that has no `url`, and had none before, gets one from the post URL template, as in `addPost()`.
|
|
623
|
+
* `mentions`, everyone the old or the new version mentions is notified.
|
|
796
624
|
* @throws {PupsError} `not_found`
|
|
797
625
|
*/
|
|
798
626
|
async editPost(id: string, changes: PostChanges | ((item: FeedItem) => FeedItem | undefined | Promise<FeedItem | undefined>), options: WithMentions = {}): Promise<PostResult> {
|
|
799
627
|
const signal = options.signal
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
let item: FeedItem = { ...changed, id, date_modified: now() }
|
|
810
|
-
// The index never changes (RFC-7 "Indexes").
|
|
811
|
-
if (oldIndex !== undefined) item = withIndex(item, oldIndex)
|
|
812
|
-
// A post of a feed with archives that never had a url may get one (RFC-7 "Post URLs").
|
|
813
|
-
if (oldIndex !== undefined && !old.url && !item.url && this.#archiveNumber(main.doc._archive?.archive_url) !== undefined)
|
|
814
|
-
item = this.#withPostUrl(item, oldIndex, await this.#postUrlTemplate(signal))
|
|
815
|
-
if (file != main) await this.#writeArchive(file, replaceAt(file.doc.items, index, item), signal)
|
|
816
|
-
const mainIndex = main.doc.items.findIndex(i => i.id == id)
|
|
817
|
-
if (mainIndex >= 0) await this.#writeMain({ ...main.doc, items: replaceAt(main.doc.items, mainIndex, item) }, main.etag, signal)
|
|
818
|
-
edit = { old, item, fileUrl }
|
|
819
|
-
return this.#result(item, fileUrl)
|
|
820
|
-
})
|
|
821
|
-
const done = edit as typeof edit
|
|
822
|
-
return done ? this.#withMentions(options.mentions, result, done.fileUrl, done.item, done.old, found => found?.date_modified == done.item.date_modified) : result
|
|
628
|
+
const current = await this.getPost(id, options)
|
|
629
|
+
if (!current) throw new PupsError('not_found', `There is no post with the id "${id}" in ${this.path} or its archives.`)
|
|
630
|
+
const old = current.item
|
|
631
|
+
const changed = typeof changes == 'function' ? await changes(clone(old)) : this.#applyChanges(clone(old), changes)
|
|
632
|
+
if (!changed) return current
|
|
633
|
+
// The index names the post for the server (RFC-7 "Posting items"), and never changes.
|
|
634
|
+
const { _pups: _marker, ...rest } = changed
|
|
635
|
+
const { item, address } = await this.#post({ ...rest, id, _archive: { ...isObject(rest._archive) ? rest._archive : {}, ...old._archive } }, signal)
|
|
636
|
+
return this.#withMentions(options.mentions, this.#result(item, address), fileOf(address), item, old, found => found?.date_modified == item.date_modified)
|
|
823
637
|
}
|
|
824
638
|
|
|
825
639
|
/**
|
|
826
|
-
* Deletes a post from its archive and from the feed file, which
|
|
827
|
-
* from the archives. With `deleteMedia`, it also deletes the files in the feed directory that the
|
|
828
|
-
* and no other post does; this reads all archives.
|
|
640
|
+
* Deletes a post (RFC-7 "Posting items"): the server removes it from its archive and from the feed file, which it
|
|
641
|
+
* fills up again from the archives. With `deleteMedia`, it also deletes the files in the feed directory that the
|
|
642
|
+
* post references and no other post does; this reads all archives.
|
|
829
643
|
* @returns the deleted item, and the public URLs of the deleted media files.
|
|
830
644
|
* @throws {PupsError} `not_found`
|
|
831
645
|
*/
|
|
832
646
|
async deletePost(id: string, options: WithMentions & { deleteMedia?: boolean } = {}): Promise<{ item: FeedItem; deletedMedia: string[]; mentions?: Promise<MentionReport[]> }> {
|
|
833
647
|
const signal = options.signal
|
|
834
|
-
|
|
835
|
-
const item = await this.#transaction(async () => {
|
|
836
|
-
const { main, file, index } = await this.#findWritable(id, signal)
|
|
837
|
-
fileUrl = this.#session.publicUrl(file.path)
|
|
838
|
-
const item = file.doc.items[index]!
|
|
839
|
-
if (file != main) {
|
|
840
|
-
await this.#writeArchive(file, file.doc.items.filter(i => i.id != id), signal)
|
|
841
|
-
// Fill the feed file up again from the archives, which no longer contain the item.
|
|
842
|
-
const newest: FeedItem[] = []
|
|
843
|
-
for await (const archive of this.#chain(main, signal)) {
|
|
844
|
-
newest.push(...archive.doc.items.filter(i => i.id != id))
|
|
845
|
-
if (newest.length >= this.#maxItems) break
|
|
846
|
-
}
|
|
847
|
-
await this.#writeMain({ ...main.doc, items: this.#newestItems(newest, [], main.doc) }, main.etag, signal)
|
|
848
|
-
} else
|
|
849
|
-
await this.#writeMain({ ...main.doc, items: main.doc.items.filter(i => i.id != id) }, main.etag, signal)
|
|
850
|
-
return item
|
|
851
|
-
})
|
|
648
|
+
const { item, address } = await this.#post({ id, _pups: { delete: true } }, signal)
|
|
852
649
|
const deletedMedia: string[] = []
|
|
853
650
|
if (options.deleteMedia) {
|
|
854
651
|
const candidates = new Set(this.#mediaUrls(item))
|
|
@@ -860,7 +657,7 @@ export class PupsFeed {
|
|
|
860
657
|
if (path && await this.#deleteQuietly(path, signal)) deletedMedia.push(url)
|
|
861
658
|
}
|
|
862
659
|
}
|
|
863
|
-
const mentions = this.#sendMentions(options.mentions,
|
|
660
|
+
const mentions = this.#sendMentions(options.mentions, fileOf(address), undefined, item, found => !found)
|
|
864
661
|
return { item, deletedMedia, ...mentions ? { mentions } : {} }
|
|
865
662
|
}
|
|
866
663
|
|
|
@@ -877,9 +674,9 @@ export class PupsFeed {
|
|
|
877
674
|
|
|
878
675
|
/**
|
|
879
676
|
* Submits a post that answers an item of an RFC-4 feed, e.g. a comment, to that feed's `_submissions.url`
|
|
880
|
-
* (RFC-4 "Submitting"): `source` is the post's
|
|
881
|
-
*
|
|
882
|
-
*
|
|
677
|
+
* (RFC-4 "Submitting"): `source` is the post's address, `target` its `_source.inReplyTo`. Call it after `addPost()`
|
|
678
|
+
* or `editPost()`, also to submit an edit or a delete again. Show the target's terms of service and what its
|
|
679
|
+
* `republish` means before the first submission.
|
|
883
680
|
* @throws {PupsError} `not_found`; `invalid_request` if the post answers nothing; `unsupported` if the target
|
|
884
681
|
* feed takes no submissions.
|
|
885
682
|
* @deprecated Comments and group posts don't belong in a feed for readers (RFC-7 "Comment feeds"). Use
|
|
@@ -954,22 +751,13 @@ export class PupsFeed {
|
|
|
954
751
|
// Not JSON: handled below.
|
|
955
752
|
}
|
|
956
753
|
if (!isFeedDocument(doc)) throw new PupsError('invalid_feed', `${path} is not a JSON Feed.`)
|
|
957
|
-
return { path, doc
|
|
958
|
-
}
|
|
959
|
-
|
|
960
|
-
/** Reads a file that must exist and carry the `_pups` marker. */
|
|
961
|
-
async #readControlled(path: string, signal?: AbortSignal): Promise<Loaded> {
|
|
962
|
-
const loaded = await this.#read(path, signal)
|
|
963
|
-
if (!loaded)
|
|
964
|
-
throw new PupsError('not_found', path == this.path ? `There is no feed at ${path} yet. Set it up with create().` : `${path} doesn't exist.`)
|
|
965
|
-
if (!isControlled(loaded.doc)) throw notControlled(path)
|
|
966
|
-
return loaded
|
|
754
|
+
return { path, doc, etag: file.etag }
|
|
967
755
|
}
|
|
968
756
|
|
|
969
757
|
/**
|
|
970
758
|
* The files that hold all items: the archives from `_archive.archive_url` along `next_url`, as far as they are files
|
|
971
|
-
* on this server. A feed without archives
|
|
972
|
-
*
|
|
759
|
+
* on this server. A feed without archives (handed over, not written yet) holds its items in the feed file and the
|
|
760
|
+
* pages its `next_url` names.
|
|
973
761
|
*/
|
|
974
762
|
async *#chain(main: Loaded, signal?: AbortSignal): AsyncGenerator<Loaded> {
|
|
975
763
|
const visited = new Set<string>([main.path])
|
|
@@ -994,301 +782,52 @@ export class PupsFeed {
|
|
|
994
782
|
}
|
|
995
783
|
}
|
|
996
784
|
|
|
997
|
-
async #find(main: Loaded, id: string, signal?: AbortSignal): Promise<Found | undefined> {
|
|
998
|
-
for await (const file of this.#chain(main, signal)) {
|
|
999
|
-
const index = file.doc.items.findIndex(item => item.id == id)
|
|
1000
|
-
if (index >= 0) return { main, file, index }
|
|
1001
|
-
}
|
|
1002
|
-
return
|
|
1003
|
-
}
|
|
1004
|
-
|
|
1005
|
-
async #findWritable(id: string, signal?: AbortSignal): Promise<Found> {
|
|
1006
|
-
const main = await this.#readControlled(this.path, signal)
|
|
1007
|
-
const found = await this.#find(main, id, signal)
|
|
1008
|
-
if (!found) throw new PupsError('not_found', `There is no post with the id "${id}" in ${this.path} or its archives.`)
|
|
1009
|
-
if (!isControlled(found.file.doc)) throw notControlled(found.file.path)
|
|
1010
|
-
return found
|
|
1011
|
-
}
|
|
1012
|
-
|
|
1013
785
|
// Writing
|
|
1014
786
|
|
|
1015
|
-
/**
|
|
1016
|
-
async #
|
|
1017
|
-
|
|
1018
|
-
try {
|
|
1019
|
-
return await operation()
|
|
1020
|
-
} catch (e) {
|
|
1021
|
-
if (!isPupsError(e, 'precondition_failed') || attempt >= this.#maxAttempts) throw e
|
|
1022
|
-
}
|
|
1023
|
-
}
|
|
1024
|
-
|
|
1025
|
-
/**
|
|
1026
|
-
* Writes the feed file. Sets the fixed fields, so that a feed the owner handed over gets them on its first write,
|
|
1027
|
-
* and upgrades JSON Feed 1.0 (RFC-7 "Handing over an existing feed"). A feed with archives gets the archive template
|
|
1028
|
-
* and size of RFC-13, and its optional `next_url` is kept equal to `archive_url` (RFC-7 "Reading").
|
|
1029
|
-
*/
|
|
1030
|
-
async #writeMain(doc: FeedDocument, etag: string, signal?: AbortSignal): Promise<FeedDocument> {
|
|
1031
|
-
const { author, ...rest } = doc
|
|
1032
|
-
const fixed: FeedDocument = {
|
|
1033
|
-
...rest,
|
|
1034
|
-
version: JSON_FEED_VERSION,
|
|
1035
|
-
home_page_url: this.#session.me,
|
|
1036
|
-
feed_url: this.url,
|
|
1037
|
-
user_comment: mainComment,
|
|
1038
|
-
}
|
|
1039
|
-
if (author !== undefined && fixed.authors === undefined) fixed.authors = [author as FeedAuthor]
|
|
1040
|
-
const archiveUrl = fixed._archive?.archive_url
|
|
1041
|
-
if (fixed._archive) fixed._archive = { about: ARCHIVE_SPEC, ...fixed._archive }
|
|
1042
|
-
if (this.#archiveNumber(archiveUrl) !== undefined) {
|
|
1043
|
-
fixed._archive = { ...fixed._archive, archive_template: this.#archiveTemplate(), archive_size: this.#archiveSize }
|
|
1044
|
-
if (fixed.next_url !== undefined) fixed.next_url = archiveUrl
|
|
1045
|
-
}
|
|
1046
|
-
await this.#session.replace(this.path, serialize(fixed), etag, { contentType: FEED_CONTENT_TYPE, signal })
|
|
1047
|
-
return fixed
|
|
1048
|
-
}
|
|
1049
|
-
|
|
1050
|
-
/**
|
|
1051
|
-
* Writes an archive with `items`, and with the counter `next_index` if given. An archive of an earlier draft gets its
|
|
1052
|
-
* `_archive` fields and comment of RFC-13 (`number`, `updates_url`).
|
|
1053
|
-
*/
|
|
1054
|
-
async #writeArchive(archive: Loaded, items: FeedItem[], signal?: AbortSignal, nextIndex?: number) {
|
|
1055
|
-
const n = this.#archiveNumber(this.#session.publicUrl(archive.path))
|
|
1056
|
-
const fields: ArchiveFields = { about: ARCHIVE_SPEC, ...archive.doc._archive, updates_url: this.url, ...n === undefined ? {} : { number: n } }
|
|
1057
|
-
if (nextIndex !== undefined) fields.next_index = nextIndex
|
|
1058
|
-
const doc: FeedDocument = { ...archive.doc, user_comment: archiveComment(this.url), items, _archive: fields }
|
|
1059
|
-
await this.#session.replace(archive.path, serialize(doc), archive.etag, { contentType: FEED_CONTENT_TYPE, signal })
|
|
1060
|
-
}
|
|
1061
|
-
|
|
1062
|
-
/**
|
|
1063
|
-
* The items of the feed file: the newest archive items first (which also repairs items an interrupted post wrote
|
|
1064
|
-
* only into the archive), then the other items of the feed file, at most `maxItems`, fewer if `maxBytes` is set
|
|
1065
|
-
* and exceeded, but at least 10.
|
|
1066
|
-
*/
|
|
1067
|
-
#newestItems(recent: FeedItem[], current: FeedItem[], doc: FeedDocument): FeedItem[] {
|
|
1068
|
-
const ids = new Set(recent.map(item => item.id))
|
|
1069
|
-
const items = [...recent, ...current.filter(item => !ids.has(item.id))].slice(0, this.#maxItems)
|
|
1070
|
-
if (this.#maxBytes)
|
|
1071
|
-
while (items.length > minFeedItems && byteLength(serialize({ ...doc, items })) > this.#maxBytes) items.pop()
|
|
1072
|
-
return items
|
|
1073
|
-
}
|
|
1074
|
-
|
|
1075
|
-
// Archives
|
|
1076
|
-
|
|
1077
|
-
#archivePath(n: number) {
|
|
1078
|
-
return `${this.directory}feed.${String(n).padStart(6, '0')}.json`
|
|
1079
|
-
}
|
|
1080
|
-
|
|
1081
|
-
#archiveUrl(n: number) {
|
|
1082
|
-
return this.#session.publicUrl(this.#archivePath(n))
|
|
1083
|
-
}
|
|
1084
|
-
|
|
1085
|
-
/** `_archive.archive_template` (RFC-13): the archive URL with `{number}` for the number. */
|
|
1086
|
-
#archiveTemplate() {
|
|
1087
|
-
return this.#archiveUrl(0).replace(/000000\.json$/, '{number}.json')
|
|
1088
|
-
}
|
|
1089
|
-
|
|
1090
|
-
/** The number of the archive at `url`, or undefined if `url` is not an archive file of this feed. */
|
|
1091
|
-
#archiveNumber(url: string | undefined): number | undefined {
|
|
1092
|
-
const path = url && pathFromPublicUrl(this.#session.server, url)
|
|
1093
|
-
if (!path?.startsWith(this.directory)) return
|
|
1094
|
-
const match = /^feed\.(\d{6})\.json$/.exec(path.slice(this.directory.length))
|
|
1095
|
-
return match ? Number(match[1]) : undefined
|
|
1096
|
-
}
|
|
1097
|
-
|
|
1098
|
-
/**
|
|
1099
|
-
* A new archive with the feed fields of the feed file (RFC-13 "Feed fields of an archive"), except `_submissions`:
|
|
1100
|
-
* whether a feed accepts comments is said by its feed file alone.
|
|
1101
|
-
*/
|
|
1102
|
-
#newArchive(n: number, main: FeedDocument, items: FeedItem[], nextUrl: string | undefined, nextIndex?: number): FeedDocument {
|
|
1103
|
-
const { items: _items, next_url: _next, user_comment: _comment, hubs: _hubs, author: _author, _pups, _archive, _submissions: _s, ...fields } = main
|
|
1104
|
-
const podcast = fields._podcast
|
|
1105
|
-
if (isObject(podcast) && 'liveItems' in podcast) {
|
|
1106
|
-
const { liveItems: _live, ...rest } = podcast
|
|
1107
|
-
fields._podcast = rest
|
|
1108
|
-
}
|
|
1109
|
-
return {
|
|
1110
|
-
...fields,
|
|
1111
|
-
version: JSON_FEED_VERSION,
|
|
1112
|
-
home_page_url: this.#session.me,
|
|
1113
|
-
feed_url: this.#archiveUrl(n),
|
|
1114
|
-
user_comment: archiveComment(this.url),
|
|
1115
|
-
...nextUrl ? { next_url: nextUrl } : {},
|
|
1116
|
-
items,
|
|
1117
|
-
_pups: { about: PUPS_FEED_SPEC, ..._pups, pupsControlled: true },
|
|
1118
|
-
_archive: { about: ARCHIVE_SPEC, updates_url: this.url, number: n, ...nextIndex === undefined ? {} : { next_index: nextIndex } },
|
|
1119
|
-
}
|
|
1120
|
-
}
|
|
1121
|
-
|
|
1122
|
-
async #createArchive(n: number, main: FeedDocument, items: FeedItem[], nextUrl: string | undefined, signal?: AbortSignal, nextIndex?: number) {
|
|
1123
|
-
await this.#session.create(this.#archivePath(n), serialize(this.#newArchive(n, main, items, nextUrl, nextIndex)), { contentType: FEED_CONTENT_TYPE, signal })
|
|
1124
|
-
}
|
|
1125
|
-
|
|
1126
|
-
/**
|
|
1127
|
-
* Reads an archive for writing.
|
|
1128
|
-
* @returns undefined if it doesn't exist, 'foreign' if it exists but is no PUPS archive.
|
|
1129
|
-
*/
|
|
1130
|
-
async #readArchive(path: string, signal?: AbortSignal): Promise<Loaded | 'foreign' | undefined> {
|
|
787
|
+
/** Sends an item to the server (RFC-7 "Posting items"). */
|
|
788
|
+
async #post(item: Record<string, unknown>, signal?: AbortSignal): Promise<Posted> {
|
|
789
|
+
this.#checkKept()
|
|
1131
790
|
try {
|
|
1132
|
-
|
|
1133
|
-
return loaded && !isControlled(loaded.doc) ? 'foreign' : loaded
|
|
791
|
+
return posted((await this.#session.post(this.path, item, { signal })).body, this.path)
|
|
1134
792
|
} catch (e) {
|
|
1135
|
-
if (isPupsError(e, '
|
|
793
|
+
if (isPupsError(e, 'not_pups_controlled')) throw notControlled(this.path)
|
|
794
|
+
if (isPupsError(e, 'not_found') && !await this.#session.get(this.path, { signal }))
|
|
795
|
+
throw new PupsError('not_found', `There is no feed at ${this.path} yet. Set it up with create().`, { cause: e })
|
|
1136
796
|
throw e
|
|
1137
797
|
}
|
|
1138
798
|
}
|
|
1139
799
|
|
|
1140
800
|
/**
|
|
1141
|
-
*
|
|
1142
|
-
*
|
|
1143
|
-
* feed
|
|
1144
|
-
* @returns the URL of the newest archive and the feed's items with their indexes, or undefined if the feed has no
|
|
1145
|
-
* items.
|
|
801
|
+
* Changes the feed file's fields with `change` and writes it with `If-Match`; reads it again and repeats when
|
|
802
|
+
* someone else wrote in between, up to `maxAttempts` times. The server keeps the items and the archive fields.
|
|
803
|
+
* @returns the feed as written.
|
|
1146
804
|
*/
|
|
1147
|
-
async #
|
|
1148
|
-
|
|
1149
|
-
|
|
1150
|
-
|
|
1151
|
-
|
|
1152
|
-
|
|
1153
|
-
|
|
1154
|
-
const nextUrl = n ? this.#archiveUrl(n - 1) : main.doc.next_url
|
|
1155
|
-
const nextIndex = n == count - 1 ? indexed.next : undefined
|
|
805
|
+
async #change(change: (doc: FeedDocument) => void, signal?: AbortSignal): Promise<FeedDocument> {
|
|
806
|
+
for (let attempt = 1; ; attempt++) {
|
|
807
|
+
const main = await this.#read(this.path, signal)
|
|
808
|
+
if (!main) throw new PupsError('not_found', `There is no feed at ${this.path} yet. Set it up with create().`)
|
|
809
|
+
if (!isControlled(main.doc)) throw notControlled(this.path)
|
|
810
|
+
const doc = clone(main.doc)
|
|
811
|
+
change(doc)
|
|
1156
812
|
try {
|
|
1157
|
-
await this.#
|
|
813
|
+
await this.#session.replace(this.path, serialize(doc), main.etag, { contentType: FEED_CONTENT_TYPE, signal })
|
|
814
|
+
return doc
|
|
1158
815
|
} catch (e) {
|
|
1159
|
-
if (!isPupsError(e, 'precondition_failed')) throw e
|
|
1160
|
-
// Left behind by an interrupted start: complete it, as long as it holds only these items.
|
|
1161
|
-
const existing = await this.#readArchive(this.#archivePath(n), signal)
|
|
1162
|
-
const ids = new Set(items.map(item => item.id))
|
|
1163
|
-
if (!existing || existing == 'foreign' || !existing.doc.items.every(item => ids.has(item.id)))
|
|
1164
|
-
throw new PupsError('conflict', `${this.#archivePath(n)} exists already, but ${this.path} has no archive_url. Remove the file, or set _archive.archive_url (RFC-7, Starting the archives).`)
|
|
1165
|
-
await this.#writeArchive(existing, items, signal, nextIndex)
|
|
816
|
+
if (!isPupsError(e, 'precondition_failed') || attempt >= this.#maxAttempts) throw e
|
|
1166
817
|
}
|
|
1167
|
-
url = this.#archiveUrl(n)
|
|
1168
|
-
}
|
|
1169
|
-
return url ? { url, items: indexed.items } : undefined
|
|
1170
|
-
}
|
|
1171
|
-
|
|
1172
|
-
/**
|
|
1173
|
-
* Indexes a feed with archives whose newest archive has no counter, or items without an index (RFC-7 "Indexing an
|
|
1174
|
-
* existing feed"): gives every archived item without an index one, the oldest first, writes the changed archives
|
|
1175
|
-
* from the oldest to the newest, the newest with the counter, and then the feed file's copies.
|
|
1176
|
-
*/
|
|
1177
|
-
async #indexArchives(main: Loaded, signal?: AbortSignal): Promise<void> {
|
|
1178
|
-
const files: Loaded[] = []
|
|
1179
|
-
for await (const file of this.#chain(main, signal)) {
|
|
1180
|
-
// Pages behind the oldest archive of a handed-over feed are never written, and keep their items unindexed.
|
|
1181
|
-
if (file == main || !isControlled(file.doc)) break
|
|
1182
|
-
files.push(file)
|
|
1183
|
-
}
|
|
1184
|
-
if (!files.length) return
|
|
1185
|
-
let next = nextFree(files.flatMap(file => file.doc.items), ...files.map(file => counterOf(file.doc)))
|
|
1186
|
-
const indexes = new Map<string, number>()
|
|
1187
|
-
for (const file of [...files].reverse()) {
|
|
1188
|
-
const indexed = indexItems(file.doc.items, next)
|
|
1189
|
-
next = indexed.next
|
|
1190
|
-
for (const item of indexed.items) indexes.set(item.id, itemIndex(item)!)
|
|
1191
|
-
const newest = file == files[0]
|
|
1192
|
-
if (indexed.changed || (newest && counterOf(file.doc) !== next)) await this.#writeArchive(file, indexed.items, signal, newest ? next : undefined)
|
|
1193
818
|
}
|
|
1194
|
-
let changed = false
|
|
1195
|
-
const items = main.doc.items.map(item => {
|
|
1196
|
-
const index = indexes.get(item.id)
|
|
1197
|
-
if (index === undefined || itemIndex(item) === index) return item
|
|
1198
|
-
changed = true
|
|
1199
|
-
return withIndex(item, index)
|
|
1200
|
-
})
|
|
1201
|
-
if (changed) await this.#writeMain({ ...main.doc, items }, main.etag, signal)
|
|
1202
819
|
}
|
|
1203
820
|
|
|
1204
|
-
/**
|
|
1205
|
-
|
|
1206
|
-
|
|
1207
|
-
|
|
1208
|
-
|
|
1209
|
-
|
|
1210
|
-
*/
|
|
1211
|
-
async #addToArchive(main: Loaded, doc: FeedDocument, item: FeedItem, signal?: AbortSignal, template?: string): Promise<{ url: string; recent: FeedItem[]; item: FeedItem }> {
|
|
1212
|
-
const place = (index: number) => this.#withPostUrl(withIndex(item, index), index, template)
|
|
1213
|
-
const newest = this.#archiveNumber(doc._archive?.archive_url)
|
|
1214
|
-
let previous: FeedItem[] = []
|
|
1215
|
-
// The index for the item if it goes into a new archive.
|
|
1216
|
-
let next = nextFree(doc.items, newest === undefined ? counterOf(doc) : undefined)
|
|
1217
|
-
if (newest !== undefined) {
|
|
1218
|
-
const existing = await this.#readArchive(this.#archivePath(newest), signal)
|
|
1219
|
-
if (!existing) {
|
|
1220
|
-
// The owner deleted it: create it again, linked to the next older archive, with an index above all known ones.
|
|
1221
|
-
const before = newest ? await this.#readArchive(this.#archivePath(newest - 1), signal) : undefined
|
|
1222
|
-
if (before && before != 'foreign') next = nextFree([...doc.items, ...before.doc.items], counterOf(before.doc))
|
|
1223
|
-
const stored = place(next)
|
|
1224
|
-
await this.#createArchive(newest, doc, [stored], newest ? this.#archiveUrl(newest - 1) : undefined, signal, next + 1)
|
|
1225
|
-
return { url: this.#archiveUrl(newest), recent: [stored], item: stored }
|
|
1226
|
-
}
|
|
1227
|
-
if (existing != 'foreign') {
|
|
1228
|
-
if (counterOf(existing.doc) === undefined) {
|
|
1229
|
-
await this.#indexArchives(main, signal)
|
|
1230
|
-
throw conflict(`${this.path} was indexed, so it`)
|
|
1231
|
-
}
|
|
1232
|
-
// Items that a client without indexes added get theirs first (RFC-7 "Items without an index").
|
|
1233
|
-
const indexed = indexItems(existing.doc.items, nextFree(existing.doc.items, counterOf(existing.doc)))
|
|
1234
|
-
const copy = indexed.items.find(i => i.id == item.id)
|
|
1235
|
-
const stored = place(copy ? itemIndex(copy)! : indexed.next)
|
|
1236
|
-
const merged = mergeItems(indexed.items, [stored])
|
|
1237
|
-
if (merged.length <= this.#archiveSize) {
|
|
1238
|
-
await this.#writeArchive(existing, merged, signal, copy ? indexed.next : indexed.next + 1)
|
|
1239
|
-
return { url: this.#archiveUrl(newest), recent: merged, item: stored }
|
|
1240
|
-
}
|
|
1241
|
-
if (indexed.changed) await this.#writeArchive(existing, indexed.items, signal, indexed.next)
|
|
1242
|
-
previous = indexed.items
|
|
1243
|
-
next = indexed.next
|
|
1244
|
-
}
|
|
1245
|
-
}
|
|
1246
|
-
// The archive before the new one: the newest, or for a feed without archives the end of its old chain.
|
|
1247
|
-
const nextUrl = newest === undefined ? doc.next_url : this.#archiveUrl(newest)
|
|
1248
|
-
for (let n = newest === undefined ? 0 : newest + 1; n <= maxArchiveNumber; n++) {
|
|
1249
|
-
const stored = place(next)
|
|
1250
|
-
try {
|
|
1251
|
-
await this.#createArchive(n, doc, [stored], nextUrl, signal, next + 1)
|
|
1252
|
-
return { url: this.#archiveUrl(n), recent: [stored, ...previous], item: stored }
|
|
1253
|
-
} catch (e) {
|
|
1254
|
-
if (!isPupsError(e, 'precondition_failed')) throw e
|
|
1255
|
-
}
|
|
1256
|
-
// The archive exists: left behind by an interrupted client, or created by another client just now.
|
|
1257
|
-
const existing = await this.#readArchive(this.#archivePath(n), signal)
|
|
1258
|
-
if (existing && existing != 'foreign' && existing.doc.next_url == nextUrl) {
|
|
1259
|
-
const indexed = indexItems(existing.doc.items, nextFree(existing.doc.items, counterOf(existing.doc), next))
|
|
1260
|
-
const copy = indexed.items.find(i => i.id == item.id)
|
|
1261
|
-
const added = place(copy ? itemIndex(copy)! : indexed.next)
|
|
1262
|
-
const merged = mergeItems(indexed.items, [added])
|
|
1263
|
-
if (merged.length <= this.#archiveSize) {
|
|
1264
|
-
await this.#writeArchive(existing, merged, signal, copy ? indexed.next : indexed.next + 1)
|
|
1265
|
-
return { url: this.#archiveUrl(n), recent: [...merged, ...previous], item: added }
|
|
1266
|
-
}
|
|
1267
|
-
}
|
|
1268
|
-
const current = await this.#session.get(this.path, { signal })
|
|
1269
|
-
if (current?.etag != main.etag) throw conflict(this.path)
|
|
1270
|
-
}
|
|
1271
|
-
throw new PupsError('conflict', `No free archive number in ${this.directory}.`)
|
|
821
|
+
/** The number of the archive at `url`, or undefined if `url` is not an archive file of this feed. */
|
|
822
|
+
#archiveNumber(url: string | undefined): number | undefined {
|
|
823
|
+
const path = url && pathFromPublicUrl(this.#session.server, url)
|
|
824
|
+
if (!path?.startsWith(this.directory)) return
|
|
825
|
+
const match = /^feed\.(\d{6,})\.json$/.exec(path.slice(this.directory.length))
|
|
826
|
+
return match ? Number(match[1]) : undefined
|
|
1272
827
|
}
|
|
1273
828
|
|
|
1274
829
|
// Items and media
|
|
1275
830
|
|
|
1276
|
-
/** The site's post URL template from `host-meta.json`, read once; undefined if there is none or it can't be read. */
|
|
1277
|
-
#postUrlTemplate(signal?: AbortSignal): Promise<string | undefined> {
|
|
1278
|
-
if (!this.#postUrls) return Promise.resolve(undefined)
|
|
1279
|
-
this.#template ??= this.#session.hostMeta.get({ signal })
|
|
1280
|
-
.then(doc => doc.properties?.[POST_URL_TEMPLATE])
|
|
1281
|
-
.then(template => validTemplate(template) ? template : undefined, () => undefined)
|
|
1282
|
-
return this.#template
|
|
1283
|
-
}
|
|
1284
|
-
|
|
1285
|
-
/** The item with the `url` from the post URL template (RFC-7 "Post URLs"), unless it has one. */
|
|
1286
|
-
#withPostUrl(item: FeedItem, index: number, template?: string): FeedItem {
|
|
1287
|
-
if (!template || item.url) return item
|
|
1288
|
-
const vars: Record<string, string> = { feed: this.path.slice(1, -'.json'.length), index: String(index), id: encodeURIComponent(item.id) }
|
|
1289
|
-
return { ...item, url: template.replace(/\{(feed|index|id)\}/g, (_, name: string) => vars[name]!) }
|
|
1290
|
-
}
|
|
1291
|
-
|
|
1292
831
|
#newItem(input: PostInput): FeedItem {
|
|
1293
832
|
const { id, markdown, date_published, ...fields } = input
|
|
1294
833
|
if (id !== undefined && (typeof id != 'string' || !id))
|
|
@@ -1301,13 +840,12 @@ export class PupsFeed {
|
|
|
1301
840
|
if (markdown !== undefined) this.#setMarkdown(item, markdown)
|
|
1302
841
|
if (item.content_html === undefined && item.content_text === undefined)
|
|
1303
842
|
throw new PupsError('invalid_request', 'A post needs content_html, content_text or markdown (JSON Feed requires content).')
|
|
1304
|
-
//
|
|
1305
|
-
|
|
1306
|
-
|
|
1307
|
-
|
|
1308
|
-
|
|
1309
|
-
|
|
1310
|
-
else delete item[key]
|
|
843
|
+
// An item with an index names an existing item; the server gives new items theirs (RFC-7 "Posting items").
|
|
844
|
+
delete item._pups
|
|
845
|
+
if (isObject(item._archive)) {
|
|
846
|
+
const { index: _index, ...rest } = item._archive
|
|
847
|
+
if (Object.keys(rest).length) item._archive = rest
|
|
848
|
+
else delete item._archive
|
|
1311
849
|
}
|
|
1312
850
|
return item
|
|
1313
851
|
}
|
|
@@ -1339,9 +877,9 @@ export class PupsFeed {
|
|
|
1339
877
|
if (item.content_html === undefined) item.content_text = markdown
|
|
1340
878
|
}
|
|
1341
879
|
|
|
1342
|
-
#result(item: FeedItem,
|
|
880
|
+
#result(item: FeedItem, address: string): PostResult {
|
|
1343
881
|
const index = itemIndex(item)
|
|
1344
|
-
return { id: item.id, ...index === undefined ? {} : { index }, item, url:
|
|
882
|
+
return { id: item.id, ...index === undefined ? {} : { index }, item, url: address }
|
|
1345
883
|
}
|
|
1346
884
|
|
|
1347
885
|
/**
|
|
@@ -1385,14 +923,11 @@ export class PupsFeed {
|
|
|
1385
923
|
let old: unknown
|
|
1386
924
|
let doc: FeedDocument
|
|
1387
925
|
try {
|
|
1388
|
-
doc = await this.#
|
|
1389
|
-
|
|
1390
|
-
|
|
1391
|
-
|
|
1392
|
-
|
|
1393
|
-
else delete changed[field]
|
|
1394
|
-
return this.#writeMain(changed, main.etag, signal)
|
|
1395
|
-
})
|
|
926
|
+
doc = await this.#change(doc => {
|
|
927
|
+
old = doc[field]
|
|
928
|
+
if (uploaded) doc[field] = uploaded.url
|
|
929
|
+
else delete doc[field]
|
|
930
|
+
}, signal)
|
|
1396
931
|
} catch (e) {
|
|
1397
932
|
if (uploaded) await this.#deleteQuietly(uploaded.path)
|
|
1398
933
|
throw e
|
|
@@ -1423,6 +958,11 @@ export class PupsFeed {
|
|
|
1423
958
|
throw new PupsError('invalid_request', `"${key}" is not a feed field the owner can set. Use title, description, authors, language, expired or an extension starting with "_".`)
|
|
1424
959
|
}
|
|
1425
960
|
|
|
961
|
+
#checkKept() {
|
|
962
|
+
if (!this.#session.server.keptFeeds)
|
|
963
|
+
throw new PupsError('unsupported', `This PUPS server doesn't keep feeds: its configuration has no "kept_feeds": true (RFC-7, Server requirements).`)
|
|
964
|
+
}
|
|
965
|
+
|
|
1426
966
|
#checkWritable() {
|
|
1427
967
|
if (!this.writable)
|
|
1428
968
|
throw new PupsError('insufficient_scope', `Writing ${this.path} needs the scopes ${this.scopes.join(' ')} (RFC-7, Server requirements).`)
|