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 "context"
5 "encoding/json"
6 "errors"
7 "fmt"
8 "time"
9
10 "amadan.net/oficina/equipo/internal/copydesk"
11 "amadan.net/oficina/equipo/internal/wire"
12 "gorm.io/gorm"
13)
14
15type copyReviewRecord struct {
16 ID string `gorm:"primaryKey"`
17 ProjectID string
18 TaskID string `gorm:"index:idx_copy_reviews_task"`
19 StepID string `gorm:"index:idx_copy_reviews_attempt,priority:1"`
20 Attempt int64 `gorm:"index:idx_copy_reviews_attempt,priority:2"`
21 Owner string
22 Delegation int64
23 Candidate string
24 Input string
25 Key string
26 Text string
27 Hash string
28 Flag string
29 CreatedAt time.Time
30}
31
32func (copyReviewRecord) TableName() string { return "chat_copy_reviews" }
33
34type copyFeedbackRecord struct {
35 Learned bool
36 ID string `gorm:"primaryKey"`
37 ProjectID string
38 Owner string
39 SourceID string
40 Key string
41 Before string
42 After string
43 Reason string
44 Origin string
45 CreatedAt time.Time
46}
47
48func (copyFeedbackRecord) TableName() string { return "chat_copy_feedback" }
49
50func (t *txn) copyDelegation(project string) (copydesk.Delegation, error) {
51 var p projectRecord
52 if err := t.db.Where("id = ?", project).First(&p).Error; err != nil {
53 return copydesk.Delegation{}, err
54 }
55 if p.CopyOwner == "" || p.State != wire.ProjectActive {
56 return copydesk.Delegation{}, nil
57 }
58 if err := t.isOwner(p.CopyOwner); err != nil {
59 if errors.Is(err, ErrForbidden) {
60 return copydesk.Delegation{}, nil
61 }
62 return copydesk.Delegation{}, err
63 }
64 return copydesk.Delegation{Owner: p.CopyOwner, Revision: p.CopyRevision}, nil
65}
66func (s *Store) SetCopyDelegation(ctx context.Context, person, project string, enabled bool, revision int64) error {
67 return s.tx(ctx, func(t *txn) error {
68 if err := s.authority.CurrentPerson(ctx, t.db); err != nil {
69 return err
70 }
71 if err := t.isOwner(person); err != nil {
72 return err
73 }
74 var p projectRecord
75 if err := t.db.Where("id = ? AND state = ?", project, wire.ProjectActive).First(&p).Error; err != nil {
76 return err
77 }
78 if p.CopyRevision != revision {
79 return ErrCardConflict
80 }
81 p.CopyOwner = ""
82 if enabled {
83 p.CopyOwner = person
84 }
85 p.CopyRevision++
86 p.Revision++
87 p.UpdatedAt = t.now()
88 if err := t.db.Model(&projectRecord{}).Where("id = ?", p.ID).Select("copy_owner", "copy_revision", "revision", "updated_at").Updates(&p).Error; err != nil {
89 return err
90 }
91 if err := s.authority.Audit(t.db, person, "copy.delegation", p.ID); err != nil {
92 return err
93 }
94 if err := t.withdrawProjectCopy(project); err != nil {
95 return err
96 }
97 return t.projectEvent(p)
98 })
99}
100
101type CopyImport struct {
102 originals map[string]string
103 Version int `json:"version"`
104 SourceID string `json:"source_id"`
105 Strings []copydesk.Feedback `json:"strings"`
106}
107
108func (s *Store) ImportCopyReview(ctx context.Context, person, project string, in CopyImport) error {
109 if in.Version != 1 || in.SourceID == "" || len(in.SourceID) > 200 || len(in.Strings) == 0 || len(in.Strings) > 40 {
110 return ErrInvalid
111 }
112 return s.tx(ctx, func(t *txn) error {
113 if err := s.authority.CurrentPerson(ctx, t.db); err != nil {
114 return err
115 }
116 d, err := t.copyDelegation(project)
117 if err != nil {
118 return err
119 }
120 if d.Owner != person {
121 return ErrForbidden
122 }
123 for _, f := range in.Strings {
124 if _, legacy := in.originals[f.Key]; legacy && len(f.Before)+len(f.After)+len(f.Reason) > 400 {
125 continue
126 }
127 f.Actor, f.SourceID, f.Origin = person, in.SourceID, "copy-review"
128 if !copydesk.CanLearn(d, f) {
129 return ErrInvalid
130 }
131 var n int64
132 if err := t.db.Model(&copyReviewRecord{}).Where("project_id = ? AND owner = ? AND key = ? AND hash = ?", project, person, f.Key, copydesk.Hash(f.Before)).Count(&n).Error; err != nil {
133 return err
134 }
135 if original, imported := in.originals[f.Key]; imported {
136 if f.Before != original {
137 return ErrInvalid
138 }
139 } else if n == 0 {
140 return ErrInvalid
141 }
142 id := copydesk.Hash(project + "\x00" + person + "\x00" + in.SourceID + "\x00" + f.Key)
143 var old copyFeedbackRecord
144 err := t.db.Where("id = ?", id).First(&old).Error
145 if err == nil {
146 if old.Before != f.Before || old.After != f.After || old.Reason != f.Reason {
147 return ErrCardConflict
148 }
149 continue
150 }
151 if !errors.Is(err, gorm.ErrRecordNotFound) {
152 return err
153 }
154 if err := t.db.Create(&copyFeedbackRecord{ID: id, ProjectID: project, Owner: person, SourceID: in.SourceID, Key: f.Key, Before: f.Before, After: f.After, Reason: f.Reason, Origin: f.Origin, CreatedAt: t.now()}).Error; err != nil {
155 return err
156 }
157 }
158 return s.authority.Audit(t.db, person, "copy.import", project)
159 })
160}
161
162func (t *txn) copyAuthorized(k taskRecord, s stepRecord) error {
163 if wire.FinalStage(k.Stage) || k.CancelRequested || s.Withdrawn || k.MovedFrom != "" {
164 return ErrForbidden
165 }
166 steps, err := t.steps(k.ID)
167 if err != nil {
168 return err
169 }
170 progress := progressFor(k, steps)
171 if progress.candidate != s.CandidateSHA || progress.last == nil || progress.last.ID != s.ID {
172 return ErrForbidden
173 }
174 d, err := t.copyDelegation(k.ProjectID)
175 if err != nil {
176 return err
177 }
178 if d.Owner == "" || d.Owner != s.CopyOwner || d.Revision != s.CopyRevision {
179 return ErrForbidden
180 }
181 return nil
182}
183func (t *txn) copyDone(l Lease, k *taskRecord, s *stepRecord, v *wire.StepReport) error {
184 if l.Protocol < wire.ProtocolCopy || v.Copy == nil || !v.Termination || !v.Pushed {
185 return ErrRefused
186 }
187 if err := t.copyAuthorized(*k, *s); err != nil {
188 return err
189 }
190 c := v.Copy
191 if copyRecovery(*s) {
192 if c.Signature == nil || !c.Signature.CopyOnly() {
193 return ErrInvalid
194 }
195 k.BaseSHA = s.BaseSHA
196 }
197 if c.Owner != s.CopyOwner || c.Delegation != s.CopyRevision || c.Input != s.CandidateSHA || c.Extraction.Version != 1 || c.Tokens < 0 {
198 return ErrInvalid
199 }
200 words, err := copydesk.ValidateReview(c.Extraction.Strings, c.Review)
201 if err != nil {
202 return ErrInvalid
203 }
204 for _, w := range words {
205 if c.Suggestions {
206 continue
207 }
208 if w.Action != "accept" {
209 return ErrInvalid
210 }
211 id := copydesk.Hash(fmt.Sprintf("%s/%d/%s", s.ID, s.Attempt, w.Key))
212 row := copyReviewRecord{ID: id, ProjectID: k.ProjectID, TaskID: k.ID, StepID: s.ID, Attempt: s.Attempt, Owner: s.CopyOwner, Delegation: s.CopyRevision, Candidate: v.CandidateSHA, Input: c.Input, Key: w.Key, Text: w.Text, Hash: copydesk.Hash(w.Text), Flag: w.Flag, CreatedAt: t.now()}
213 if err := t.db.Create(&row).Error; err != nil {
214 return err
215 }
216 }
217 raw, _ := json.Marshal(c)
218 s.CopyResult = string(raw)
219 s.CandidateSHA = v.CandidateSHA
220 s.Pushed = true
221 return t.s.authority.Audit(t.db, "runner:"+l.Runner.RunnerID, "copy.accept", s.ID)
222}
223