| 1 | package conversation |
| 2 | |
| 3 | import ( |
| 4 | "amadan.net/oficina/equipo/internal/equipo/kernel" |
| 5 | "context" |
| 6 | "errors" |
| 7 | "gorm.io/gorm" |
| 8 | ) |
| 9 | |
| 10 | // ReadOnly uses existing streams and authenticated positions without lazy writes. |
| 11 | func (s *Store) ReadOnly(codec kernel.CursorCodec) *Store { |
| 12 | s.readOnly = true |
| 13 | s.cursorCodec = codec |
| 14 | return s |
| 15 | } |
| 16 | func (s *Store) readMain(teamID string, actor kernel.Actor) (Main, error) { |
| 17 | var row Main |
| 18 | err := s.tx.Where("team_id=? AND person=?", teamID, actor.Address).First(&row).Error |
| 19 | if errors.Is(err, gorm.ErrRecordNotFound) { |
| 20 | return Main{TeamID: teamID, Person: actor.Address, Revision: 1}, nil |
| 21 | } |
| 22 | return row, err |
| 23 | } |
| 24 | func (s *Store) readThread(task TaskAccess) (Thread, error) { |
| 25 | var row Thread |
| 26 | err := s.tx.Where("task_id=? AND team_id=?", task.ID, task.TeamID).First(&row).Error |
| 27 | if errors.Is(err, gorm.ErrRecordNotFound) { |
| 28 | return Thread{TaskID: task.ID, TeamID: task.TeamID, Revision: 1}, nil |
| 29 | } |
| 30 | return row, err |
| 31 | } |
| 32 | |
| 33 | // ReadOnly retains application queries while replacing their transaction runner. |
| 34 | func (s *Application) ReadOnly(read func(context.Context, func(*gorm.DB) error) error, codec kernel.CursorCodec) *Application { |
| 35 | out := *s |
| 36 | out.Write = read |
| 37 | out.Conversation = func(tx *gorm.DB) *Store { return s.Conversation(tx).ReadOnly(codec) } |
| 38 | return &out |
| 39 | } |
| 40 | |
| 41 | func (s *Application) PreferenceRevision(ctx context.Context, a Actor, id string) (revision int64, err error) { |
| 42 | err = s.Write(ctx, func(tx *gorm.DB) error { |
| 43 | cs := s.Conversation(tx) |
| 44 | main, e := cs.main(a) |
| 45 | if e != nil { |
| 46 | return e |
| 47 | } |
| 48 | if id == "" { |
| 49 | revision = main.Revision |
| 50 | return nil |
| 51 | } |
| 52 | row, _, e := cs.preference(main, id) |
| 53 | revision = row.Revision |
| 54 | return e |
| 55 | }) |
| 56 | return |
| 57 | } |
| 58 | |