pups-client 0.5.0 → 0.7.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 +6 -7
- package/dist/comments.d.ts +92 -0
- package/dist/comments.d.ts.map +1 -0
- package/dist/comments.js +203 -0
- package/dist/comments.js.map +1 -0
- package/dist/feed.d.ts +27 -9
- package/dist/feed.d.ts.map +1 -1
- package/dist/feed.js +94 -40
- package/dist/feed.js.map +1 -1
- package/dist/index.d.ts +4 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -1
- package/dist/index.js.map +1 -1
- package/dist/mentions.d.ts +2 -2
- package/dist/mentions.d.ts.map +1 -1
- package/dist/mentions.js +7 -5
- package/dist/mentions.js.map +1 -1
- package/dist/session.d.ts +6 -0
- package/dist/session.d.ts.map +1 -1
- package/dist/session.js +9 -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/comments.ts +247 -0
- package/src/feed.ts +113 -45
- package/src/index.ts +5 -1
- package/src/mentions.ts +9 -5
- package/src/session.ts +10 -0
- package/src/version.ts +1 -1
package/src/feed.ts
CHANGED
|
@@ -19,6 +19,9 @@ export const POST_URL_TEMPLATE = 'https://bigwide.net/ns/post-url-template'
|
|
|
19
19
|
/** The `about` of the `_pups` object: RFC-7. Not its final location; readers must not rely on it. */
|
|
20
20
|
export const PUPS_FEED_SPEC = 'https://github.com/bigwide-net/RFCs/blob/main/RFC-7-Feed-Publishing-with-PUPS.md'
|
|
21
21
|
|
|
22
|
+
/** The `about` of the `_archive` object: RFC-13 (JSON Feed Archives). Not its final location either. */
|
|
23
|
+
export const ARCHIVE_SPEC = 'https://github.com/bigwide-net/RFCs/blob/main/RFC-13-JSON-Feed-Archives.md'
|
|
24
|
+
|
|
22
25
|
const RFC1_URL = 'https://github.com/bigwide-net/RFCs/blob/main/RFC-1-Source-for-JSON-Feeds.md'
|
|
23
26
|
const RFC4_URL = 'https://github.com/bigwide-net/RFCs/blob/main/RFC-4-Feed-Submissions.md'
|
|
24
27
|
const JSON_FEED_VERSION = 'https://jsonfeed.org/version/1.1'
|
|
@@ -27,9 +30,9 @@ const maxArchiveNumber = 999_999
|
|
|
27
30
|
/** The feed file keeps at least this many items, also when `maxBytes` is exceeded (RFC-7 "Size in bytes"). */
|
|
28
31
|
const minFeedItems = 10
|
|
29
32
|
|
|
30
|
-
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
|
|
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.`
|
|
31
34
|
const archiveComment = (feedUrl: string) =>
|
|
32
|
-
`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
|
|
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.`
|
|
33
36
|
|
|
34
37
|
/** Feed names (RFC-7 "Named feeds"): 1 to 40 lowercase letters, digits and `-`, not starting or ending with `-`. */
|
|
35
38
|
const feedNamePattern = /^[a-z0-9](?:[a-z0-9-]{0,38}[a-z0-9])?$/
|
|
@@ -38,7 +41,7 @@ const feedNamePattern = /^[a-z0-9](?:[a-z0-9-]{0,38}[a-z0-9])?$/
|
|
|
38
41
|
const mainMediaFolders = ['images', 'media']
|
|
39
42
|
|
|
40
43
|
/** Top-level fields that are fixed or managed by RFC-7, and can't be set through `create()` or `updateSettings()`. */
|
|
41
|
-
const reservedFields = new Set(['version', 'home_page_url', 'feed_url', 'user_comment', 'next_url', 'items', 'hubs', '_pups', 'icon', 'favicon'])
|
|
44
|
+
const reservedFields = new Set(['version', 'home_page_url', 'feed_url', 'user_comment', 'next_url', 'items', 'hubs', '_pups', '_archive', 'icon', 'favicon'])
|
|
42
45
|
|
|
43
46
|
export interface FeedAuthor {
|
|
44
47
|
name?: string
|
|
@@ -73,28 +76,43 @@ export interface FeedItem {
|
|
|
73
76
|
tags?: string[]
|
|
74
77
|
language?: string
|
|
75
78
|
attachments?: FeedAttachment[]
|
|
76
|
-
/** The item's `
|
|
77
|
-
|
|
79
|
+
/** The item's `_archive` object, with its index (RFC-13 "Indexes"). */
|
|
80
|
+
_archive?: ItemArchive
|
|
78
81
|
[field: string]: unknown
|
|
79
82
|
}
|
|
80
83
|
|
|
81
|
-
/** The `
|
|
82
|
-
export interface
|
|
83
|
-
/** The item's index: 0 for a feed's first item, then counting up; never changed, never used again (RFC-
|
|
84
|
+
/** The `_archive` object of an item (RFC-13). */
|
|
85
|
+
export interface ItemArchive {
|
|
86
|
+
/** The item's index: 0 for a feed's first item, then counting up; never changed, never used again (RFC-13 "Indexes"). */
|
|
84
87
|
index?: number
|
|
85
88
|
[field: string]: unknown
|
|
86
89
|
}
|
|
87
90
|
|
|
88
|
-
/** The `_pups`
|
|
91
|
+
/** @deprecated The index moved from `_pups` to `_archive` (RFC-13): use ItemArchive. */
|
|
92
|
+
export type ItemMarker = ItemArchive
|
|
93
|
+
|
|
94
|
+
/** The `_pups` object of a feed file or archive: the marker of RFC-7. */
|
|
89
95
|
export interface PupsMarker {
|
|
90
96
|
about?: string
|
|
91
97
|
pupsControlled?: boolean
|
|
98
|
+
[field: string]: unknown
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/** The `_archive` object of a feed file or archive (RFC-13). */
|
|
102
|
+
export interface ArchiveFields {
|
|
103
|
+
about?: string
|
|
92
104
|
/** In the feed file: the public URL of the newest archive. */
|
|
93
105
|
archive_url?: string
|
|
106
|
+
/** In the feed file: the URL of every archive, with `{number}` for its six-digit number. */
|
|
107
|
+
archive_template?: string
|
|
108
|
+
/** In the feed file: the most items an archive holds. */
|
|
109
|
+
archive_size?: number
|
|
94
110
|
/** In an archive: the public URL of the feed file, which readers poll. */
|
|
95
111
|
updates_url?: string
|
|
112
|
+
/** In an archive: its number, from 0 for the oldest. */
|
|
113
|
+
number?: number
|
|
96
114
|
/**
|
|
97
|
-
* The index the next new item gets (RFC-
|
|
115
|
+
* The index the next new item gets (RFC-13 "Indexes"). Valid in the newest archive, or in the feed file of a feed
|
|
98
116
|
* without archives.
|
|
99
117
|
*/
|
|
100
118
|
next_index?: number
|
|
@@ -118,6 +136,7 @@ export interface FeedDocument {
|
|
|
118
136
|
hubs?: unknown[]
|
|
119
137
|
items: FeedItem[]
|
|
120
138
|
_pups?: PupsMarker
|
|
139
|
+
_archive?: ArchiveFields
|
|
121
140
|
[field: string]: unknown
|
|
122
141
|
}
|
|
123
142
|
|
|
@@ -343,13 +362,42 @@ function mergeItems(existing: FeedItem[], incoming: FeedItem[]): FeedItem[] {
|
|
|
343
362
|
return [...incoming.filter(item => !present.has(item.id)), ...existing.map(item => byId.get(item.id) ?? item)]
|
|
344
363
|
}
|
|
345
364
|
|
|
346
|
-
/** The index of an item (RFC-
|
|
365
|
+
/** The index of an item (RFC-13 "Indexes"), also from `_pups` of earlier drafts, or undefined if it has none. */
|
|
347
366
|
export function itemIndex(item: FeedItem): number | undefined {
|
|
348
|
-
const index = isObject(item._pups) ? item._pups.index : undefined
|
|
367
|
+
const index = isObject(item._archive) && item._archive.index !== undefined ? item._archive.index : isObject(item._pups) ? item._pups.index : undefined
|
|
349
368
|
return typeof index == 'number' && Number.isSafeInteger(index) && index >= 0 ? index : undefined
|
|
350
369
|
}
|
|
351
370
|
|
|
352
|
-
const withIndex = (item: FeedItem, index: number): FeedItem => ({ ...item,
|
|
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
|
+
}
|
|
353
401
|
|
|
354
402
|
/** A post URL template (RFC-6 "Post URLs"): an https URL with `{index}` or `{id}`. */
|
|
355
403
|
function validTemplate(template: unknown): template is string {
|
|
@@ -361,9 +409,9 @@ function validTemplate(template: unknown): template is string {
|
|
|
361
409
|
}
|
|
362
410
|
}
|
|
363
411
|
|
|
364
|
-
/** The counter `
|
|
412
|
+
/** The counter `_archive.next_index` of a file, if it has a valid one. */
|
|
365
413
|
function counterOf(doc: FeedDocument): number | undefined {
|
|
366
|
-
const next = doc.
|
|
414
|
+
const next = doc._archive?.next_index
|
|
367
415
|
return typeof next == 'number' && Number.isSafeInteger(next) && next >= 0 ? next : undefined
|
|
368
416
|
}
|
|
369
417
|
|
|
@@ -653,21 +701,21 @@ export class PupsFeed {
|
|
|
653
701
|
if (firstAttempt && main.doc.items.some(i => i.id == item.id))
|
|
654
702
|
throw new PupsError('invalid_request', `The feed already has an item with the id "${item.id}".`)
|
|
655
703
|
firstAttempt = false
|
|
656
|
-
const doc: FeedDocument = { ...main.doc,
|
|
657
|
-
const hasArchives = this.#archiveNumber(doc.
|
|
704
|
+
const doc: FeedDocument = { ...main.doc, _archive: { ...main.doc._archive } }
|
|
705
|
+
const hasArchives = this.#archiveNumber(doc._archive!.archive_url) !== undefined
|
|
658
706
|
if (!hasArchives && !this.#archivesWanted) {
|
|
659
707
|
// A feed without archives: the feed file is all there is, and holds the counter (RFC-7 "Posting").
|
|
660
708
|
archiveUrl = this.url
|
|
661
709
|
const indexed = indexItems(doc.items, nextFree(doc.items, counterOf(doc)))
|
|
662
710
|
stored = withIndex(item, indexed.next)
|
|
663
|
-
doc.
|
|
711
|
+
doc._archive!.next_index = indexed.next + 1
|
|
664
712
|
await this.#writeMain({ ...doc, items: this.#newestItems([stored], indexed.items, doc) }, main.etag, signal)
|
|
665
713
|
return
|
|
666
714
|
}
|
|
667
715
|
if (!hasArchives) {
|
|
668
716
|
const started = await this.#startArchives(main, signal)
|
|
669
717
|
if (started) {
|
|
670
|
-
doc.
|
|
718
|
+
doc._archive!.archive_url = started.url
|
|
671
719
|
doc.items = started.items
|
|
672
720
|
}
|
|
673
721
|
}
|
|
@@ -675,12 +723,12 @@ export class PupsFeed {
|
|
|
675
723
|
const added = await this.#addToArchive(main, doc, item, signal, template)
|
|
676
724
|
archiveUrl = added.url
|
|
677
725
|
stored = added.item
|
|
678
|
-
doc.
|
|
726
|
+
doc._archive!.archive_url = added.url
|
|
679
727
|
if (!hasArchives) {
|
|
680
728
|
// The old chain of a handed-over feed now continues behind the oldest archive, and the newest archive
|
|
681
729
|
// holds the counter.
|
|
682
730
|
delete doc.next_url
|
|
683
|
-
delete doc.
|
|
731
|
+
delete doc._archive!.next_index
|
|
684
732
|
}
|
|
685
733
|
doc.items = this.#newestItems(added.recent, doc.items, doc)
|
|
686
734
|
await this.#writeMain(doc, main.etag, signal)
|
|
@@ -697,11 +745,11 @@ export class PupsFeed {
|
|
|
697
745
|
const signal = options.signal
|
|
698
746
|
await this.#transaction(async () => {
|
|
699
747
|
const main = await this.#readControlled(this.path, signal)
|
|
700
|
-
const newest = this.#archiveNumber(main.doc.
|
|
748
|
+
const newest = this.#archiveNumber(main.doc._archive?.archive_url)
|
|
701
749
|
if (newest === undefined) {
|
|
702
750
|
const indexed = indexItems(main.doc.items, nextFree(main.doc.items, counterOf(main.doc)))
|
|
703
751
|
if (indexed.changed || counterOf(main.doc) !== indexed.next)
|
|
704
|
-
await this.#writeMain({ ...main.doc, items: indexed.items,
|
|
752
|
+
await this.#writeMain({ ...main.doc, items: indexed.items, _archive: { ...main.doc._archive, next_index: indexed.next } }, main.etag, signal)
|
|
705
753
|
return
|
|
706
754
|
}
|
|
707
755
|
const archive = await this.#readArchive(this.#archivePath(newest), signal)
|
|
@@ -721,7 +769,7 @@ export class PupsFeed {
|
|
|
721
769
|
}
|
|
722
770
|
|
|
723
771
|
/**
|
|
724
|
-
* All posts, newest first: the archives from `
|
|
772
|
+
* All posts, newest first: the archives from `_archive.archive_url` along `next_url` (for a feed without archives,
|
|
725
773
|
* the feed file and its `next_url` chain). Each post is returned once.
|
|
726
774
|
*/
|
|
727
775
|
async *posts(options: Signal = {}): AsyncGenerator<FeedItem> {
|
|
@@ -762,7 +810,7 @@ export class PupsFeed {
|
|
|
762
810
|
// The index never changes (RFC-7 "Indexes").
|
|
763
811
|
if (oldIndex !== undefined) item = withIndex(item, oldIndex)
|
|
764
812
|
// A post of a feed with archives that never had a url may get one (RFC-7 "Post URLs").
|
|
765
|
-
if (oldIndex !== undefined && !old.url && !item.url && this.#archiveNumber(main.doc.
|
|
813
|
+
if (oldIndex !== undefined && !old.url && !item.url && this.#archiveNumber(main.doc._archive?.archive_url) !== undefined)
|
|
766
814
|
item = this.#withPostUrl(item, oldIndex, await this.#postUrlTemplate(signal))
|
|
767
815
|
if (file != main) await this.#writeArchive(file, replaceAt(file.doc.items, index, item), signal)
|
|
768
816
|
const mainIndex = main.doc.items.findIndex(i => i.id == id)
|
|
@@ -834,6 +882,8 @@ export class PupsFeed {
|
|
|
834
882
|
* and what its `republish` means before the first submission.
|
|
835
883
|
* @throws {PupsError} `not_found`; `invalid_request` if the post answers nothing; `unsupported` if the target
|
|
836
884
|
* feed takes no submissions.
|
|
885
|
+
* @deprecated Comments and group posts don't belong in a feed for readers (RFC-7 "Comment feeds"). Use
|
|
886
|
+
* `session.comments.post()`, which keeps them in comment feeds in the app's directory.
|
|
837
887
|
*/
|
|
838
888
|
async submit(id: string, options: Signal = {}): Promise<Submission> {
|
|
839
889
|
const post = await this.getPost(id, options)
|
|
@@ -904,7 +954,7 @@ export class PupsFeed {
|
|
|
904
954
|
// Not JSON: handled below.
|
|
905
955
|
}
|
|
906
956
|
if (!isFeedDocument(doc)) throw new PupsError('invalid_feed', `${path} is not a JSON Feed.`)
|
|
907
|
-
return { path, doc, etag: file.etag }
|
|
957
|
+
return { path, doc: upgradeArchiveFields(doc), etag: file.etag }
|
|
908
958
|
}
|
|
909
959
|
|
|
910
960
|
/** Reads a file that must exist and carry the `_pups` marker. */
|
|
@@ -917,13 +967,13 @@ export class PupsFeed {
|
|
|
917
967
|
}
|
|
918
968
|
|
|
919
969
|
/**
|
|
920
|
-
* The files that hold all items: the archives from `
|
|
970
|
+
* The files that hold all items: the archives from `_archive.archive_url` along `next_url`, as far as they are files
|
|
921
971
|
* on this server. A feed without archives yet (handed over, not posted to) holds its items in the feed file and
|
|
922
972
|
* the pages its `next_url` names.
|
|
923
973
|
*/
|
|
924
974
|
async *#chain(main: Loaded, signal?: AbortSignal): AsyncGenerator<Loaded> {
|
|
925
975
|
const visited = new Set<string>([main.path])
|
|
926
|
-
const archive = main.doc.
|
|
976
|
+
const archive = main.doc._archive?.archive_url
|
|
927
977
|
let file: Loaded | undefined = main
|
|
928
978
|
let next = archive && this.#archiveNumber(archive) !== undefined ? archive : undefined
|
|
929
979
|
if (next) file = undefined
|
|
@@ -974,8 +1024,8 @@ export class PupsFeed {
|
|
|
974
1024
|
|
|
975
1025
|
/**
|
|
976
1026
|
* Writes the feed file. Sets the fixed fields, so that a feed the owner handed over gets them on its first write,
|
|
977
|
-
* and upgrades JSON Feed 1.0 (RFC-7 "Handing over an existing feed").
|
|
978
|
-
*
|
|
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").
|
|
979
1029
|
*/
|
|
980
1030
|
async #writeMain(doc: FeedDocument, etag: string, signal?: AbortSignal): Promise<FeedDocument> {
|
|
981
1031
|
const { author, ...rest } = doc
|
|
@@ -987,14 +1037,25 @@ export class PupsFeed {
|
|
|
987
1037
|
user_comment: mainComment,
|
|
988
1038
|
}
|
|
989
1039
|
if (author !== undefined && fixed.authors === undefined) fixed.authors = [author as FeedAuthor]
|
|
990
|
-
|
|
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
|
+
}
|
|
991
1046
|
await this.#session.replace(this.path, serialize(fixed), etag, { contentType: FEED_CONTENT_TYPE, signal })
|
|
992
1047
|
return fixed
|
|
993
1048
|
}
|
|
994
1049
|
|
|
995
|
-
/**
|
|
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
|
+
*/
|
|
996
1054
|
async #writeArchive(archive: Loaded, items: FeedItem[], signal?: AbortSignal, nextIndex?: number) {
|
|
997
|
-
const
|
|
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 }
|
|
998
1059
|
await this.#session.replace(archive.path, serialize(doc), archive.etag, { contentType: FEED_CONTENT_TYPE, signal })
|
|
999
1060
|
}
|
|
1000
1061
|
|
|
@@ -1021,6 +1082,11 @@ export class PupsFeed {
|
|
|
1021
1082
|
return this.#session.publicUrl(this.#archivePath(n))
|
|
1022
1083
|
}
|
|
1023
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
|
+
|
|
1024
1090
|
/** The number of the archive at `url`, or undefined if `url` is not an archive file of this feed. */
|
|
1025
1091
|
#archiveNumber(url: string | undefined): number | undefined {
|
|
1026
1092
|
const path = url && pathFromPublicUrl(this.#session.server, url)
|
|
@@ -1030,12 +1096,11 @@ export class PupsFeed {
|
|
|
1030
1096
|
}
|
|
1031
1097
|
|
|
1032
1098
|
/**
|
|
1033
|
-
* A new archive with the feed fields of the feed file (RFC-
|
|
1034
|
-
* accepts comments is said by its feed file alone.
|
|
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.
|
|
1035
1101
|
*/
|
|
1036
1102
|
#newArchive(n: number, main: FeedDocument, items: FeedItem[], nextUrl: string | undefined, nextIndex?: number): FeedDocument {
|
|
1037
|
-
const { items: _items, next_url: _next, user_comment: _comment, hubs: _hubs, author: _author, _pups, _submissions: _s, ...fields } = main
|
|
1038
|
-
const { archive_url: _archive, next_index: _counter, ...marker } = _pups ?? {}
|
|
1103
|
+
const { items: _items, next_url: _next, user_comment: _comment, hubs: _hubs, author: _author, _pups, _archive, _submissions: _s, ...fields } = main
|
|
1039
1104
|
const podcast = fields._podcast
|
|
1040
1105
|
if (isObject(podcast) && 'liveItems' in podcast) {
|
|
1041
1106
|
const { liveItems: _live, ...rest } = podcast
|
|
@@ -1049,7 +1114,8 @@ export class PupsFeed {
|
|
|
1049
1114
|
user_comment: archiveComment(this.url),
|
|
1050
1115
|
...nextUrl ? { next_url: nextUrl } : {},
|
|
1051
1116
|
items,
|
|
1052
|
-
_pups: { about: PUPS_FEED_SPEC, ...
|
|
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 } },
|
|
1053
1119
|
}
|
|
1054
1120
|
}
|
|
1055
1121
|
|
|
@@ -1095,7 +1161,7 @@ export class PupsFeed {
|
|
|
1095
1161
|
const existing = await this.#readArchive(this.#archivePath(n), signal)
|
|
1096
1162
|
const ids = new Set(items.map(item => item.id))
|
|
1097
1163
|
if (!existing || existing == 'foreign' || !existing.doc.items.every(item => ids.has(item.id)))
|
|
1098
|
-
throw new PupsError('conflict', `${this.#archivePath(n)} exists already, but ${this.path} has no archive_url. Remove the file, or set
|
|
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).`)
|
|
1099
1165
|
await this.#writeArchive(existing, items, signal, nextIndex)
|
|
1100
1166
|
}
|
|
1101
1167
|
url = this.#archiveUrl(n)
|
|
@@ -1144,7 +1210,7 @@ export class PupsFeed {
|
|
|
1144
1210
|
*/
|
|
1145
1211
|
async #addToArchive(main: Loaded, doc: FeedDocument, item: FeedItem, signal?: AbortSignal, template?: string): Promise<{ url: string; recent: FeedItem[]; item: FeedItem }> {
|
|
1146
1212
|
const place = (index: number) => this.#withPostUrl(withIndex(item, index), index, template)
|
|
1147
|
-
const newest = this.#archiveNumber(doc.
|
|
1213
|
+
const newest = this.#archiveNumber(doc._archive?.archive_url)
|
|
1148
1214
|
let previous: FeedItem[] = []
|
|
1149
1215
|
// The index for the item if it goes into a new archive.
|
|
1150
1216
|
let next = nextFree(doc.items, newest === undefined ? counterOf(doc) : undefined)
|
|
@@ -1235,11 +1301,13 @@ export class PupsFeed {
|
|
|
1235
1301
|
if (markdown !== undefined) this.#setMarkdown(item, markdown)
|
|
1236
1302
|
if (item.content_html === undefined && item.content_text === undefined)
|
|
1237
1303
|
throw new PupsError('invalid_request', 'A post needs content_html, content_text or markdown (JSON Feed requires content).')
|
|
1238
|
-
// The index is given out when the item is added (RFC-
|
|
1239
|
-
|
|
1240
|
-
const
|
|
1241
|
-
if (
|
|
1242
|
-
|
|
1304
|
+
// The index is given out when the item is added (RFC-13 "Indexes").
|
|
1305
|
+
for (const key of ['_archive', '_pups'] as const) {
|
|
1306
|
+
const value = item[key]
|
|
1307
|
+
if (!isObject(value)) continue
|
|
1308
|
+
const { index: _index, ...rest } = value
|
|
1309
|
+
if (Object.keys(rest).length) item[key] = rest
|
|
1310
|
+
else delete item[key]
|
|
1243
1311
|
}
|
|
1244
1312
|
return item
|
|
1245
1313
|
}
|
package/src/index.ts
CHANGED
|
@@ -16,8 +16,9 @@ export type {
|
|
|
16
16
|
} from './client.js'
|
|
17
17
|
export { PupsSession } from './session.js'
|
|
18
18
|
export type { Body, JsonBody, SessionChangeEvent, StoredFile, WriteOptions, WriteResult } from './session.js'
|
|
19
|
-
export { FEED_REL, POST_URL_TEMPLATE, PUPS_FEED_SPEC, PupsFeed, feedScopes, itemIndex } from './feed.js'
|
|
19
|
+
export { ARCHIVE_SPEC, FEED_REL, POST_URL_TEMPLATE, PUPS_FEED_SPEC, PupsFeed, feedScopes, itemIndex } from './feed.js'
|
|
20
20
|
export type {
|
|
21
|
+
ArchiveFields,
|
|
21
22
|
CommentPolicy,
|
|
22
23
|
CommentsState,
|
|
23
24
|
FeedAttachment,
|
|
@@ -30,6 +31,7 @@ export type {
|
|
|
30
31
|
FeedSettingsChanges,
|
|
31
32
|
FeedSetup,
|
|
32
33
|
FeedState,
|
|
34
|
+
ItemArchive,
|
|
33
35
|
ItemMarker,
|
|
34
36
|
MentionChoice,
|
|
35
37
|
MediaInput,
|
|
@@ -40,6 +42,8 @@ export type {
|
|
|
40
42
|
PupsMarker,
|
|
41
43
|
} from './feed.js'
|
|
42
44
|
export { HostMetaEditor } from './host-meta.js'
|
|
45
|
+
export { PupsComments, commentFeedPath } from './comments.js'
|
|
46
|
+
export type { CommentChanges, CommentInput, CommentResult } from './comments.js'
|
|
43
47
|
export { MENTION_REL, Mentions } from './mentions.js'
|
|
44
48
|
export type { Delivery, DeliveryStatus, FoundItem, MentionReport, MentionSource, MentionsOptions, PlannedMention, PostChange, PostMentions, Submission, SubmissionStatus } from './mentions.js'
|
|
45
49
|
export { accountHost, findHandles, handleText, itemHandles, visibleText } from './handles.js'
|
package/src/mentions.ts
CHANGED
|
@@ -151,6 +151,10 @@ function splitRef(ref: string): [string, string | undefined] {
|
|
|
151
151
|
return [ref.slice(0, at), id]
|
|
152
152
|
}
|
|
153
153
|
|
|
154
|
+
/** A field of a feed's RFC-13 `_archive`, or of `_pups` in feeds of earlier drafts (RFC-13 "Earlier drafts"). */
|
|
155
|
+
const archiveField = (doc: Record<string, unknown>, key: string): unknown =>
|
|
156
|
+
(isObject(doc._archive) ? doc._archive[key] : undefined) ?? (isObject(doc._pups) ? doc._pups[key] : undefined)
|
|
157
|
+
|
|
154
158
|
/**
|
|
155
159
|
* Sends webmentions: RFC-11 mentions to the domains a post names or answers, found through their `host-meta.json`,
|
|
156
160
|
* and RFC-4 submissions to the endpoint a target feed names in `_submissions`. Plain Webmention, form-encoded.
|
|
@@ -363,16 +367,16 @@ export class Mentions {
|
|
|
363
367
|
}
|
|
364
368
|
|
|
365
369
|
/**
|
|
366
|
-
* Finds an item, given as feed URL plus `#<id>`, as RFC-4 does: in the feed, then along `next_url` (or RFC-
|
|
367
|
-
* `
|
|
370
|
+
* Finds an item, given as feed URL plus `#<id>`, as RFC-4 does: in the feed, then along `next_url` (or RFC-13's
|
|
371
|
+
* `_archive.archive_url`) on the same host, at most 10 pages. JSON Feed only.
|
|
368
372
|
* @throws {PupsError} `network`, `not_found` or `invalid_feed` if the first feed can't be read.
|
|
369
373
|
*/
|
|
370
374
|
async findItem(ref: string, options: { signal?: AbortSignal; item?: boolean } = {}): Promise<FoundItem> {
|
|
371
375
|
const [url, id] = splitRef(ref)
|
|
372
376
|
const first = await this.#readFeed(url, options.signal)
|
|
373
|
-
// An RFC-
|
|
377
|
+
// An RFC-13 archive takes its _submissions from its feed file (RFC-7 "Receiving comments").
|
|
374
378
|
let owner = first
|
|
375
|
-
const updates =
|
|
379
|
+
const updates = archiveField(first.doc, 'updates_url')
|
|
376
380
|
if (typeof updates == 'string') {
|
|
377
381
|
const feedUrl = new URL(updates, first.url)
|
|
378
382
|
owner = feedUrl.host == new URL(first.url).host ? await this.#readFeed(feedUrl.href, options.signal).catch(() => ({ url: feedUrl.href, doc: {} })) : { url: feedUrl.href, doc: {} }
|
|
@@ -390,7 +394,7 @@ export class Mentions {
|
|
|
390
394
|
const author = accountHost(socialspec(item)) ?? (submissions ? undefined : accountHost(socialspec(owner.doc)))
|
|
391
395
|
return { ...result, item, ...author ? { author } : {} }
|
|
392
396
|
}
|
|
393
|
-
const next: unknown = feed.doc.next_url ?? (
|
|
397
|
+
const next: unknown = feed.doc.next_url ?? archiveField(feed.doc, 'archive_url')
|
|
394
398
|
if (typeof next != 'string') break
|
|
395
399
|
const nextUrl: URL = new URL(next, feed.url)
|
|
396
400
|
if (nextUrl.host != new URL(url).host || seen.has(nextUrl.href)) break
|
package/src/session.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { PupsComments } from './comments.js'
|
|
1
2
|
import { discover, type PupsServer } from './discovery.js'
|
|
2
3
|
import { errorFromResponse, isPupsError, PupsError } from './errors.js'
|
|
3
4
|
import { listFeeds, PupsFeed, type FeedLink, type FeedOptions } from './feed.js'
|
|
@@ -100,6 +101,7 @@ export class PupsSession {
|
|
|
100
101
|
/** Edits `host-meta.json` through the server's host-meta endpoint. */
|
|
101
102
|
readonly hostMeta: HostMetaEditor
|
|
102
103
|
#mentions?: Mentions
|
|
104
|
+
#comments?: PupsComments
|
|
103
105
|
/** @internal */
|
|
104
106
|
readonly _tokens: TokenManager
|
|
105
107
|
readonly #ctx: SessionContext
|
|
@@ -129,6 +131,14 @@ export class PupsSession {
|
|
|
129
131
|
return this.#mentions ??= new Mentions({ fetch: this.#ctx.fetch, me: this.me })
|
|
130
132
|
}
|
|
131
133
|
|
|
134
|
+
/**
|
|
135
|
+
* The owner's comments and posts to discussion groups (RFC-4 submissions), kept in comment feeds in the app's own
|
|
136
|
+
* directory, never in a feed for readers (RFC-7 "Comment feeds"). Needs the `appdata` scope.
|
|
137
|
+
*/
|
|
138
|
+
get comments(): PupsComments {
|
|
139
|
+
return this.#comments ??= new PupsComments(this)
|
|
140
|
+
}
|
|
141
|
+
|
|
132
142
|
/** The server configuration, pinned at login. */
|
|
133
143
|
get server(): PupsServer {
|
|
134
144
|
return this._tokens.record.server
|
package/src/version.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export const VERSION = '0.
|
|
1
|
+
export const VERSION = '0.7.0'
|