| 1 | package mapa |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "time" |
| 7 | |
| 8 | "amadan.net/oficina/mapa/internal/mapa/markup" |
| 9 | "amadan.net/oficina/mapa/internal/mapa/seal" |
| 10 | ) |
| 11 | |
| 12 | // The §3.4 verbs on team pages: rename, free an alias, delete, restore a |
| 13 | // deleted page as a new one, and update links after a rename. |
| 14 | |
| 15 | // HistoryWindow is how long page history and deleted pages are kept |
| 16 | // (§5; Paul, 2026-11-12: 90 days). |
| 17 | const HistoryWindow = 90 * 24 * time.Hour |
| 18 | |
| 19 | // errNoChange ends a write that turned out to change nothing. It is not |
| 20 | // a failure, and it commits nothing, which Store.write would otherwise |
| 21 | // refuse as an unlogged write. |
| 22 | var errNoChange = errors.New("mapa: nothing to change") |
| 23 | |
| 24 | // quiet maps errNoChange to success. |
| 25 | func quiet(err error) error { |
| 26 | if errors.Is(err, errNoChange) { |
| 27 | return nil |
| 28 | } |
| 29 | return err |
| 30 | } |
| 31 | |
| 32 | // teamPage loads a live team page the member may open, with its record. |
| 33 | func (tx *Tx) teamPage(memberID, pageID int64) (Page, Record, error) { |
| 34 | page, err := tx.visiblePage(memberID, pageID) |
| 35 | if err == nil && page.Kind != KindPage { |
| 36 | err = ErrNotFound |
| 37 | } |
| 38 | if err != nil { |
| 39 | return page, Record{}, err |
| 40 | } |
| 41 | record, err := tx.store.OpenPage(page) |
| 42 | return page, record, err |
| 43 | } |
| 44 | |
| 45 | // Rename gives a team page a new title. The old one becomes an alias, so |
| 46 | // every [[Old]] still resolves to it, and only this page gets a new |
| 47 | // revision (decision 9, gate 3). |
| 48 | func (store *Store) Rename(ctx context.Context, who string, pageID int64, |
| 49 | rawTitle string) error { |
| 50 | _, err := store.Retitle(ctx, who, pageID, rawTitle) |
| 51 | return err |
| 52 | } |
| 53 | |
| 54 | // Renamed is what a rename did: the revision it was made over (From) |
| 55 | // and the one it minted (To). When the title was already the page's, |
| 56 | // nothing was written, and they are the rename that put it there if |
| 57 | // that rename minted the current revision (renamedTo), else both 0. |
| 58 | type Renamed struct { |
| 59 | Title string |
| 60 | From, To int64 |
| 61 | } |
| 62 | |
| 63 | // Retitle is Rename, answering what it did, for the in-page title |
| 64 | // (level-up §4.1): the editor moves its base from From to To. |
| 65 | func (store *Store) Retitle(ctx context.Context, who string, pageID int64, |
| 66 | rawTitle string) (Renamed, error) { |
| 67 | title, err := CleanTitle(rawTitle) |
| 68 | if err != nil { |
| 69 | return Renamed{}, err |
| 70 | } |
| 71 | done := Renamed{Title: title} |
| 72 | err = quiet(store.write(ctx, func(tx *Tx) error { |
| 73 | actor, err := tx.member(who) |
| 74 | if err != nil { |
| 75 | return err |
| 76 | } |
| 77 | page, record, err := tx.teamPage(actor, pageID) |
| 78 | if err != nil { |
| 79 | return err |
| 80 | } |
| 81 | if record.Title == title { |
| 82 | if done.From, done.To, err = tx.renamedTo(page); err != nil { |
| 83 | return err |
| 84 | } |
| 85 | return errNoChange |
| 86 | } |
| 87 | if Fold(title) != Fold(record.Title) { |
| 88 | if err := tx.retitle(page.ID, record.Title, title); err != nil { |
| 89 | return err |
| 90 | } |
| 91 | } |
| 92 | record.Title, done.From = title, page.Revision |
| 93 | err = tx.commit(&page, record, actor, VerbRenamed, attachNothing) |
| 94 | if err != nil { |
| 95 | return err |
| 96 | } |
| 97 | done.To = page.Revision |
| 98 | return tx.carryDrafts(page.ID, done.From, done.To) |
| 99 | })) |
| 100 | return done, err |
| 101 | } |
| 102 | |
| 103 | // renamedTo answers, for a title (or a look) that is already the |
| 104 | // page's, the rename (or styled write) that made it so, when that |
| 105 | // minted the page's current revision (columns and icons spec §4.1): |
| 106 | // the revision it was made over and its own. A rename whose reply was |
| 107 | // lost is sent again, and finds its title in place; this answer lets |
| 108 | // the tab's saver follow it as if the first reply had come (Fable's L3 |
| 109 | // on ux/fixes-1). Both are 0 when anything else minted the head since. |
| 110 | func (tx *Tx) renamedTo(page Page) (from, to int64, err error) { |
| 111 | var minted int64 |
| 112 | err = tx.db.Model(&Event{}).Where("object_kind = ? AND object_id = ? "+ |
| 113 | "AND verb IN ? AND revision = ?", ObjectPage, page.ID, |
| 114 | []string{VerbRenamed, VerbStyled}, page.Revision). |
| 115 | Count(&minted).Error |
| 116 | if err != nil || minted == 0 { |
| 117 | return 0, 0, err |
| 118 | } |
| 119 | var before []int64 |
| 120 | err = tx.db.Model(&Event{}).Where("object_kind = ? AND object_id = ? "+ |
| 121 | "AND revision NOT IN (0, ?)", ObjectPage, page.ID, page.Revision). |
| 122 | Order("id DESC").Limit(1).Pluck("revision", &before).Error |
| 123 | if err != nil || len(before) == 0 { |
| 124 | return 0, 0, err |
| 125 | } |
| 126 | return before[0], page.Revision, nil |
| 127 | } |
| 128 | |
| 129 | // carryDrafts moves every draft on the page based on from to to. A |
| 130 | // rename's revision holds exactly from's body, so a draft based on one |
| 131 | // is based on the other; left behind, the draft's next commit would |
| 132 | // merge against a "teammate's" revision that changed nothing, and say |
| 133 | // so. Only the base moves: not the text, nor the version a tab holds, |
| 134 | // nor the time it was written (presence and the draft window read it). |
| 135 | // A draft on an older revision stays where it is: carried, its commit |
| 136 | // would be a plain save over the edits between (TestAStaleDraftIsNot |
| 137 | // Carried, and make mutate). |
| 138 | func (tx *Tx) carryDrafts(pageID, from, to int64) error { |
| 139 | return tx.db.Model(&Draft{}). |
| 140 | Where("page_id = ? AND base_revision = ?", pageID, from). |
| 141 | UpdateColumn("base_revision", to).Error |
| 142 | } |
| 143 | |
| 144 | // retitle claims the new title and keeps the old as an alias. Taking |
| 145 | // back one of the page's own aliases drops it from the alias list. |
| 146 | func (tx *Tx) retitle(pageID int64, old, title string) error { |
| 147 | if err := tx.claimTitle(pageID, title); err != nil { |
| 148 | return err |
| 149 | } |
| 150 | aliases, err := tx.store.aliases(tx.db, pageID) |
| 151 | if err != nil { |
| 152 | return err |
| 153 | } |
| 154 | for _, alias := range aliases { |
| 155 | if Fold(alias.Title) == Fold(title) { |
| 156 | if err := tx.db.Delete(&Alias{}, alias.ID).Error; err != nil { |
| 157 | return err |
| 158 | } |
| 159 | } |
| 160 | } |
| 161 | sealed, err := tx.store.sealer.Seal(seal.PurposeAlias, []byte(old)) |
| 162 | if err != nil { |
| 163 | return err |
| 164 | } |
| 165 | return tx.db.Omit("Page").Create(&Alias{PageID: pageID, |
| 166 | SealedTitle: sealed}).Error |
| 167 | } |
| 168 | |
| 169 | // FreeAlias releases one of a team page's old titles. Links typed with it |
| 170 | // then become pending, or bind to a new page that takes it (§3.4). |
| 171 | func (store *Store) FreeAlias(ctx context.Context, who string, pageID, |
| 172 | aliasID int64) error { |
| 173 | return store.write(ctx, func(tx *Tx) error { |
| 174 | actor, err := tx.member(who) |
| 175 | if err != nil { |
| 176 | return err |
| 177 | } |
| 178 | page, _, err := tx.teamPage(actor, pageID) |
| 179 | if err != nil { |
| 180 | return err |
| 181 | } |
| 182 | alias, err := tx.store.alias(tx.db, pageID, aliasID) |
| 183 | if err != nil { |
| 184 | return err |
| 185 | } |
| 186 | fingerprint, err := tx.store.titlePrint(alias.Title) |
| 187 | if err != nil { |
| 188 | return err |
| 189 | } |
| 190 | err = tx.db.Where("print = ? AND page_id = ?", fingerprint, |
| 191 | pageID).Delete(&TitlePrint{}).Error |
| 192 | if err != nil { |
| 193 | return err |
| 194 | } |
| 195 | if err := tx.db.Delete(&Alias{}, alias.ID).Error; err != nil { |
| 196 | return err |
| 197 | } |
| 198 | return tx.log(actor, ObjectPage, pageID, VerbAliasFreed, |
| 199 | page.Revision) |
| 200 | }) |
| 201 | } |
| 202 | |
| 203 | // Delete takes a page away from everyone who could open it, so it is |
| 204 | // logged with its actor. A team page's title and aliases are freed, and |
| 205 | // links to it become pending (§3.4). The row stays, gone, so the page |
| 206 | // can be restored as a new one inside the window. |
| 207 | func (store *Store) Delete(ctx context.Context, who string, |
| 208 | pageID int64) error { |
| 209 | return store.write(ctx, func(tx *Tx) error { |
| 210 | actor, err := tx.member(who) |
| 211 | if err != nil { |
| 212 | return err |
| 213 | } |
| 214 | page, err := tx.visiblePage(actor, pageID) |
| 215 | if err != nil { |
| 216 | return err |
| 217 | } |
| 218 | err = tx.db.Model(&page).Select("Gone", "UpdatedAt").Updates( |
| 219 | Page{Gone: true, UpdatedAt: tx.at}).Error |
| 220 | if err != nil { |
| 221 | return err |
| 222 | } |
| 223 | for _, held := range []any{&TitlePrint{}, &Alias{}, |
| 224 | &DailyPrint{}} { |
| 225 | if err := tx.db.Where("page_id = ?", pageID). |
| 226 | Delete(held).Error; err != nil { |
| 227 | return err |
| 228 | } |
| 229 | } |
| 230 | return tx.log(actor, ObjectPage, pageID, VerbDeleted, page.Revision) |
| 231 | }) |
| 232 | } |
| 233 | |
| 234 | // UpdateLinks points every link typed with one of a team page's aliases |
| 235 | // at its current title, in the candidates the person may edit: team |
| 236 | // pages and their own private pages only (§3.4). It answers how many of |
| 237 | // those it changed, never a count that includes anyone else's. |
| 238 | func (store *Store) UpdateLinks(ctx context.Context, who string, |
| 239 | pageID int64, candidates []int64) (int, error) { |
| 240 | changed := 0 |
| 241 | err := store.write(ctx, func(tx *Tx) error { |
| 242 | actor, err := tx.member(who) |
| 243 | if err != nil { |
| 244 | return err |
| 245 | } |
| 246 | _, record, err := tx.teamPage(actor, pageID) |
| 247 | if err != nil { |
| 248 | return err |
| 249 | } |
| 250 | old, err := tx.store.aliasFolds(tx.db, pageID) |
| 251 | if err != nil { |
| 252 | return err |
| 253 | } |
| 254 | matches := func(target string) bool { return old[Fold(target)] } |
| 255 | for _, candidate := range candidates { |
| 256 | edited, err := tx.retarget(actor, candidate, matches, |
| 257 | record.Title) |
| 258 | if err != nil { |
| 259 | return err |
| 260 | } |
| 261 | changed += edited |
| 262 | } |
| 263 | if changed == 0 { |
| 264 | return errNoChange |
| 265 | } |
| 266 | return nil |
| 267 | }) |
| 268 | return changed, quiet(err) |
| 269 | } |
| 270 | |
| 271 | // retarget edits one candidate page as the person, if it is theirs to |
| 272 | // edit and holds a matching link. It reports 1 for a page it changed. |
| 273 | func (tx *Tx) retarget(actor, pageID int64, matches func(string) bool, |
| 274 | title string) (int, error) { |
| 275 | page, err := tx.visiblePage(actor, pageID) |
| 276 | if errors.Is(err, ErrNotFound) { |
| 277 | return 0, nil |
| 278 | } |
| 279 | if err != nil { |
| 280 | return 0, err |
| 281 | } |
| 282 | record, err := tx.store.OpenPage(page) |
| 283 | if err != nil { |
| 284 | return 0, err |
| 285 | } |
| 286 | body, count := markup.Retarget(record.Body, matches, title) |
| 287 | if count == 0 { |
| 288 | return 0, nil |
| 289 | } |
| 290 | record.Body = body |
| 291 | return 1, tx.commit(&page, record, actor, |
| 292 | linkVerb(page, VerbRelinked), attachNothing) |
| 293 | } |
| 294 | |