Repository navigation
Expand file tree
/
Copy pathsolidDeckRepository.ts
More file actions
1227 lines (1152 loc) · 49.5 KB
/
Copy pathsolidDeckRepository.ts
File metadata and controls
1227 lines (1152 loc) · 49.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SPDX-FileCopyrightText: 2026 Anton Wiklund and Solid Memo contributors
//
// SPDX-License-Identifier: MIT
import {
type Dataset,
type NamedSubject,
type Subject,
type SubjectBuilder,
buildThing,
createSolidDataset,
getThing,
getUrlAll,
setThing,
} from "./client/rdf";
import type { LangText } from "@solid-memo/domain/langText";
import { copyKeywords, noKeywords, type LangTexts } from "@solid-memo/domain/keywords";
import type { CardChangeOptions, CardChanges, DeckRepository } from "@solid-memo/application/ports";
import type { KeptDocuments } from "@solid-memo/domain/instance";
import {
CARD_FORMAT_VERSION,
DECK_FORMAT_VERSION,
DEFAULT_DECK_DIRECTION,
boundedCompletedChapters,
withBoundedMetadata,
type Card,
type CardContent,
type Deck,
type DeckDirection,
} from "@solid-memo/domain/deck";
import { cardToRecord } from "@solid-memo/domain/deckRecord";
import { editCompletedChapters as completedChaptersAfter } from "@solid-memo/domain/course";
import { withStatedLanguages } from "@solid-memo/domain/deckLanguages";
import { withCardLanguage } from "@solid-memo/domain/deckCardLanguage";
import { storedPictureHosts, withPictureHost } from "@solid-memo/domain/deckPictures";
import type { DeckPause } from "@solid-memo/domain/deckPause";
import {
cardsContainerOf,
catalogUrlOf,
documentsInUse,
ensureTrailingSlash,
canonicalUrlOf,
reviewsContainerOf,
subjectOfDocument,
} from "@solid-memo/domain/instanceLayout";
import { isDeckInPlace } from "@solid-memo/domain/deckAddress";
import { documentUrlOf, fragmentIdOf } from "@solid-memo/domain/subjectUrl";
import { isDeckOutdated, isOutdated, upgradeCard, upgradeDeck } from "@solid-memo/domain/migration";
import { ABSENT_VERSION } from "@solid-memo/domain/studyDigest";
import { CARD_V7, DECK_V6 } from "@solid-memo/vocab/descriptors.generated";
import {
applyDeckTreeEdit,
buildTree,
type StoredLayout,
treeChanges,
} from "@solid-memo/domain/deckTree";
import {
getSolidDatasetOrNull,
PreconditionFailedError,
removeOwned,
saveDataset,
versionOf,
type Removal,
UNREADABLE,
} from "./datasets";
import { deckDocumentSubjects } from "./ownership";
import { DCTERMS, RDF, SCHEMA, SM } from "./vocab";
import {
deckSubjects,
decksSubjects,
toDistractor,
toCard,
toCards,
toCatalog,
toDecks,
decksWithAgentsWithinBound,
withCatalog,
withDeck,
withDecks,
writeDistractors,
withoutCardSubjects,
withoutUnnamedDistractors,
assertDistractorsOwnedOnce,
withoutDecks,
withSuggestedAnswers,
} from "./mappers/deckMapper";
import { toStoredLayout, withTreeChanges } from "./mappers/deckTreeMapper";
import { createDeckDocumentGuard, type UserStorages } from "./deckPlace";
import { editOf, type DatasetEdit } from "./datasetEdit";
import { noWriteCheck, type WriteCheck } from "./writeCheck";
import { withoutReviewStates } from "./mappers/reviewStateMapper";
import {
datetimeLiteral,
readDatetime,
carriedTo,
recordThing,
unlessNewer,
unlessTyped,
withoutForeignShapedClasses,
} from "./records";
import { mapSince, readSince } from "./readSince";
import { AppError } from "@solid-memo/domain/appError";
import { linksGuestPod } from "@solid-memo/domain/guest";
import { finishCutOffImport, unsent } from "@solid-memo/application/writes";
import { sameDeckState, withDeckChanges } from "@solid-memo/domain/deckUpgrade";
import { refuseFullDeckList, withinCatalogLimit } from "./catalogLinks";
/**
* A card to write, by its id: a new card is stamped with the time of
* writing; one with `fromRelease` names the release it was taken from.
*/
type CardToWrite = CardContent & {
id: string;
createdAt?: string;
retired?: true;
fromRelease?: string;
};
/** A card to write, onto the subject it has in the document now, if any. */
interface CardWrite {
card: CardToWrite;
existing: NamedSubject | null;
}
export interface SolidDeckRepositoryDeps {
fetch: typeof globalThis.fetch;
/** The fetch of a deck's cards read as no one (listCards, readCardsSince `asNoOne`); `fetch` by default. */
publicFetch?: typeof globalThis.fetch;
now: () => Date;
randomId: () => string;
/** Checks what is about to be written; see writeCheck.ts. */
checkWrite?: WriteCheck;
/** The signed-in user's storages, for where a deck's document is (deckPlace.ts UserStorages); none by default. */
userStorages?: UserStorages;
}
/**
* How often an edit of the deck arrangement, a chapter's completion, or
* the entry of a guest's deck added (addDeck) is made, in all, while the
* catalog document keeps changing elsewhere (412). The edit is applied
* again to the document as it is then, so a retry keeps what changed.
*/
const TREE_ATTEMPTS = 3;
/**
* The arrangement the catalog states (toStoredLayout), or
* catalogTooManyLinks past MAX_CATALOG_LINKS decks, as listDecks gives:
* each deck's documents are read by whoever shows the tree.
*/
function cappedLayout(dataset: Dataset, catalogUrl: string): StoredLayout {
const layout = toStoredLayout(dataset, catalogUrl);
withinCatalogLimit(layout.decks);
return layout;
}
export function createSolidDeckRepository({
fetch,
publicFetch = fetch,
now,
randomId,
checkWrite = noWriteCheck,
userStorages,
}: SolidDeckRepositoryDeps): DeckRepository {
const guard = createDeckDocumentGuard(fetch, userStorages);
/** Save a document once the subjects the write touched are checked. */
async function save(
url: string,
dataset: Dataset,
subjects: readonly string[],
options?: { whole?: boolean },
): Promise<void> {
await checkWrite(dataset, subjects, "pod");
await saveDataset(url, dataset, fetch, options);
}
/**
* Save a deck's cards document, saying whose it is, once the subjects
* the write touched are checked: only one that is the deck's to change
* (deckPlace.ts, guard.writable), else nothing is sent.
*/
async function saveCardsDocument(
deck: Deck,
dataset: Dataset,
subjects: readonly string[],
options?: { whole?: boolean },
): Promise<void> {
const written = await checkedCardsDocument(deck, dataset, subjects);
await saveDataset(deck.cardsDocumentUrl, written, fetch, options);
}
/** The cards document saveCardsDocument sends, once checked as it checks it; nothing is sent. */
async function checkedCardsDocument(
deck: Deck,
dataset: Dataset,
subjects: readonly string[],
): Promise<Dataset> {
assertDistractorsOwnedOnce(dataset, subjects);
const written = await guard.writable(deck, deck.cardsDocumentUrl, dataset);
await checkWrite(written, subjects, "pod");
return written;
}
/**
* The write applyCardChanges makes, read, refused and checked as it
* is, but not sent: the function returned sends it (DeckRepository).
*/
async function prepareCardChanges(
deck: Deck,
changes: CardChanges,
{ whole, version, carried }: CardChangeOptions = {},
): Promise<() => Promise<void>> {
const read = await getSolidDatasetOrNull(deck.cardsDocumentUrl, fetch);
// Changes made from an earlier read go only to the document as it was then: a later edit is not undone.
if (
version !== undefined &&
(read === null ? ABSENT_VERSION : (versionOf(read) ?? null)) !== version
) {
throw new PreconditionFailedError(
deck.cardsDocumentUrl,
version === ABSENT_VERSION ? "absent" : "unchanged",
);
}
// A card copied from another deck starts from what that deck's document says of it.
const source =
carried === undefined
? null
: await getSolidDatasetOrNull(
carried.deck.cardsDocumentUrl,
carried.asNoOne === true ? publicFetch : fetch,
);
const changed = withCardChanges(
read ?? createSolidDataset(),
deck,
changes,
source === null
? undefined
: (id) => {
if (!Object.hasOwn(carried!.ids, id)) return null;
return getThing(source, `${carried!.deck.cardsDocumentUrl}#${carried!.ids[id]}`);
},
);
const written = await checkedCardsDocument(deck, changed.dataset, changed.subjects);
// If-Match the read above: a document changed since is not written over.
return () => saveDataset(deck.cardsDocumentUrl, written, fetch, { whole });
}
return {
async listDecks(instanceUrl): Promise<Deck[]> {
const catalogUrl = catalogUrlOf(instanceUrl);
const dataset = await getSolidDatasetOrNull(catalogUrl, fetch);
if (dataset === null) return [];
// Each deck's documents are read by whoever lists them: no more decks than a catalog may have.
return withinCatalogLimit(toDecks(dataset, catalogUrl));
},
async readCatalog(instanceUrl) {
const catalogUrl = catalogUrlOf(instanceUrl);
const dataset = await getSolidDatasetOrNull(catalogUrl, fetch);
return dataset === null ? null : toCatalog(dataset, catalogUrl);
},
async saveCatalog(instanceUrl, catalog) {
const catalogUrl = catalogUrlOf(instanceUrl);
const dataset = (await getSolidDatasetOrNull(catalogUrl, fetch)) ?? createSolidDataset();
await save(catalogUrl, withCatalog(dataset, catalogUrl, catalog), [
`${catalogUrl}#catalog`,
catalog.publisher.webId,
]);
},
async upgradeDecks(instanceUrl, catalog) {
const catalogUrl = catalogUrlOf(instanceUrl);
const read = await getSolidDatasetOrNull(catalogUrl, fetch);
let dataset = read ?? createSolidDataset();
// Each entry stored in an older format, as read and brought up to date in memory, written in this app's; one whose agents no write would take is left as it is, not keeping the others back.
const written = decksWithAgentsWithinBound(
dataset,
toDecks(dataset, catalogUrl).filter(isDeckOutdated).map(upgradeDeck),
);
dataset = withDecks(dataset, written);
const subjects = decksSubjects(dataset, written);
// Only where there is none: one that does not fit its shape is invalid data, left as it is.
if (catalog !== null && toCatalog(dataset, catalogUrl) === null) {
dataset = withCatalog(dataset, catalogUrl, catalog);
subjects.push(`${catalogUrl}#catalog`, catalog.publisher.webId);
}
if (subjects.length === 0) return false;
// If-Match the read above (a creation, where there was no document): written only as it was read.
await save(catalogUrl, dataset, subjects);
return true;
},
createDeck(instanceUrl, title): Promise<Deck> {
return registerDeck(newDeck(instanceUrl, title));
},
registerDeck,
async importDeck(instanceUrl, content): Promise<Deck> {
// What a copy keeps of the release, by address, never links to a guest's pod: a guest's copy would stay there (GHSA-hc3p-fpwj-fvjp).
if (linksGuestPod([content.license, content.themes, content.cards]))
throw new AppError("releaseLinksGuestPod", { url: content.url });
const deck = newDeck(instanceUrl, content.title, content);
// Each card names the release it is taken from: the series', which an upgrade may change.
const written = withCards(createSolidDataset(), deck, content.cards, (_, card) => ({
card: { ...card, fromRelease: content.url },
existing: null,
}));
// Checked first: an entry that would be refused leaves no cards document behind.
const entry = await catalogWithEntry(deck);
await checkWrite(entry.dataset, entry.subjects, "pod");
try {
await saveCardsDocument(deck, written.dataset, written.subjects);
} catch (error) {
// A write refused was never made: nothing of the import's is there.
if (unsent(error)) throw error;
return finishImport(deck, error);
}
return registerDeck(deck).catch((error: unknown) => finishImport(deck, error));
},
async addDeck(deck): Promise<Deck> {
const catalogUrl = documentUrlOf(deck.url);
const written: Deck = { ...deck, formatVersion: DECK_FORMAT_VERSION };
for (let attempt = 1; ; attempt++) {
const dataset = (await getSolidDatasetOrNull(catalogUrl, fetch)) ?? createSolidDataset();
if (getThing(dataset, deck.url) !== null)
throw new PreconditionFailedError(deck.url, "absent");
refuseFullDeckList(toDecks(dataset, catalogUrl));
const updated = withOwnTerms(withDeck(dataset, written), deck);
try {
// If-Match the read above: the catalog changed elsewhere meanwhile is read and the entry added again.
await save(catalogUrl, updated, deckSubjects(updated, written));
return written;
} catch (error) {
if (!(error instanceof PreconditionFailedError) || attempt === TREE_ATTEMPTS) throw error;
}
}
},
renameDeck(deck, title): Promise<Deck> {
return saveDeck({ ...deck, title });
},
saveDeck,
saveDecks,
removeDeck(deck) {
return removeDecks([deck]);
},
removeDecks,
async readDeckTree(instanceUrl) {
const catalogUrl = catalogUrlOf(instanceUrl);
const dataset = await getSolidDatasetOrNull(catalogUrl, fetch);
return buildTree(cappedLayout(dataset ?? createSolidDataset(), catalogUrl));
},
async editDeckTree(instanceUrl, edit) {
const catalogUrl = catalogUrlOf(instanceUrl);
for (let attempt = 1; ; attempt++) {
const dataset = (await getSolidDatasetOrNull(catalogUrl, fetch)) ?? createSolidDataset();
const stored = cappedLayout(dataset, catalogUrl);
const before = buildTree(stored);
const after = applyDeckTreeEdit(before, edit);
if (after === before) return after;
const { dataset: updated, subjects } = withTreeChanges(
dataset,
catalogUrl,
treeChanges(stored, after),
);
// Only the groups and the catalog are checked: a deck's position is
// no shape's, and a deck set aside beside the moved one must not stop it.
await checkWrite(updated, subjects, "pod");
try {
// A PATCH, If-Match the read above: where the pod has no strong ETag
// to match, a whole PUT would undo what changed meanwhile.
await saveDataset(catalogUrl, updated, fetch);
return after;
} catch (error) {
if (!(error instanceof PreconditionFailedError) || attempt === TREE_ATTEMPTS) throw error;
}
}
},
async listCards(deck, { asNoOne = false } = {}): Promise<Card[]> {
// Checked here too, not only where the deck was listed: a deck out
// of place is never read (docs/data-model.md).
if (!isDeckInPlace(deck)) throw new AppError("deckGone", { deck: deck.title });
const dataset = await getSolidDatasetOrNull(
deck.cardsDocumentUrl,
asNoOne ? publicFetch : fetch,
);
if (dataset === null) return [];
return toCards(dataset, deck.cardsDocumentUrl);
},
async readCardsSince(deck, version, { asNoOne = false } = {}) {
const read = await readSince(deck.cardsDocumentUrl, version, asNoOne ? publicFetch : fetch);
return mapSince(read, (dataset) =>
dataset === null ? [] : toCards(dataset, deck.cardsDocumentUrl),
);
},
async addCard(deck, content): Promise<Card> {
const id = `card-${randomId()}`;
const card: Card = {
id,
url: `${deck.cardsDocumentUrl}#${id}`,
...content,
createdAt: now().toISOString(),
formatVersion: CARD_FORMAT_VERSION,
};
const dataset =
(await getSolidDatasetOrNull(deck.cardsDocumentUrl, fetch)) ?? createSolidDataset();
const written = withCard(dataset, deck, card, null);
await saveCardsDocument(deck, written.dataset, written.subjects);
return card;
},
async updateCard(deck, card, content): Promise<Card> {
const dataset = await getSolidDatasetOrNull(deck.cardsDocumentUrl, fetch);
if (dataset === null) {
throw new AppError("cardsDocumentGone", { deck: deck.title });
}
const thing = getThing(dataset, card.url);
if (thing === null) {
throw new AppError("cardGone", { card: card.url });
}
// An edit that does not state distractors (the card editor's, while its wrong options are
// unchanged) keeps the card's,
// and one that does not state a text format, or what the card is about, keeps the card's too.
const distractors = content.distractors ?? card.distractors;
const textFormat = content.textFormat ?? card.textFormat;
const subjects = content.subjects ?? card.subjects;
const updated: Card = {
id: card.id,
url: card.url,
createdAt: card.createdAt,
...content,
...(distractors === undefined ? {} : { distractors }),
...(textFormat === undefined ? {} : { textFormat }),
...(subjects === undefined ? {} : { subjects }),
formatVersion: CARD_FORMAT_VERSION,
...(card.retired === true ? { retired: true } : {}),
};
const written = withCard(dataset, deck, updated, thing);
await saveCardsDocument(deck, written.dataset, written.subjects);
return updated;
},
async upgradeCards(deck): Promise<boolean> {
const dataset = await getSolidDatasetOrNull(deck.cardsDocumentUrl, fetch);
if (dataset === null) return false;
// Each card stored in an older format, as read and brought up to date in memory, written in this app's.
const written = withCards(
dataset,
deck,
toCards(dataset, deck.cardsDocumentUrl).filter(isOutdated),
(edit, card) => ({
card: upgradeCard(card),
existing: edit.getThing(card.url),
}),
);
if (written.subjects.length === 0) return false;
// If-Match the read above; an edit too large for one PATCH is one PUT of the whole document (datasets.ts).
await saveCardsDocument(deck, written.dataset, written.subjects);
return true;
},
async stateCardLanguages(deck, cardIds, languages): Promise<number> {
const dataset = await getSolidDatasetOrNull(deck.cardsDocumentUrl, fetch);
if (dataset === null) return 0;
const stated: string[] = [];
const written = withCards(dataset, deck, cardIds, (edit, id) => {
const thing = edit.getThing(`${deck.cardsDocumentUrl}#${id}`);
const card = thing === null ? null : toCard(thing, dataset);
const restated = card === null ? null : withStatedLanguages(card, languages);
if (restated === null) return null;
stated.push(restated.url);
return { card: restated, existing: thing };
});
// One PUT of the whole document, If-Match the read above: a cards document changed
// meanwhile is not overwritten, and no PATCH of text is cut short (saveDataset's `whole`).
if (stated.length > 0)
await saveCardsDocument(deck, written.dataset, stated, { whole: true });
return stated.length;
},
async applyCardChanges(deck, changes, options): Promise<void> {
await (await prepareCardChanges(deck, changes, options))();
},
prepareCardChanges,
async addCardIfAbsent(deck, card): Promise<boolean> {
const read = await getSolidDatasetOrNull(deck.cardsDocumentUrl, fetch);
const dataset = read ?? createSolidDataset();
const there = getThing(dataset, `${deck.cardsDocumentUrl}#${card.id}`);
if (there !== null && toCard(there, dataset) !== null) return false;
const changed = withCardChanges(dataset, deck, { save: [card], remove: [] });
await saveCardsDocument(deck, changed.dataset, changed.subjects);
return true;
},
readDeck,
async upgradeDeckEntry(current, next): Promise<Deck> {
const { catalogUrl, dataset, subjects, written } = await upgradedEntry(current, next);
// The write carries If-Match of the read above: the entry changes as it was checked, or not at all.
await save(catalogUrl, dataset, subjects);
return written;
},
async checkDeckEntryUpgrade(current, next) {
const { dataset, subjects } = await upgradedEntry(current, next);
await checkWrite(dataset, subjects, "pod");
},
deleteDocument,
completeChapter(deck, chapterUrl) {
return editCompletedChapters(deck, (completed) =>
// Completed in any release: a chapter keeps its fragment id from one release to the next.
completed.some((url) => fragmentIdOf(url) === fragmentIdOf(chapterUrl))
? null
: { add: [chapterUrl], remove: [] },
);
},
setCompletedChapters(deck, edit) {
return editCompletedChapters(
deck,
(completed) => {
const kept = new Set(completedChaptersAfter(completed, edit));
const remove = completed.filter((url) => !kept.has(url));
return remove.length === 0 ? null : { add: [], remove };
},
// The Studio's edit: the deck's entry must be as its shape says, as for any other edit of it.
true,
);
},
setCardLanguage(deck, language) {
return editEntry(deck, (stored) =>
stored.cardLanguage === (language ?? undefined)
? null
: {
edit: (entry) => {
const cleared = entry.removeAll(SM.deckCardLanguage);
return language === null
? cleared
: cleared.addStringNoLocale(SM.deckCardLanguage, language);
},
deck: withCardLanguage(stored, language),
},
);
},
setPictureHost(deck, host, shown) {
return editEntry(deck, (stored) => {
if ((stored.pictureHosts ?? []).includes(host) === shown) return null;
const changed = withPictureHost(stored, host, shown);
return {
edit: (entry) =>
entry
.removeAll(SM.deckPictureHost)
.setStringAll(SM.deckPictureHost, hostStrings(changed.pictureHosts)),
deck: changed,
};
});
},
setPause(deck, pause) {
return editEntry(deck, (stored) => {
if (samePause(stored.pause, pause ?? undefined)) return null;
const { pause: _old, ...rest } = stored;
return {
edit: (entry) => withPauseTerms(entry.removeAll(SM.deckPausedAt), pause ?? undefined),
deck: pause === null ? rest : { ...rest, pause },
};
});
},
async removeCard(deck, card): Promise<void> {
guard.assertUsable(deck);
// Only a card of the deck's own cards document is removed, with the review states of its
// fragment: another subject that shares its id is no card of the deck (ownSubjectsOf).
if (!subjectOfDocument(deck.cardsDocumentUrl)(card.url))
throw new AppError("cardGone", { card: card.url });
const cardId = fragmentIdOf(card.url);
const cards = await getSolidDatasetOrNull(deck.cardsDocumentUrl, fetch);
const named = `${deck.cardsDocumentUrl}#${cardId}`;
if (card.url !== named && cards !== null && getThing(cards, named) !== null)
throw new AppError("cardGone", { card: card.url });
const reviews = await getSolidDatasetOrNull(deck.reviewsDocumentUrl, fetch);
// Both documents are held to the guard before either is written.
const cardsWritten =
cards === null
? null
: await guard.writable(deck, deck.cardsDocumentUrl, withoutCard(cards, card.url));
const reviewsWritten =
reviews === null
? null
: await guard.writable(
deck,
deck.reviewsDocumentUrl,
// Its states both ways, whether they name it (sm:reviewOf) or are named after it.
withoutReviewStates(reviews, deck, [
{ cardId, direction: "front-to-back" },
{ cardId, direction: "back-to-front" },
]),
);
for (const [url, written] of [
[deck.cardsDocumentUrl, cardsWritten],
[deck.reviewsDocumentUrl, reviewsWritten],
] as const) {
if (written !== null) await saveDataset(url, written, fetch);
}
},
unclaimedDocuments(deck, budget) {
return guard.unclaimed(deck, budget);
},
isUnclaimedAsRead(decks, url, budget) {
// A read that fails, or is not made in time, says nothing of a document the check has just found faulty.
const read = () => guard.unclaimedAsRead(decks, url, false);
return budget === undefined ? read() : budget.spend(false, read);
},
claimDocuments(deck) {
return guard.claim(deck);
},
};
/**
* Change the chapters a course's deck completed (sm:completedChapter):
* what `change` adds and removes of them as the entry has them now
* (editEntry), in time linear in them. No change (null) writes
* nothing; a change writes those it keeps and those it adds after
* them, at most MAX_COMPLETED_CHAPTERS (boundedCompletedChapters,
* the deck's release and the chapters added counting as the course's):
* past the bound, values of other documents go first, then the
* oldest, so the entry never holds more than a read keeps (toDeck)
* and other values never push out a real completion. sm:completedChapter is no shape's (like a
* deck's sm:position): a learner's completion checks
* nothing a shape owns, while an edit in the Studio (`checked`) has the
* entry checked as any other edit of it is.
*/
function editCompletedChapters(
deck: Deck,
change: (completed: readonly string[]) => { add: string[]; remove: string[] } | null,
checked = false,
): Promise<Deck> {
return editEntry(
deck,
(stored) => {
const completed = stored.completedChapters ?? [];
const changed = change(completed);
if (changed === null) return null;
const removed = new Set(changed.remove);
const kept = completed.filter((url) => !removed.has(url));
// Never more than a read keeps, the course's own kept before any other value.
const after = boundedCompletedChapters(
[...kept, ...changed.add],
[stored.sourceUrl, ...changed.add],
);
const { completedChapters: _, ...rest } = stored;
return {
// One write of them all: a removal each would copy them all each time (GHSA-gq27-7968-wfcv).
edit: (entry) => entry.setIriAll(SM.completedChapter, after),
deck: after.length === 0 ? rest : { ...rest, completedChapters: after },
};
},
checked,
);
}
/**
* Edit a deck's catalog entry as it is now, for a term no shape owns
* and withDeck keeps as stored: `change` is given the deck the entry
* says, and returns how to edit the entry and the deck it then says, or
* null for no change, which writes nothing. ONE save made only if the
* catalog document is as it was read (If-Match), else read and changed
* again, TREE_ATTEMPTS times in all; `checked` has the entry checked
* first. Throws deckGone when the deck has no entry.
*/
async function editEntry(
deck: Deck,
change: (stored: Deck) => {
edit: (entry: SubjectBuilder) => SubjectBuilder;
deck: Deck;
} | null,
checked = false,
): Promise<Deck> {
const catalogUrl = documentUrlOf(deck.url);
for (let attempt = 1; ; attempt++) {
const dataset = await getSolidDatasetOrNull(catalogUrl, fetch);
const stored =
dataset === null
? undefined
: toDecks(dataset, catalogUrl).find((candidate) => candidate.url === deck.url);
if (stored === undefined) throw new AppError("deckGone", { deck: deck.title });
const changed = change(stored);
if (changed === null) return stored;
// A class another app gave the entry that a shape asks something of goes, as recordThing drops it.
const entry = changed.edit(
buildThing(
withoutForeignShapedClasses(unlessNewer(getThing(dataset!, deck.url)!), DECK_V6),
),
);
const updated = setThing(dataset!, entry.build());
if (checked) await checkWrite(updated, [deck.url], "pod");
try {
// A PATCH of the triples changed, If-Match the read above.
await saveDataset(catalogUrl, updated, fetch);
return changed.deck;
} catch (error) {
if (!(error instanceof PreconditionFailedError) || attempt === TREE_ATTEMPTS) throw error;
}
}
}
/** Rewrite a deck's catalog entry in place (saveDecks). */
async function saveDeck(deck: Deck): Promise<Deck> {
return (await saveDecks([deck]))[0]!;
}
/**
* Rewrite decks' catalog entries in place, in this app's format, one
* save per catalog document: each entry's own predicates are replaced
* from the deck, so unknown triples survive; its agents and
* distribution are written beside it. Returns the decks as written.
*/
async function saveDecks(decks: readonly Deck[]): Promise<Deck[]> {
const written = decks.map((deck): Deck => ({ ...deck, formatVersion: DECK_FORMAT_VERSION }));
for (const [catalogUrl, inDocument] of byDocument(written)) {
const dataset = await getSolidDatasetOrNull(catalogUrl, fetch);
const gone = inDocument.find(
(deck) => dataset === null || getThing(dataset, deck.url) === null,
);
if (gone !== undefined) throw new AppError("deckGone", { deck: gone.title });
const updated = withDecks(dataset!, inDocument);
await save(catalogUrl, updated, decksSubjects(updated, inDocument));
}
return written;
}
/**
* Remove decks: their cards and reviews documents first, then their
* entries, in one write of each catalog document. A document a deck
* that stays uses too (another app may point two decks at one) is kept,
* and so is every document that is not the leaving deck's to delete
* (deckPlace.ts, guard.standing), whatever deck is handed in: of a deck
* whose documents are unclaimed, or that cannot be used, only the entry
* is removed. Of a document that also holds another app's data, only
* the decks' cards and review states are removed (removeOwned), and
* the document is kept, which the result names. A document in the
* instance's folder that fails to be read stops the removal before the
* entries are written, so they still name what is left and the removal
* can be made again; a document already gone by then counts as removed.
* An entry, a distribution or the catalog node a newer version of the
* app wrote refuses the removal (writtenByNewerApp) before anything is
* deleted: the catalog to write is made first.
*/
async function removeDecks(decks: readonly Deck[]): Promise<KeptDocuments> {
const keptDocuments: string[] = [];
for (const [catalogUrl, inDocument] of byDocument(decks)) {
const dataset = await getSolidDatasetOrNull(catalogUrl, fetch);
const leaving = new Set(inDocument.map((deck) => deck.url));
// Compared as a request sends them: one document spelled two ways is one.
const used = new Set(
[
...documentsInUse(
dataset === null
? []
: toDecks(dataset, catalogUrl).filter((other) => !leaving.has(other.url)),
),
].map(canonicalUrlOf),
);
const usable = inDocument.filter(isDeckInPlace);
const unused = [...documentsInUse(usable)].filter((url) => !used.has(canonicalUrlOf(url)));
// Each document's decks found at once, and the entries told once, not per document.
const decksAt = decksByDocument(usable);
const entries = deckDocumentSubjects([...leaving]);
// The catalog as it will be written, before any document goes: what it refuses (writtenByNewerApp) leaves them all.
const catalog = dataset === null ? null : withoutDecks(dataset, inDocument);
for (const url of unused) {
const removal = await removeIfTheirs(url, decksAt.get(canonicalUrlOf(url)!)!, entries);
if (removal === "kept") keptDocuments.push(url);
}
if (catalog === null) continue;
await saveDataset(catalogUrl, catalog, fetch);
}
return { keptDocuments };
}
/**
* Remove the decks' cards and review states from the document at
* `url`, and the document when nothing else is left in it (removeOwned,
* If-Match its read), when it is one of the decks' to remove from;
* else keep it whole (null). One the pod serves that cannot be read as
* RDF is kept whole too, and said to be ("kept"), and so is one outside
* the instance's folder whose read fails (deckPlace.ts, readToRemove).
* Any other failed read (refused, a server error, no answer, a body cut
* off) throws: whether it is theirs is not known, so nothing may be
* taken as settled. `owned` is what of it goes with the entries of the
* decks removed (deckDocumentSubjects): the document's saying it is
* theirs goes when it names no other.
*/
async function removeIfTheirs(
url: string,
decks: readonly Deck[],
owned: ReturnType<typeof deckDocumentSubjects>,
): Promise<Removal | null> {
const existing = await guard.readToRemove(decks[0]!, url);
if (existing === UNREADABLE) return "kept";
if (existing === null) return null;
for (const deck of decks) {
if ((await guard.standing(deck, url, existing)) !== "unclaimed")
return removeOwned(url, owned, fetch, () => guard.readToRemove(decks[0]!, url));
}
return null;
}
/**
* A fresh deck's identity and document locations, before any write.
* An import carries over the library release's provenance — its
* authors, license, description, topics and keywords (per language,
* an older release's untagged ones kept untagged), and which
* release it is — and the direction it is meant to be studied in. The
* format version is always this app's own: the copy is written in the
* format this app writes.
*/
function newDeck(
instanceUrl: string,
title: LangText,
source?: {
url: string;
authors: string[];
license?: string;
description?: LangText;
direction: DeckDirection;
themes: string[];
keywords: LangTexts;
isCourse?: true;
},
): Deck {
const base = ensureTrailingSlash(instanceUrl);
const id = `deck-${randomId()}`;
return withBoundedMetadata({
id,
url: `${catalogUrlOf(base)}#${id}`,
title,
cardsDocumentUrl: `${cardsContainerOf(base)}${id}.ttl`,
reviewsDocumentUrl: `${reviewsContainerOf(base)}${id}.ttl`,
createdAt: now().toISOString(),
formatVersion: DECK_FORMAT_VERSION,
direction: source?.direction ?? DEFAULT_DECK_DIRECTION,
authors: source?.authors ?? [],
...(source?.license === undefined ? {} : { license: source.license }),
...(source?.description === undefined ? {} : { description: source.description }),
...(source === undefined ? {} : { sourceUrl: source.url }),
...(source === undefined || source.themes.length === 0 ? {} : { themes: source.themes }),
...(source === undefined || noKeywords(source.keywords)
? {}
: { keywords: copyKeywords(source.keywords) }),
...(source?.isCourse === true ? { course: true as const } : {}),
});
}
/**
* The cards document with cards written by fragment id, new or
* existing (an existing card keeps its creation time and triples this
* app does not know), and others removed, each with its distractors.
* Returns the subjects written, for the write check.
*/
function withCardChanges(
dataset: Dataset,
deck: Deck,
{
save: saved,
remove,
}: {
save: (CardContent & { id: string; createdAt?: string; retired?: true })[];
remove: string[];
},
carried: (id: string) => Subject | null = () => null,
): { dataset: Dataset; subjects: string[] } {
const urlOf = (id: string) => `${deck.cardsDocumentUrl}#${id}`;
const edit = editOf(dataset);
// A card or a distractor the document does not hold starts from the subject it is a copy of,
// without the release that one came from and the distractors it names, which are its deck's.
for (const card of saved) {
for (const { id } of [card, ...(card.distractors ?? [])]) {
const from = edit.getThing(urlOf(id)) === null ? carried(id) : null;
if (from !== null)
edit.setThing(carriedTo(from, urlOf(id), [SM.fromRelease, SM.distractor]));
}
}
// Distractors no card names any longer go once all are written: one moved to another card stays.
const written = writeCards(edit, deck, saved, (_, card) => {
const existing = edit.getThing(urlOf(card.id));
const createdAt =
existing === null ? undefined : readDatetime(existing, DCTERMS.created)?.toISOString();
return { card: { ...card, ...(createdAt === undefined ? {} : { createdAt }) }, existing };
});
const updated = edit.done();
const removed = remove.map(urlOf);
const dropped = [
...written.dropped,
...removed.flatMap((url) => distractorsNamedBy(updated, url)),
];
return {
dataset: withoutUnnamedDistractors(withoutCardSubjects(updated, removed), dropped),
subjects: written.subjects,
};
}
/**
* The cards document with the card written and its distractors
* beside it (withCardWritten), those it named before and no longer
* does removed, unless another card names them.
*/
function withCard(
dataset: Dataset,
deck: Deck,
card: CardToWrite,
existing: NamedSubject | null,
): { dataset: Dataset; subjects: string[] } {
return withCards(dataset, deck, [card], () => ({ card, existing }));
}
/**
* The cards document with a card written for each of `items` that
* `toWrite` gives one for (reading the document as written so far),
* all at once (writeCards); then the distractors the cards named
* before and no longer do are removed, unless another card names them.
*/
function withCards<T>(
dataset: Dataset,
deck: Deck,
items: Iterable<T>,
toWrite: (edit: DatasetEdit<Dataset>, item: T) => CardWrite | null,
): { dataset: Dataset; subjects: string[] } {
const edit = editOf(dataset);
const written = writeCards(edit, deck, items, toWrite);
return {
dataset: withoutUnnamedDistractors(edit.done(), written.dropped),
subjects: written.subjects,
};
}
/**
* Writes a card for each of `items` that `toWrite` gives one for
* (withCardWritten), each read and written through one edit of the
* document, which is made once (editOf): many cards take time that
* grows with them and the document, not their product. A distractor
* two of the cards name is written by the first only (`writers`, each
* distractor URL by the card that wrote it): cards that state their
* languages read it from one document, so each has the same, and any
* other write that gives it to two cards is refused when saved
* (assertDistractorsOwnedOnce), so a distractor named by many cards is
* never written once for each. Returns the subjects written and the
* distractors the cards dropped.
*/
function writeCards<T>(
edit: DatasetEdit<Dataset>,
deck: Deck,
items: Iterable<T>,
toWrite: (edit: DatasetEdit<Dataset>, item: T) => CardWrite | null,
): { subjects: string[]; dropped: string[] } {
const subjects: string[] = [];
const dropped: string[] = [];
const writers = new Map<string, string>();
for (const item of items) {
const write = toWrite(edit, item);
if (write === null) continue;
const written = withCardWritten(edit, deck, write.card, write.existing, writers);
for (const subject of written.subjects) subjects.push(subject);
for (const url of written.dropped) dropped.push(url);
}
return { subjects, dropped };
}
/**
* Writes the card (cardThing) and its distractors beside it; a card
* is never written onto a distractor, nor a distractor onto a card
* (unlessTyped), and a distractor another card of the write wrote
* (`writers`) is not written again. Returns the subjects written, the
* card's, then its distractors', and the distractors it named before
* and no longer does (`dropped`), still in the document: only those it
* could read (toDistractor), so a link to any other subject leaves the
* card and never takes its target with it.
*/
function withCardWritten(