| 1 | package chat |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/json" |
| 6 | "errors" |
| 7 | "time" |
| 8 | |
| 9 | "amadan.net/oficina/equipo/internal/wire" |
| 10 | "gorm.io/gorm" |
| 11 | ) |
| 12 | |
| 13 | // notebookRecord is the newest notebook a person's runner reported, for |
| 14 | // the owner's view. The runner keeps the notebook itself; this copy only |
| 15 | // shows the person what their coordinator remembers and why. |
| 16 | type notebookRecord struct { |
| 17 | Corrections string |
| 18 | Person string `gorm:"primaryKey"` |
| 19 | Entries string |
| 20 | Revision int64 |
| 21 | UpdatedAt time.Time |
| 22 | } |
| 23 | |
| 24 | func (notebookRecord) TableName() string { return "chat_notebooks" } |
| 25 | |
| 26 | // notebookReport stores a runner's notebook for its own person, from the |
| 27 | // primary runner only, bounded like the runner's own. |
| 28 | func (t *txn) notebookReport(l Lease, v *wire.NotebookReport) error { |
| 29 | person := l.Runner.Person |
| 30 | primary, err := t.s.authority.PrimaryRunner(t.db, person) |
| 31 | if err != nil || primary != l.Runner.RunnerID || v.Person != person { |
| 32 | return err |
| 33 | } |
| 34 | entries := wire.PersonalNotes(v.Entries) |
| 35 | if l.Protocol < wire.ProtocolInjectionDefences { |
| 36 | for i := range entries { |
| 37 | if entries[i].Source == wire.NotePerson { |
| 38 | entries[i].Source = wire.NoteOwn |
| 39 | } |
| 40 | } |
| 41 | } |
| 42 | total := 0 |
| 43 | for _, e := range entries { |
| 44 | total += len(e.Text) |
| 45 | if e.Source != wire.NotePerson && e.Source != wire.NoteRepo && e.Source != wire.NoteOwn || len(e.Text) > wire.MaxEntryBytes || len(e.ID) > 20 || len(e.Repo) > 300 || len(e.Path) > 300 { |
| 46 | return ErrInvalid |
| 47 | } |
| 48 | } |
| 49 | if len(entries) > wire.MaxNoteEntries || total > wire.MaxNotebookBytes { |
| 50 | return ErrInvalid |
| 51 | } |
| 52 | return t.saveNotebook(person, entries) |
| 53 | } |
| 54 | |
| 55 | func (t *txn) saveNotebook(person string, entries []wire.NotebookEntry) error { |
| 56 | // Housekeeping never writes a notebook in slice 1: not another |
| 57 | // person's, and never clearing or replacing the person's own. |
| 58 | if err := t.hkForbid("notebook"); err != nil { |
| 59 | return err |
| 60 | } |
| 61 | if entries == nil { |
| 62 | entries = []wire.NotebookEntry{} |
| 63 | } |
| 64 | raw, err := json.Marshal(entries) |
| 65 | if err != nil { |
| 66 | return err |
| 67 | } |
| 68 | var r notebookRecord |
| 69 | err = t.db.Where("person = ?", person).First(&r).Error |
| 70 | if errors.Is(err, gorm.ErrRecordNotFound) { |
| 71 | return t.db.Create(¬ebookRecord{Person: person, Entries: string(raw), Revision: 1, UpdatedAt: t.now()}).Error |
| 72 | } |
| 73 | if err != nil { |
| 74 | return err |
| 75 | } |
| 76 | return t.db.Model(¬ebookRecord{}).Where("person = ?", person).Select("entries", "revision", "updated_at").Updates(¬ebookRecord{Entries: string(raw), Revision: r.Revision + 1, UpdatedAt: t.now()}).Error |
| 77 | } |
| 78 | |
| 79 | // Notebook is GET /api/v1/notebook: what the person's coordinator noted, |
| 80 | // each entry with where it came from. |
| 81 | func (s *Store) Notebook(ctx context.Context, person string) (out wire.Notebook, err error) { |
| 82 | err = s.read(ctx, func(tx *gorm.DB) error { |
| 83 | if err := s.authority.CurrentPerson(ctx, tx); err != nil { |
| 84 | return err |
| 85 | } |
| 86 | out = wire.Notebook{ID: person, Entries: []wire.NotebookEntry{}} |
| 87 | var r notebookRecord |
| 88 | err := tx.Where("person = ?", person).First(&r).Error |
| 89 | if errors.Is(err, gorm.ErrRecordNotFound) { |
| 90 | return nil |
| 91 | } |
| 92 | if err != nil { |
| 93 | return err |
| 94 | } |
| 95 | updated := r.UpdatedAt |
| 96 | out.Revision, out.UpdatedAt = r.Revision, &updated |
| 97 | if err := json.Unmarshal([]byte(r.Entries), &out.Entries); err != nil { |
| 98 | return err |
| 99 | } |
| 100 | var corrections []wire.NotebookEntry |
| 101 | if r.Corrections != "" { |
| 102 | if err := json.Unmarshal([]byte(r.Corrections), &corrections); err != nil { |
| 103 | return err |
| 104 | } |
| 105 | } |
| 106 | out.Entries = wire.MergePlacementNotes(out.Entries, corrections) |
| 107 | return nil |
| 108 | }) |
| 109 | return out, err |
| 110 | } |
| 111 | |
| 112 | // ErrBusy means the coordinator is in a turn; reset waits for it to end. |
| 113 | var ErrBusy = errors.New("chat: coordinator busy") |
| 114 | |
| 115 | // ResetCoordinator clears the person's notebook and starts their |
| 116 | // coordinator afresh: the runner empties the desk and forgets the |
| 117 | // session (nothing resumed still carries the old notes), and the |
| 118 | // coordinator moves in again. Refused while a turn is queued or running. |
| 119 | func (s *Store) ResetCoordinator(ctx context.Context, person string) (out wire.Coordinator, err error) { |
| 120 | err = s.tx(ctx, func(t *txn) error { |
| 121 | if err := s.authority.CurrentPerson(ctx, t.db); err != nil { |
| 122 | return err |
| 123 | } |
| 124 | if err := t.isOwner(person); err != nil { |
| 125 | return err |
| 126 | } |
| 127 | var busy int64 |
| 128 | if err := t.db.Model(&turnRecord{}).Where("person = ? AND state IN ? AND "+personLane, person, wire.InFlight).Count(&busy).Error; err != nil { |
| 129 | return err |
| 130 | } |
| 131 | if busy > 0 { |
| 132 | return ErrBusy |
| 133 | } |
| 134 | if err := t.command(person, wire.FrameReset, wire.ResetCommand{Person: person}); err != nil { |
| 135 | return err |
| 136 | } |
| 137 | if err := t.saveNotebook(person, nil); err != nil { |
| 138 | return err |
| 139 | } |
| 140 | if err := t.savePlacementNotes(person, nil); err != nil { |
| 141 | return err |
| 142 | } |
| 143 | if err := t.setCoordinator(person, wire.CoordinatorNew, ""); err != nil { |
| 144 | return err |
| 145 | } |
| 146 | c, err := t.coordinator(person) |
| 147 | out = coordinatorView(c) |
| 148 | return err |
| 149 | }) |
| 150 | return out, err |
| 151 | } |
| 152 | |
| 153 | func (t *txn) placementNotes(person string) ([]wire.NotebookEntry, error) { |
| 154 | var n notebookRecord |
| 155 | err := t.db.First(&n, "person = ?", person).Error |
| 156 | if errors.Is(err, gorm.ErrRecordNotFound) { |
| 157 | return nil, nil |
| 158 | } |
| 159 | if err != nil { |
| 160 | return nil, err |
| 161 | } |
| 162 | var entries []wire.NotebookEntry |
| 163 | if n.Corrections != "" { |
| 164 | if err := json.Unmarshal([]byte(n.Corrections), &entries); err != nil { |
| 165 | return nil, err |
| 166 | } |
| 167 | } |
| 168 | return entries, nil |
| 169 | } |
| 170 | |
| 171 | func (t *txn) savePlacementNotes(person string, entries []wire.NotebookEntry) error { |
| 172 | var n notebookRecord |
| 173 | err := t.db.First(&n, "person = ?", person).Error |
| 174 | if errors.Is(err, gorm.ErrRecordNotFound) { |
| 175 | if err := t.saveNotebook(person, nil); err != nil { |
| 176 | return err |
| 177 | } |
| 178 | } else if err != nil { |
| 179 | return err |
| 180 | } |
| 181 | raw, err := json.Marshal(wire.BoundedPlacementNotes(entries)) |
| 182 | if err != nil { |
| 183 | return err |
| 184 | } |
| 185 | return t.db.Model(¬ebookRecord{}).Where("person = ?", person).Updates(map[string]any{"corrections": string(raw), "revision": gorm.Expr("revision + 1"), "updated_at": t.now()}).Error |
| 186 | } |
| 187 | |
| 188 | func (s *Store) RemovePlacementNote(ctx context.Context, person, id string) error { |
| 189 | return s.tx(ctx, func(t *txn) error { |
| 190 | if err := s.authority.CurrentPerson(ctx, t.db); err != nil { |
| 191 | return err |
| 192 | } |
| 193 | if err := t.isOwner(person); err != nil { |
| 194 | return err |
| 195 | } |
| 196 | entries, err := t.placementNotes(person) |
| 197 | if err != nil { |
| 198 | return err |
| 199 | } |
| 200 | for i, e := range entries { |
| 201 | if e.ID == id { |
| 202 | if err := s.authority.Audit(t.db, person, "placement.correction_removed", id); err != nil { |
| 203 | return err |
| 204 | } |
| 205 | return t.savePlacementNotes(person, append(entries[:i], entries[i+1:]...)) |
| 206 | } |
| 207 | } |
| 208 | return ErrNotFound |
| 209 | }) |
| 210 | } |
| 211 | |