oficina / equipo Public

Clone
git clone https://amadan.net/oficina/equipo

Plain git — no account needed to clone.

Download

Download this file

1package chat
2
3import (
4 "encoding/json"
5 "fmt"
6 "strings"
7 "unicode"
8
9 "amadan.net/oficina/equipo/internal/wire"
10)
11
12const originComposer = "composer"
13const originGenerated = "generated"
14
15// Origin is assigned by the ingress, never by a model or submitted field.
16// Legacy messages have no origin and cannot grant repository creation.
17func (t *txn) createConsent(r turnRecord, repo string) (bool, error) {
18 c, seq, found, err := t.answeredPlacement(r)
19 if err != nil {
20 return false, err
21 }
22 if !found {
23 var messages []messageRecord
24 if err := t.db.Where("person = ? AND turn_id = ? AND role = ?", r.Person, r.ID, wire.RolePerson).Order("seq DESC").Limit(1).Find(&messages).Error; err != nil {
25 return false, err
26 }
27 for _, m := range messages {
28 if m.Origin == originComposer && m.Lane == wire.LaneCoordinator && explicitNewProjectFor(m.Text, repo) {
29 return true, nil
30 }
31 }
32 return false, nil
33 }
34 var p placementData
35 if json.Unmarshal([]byte(c.Placement), &p) != nil || p.NewRepo != repo || c.Choice != p.NewLabel || p.NewLabel != fmt.Sprintf(t.s.authority.Copy("placement.new_named"), ProjectName(repo)) {
36 return false, nil
37 }
38 chosen, err := t.placementChoice(r, c, p, seq)
39 if err != nil || chosen != repo {
40 return false, err
41 }
42 if p.MoveTask != "" {
43 var k taskRecord
44 if err := t.db.First(&k, "id = ?", p.MoveTask).Error; err != nil {
45 return false, err
46 }
47 if k.MoveProject != "new:"+repo || k.MoveBy != r.Person {
48 return false, nil
49 }
50 }
51 return true, nil
52}
53
54func (t *txn) answeredPlacement(r turnRecord) (cardRecord, int64, bool, error) {
55 var rows []struct {
56 Card cardRecord `gorm:"embedded"`
57 AnswerSeq int64
58 }
59 err := t.db.Table("chat_cards AS c").Select("c.*, m.seq AS answer_seq").Joins("JOIN chat_messages AS m ON m.id = c.answer_message AND m.person = c.person").Where("c.person = ? AND m.turn_id = ? AND c.state = ? AND coalesce(c.lane, '') = ? AND coalesce(c.placement, '') <> ''", r.Person, r.ID, wire.CardAnswered, r.Lane).Order("m.seq DESC").Limit(1).Scan(&rows).Error
60 if err != nil || len(rows) == 0 {
61 return cardRecord{}, 0, false, err
62 }
63 return rows[0].Card, rows[0].AnswerSeq, true, nil
64}
65
66func uncertainPlacement(text string) bool {
67 if strings.ContainsAny(text, "?\n\r\"'`“”‘’;:,") {
68 return true
69 }
70 words := strings.FieldsFunc(strings.ToLower(text), func(r rune) bool {
71 return !unicode.IsLetter(r) && !unicode.IsNumber(r) && r != '/' && r != '-' && r != '_'
72 })
73 for _, w := range words {
74 switch w {
75 case "if", "unless", "when", "whenever", "once", "after", "before", "until", "later", "tomorrow", "today", "tonight", "eventually", "someday", "soon", "next", "first", "then", "maybe", "perhaps", "possibly", "probably", "might", "may", "could", "should", "would", "consider", "considering", "think", "guess", "hope", "prefer", "rather", "tentatively", "ideally", "potentially", "conditionally", "provided", "providing", "assuming", "subject", "pending", "only", "not", "no", "never", "wait", "hold", "stop", "actually", "instead", "otherwise", "or", "but", "although", "though", "except", "without", "whether", "depends", "depending", "as", "is", "was", "future", "sometime", "preferably", "on", "at", "scheduled", "schedule", "defer", "postpone", "pause", "cancel", "scratch", "retract", "withdraw", "forget", "correction", "alternatively", "supposing", "suppose", "contingent", "optional", "optionally", "tentative":
76 return true
77 }
78 }
79 return false
80}
81
82func explicitNewProject(text string) bool {
83 text = strings.ToLower(strings.TrimSpace(text))
84 if uncertainPlacement(text) {
85 return false
86 }
87 for _, prefix := range []string{"create a new project", "make a new project", "start a new project", "create a new repository", "make a new repository", "please create a new project", "please create a new repository"} {
88 tail, ok := strings.CutPrefix(text, prefix)
89 if !ok {
90 continue
91 }
92 if tail == "" || tail == "." || tail == "!" {
93 return true
94 }
95 for _, join := range []string{" for ", " called ", " named ", " in ", " with "} {
96 if strings.HasPrefix(tail, join) && strings.TrimSpace(strings.TrimPrefix(tail, join)) != "" {
97 return true
98 }
99 }
100 }
101 return false
102}
103
104func explicitNewProjectFor(text, repo string) bool {
105 if !explicitNewProject(text) {
106 return false
107 }
108 text = strings.ToLower(strings.TrimSuffix(strings.TrimSpace(text), "."))
109 for _, separator := range []string{" called ", " named "} {
110 if _, name, ok := strings.Cut(text, separator); ok {
111 return name == ProjectName(repo) || name == strings.TrimPrefix(repo, "amadan.net/") || name == repo || name == repo[strings.LastIndex(repo, "/")+1:]
112 }
113 }
114 return true
115}
116
117func typedProjectChoice(text string, repos []string) string {
118 text = strings.ToLower(strings.TrimSuffix(strings.TrimSpace(text), "."))
119 for _, prefix := range []string{"no wait, ", "actually, ", "instead, ", "no, ", "wait, "} {
120 text = strings.TrimPrefix(text, prefix)
121 }
122 for _, prefix := range []string{"use ", "put it in ", "add to ", "add it to ", "move it to ", "please use "} {
123 name, ok := strings.CutPrefix(text, prefix)
124 if !ok {
125 continue
126 }
127 chosen := ""
128 for _, repo := range repos {
129 if name == repo || name == ProjectName(repo) || name == repo[strings.LastIndex(repo, "/")+1:] {
130 if chosen != "" {
131 return ""
132 }
133 chosen = repo
134 }
135 }
136 return chosen
137 }
138 return ""
139}
140
141func withdrawsPlacement(text string) bool {
142 text = strings.ToLower(text)
143 for _, phrase := range []string{"no wait", "wait", "don't", "don’t", "do not", "cancel", "never mind", "scratch", "forget", "instead", "actually"} {
144 if strings.Contains(text, phrase) {
145 return true
146 }
147 }
148 if uncertainPlacement(text) {
149 for _, phrase := range []string{"project", "repository", "put it in", "add it to", "use ", "move it to"} {
150 if strings.Contains(text, phrase) {
151 return true
152 }
153 }
154 }
155 return false
156}
157