| 1 | package mapa |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "fmt" |
| 7 | "strings" |
| 8 | |
| 9 | "gorm.io/gorm" |
| 10 | |
| 11 | "amadan.net/oficina/mapa/internal/mapa/markup" |
| 12 | "amadan.net/oficina/mapa/internal/mapa/seal" |
| 13 | ) |
| 14 | |
| 15 | // ErrNotFound is a page that is not there, or that the person may not |
| 16 | // open. The two are one answer: a row you may not see is not there. |
| 17 | var ErrNotFound = errors.New("mapa: no such page") |
| 18 | |
| 19 | // ErrStale is a first save of a day whose note appeared meanwhile. |
| 20 | var ErrStale = errors.New("mapa: the page changed since you opened it") |
| 21 | |
| 22 | // visiblePage loads a live page the member may open: any team page, or a |
| 23 | // private page they own (§3.3). |
| 24 | func (tx *Tx) visiblePage(memberID, pageID int64) (Page, error) { |
| 25 | var page Page |
| 26 | err := tx.db.Where("id = ? AND gone = ? AND (rung = ? OR owner_id = ?)", |
| 27 | pageID, false, RungTeam, memberID).First(&page).Error |
| 28 | if errors.Is(err, gorm.ErrRecordNotFound) { |
| 29 | return page, ErrNotFound |
| 30 | } |
| 31 | return page, err |
| 32 | } |
| 33 | |
| 34 | // freshRevision draws a revision this page has never had. Random, so a |
| 35 | // restored database cannot reissue one memoria holds (§5, doctrine |
| 36 | // §6.6); checked against the page's history, so it is never reused. |
| 37 | func (tx *Tx) freshRevision(pageID int64) (int64, error) { |
| 38 | for { |
| 39 | revision, err := randomPositive() |
| 40 | if err != nil { |
| 41 | return 0, err |
| 42 | } |
| 43 | var used int64 |
| 44 | err = tx.db.Model(&Event{}).Where("object_kind = ? AND "+ |
| 45 | "object_id = ? AND revision = ?", ObjectPage, pageID, |
| 46 | revision).Count(&used).Error |
| 47 | if err != nil || used == 0 { |
| 48 | return revision, err |
| 49 | } |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | // commit mints a new revision of page holding record: the page row, the |
| 54 | // revision store and one event, together. Nothing edits a revision in |
| 55 | // place (doctrine §6.6). |
| 56 | func (tx *Tx) commit(page *Page, record Record, actor int64, |
| 57 | verb string, attach attaching) error { |
| 58 | previous := page.SealedRecord |
| 59 | revision, err := tx.freshRevision(page.ID) |
| 60 | if err != nil { |
| 61 | return err |
| 62 | } |
| 63 | // The verbs carry the whole record forward, so without this a |
| 64 | // teammate's edit of an imported page would read "imported from |
| 65 | // Notion". Origin is kept: the text did come from there. |
| 66 | record.Imported = record.Imported && tx.importing |
| 67 | sealed, err := tx.store.sealRecord(seal.PurposeRecord, record) |
| 68 | if err != nil { |
| 69 | return err |
| 70 | } |
| 71 | page.Revision, page.SealedRecord, page.UpdatedAt = revision, sealed, |
| 72 | tx.at |
| 73 | err = tx.db.Model(page).Select("Revision", "SealedRecord", |
| 74 | "UpdatedAt").Updates(page).Error |
| 75 | if err != nil { |
| 76 | return fmt.Errorf("mapa: updating a page: %w", err) |
| 77 | } |
| 78 | if err := tx.db.Create(&Revision{PageID: page.ID, Revision: revision, |
| 79 | At: tx.at, ActorID: actor, SealedRecord: sealed}).Error; err != nil { |
| 80 | return fmt.Errorf("mapa: storing a revision: %w", err) |
| 81 | } |
| 82 | err = tx.settleImages(page, previous, record, actor, attach) |
| 83 | if err != nil { |
| 84 | return err |
| 85 | } |
| 86 | return tx.log(actor, ObjectPage, page.ID, verb, revision) |
| 87 | } |
| 88 | |
| 89 | // create makes a page at its first revision. Its rung is fixed here and |
| 90 | // never changes (decision 8). |
| 91 | func (tx *Tx) create(kind, rung string, owner int64, record Record, |
| 92 | actor int64, verb string, attach attaching) (Page, error) { |
| 93 | nonce, err := tx.store.nonces(rung) |
| 94 | if err != nil { |
| 95 | return Page{}, err |
| 96 | } |
| 97 | page := Page{Nonce: nonce, Kind: kind, Rung: rung, OwnerID: owner, |
| 98 | Revision: 0, CreatedAt: tx.at, UpdatedAt: tx.at} |
| 99 | if err := tx.db.Omit("Owner").Create(&page).Error; err != nil { |
| 100 | return Page{}, fmt.Errorf("mapa: creating a page: %w", err) |
| 101 | } |
| 102 | return page, tx.commit(&page, record, actor, verb, attach) |
| 103 | } |
| 104 | |
| 105 | // claimTitle reserves title for page, or refuses it as taken. A print |
| 106 | // that is already this page's (an alias it is taking back) is kept. |
| 107 | func (tx *Tx) claimTitle(pageID int64, title string) error { |
| 108 | fingerprint, err := tx.store.titlePrint(title) |
| 109 | if err != nil { |
| 110 | return err |
| 111 | } |
| 112 | var held TitlePrint |
| 113 | err = tx.db.Where("print = ?", fingerprint).First(&held).Error |
| 114 | switch { |
| 115 | case err == nil && held.PageID == pageID: |
| 116 | return nil |
| 117 | case err == nil: |
| 118 | return ErrTitleTaken |
| 119 | case !errors.Is(err, gorm.ErrRecordNotFound): |
| 120 | return err |
| 121 | } |
| 122 | return tx.db.Omit("Page").Create(&TitlePrint{Print: fingerprint, |
| 123 | PageID: pageID}).Error |
| 124 | } |
| 125 | |
| 126 | // CreateTeamPage makes a team page with a title nothing else holds. |
| 127 | func (store *Store) CreateTeamPage(ctx context.Context, who, rawTitle, |
| 128 | body string) (Page, error) { |
| 129 | title, err := CleanTitle(rawTitle) |
| 130 | if err != nil { |
| 131 | return Page{}, err |
| 132 | } |
| 133 | var page Page |
| 134 | err = store.write(ctx, func(tx *Tx) error { |
| 135 | actor, err := tx.member(who) |
| 136 | if err != nil { |
| 137 | return err |
| 138 | } |
| 139 | page, err = tx.create(KindPage, RungTeam, actor, |
| 140 | Record{Title: title, Body: cleanBody(body)}, actor, VerbCreated, |
| 141 | attachOwn(0)) |
| 142 | if err != nil { |
| 143 | return err |
| 144 | } |
| 145 | return tx.claimTitle(page.ID, title) |
| 146 | }) |
| 147 | return page, err |
| 148 | } |
| 149 | |
| 150 | // cleanBody is the one rule for a stored body: "\n" line endings, and no |
| 151 | // trailing blank lines. |
| 152 | func cleanBody(body string) string { |
| 153 | return strings.TrimRight(markup.Normalise(body), " \t\n") |
| 154 | } |
| 155 | |