| 1 | package mapa |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "strings" |
| 6 | "time" |
| 7 | |
| 8 | "amadan.net/oficina/mapa/internal/mapa/locale" |
| 9 | "amadan.net/oficina/mapa/internal/mapa/markup" |
| 10 | "amadan.net/oficina/mapa/internal/mapa/seal" |
| 11 | ) |
| 12 | |
| 13 | // The History panel's list (level-up spec §4.5): a page's versions in |
| 14 | // the 90-day window, newest first, each with who made it, when, and a |
| 15 | // one-line summary. The summary's verb is the revision's own event row; |
| 16 | // only an edit is diffed, and only against the revision before it in the |
| 17 | // window, never one the window has dropped. |
| 18 | |
| 19 | // HistoryPage is how many versions one screen of history summarises. |
| 20 | // "Show older" pages through the rest. |
| 21 | const HistoryPage = 30 |
| 22 | |
| 23 | // Version is one entry in a page's history. |
| 24 | type Version struct { |
| 25 | Revision int64 |
| 26 | // At is in the viewer's zone. |
| 27 | At time.Time |
| 28 | // Who is "You" for the viewer, else the address's local part, and |
| 29 | // Initial the letter on their avatar. |
| 30 | Who, Initial string |
| 31 | Summary string |
| 32 | // Current marks the page's current revision. |
| 33 | Current bool |
| 34 | } |
| 35 | |
| 36 | // VersionList is one screen of a page's history, newest first. |
| 37 | type VersionList struct { |
| 38 | Versions []Version |
| 39 | // Skip is how many newer versions come before these. |
| 40 | Skip int |
| 41 | // More reports older versions in the window after these. |
| 42 | More bool |
| 43 | // AgedOut reports, on the last screen, that the window has dropped |
| 44 | // the page's earlier revisions. |
| 45 | AgedOut bool |
| 46 | } |
| 47 | |
| 48 | // revisionRow is a revision's plaintext row, without its record. |
| 49 | type revisionRow struct { |
| 50 | Revision int64 |
| 51 | At time.Time |
| 52 | ActorID int64 |
| 53 | } |
| 54 | |
| 55 | // pageHistory is one page's revisions in the window, as one viewer reads |
| 56 | // them. Records are opened when a summary needs them, once each. |
| 57 | type pageHistory struct { |
| 58 | store *Store |
| 59 | ctx context.Context |
| 60 | viewer Viewer |
| 61 | page Page |
| 62 | cutoff time.Time |
| 63 | rows []revisionRow |
| 64 | records map[int64]Record |
| 65 | verbs map[int64]string |
| 66 | names map[int64]string |
| 67 | // parsed is the last version's blocks, which the next older edit's |
| 68 | // summary compares against, so each body is parsed once. |
| 69 | parsed parsedVersion |
| 70 | } |
| 71 | |
| 72 | // parsedVersion is one revision's body as blocks. |
| 73 | type parsedVersion struct { |
| 74 | revision int64 |
| 75 | blocks []markup.Block |
| 76 | } |
| 77 | |
| 78 | // History is one screen of a page's versions, from skip on, if the |
| 79 | // viewer may open the page. It reads; it never writes. |
| 80 | func (store *Store) History(ctx context.Context, viewer Viewer, |
| 81 | pageID int64, skip int) (VersionList, error) { |
| 82 | history, err := store.loadHistory(ctx, viewer, pageID) |
| 83 | if err != nil { |
| 84 | return VersionList{}, err |
| 85 | } |
| 86 | return history.list(skip) |
| 87 | } |
| 88 | |
| 89 | // loadHistory reads the page's revision rows in the window, newest |
| 90 | // first. Ties in time keep the order they were written in. |
| 91 | func (store *Store) loadHistory(ctx context.Context, viewer Viewer, |
| 92 | pageID int64) (*pageHistory, error) { |
| 93 | page, err := store.readable(ctx, viewer, pageID) |
| 94 | if err != nil { |
| 95 | return nil, err |
| 96 | } |
| 97 | history := &pageHistory{store: store, ctx: ctx, viewer: viewer, |
| 98 | page: page, cutoff: store.now().UTC().Add(-HistoryWindow), |
| 99 | records: map[int64]Record{}, verbs: map[int64]string{}, |
| 100 | names: map[int64]string{}} |
| 101 | err = store.database.WithContext(ctx).Model(&Revision{}). |
| 102 | Select("revision", "at", "actor_id"). |
| 103 | Where("page_id = ? AND at > ?", pageID, history.cutoff). |
| 104 | Order("at DESC, rowid DESC").Scan(&history.rows).Error |
| 105 | return history, err |
| 106 | } |
| 107 | |
| 108 | func (history *pageHistory) list(skip int) (VersionList, error) { |
| 109 | skip = max(0, min(skip, len(history.rows))) |
| 110 | end := min(skip+HistoryPage, len(history.rows)) |
| 111 | list := VersionList{Skip: skip, More: end < len(history.rows)} |
| 112 | if err := history.loadVerbs(skip, end); err != nil { |
| 113 | return list, err |
| 114 | } |
| 115 | for index := skip; index < end; index++ { |
| 116 | version, err := history.version(index) |
| 117 | if err != nil { |
| 118 | return list, err |
| 119 | } |
| 120 | list.Versions = append(list.Versions, version) |
| 121 | } |
| 122 | list.AgedOut = !list.More && history.agedOut() |
| 123 | return list, nil |
| 124 | } |
| 125 | |
| 126 | // agedOut reports that the page is older than the window, so its |
| 127 | // earliest revisions have been (or are about to be) swept. |
| 128 | func (history *pageHistory) agedOut() bool { |
| 129 | return !history.page.CreatedAt.After(history.cutoff) |
| 130 | } |
| 131 | |
| 132 | // find is a revision's place in the rows, or -1. |
| 133 | func (history *pageHistory) find(revision int64) int { |
| 134 | for index, row := range history.rows { |
| 135 | if row.Revision == revision { |
| 136 | return index |
| 137 | } |
| 138 | } |
| 139 | return -1 |
| 140 | } |
| 141 | |
| 142 | // loadVerbs reads the verb of each revision in rows[start:end]: the |
| 143 | // event that minted it, which is the first event naming it (a delete or |
| 144 | // a freed alias names the revision it leaves, after the fact). |
| 145 | func (history *pageHistory) loadVerbs(start, end int) error { |
| 146 | if start >= end { |
| 147 | return nil |
| 148 | } |
| 149 | revisions := make([]int64, 0, end-start) |
| 150 | for _, row := range history.rows[start:end] { |
| 151 | revisions = append(revisions, row.Revision) |
| 152 | } |
| 153 | var events []Event |
| 154 | err := history.store.database.WithContext(history.ctx). |
| 155 | Select("revision", "verb").Where("object_kind = ? AND "+ |
| 156 | "object_id = ? AND revision IN ?", ObjectPage, history.page.ID, |
| 157 | revisions).Order("id").Find(&events).Error |
| 158 | for _, event := range events { |
| 159 | if _, seen := history.verbs[event.Revision]; !seen { |
| 160 | history.verbs[event.Revision] = event.Verb |
| 161 | } |
| 162 | } |
| 163 | return err |
| 164 | } |
| 165 | |
| 166 | func (history *pageHistory) version(index int) (Version, error) { |
| 167 | row := history.rows[index] |
| 168 | summary, err := history.summary(index) |
| 169 | if err != nil { |
| 170 | return Version{}, err |
| 171 | } |
| 172 | name, err := history.name(row.ActorID) |
| 173 | if err != nil { |
| 174 | return Version{}, err |
| 175 | } |
| 176 | who := name |
| 177 | if row.ActorID == history.viewer.MemberID { |
| 178 | who = "You" |
| 179 | } |
| 180 | return Version{Revision: row.Revision, At: history.inZone(row.At), |
| 181 | Who: who, Initial: initial(name), Summary: summary, |
| 182 | Current: row.Revision == history.page.Revision}, nil |
| 183 | } |
| 184 | |
| 185 | func (history *pageHistory) inZone(at time.Time) time.Time { |
| 186 | if history.viewer.TimeZone != nil { |
| 187 | return at.In(history.viewer.TimeZone) |
| 188 | } |
| 189 | return at |
| 190 | } |
| 191 | |
| 192 | // name is the actor's local part. |
| 193 | func (history *pageHistory) name(actor int64) (string, error) { |
| 194 | if actor == history.viewer.MemberID { |
| 195 | return personName(history.viewer.Address), nil |
| 196 | } |
| 197 | if name, ok := history.names[actor]; ok { |
| 198 | return name, nil |
| 199 | } |
| 200 | address, err := history.store.MemberAddress(history.ctx, actor) |
| 201 | history.names[actor] = personName(address) |
| 202 | return history.names[actor], err |
| 203 | } |
| 204 | |
| 205 | // record is the text of rows[index], opened once. |
| 206 | func (history *pageHistory) record(index int) (Record, error) { |
| 207 | revision := history.rows[index].Revision |
| 208 | if record, ok := history.records[revision]; ok { |
| 209 | return record, nil |
| 210 | } |
| 211 | var row Revision |
| 212 | err := history.store.database.WithContext(history.ctx). |
| 213 | Where("page_id = ? AND revision = ?", history.page.ID, revision). |
| 214 | First(&row).Error |
| 215 | if err != nil { |
| 216 | return Record{}, err |
| 217 | } |
| 218 | record, err := history.store.openRecord(seal.PurposeRecord, |
| 219 | row.SealedRecord) |
| 220 | history.records[revision] = record |
| 221 | return record, err |
| 222 | } |
| 223 | |
| 224 | // summary is rows[index]'s one line, from its event verb (§4.5). |
| 225 | func (history *pageHistory) summary(index int) (string, error) { |
| 226 | plain, err := history.plainSummary(index) |
| 227 | if err != nil { |
| 228 | return "", err |
| 229 | } |
| 230 | return history.importedSummary(index, plain) |
| 231 | } |
| 232 | |
| 233 | func (history *pageHistory) plainSummary(index int) (string, error) { |
| 234 | switch history.verbs[history.rows[index].Revision] { |
| 235 | case VerbCreated: |
| 236 | return "Created this page", nil |
| 237 | case VerbRestored: |
| 238 | return "Brought back after it was deleted", nil |
| 239 | case VerbRelinked: |
| 240 | return "Updated links", nil |
| 241 | case VerbMarked: |
| 242 | return "Made a block linkable", nil |
| 243 | case VerbRenamed: |
| 244 | return history.renamed(index) |
| 245 | case VerbStyled: |
| 246 | return history.styled(index) |
| 247 | case VerbReverted: |
| 248 | return history.reverted(index) |
| 249 | } |
| 250 | return history.edited(index) |
| 251 | } |
| 252 | |
| 253 | // renamed names the old title, or the new one when the revision before |
| 254 | // has aged out of the window. |
| 255 | func (history *pageHistory) renamed(index int) (string, error) { |
| 256 | record, err := history.record(index) |
| 257 | if err != nil { |
| 258 | return "", err |
| 259 | } |
| 260 | if index+1 < len(history.rows) { |
| 261 | before, err := history.record(index + 1) |
| 262 | if err != nil { |
| 263 | return "", err |
| 264 | } |
| 265 | if before.Title != "" && before.Title != record.Title { |
| 266 | return "Renamed from " + before.Title, nil |
| 267 | } |
| 268 | } |
| 269 | return "Renamed to " + record.Title, nil |
| 270 | } |
| 271 | |
| 272 | // maxRevertSearch bounds how many older versions one restore's summary |
| 273 | // opens to find its source: a screen's worth. |
| 274 | const maxRevertSearch = HistoryPage |
| 275 | |
| 276 | // reverted names the version it restored: the newest one before it in |
| 277 | // the window with the same text, among the maxRevertSearch before it. |
| 278 | // Restore keeps no pointer to its source, so with none there it cannot |
| 279 | // be named. |
| 280 | func (history *pageHistory) reverted(index int) (string, error) { |
| 281 | record, err := history.record(index) |
| 282 | if err != nil { |
| 283 | return "", err |
| 284 | } |
| 285 | last := min(len(history.rows), index+1+maxRevertSearch) |
| 286 | for older := index + 1; older < last; older++ { |
| 287 | candidate, err := history.record(older) |
| 288 | if err != nil { |
| 289 | return "", err |
| 290 | } |
| 291 | if candidate.Body == record.Body { |
| 292 | return "Restored the version from " + versionDay( |
| 293 | history.viewer.Locale, |
| 294 | history.inZone(history.rows[older].At)), nil |
| 295 | } |
| 296 | } |
| 297 | return "Restored an earlier version", nil |
| 298 | } |
| 299 | |
| 300 | // versionDay is a version's moment in a summary: "13 November 2026 at |
| 301 | // 16:15", as the reader's locale composes a date and a time (review |
| 302 | // round 8, L4). |
| 303 | func versionDay(dates *locale.Locale, at time.Time) string { |
| 304 | return dates.Stamp(at) |
| 305 | } |
| 306 | |
| 307 | // edited is the diff's summary against the revision before, or plain |
| 308 | // "Edited" when the window holds none. |
| 309 | func (history *pageHistory) edited(index int) (string, error) { |
| 310 | if index+1 >= len(history.rows) { |
| 311 | return "Edited", nil |
| 312 | } |
| 313 | now, err := history.blocks(index) |
| 314 | if err != nil { |
| 315 | return "", err |
| 316 | } |
| 317 | before, err := history.blocks(index + 1) |
| 318 | if err != nil { |
| 319 | return "", err |
| 320 | } |
| 321 | return editSummary(history.viewer.Locale, before, now), nil |
| 322 | } |
| 323 | |
| 324 | // blocks is rows[index]'s body parsed, kept until another is asked for. |
| 325 | func (history *pageHistory) blocks(index int) ([]markup.Block, error) { |
| 326 | revision := history.rows[index].Revision |
| 327 | if history.parsed.revision == revision { |
| 328 | return history.parsed.blocks, nil |
| 329 | } |
| 330 | record, err := history.record(index) |
| 331 | if err != nil { |
| 332 | return nil, err |
| 333 | } |
| 334 | history.parsed = parsedVersion{revision: revision, |
| 335 | blocks: markup.Parse(record.Body)} |
| 336 | return history.parsed.blocks, nil |
| 337 | } |
| 338 | |
| 339 | // editSummary is one line for an edit: "Added 2 blocks", "Removed a |
| 340 | // block", or "Edited" and the nearest heading above the first change. |
| 341 | func editSummary(dates *locale.Locale, from, to []markup.Block) string { |
| 342 | if said := layoutSummary(from, to); said != "" { |
| 343 | return said |
| 344 | } |
| 345 | from, to = withoutFences(from), withoutFences(to) |
| 346 | counts := map[string]int{} |
| 347 | var first *blockChange |
| 348 | for _, change := range diffBlocks(from, to) { |
| 349 | if change.Kind == blockSame { |
| 350 | continue |
| 351 | } |
| 352 | counts[change.Kind]++ |
| 353 | if first == nil || change.At < first.At || (change.At == |
| 354 | first.At && first.Kind == blockRemoved) { |
| 355 | first = &change |
| 356 | } |
| 357 | } |
| 358 | switch { |
| 359 | case first == nil: |
| 360 | return "Edited" |
| 361 | case counts[blockChanged]+counts[blockRemoved] == 0: |
| 362 | return blocksCounted(dates, "Added", counts[blockAdded]) |
| 363 | case counts[blockChanged]+counts[blockAdded] == 0: |
| 364 | return blocksCounted(dates, "Removed", counts[blockRemoved]) |
| 365 | } |
| 366 | if heading := headingAbove(to, *first); heading != "" { |
| 367 | return "Edited " + heading |
| 368 | } |
| 369 | return "Edited" |
| 370 | } |
| 371 | |
| 372 | func blocksCounted(dates *locale.Locale, verb string, count int) string { |
| 373 | if count == 1 { |
| 374 | return verb + " a block" |
| 375 | } |
| 376 | return verb + " " + dates.Number(count) + " blocks" |
| 377 | } |
| 378 | |
| 379 | // headingAbove is the text of the nearest heading at or above a change: |
| 380 | // a changed or added heading names itself; a removal looks above where |
| 381 | // it was. The first change is the earliest in the page, an addition |
| 382 | // before a removal in the same place. |
| 383 | func headingAbove(blocks []markup.Block, change blockChange) string { |
| 384 | from := change.At |
| 385 | if change.Kind == blockRemoved { |
| 386 | from-- |
| 387 | } |
| 388 | for index := min(from, len(blocks)-1); index >= 0; index-- { |
| 389 | if blocks[index].Kind == markup.Heading { |
| 390 | return strings.TrimSpace(markup.Words(blocks[index])) |
| 391 | } |
| 392 | } |
| 393 | return "" |
| 394 | } |
| 395 | |