| 1 | package chat |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "errors" |
| 6 | "strings" |
| 7 | |
| 8 | "amadan.net/oficina/equipo/internal/wire" |
| 9 | ) |
| 10 | |
| 11 | // composerSend consumes only an unambiguous project answer to a visible typed |
| 12 | // card. Other composer messages retain the ordinary send contract. |
| 13 | func (t *txn) composerSend(lane, person string, in wire.SendMessage) (wire.Sent, bool, error) { |
| 14 | ordinary := func() (wire.Sent, bool, error) { |
| 15 | return t.sendSelectedOrigin(lane, person, in.IdempotencyKey, in.Text, in.Attachments, originComposer, in.ProjectID, nil) |
| 16 | } |
| 17 | if in.ProjectID != "" || len(in.Attachments) > 0 { |
| 18 | return ordinary() |
| 19 | } |
| 20 | var n int64 |
| 21 | if err := t.db.Model(&messageRecord{}).Where("person = ? AND lane = ? AND idempotency_key = ?", person, lane, in.IdempotencyKey).Count(&n).Error; err != nil { |
| 22 | return wire.Sent{}, false, err |
| 23 | } |
| 24 | if n > 0 { |
| 25 | return ordinary() |
| 26 | } |
| 27 | var cards []cardRecord |
| 28 | if err := t.db.Where("person = ? AND coalesce(lane, '') = ? AND kind = ? AND state = ? AND placement <> '' AND NOT EXISTS (SELECT 1 FROM chat_card_visibility v WHERE v.card_id = chat_cards.id AND v.person = chat_cards.person AND v.aside = ?)", person, lane, wire.CardQuestion, wire.CardOpen, true).Order("created_at, id").Find(&cards).Error; err != nil { |
| 29 | return wire.Sent{}, false, err |
| 30 | } |
| 31 | for _, c := range cards { |
| 32 | var data placementData |
| 33 | if err := json.Unmarshal([]byte(c.Placement), &data); err != nil { |
| 34 | return wire.Sent{}, false, err |
| 35 | } |
| 36 | if !data.ByName { |
| 37 | continue |
| 38 | } |
| 39 | p, err := t.composerProject(person, in.Text) |
| 40 | if err != nil { |
| 41 | return wire.Sent{}, false, err |
| 42 | } |
| 43 | if p == nil { |
| 44 | continue |
| 45 | } |
| 46 | if _, err = t.selectedProject(person, p.ID); errors.Is(err, ErrNotFound) || errors.Is(err, ErrNotAllowed) { |
| 47 | continue |
| 48 | } else if err != nil { |
| 49 | return wire.Sent{}, false, err |
| 50 | } |
| 51 | if err := t.placementRunner(person); errors.Is(err, ErrConflict) { |
| 52 | return ordinary() |
| 53 | } else if err != nil { |
| 54 | return wire.Sent{}, false, err |
| 55 | } |
| 56 | // Keep the person's complete words; the resolved repository is separate data. |
| 57 | data.Repos = []string{p.Repo} |
| 58 | data.Labels = []string{in.Text} |
| 59 | data.ChosenRepo = p.Repo |
| 60 | data.ByName = false |
| 61 | data.OtherLabel = "" |
| 62 | raw, _ := json.Marshal(data) |
| 63 | c.Placement = string(raw) |
| 64 | answered, _, err := t.answerQuestionSend(person, c, wire.AnswerCard{Choice: in.Text, Revision: c.Revision}, originComposer, in.IdempotencyKey, nil) |
| 65 | return wire.Sent{Message: answered.Message, Turn: answered.Turn}, err == nil, err |
| 66 | } |
| 67 | return ordinary() |
| 68 | } |
| 69 | |
| 70 | // Match the whole name first, then a small anchored phrase grammar. No word |
| 71 | // search or model extraction: extra requests and alternatives remain messages. |
| 72 | func (t *txn) composerProject(person, text string) (*projectRecord, error) { |
| 73 | p, err := t.namedProject(person, text) |
| 74 | if err != nil || p != nil { |
| 75 | return p, err |
| 76 | } |
| 77 | phrase := strings.ToLower(strings.TrimSpace(text)) |
| 78 | for _, prefix := range []string{"it should go in ", "put it in ", "use "} { |
| 79 | if name, ok := strings.CutPrefix(phrase, prefix); ok { |
| 80 | name = strings.TrimSpace(strings.TrimSuffix(name, " please")) |
| 81 | return t.namedProject(person, name) |
| 82 | } |
| 83 | } |
| 84 | return nil, nil |
| 85 | } |
| 86 | |