| 1 | package chat |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/json" |
| 6 | "slices" |
| 7 | "time" |
| 8 | |
| 9 | "amadan.net/oficina/equipo/internal/wire" |
| 10 | "gorm.io/gorm" |
| 11 | ) |
| 12 | |
| 13 | // OficinaProvider is the Oficina apps' provider name. Only their |
| 14 | // connect cards carry the person's request on once they connect |
| 15 | // (carryOn): the note names them. |
| 16 | const OficinaProvider = "oficina" |
| 17 | |
| 18 | // ConnectCard is the Assistant card the instance raises when the person |
| 19 | // asked for something a provider does and has not connected it: the |
| 20 | // question and one link, to start connecting. Provider is kept on the |
| 21 | // card (Name) so it is raised once and settled when they connect. |
| 22 | type ConnectCard struct { |
| 23 | Provider string |
| 24 | Question string |
| 25 | Link wire.CardLink |
| 26 | } |
| 27 | |
| 28 | // RaiseConnect shows the person card in the Assistant lane, unless one |
| 29 | // for the same provider is open. Raised from a call (look's), it belongs |
| 30 | // to the call's turn, and only a turn the person started raises one. |
| 31 | // open says the person has an open card for the provider now. |
| 32 | func (s *Store) RaiseConnect(ctx context.Context, person string, card ConnectCard) (open bool, err error) { |
| 33 | turn := "" |
| 34 | if row, ok := callFrom(ctx); ok { |
| 35 | turn = row.TurnID |
| 36 | } |
| 37 | err = s.tx(ctx, func(t *txn) error { |
| 38 | if _, err := t.s.authority.Me(t.db, person); err != nil { |
| 39 | return err |
| 40 | } |
| 41 | if turn != "" { |
| 42 | var kinds []string |
| 43 | if err := t.db.Model(&turnRecord{}).Where("id = ? AND person = ?", turn, person).Pluck("coalesce(kind, '')", &kinds).Error; err != nil { |
| 44 | return err |
| 45 | } |
| 46 | if len(kinds) != 1 || kinds[0] != wire.TurnPerson { |
| 47 | return nil |
| 48 | } |
| 49 | } |
| 50 | var n int64 |
| 51 | if err := t.db.Model(&cardRecord{}).Where("person = ? AND lane = ? AND kind = ? AND name = ? AND state = ?", person, wire.LaneAssistant, wire.CardConnect, card.Provider, wire.CardOpen).Count(&n).Error; err != nil { |
| 52 | return err |
| 53 | } |
| 54 | if open = n > 0; open { |
| 55 | return nil |
| 56 | } |
| 57 | links, err := json.Marshal([]wire.CardLink{card.Link}) |
| 58 | if err != nil { |
| 59 | return err |
| 60 | } |
| 61 | now := t.now() |
| 62 | c := cardRecord{ID: newID("c_"), Person: person, Kind: wire.CardConnect, State: wire.CardOpen, TurnID: turn, Question: card.Question, Options: "[]", Links: string(links), Name: card.Provider, Revision: 1, CreatedAt: now, UpdatedAt: now, Lane: wire.LaneAssistant} |
| 63 | if err := t.db.Create(&c).Error; err != nil { |
| 64 | return err |
| 65 | } |
| 66 | open = true |
| 67 | return t.cardEvent(c) |
| 68 | }) |
| 69 | return open && err == nil, err |
| 70 | } |
| 71 | |
| 72 | // connectTurnRecord is the Assistant turn queued once the person |
| 73 | // connected what a connect card asked for: one per card, the card's id |
| 74 | // its key, and AskedTurn the person's turn that raised the card. |
| 75 | type connectTurnRecord struct { |
| 76 | CardID string `gorm:"primaryKey"` |
| 77 | TurnID string `gorm:"uniqueIndex:chat_connect_turn"` |
| 78 | Person string |
| 79 | AskedTurn string |
| 80 | CreatedAt time.Time |
| 81 | } |
| 82 | |
| 83 | func (connectTurnRecord) TableName() string { return "chat_connect_turns" } |
| 84 | |
| 85 | // SettleConnect resolves the person's open connect cards for provider: |
| 86 | // they connected it. The Assistant then carries on by itself (ruling |
| 87 | // 2026-11-19), for the Oficina apps only: one team_update turn in their Assistant lane, queued |
| 88 | // behind any turn in flight, whose note (noteConnected) has it finish |
| 89 | // the request that needed the provider. The newest card is the turn's |
| 90 | // key, so a card settled again queues nothing more. A person no longer |
| 91 | // on the roster settles nothing. |
| 92 | func (s *Store) SettleConnect(ctx context.Context, person, provider string) error { |
| 93 | return s.settleConnect(ctx, person, provider, nil) |
| 94 | } |
| 95 | |
| 96 | // Connected reads, in the settling transaction, when the person's |
| 97 | // current connection to a provider was last saved; ok is false when |
| 98 | // they have none. |
| 99 | type Connected func(tx *gorm.DB) (saved time.Time, ok bool, err error) |
| 100 | |
| 101 | // SettleConnectSince is SettleConnect bound to its evidence: in the same |
| 102 | // transaction, the person's current connection (connected), and only |
| 103 | // the cards raised by the time it was saved. A card raised after it, or |
| 104 | // with no connection now, stays open. |
| 105 | func (s *Store) SettleConnectSince(ctx context.Context, person, provider string, connected Connected) error { |
| 106 | return s.settleConnect(ctx, person, provider, connected) |
| 107 | } |
| 108 | |
| 109 | func (s *Store) settleConnect(ctx context.Context, person, provider string, connected Connected) error { |
| 110 | return s.tx(ctx, func(t *txn) error { |
| 111 | if _, err := t.s.authority.Me(t.db, person); err != nil { |
| 112 | return err |
| 113 | } |
| 114 | var cards []cardRecord |
| 115 | if err := t.db.Where("person = ? AND lane = ? AND kind = ? AND name = ? AND state = ?", person, wire.LaneAssistant, wire.CardConnect, provider, wire.CardOpen).Order("created_at, id").Find(&cards).Error; err != nil { |
| 116 | return err |
| 117 | } |
| 118 | if connected != nil { |
| 119 | saved, ok, err := connected(t.db) |
| 120 | if err != nil || !ok { |
| 121 | return err |
| 122 | } |
| 123 | cards = slices.DeleteFunc(cards, func(c cardRecord) bool { return c.CreatedAt.After(saved) }) |
| 124 | } |
| 125 | for _, c := range cards { |
| 126 | c.State = wire.CardResolved |
| 127 | if err := t.saveCard(&c); err != nil { |
| 128 | return err |
| 129 | } |
| 130 | } |
| 131 | if len(cards) == 0 || provider != OficinaProvider { |
| 132 | return nil |
| 133 | } |
| 134 | return t.carryOn(cards[len(cards)-1]) |
| 135 | }) |
| 136 | } |
| 137 | |
| 138 | // ConnectOpen says the person has an open connect card for provider. |
| 139 | func (s *Store) ConnectOpen(ctx context.Context, person, provider string) (bool, error) { |
| 140 | var n int64 |
| 141 | err := s.read(ctx, func(tx *gorm.DB) error { |
| 142 | return tx.Model(&cardRecord{}).Where("person = ? AND lane = ? AND kind = ? AND name = ? AND state = ?", person, wire.LaneAssistant, wire.CardConnect, provider, wire.CardOpen).Count(&n).Error |
| 143 | }) |
| 144 | return n > 0, err |
| 145 | } |
| 146 | |
| 147 | // carryOn queues the Assistant's turn after card was settled, once per |
| 148 | // card. |
| 149 | func (t *txn) carryOn(card cardRecord) error { |
| 150 | var n int64 |
| 151 | if err := t.db.Model(&connectTurnRecord{}).Where("card_id = ?", card.ID).Count(&n).Error; err != nil || n > 0 { |
| 152 | return err |
| 153 | } |
| 154 | now := t.now() |
| 155 | r := turnRecord{ID: newID("t_"), Person: card.Person, State: wire.TurnQueued, Attempt: 1, Kind: wire.TurnTeamUpdate, Note: noteConnected, Revision: 1, CreatedAt: now, UpdatedAt: now, Lane: wire.LaneAssistant} |
| 156 | if err := t.db.Create(&r).Error; err != nil { |
| 157 | return err |
| 158 | } |
| 159 | if err := t.db.Create(&connectTurnRecord{CardID: card.ID, TurnID: r.ID, Person: card.Person, AskedTurn: card.TurnID, CreatedAt: now}).Error; err != nil { |
| 160 | return err |
| 161 | } |
| 162 | if err := t.turnEvent(r); err != nil { |
| 163 | return err |
| 164 | } |
| 165 | return t.dispatchNext(card.Person, wire.LaneAssistant) |
| 166 | } |
| 167 | |
| 168 | // connectCardView fills a connect card's link. |
| 169 | func connectCardView(v *wire.AssistantCard, c cardRecord) error { |
| 170 | if c.Links != "" { |
| 171 | if err := json.Unmarshal([]byte(c.Links), &v.Links); err != nil { |
| 172 | return err |
| 173 | } |
| 174 | } |
| 175 | if v.Links == nil { |
| 176 | v.Links = []wire.CardLink{} |
| 177 | } |
| 178 | return nil |
| 179 | } |
| 180 | |