| 1 | package mapa |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "strings" |
| 7 | "time" |
| 8 | |
| 9 | "gorm.io/gorm" |
| 10 | ) |
| 11 | |
| 12 | // LastEdit is who saved a page's current revision and when: the |
| 13 | // kicker's "Edited by Ana at 11:40" (level-up spec §4.1). It reads the |
| 14 | // revision store's plaintext row only (base §4), never a body. |
| 15 | type LastEdit struct { |
| 16 | Who string |
| 17 | At time.Time |
| 18 | } |
| 19 | |
| 20 | // LastEditOf is the page's newest revision in the window, for a viewer |
| 21 | // who may open the page; ok is false when the window holds none. |
| 22 | func (store *Store) LastEditOf(ctx context.Context, viewer Viewer, |
| 23 | pageID int64) (LastEdit, bool, error) { |
| 24 | page, err := store.readable(ctx, viewer, pageID) |
| 25 | if err != nil { |
| 26 | return LastEdit{}, false, err |
| 27 | } |
| 28 | var row Revision |
| 29 | err = store.database.WithContext(ctx).Where("page_id = ? AND "+ |
| 30 | "revision = ? AND at > ?", pageID, page.Revision, |
| 31 | store.now().UTC().Add(-HistoryWindow)).First(&row).Error |
| 32 | if errors.Is(err, gorm.ErrRecordNotFound) { |
| 33 | return LastEdit{}, false, nil |
| 34 | } |
| 35 | if err != nil { |
| 36 | return LastEdit{}, false, err |
| 37 | } |
| 38 | who := "you" |
| 39 | if row.ActorID != viewer.MemberID { |
| 40 | address, err := store.MemberAddress(ctx, row.ActorID) |
| 41 | if err != nil { |
| 42 | return LastEdit{}, false, err |
| 43 | } |
| 44 | who = personName(address) |
| 45 | } |
| 46 | at := row.At |
| 47 | if viewer.TimeZone != nil { |
| 48 | at = at.In(viewer.TimeZone) |
| 49 | } |
| 50 | return LastEdit{Who: who, At: at}, true, nil |
| 51 | } |
| 52 | |
| 53 | // personName is how Mapa names a teammate in passing: the address's |
| 54 | // local part ("ana" for ana@example.com). The full address stays in the |
| 55 | // account menu and history. |
| 56 | func personName(address string) string { |
| 57 | local, _, _ := strings.Cut(address, "@") |
| 58 | if local == "" { |
| 59 | return address |
| 60 | } |
| 61 | return local |
| 62 | } |
| 63 | |