| 1 | package chat |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "time" |
| 6 | |
| 7 | "amadan.net/oficina/equipo/internal/wire" |
| 8 | ) |
| 9 | |
| 10 | // The sources of a stored board. |
| 11 | const ( |
| 12 | // SourceBase: Base answered GET /api/v1/abilities. |
| 13 | SourceBase = "base" |
| 14 | // SourceLegacy: the defaults, renewed while a Base older than the |
| 15 | // board (its board route 404, its operations 200) honours the token. |
| 16 | SourceLegacy = "legacy" |
| 17 | ) |
| 18 | |
| 19 | // abilitiesRecord is a person's board as their Base last answered it |
| 20 | // (spec §5.3), per origin. FetchSeq orders the fetches, so a slow old |
| 21 | // answer never overwrites a newer one; ConfirmedAt is the last answer |
| 22 | // that counted, RevokedAt a 401; AuthoritativeSeen is set once the |
| 23 | // origin has answered the board and is never cleared. |
| 24 | type abilitiesRecord struct { |
| 25 | Person string `gorm:"primaryKey"` |
| 26 | Origin string `gorm:"primaryKey"` |
| 27 | Revision int64 |
| 28 | Abilities string |
| 29 | Admin bool |
| 30 | Ask string |
| 31 | Source string |
| 32 | FetchSeq int64 |
| 33 | ConfirmedAt *time.Time |
| 34 | RevokedAt *time.Time |
| 35 | AuthoritativeSeen bool |
| 36 | } |
| 37 | |
| 38 | func (abilitiesRecord) TableName() string { return "chat_assistant_abilities" } |
| 39 | |
| 40 | // board is the record as Base's board: its switches, admin and ask. |
| 41 | func (r abilitiesRecord) board() (wire.AbilityBoard, error) { |
| 42 | b := wire.AbilityBoard{Revision: r.Revision, Admin: r.Admin, Ask: r.Ask, Abilities: map[string]bool{}} |
| 43 | if r.Abilities == "" { |
| 44 | return b, nil |
| 45 | } |
| 46 | if err := json.Unmarshal([]byte(r.Abilities), &b.Abilities); err != nil { |
| 47 | return wire.AbilityBoard{}, err |
| 48 | } |
| 49 | return b, nil |
| 50 | } |
| 51 | |
| 52 | // defaultBoard is every key on: what a legacy Base's members have. |
| 53 | func defaultBoard() map[string]bool { |
| 54 | out := map[string]bool{} |
| 55 | for _, k := range wire.Abilities { |
| 56 | out[k] = true |
| 57 | } |
| 58 | return out |
| 59 | } |
| 60 | |