oficina / mapa Public

Clone
git clone https://amadan.net/oficina/mapa

Plain git — no account needed to clone.

Download

Download this file

1package mapa
2
3import (
4 "context"
5 "errors"
6
7 "gorm.io/gorm"
8
9 "amadan.net/oficina/mapa/internal/mapa/seal"
10)
11
12// History (§5): every revision inside the 90-day window, with who made
13// it and when, and restore, which mints a NEW revision holding an old
14// one's text (Fichas decision 2's shape). A revision is never edited in
15// place (doctrine §6.6).
16
17// VerbReverted is a restore from history.
18const VerbReverted = "reverted"
19
20// readable is a live page the viewer may open, read without writing.
21func (store *Store) readable(ctx context.Context, viewer Viewer,
22 pageID int64) (Page, error) {
23 var page Page
24 err := store.database.WithContext(ctx).Where("id = ? AND gone = ? AND "+
25 "(rung = ? OR owner_id = ?)", pageID, false, RungTeam,
26 viewer.MemberID).First(&page).Error
27 if errors.Is(err, gorm.ErrRecordNotFound) {
28 return page, ErrNotFound
29 }
30 return page, err
31}
32
33// Chosen is one version on screen (§4.5): its text, the text of the
34// version before it in the window if there is one, its verb, and the
35// screen of history that holds it.
36type Chosen struct {
37 Version Version
38 Record Record
39 Before *Record
40 Verb string
41 List VersionList
42}
43
44// ChooseVersion is one revision of a page in the window, if the viewer
45// may open the page; a revision outside the window is not found.
46func (store *Store) ChooseVersion(ctx context.Context, viewer Viewer,
47 pageID, revision int64) (Chosen, error) {
48 history, err := store.loadHistory(ctx, viewer, pageID)
49 if err != nil {
50 return Chosen{}, err
51 }
52 index := history.find(revision)
53 if index < 0 {
54 return Chosen{}, ErrNotFound
55 }
56 list, err := history.list(index / HistoryPage * HistoryPage)
57 if err != nil {
58 return Chosen{}, err
59 }
60 chosen := Chosen{List: list, Verb: history.verbs[revision],
61 Version: list.Versions[index%HistoryPage]}
62 if chosen.Record, err = history.record(index); err != nil {
63 return chosen, err
64 }
65 if index+1 < len(history.rows) {
66 before, err := history.record(index + 1)
67 chosen.Before = &before
68 return chosen, err
69 }
70 return chosen, nil
71}
72
73// Revert restores an old revision's body as a new revision. The title
74// stays as it is now: a title is the namespace's, and taking an old one
75// back is a rename, with its own checks.
76func (store *Store) Revert(ctx context.Context, who string, pageID,
77 revision int64) error {
78 return store.write(ctx, func(tx *Tx) error {
79 actor, err := tx.member(who)
80 if err != nil {
81 return err
82 }
83 page, err := tx.visiblePage(actor, pageID)
84 if err != nil {
85 return err
86 }
87 var row Revision
88 err = tx.db.Where("page_id = ? AND revision = ? AND at > ?",
89 pageID, revision, tx.at.Add(-HistoryWindow)).First(&row).Error
90 if errors.Is(err, gorm.ErrRecordNotFound) {
91 return ErrNotFound
92 }
93 if err != nil {
94 return err
95 }
96 old, err := store.openRecord(seal.PurposeRecord, row.SealedRecord)
97 if err != nil {
98 return err
99 }
100 record, err := store.OpenPage(page)
101 if err != nil {
102 return err
103 }
104 record.Body = old.Body
105 // A Revert carries old text, not the person's own: it attaches
106 // nothing, and clears the stamps of what it names (§3.2).
107 return tx.commit(&page, record, actor, VerbReverted, attachNothing)
108 })
109}
110