| 1 | package conversation |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "crypto/rand" |
| 6 | "encoding/json" |
| 7 | "errors" |
| 8 | "fmt" |
| 9 | "slices" |
| 10 | "strings" |
| 11 | |
| 12 | "amadan.net/oficina/equipo/internal/coordinator" |
| 13 | "amadan.net/oficina/equipo/internal/equipo/kernel" |
| 14 | "amadan.net/oficina/equipo/internal/equipo/setup" |
| 15 | "gorm.io/gorm" |
| 16 | ) |
| 17 | |
| 18 | type ProjectProposal struct { |
| 19 | ID string `gorm:"primaryKey"` |
| 20 | ConversationID, BindingID string |
| 21 | Person string `gorm:"index:project_proposal_person"` |
| 22 | Repository, Name, Why, State string |
| 23 | Generation, Revision, ProjectID int64 |
| 24 | } |
| 25 | |
| 26 | func (ProjectProposal) TableName() string { return "project_proposals" } |
| 27 | |
| 28 | func checkReported(tx *gorm.DB, person, repository string, invalid error) error { |
| 29 | canonical, err := setup.CanonicalRepository(repository, invalid) |
| 30 | if err != nil || canonical != repository { |
| 31 | return invalid |
| 32 | } |
| 33 | repos, err := setup.ReportedRepositories(tx, person) |
| 34 | if err != nil { |
| 35 | return err |
| 36 | } |
| 37 | if !slices.Contains(repos, repository) { |
| 38 | return invalid |
| 39 | } |
| 40 | return nil |
| 41 | } |
| 42 | func (s *Store) proposeProject(row TurnRecord, a coordinator.ProposeProject, heading string) (CoordinatorToolResult, error) { |
| 43 | var count int64 |
| 44 | if err := s.tx.Table("projects").Where("repository=?", a.Repository).Count(&count).Error; err != nil { |
| 45 | return CoordinatorToolResult{}, err |
| 46 | } |
| 47 | if count > 0 { |
| 48 | return CoordinatorToolResult{}, s.deps.Conflict |
| 49 | } |
| 50 | var pending []ProjectProposal |
| 51 | if err := s.tx.Where("person=? AND binding_id=? AND generation=? AND repository=? AND state='pending'", row.Person, row.BindingID, row.Generation, a.Repository).Find(&pending).Error; err != nil { |
| 52 | return CoordinatorToolResult{}, err |
| 53 | } |
| 54 | if len(pending) > 0 { |
| 55 | return result(pending[0].ID, pending[0].Revision, pending[0]) |
| 56 | } |
| 57 | p := ProjectProposal{ID: rand.Text(), ConversationID: row.ConversationID, BindingID: row.BindingID, Person: row.Person, Repository: a.Repository, Name: a.Repository[strings.LastIndex(a.Repository, "/")+1:], Why: a.Why, State: "pending", Generation: row.Generation, Revision: 1} |
| 58 | if err := s.tx.Create(&p).Error; err != nil { |
| 59 | return CoordinatorToolResult{}, err |
| 60 | } |
| 61 | msg, err := s.appendMessage(row.ConversationID, row.ConversationID, "", "coordinator", "coordinator:"+row.BindingID, fmt.Sprintf(heading, p.Name, p.Repository), "project_proposal") |
| 62 | if err != nil { |
| 63 | return CoordinatorToolResult{}, err |
| 64 | } |
| 65 | if err = s.tx.Create(&cardRecord{ID: msg.ID, StreamID: row.ConversationID, Kind: "project_setup", ResourceID: p.ID, Revision: 1, Sequence: msg.Sequence}).Error; err != nil { |
| 66 | return CoordinatorToolResult{}, err |
| 67 | } |
| 68 | return result(p.ID, 1, p) |
| 69 | } |
| 70 | func (s *Store) projectProposal(a Actor, id string) (ProjectProposal, error) { |
| 71 | var p ProjectProposal |
| 72 | _, err := s.deps.Authorize(s.tx, a) |
| 73 | if err != nil { |
| 74 | return p, err |
| 75 | } |
| 76 | err = s.tx.Where("id=? AND person=?", id, a.Address).First(&p).Error |
| 77 | if errors.Is(err, gorm.ErrRecordNotFound) { |
| 78 | return p, s.deps.NotFound |
| 79 | } |
| 80 | return p, err |
| 81 | } |
| 82 | func (s *Application) AnswerProjectProposal(ctx context.Context, a Actor, id string, accept bool, m Mutation) error { |
| 83 | a.Address = s.Normalize(a.Address) |
| 84 | return s.Write(ctx, func(tx *gorm.DB) error { |
| 85 | cs := s.Conversation(tx) |
| 86 | p, err := cs.projectProposal(a, id) |
| 87 | if err != nil { |
| 88 | return err |
| 89 | } |
| 90 | b, err := cs.Binding(a) |
| 91 | if err != nil { |
| 92 | return err |
| 93 | } |
| 94 | if b.ID != p.BindingID || b.Generation != p.Generation { |
| 95 | return s.Conflict |
| 96 | } |
| 97 | if s.Projects == nil { |
| 98 | return s.Unavailable |
| 99 | } |
| 100 | projects := s.Projects(tx) |
| 101 | if accept { |
| 102 | if err = projects.Owner(tx, a); err != nil { |
| 103 | return err |
| 104 | } |
| 105 | } |
| 106 | input := struct { |
| 107 | Accept bool |
| 108 | Revision int64 |
| 109 | }{accept, m.Revision} |
| 110 | var saved int64 |
| 111 | if ok, err := cs.CoordinatorReplay(a, "project_setup.answer", id, m, input, &saved); ok || err != nil { |
| 112 | return err |
| 113 | } |
| 114 | if p.State != "pending" || p.Revision != m.Revision { |
| 115 | return s.Conflict |
| 116 | } |
| 117 | if accept { |
| 118 | if err = checkReported(tx, a.Address, p.Repository, s.Invalid); err != nil { |
| 119 | return err |
| 120 | } |
| 121 | project, err := projects.SaveNewProject(tx.Statement.Context, a, kernel.Project{Repository: p.Repository}, nil, "proposal:"+p.ID) |
| 122 | if err != nil { |
| 123 | return err |
| 124 | } |
| 125 | p.ProjectID, p.State = project.ID, "created" |
| 126 | } else { |
| 127 | p.State = "declined" |
| 128 | } |
| 129 | p.Revision++ |
| 130 | if err = tx.Model(&ProjectProposal{}).Where("id=?", p.ID).Select("project_id", "state", "revision").Updates(&p).Error; err != nil { |
| 131 | return err |
| 132 | } |
| 133 | if err = tx.Model(&cardRecord{}).Where("resource_id=? AND kind='project_setup'", p.ID).Select("revision").Updates(map[string]any{"revision": p.Revision}).Error; err != nil { |
| 134 | return err |
| 135 | } |
| 136 | body := fmt.Sprintf(s.ProjectDeclined, p.Name, p.Repository) |
| 137 | if accept { |
| 138 | body = fmt.Sprintf(s.ProjectCreated, p.Name, p.Repository) |
| 139 | } |
| 140 | msg, err := cs.appendMessage(p.ConversationID, p.ConversationID, "", "person", a.Address, body, "coordinator") |
| 141 | if err != nil { |
| 142 | return err |
| 143 | } |
| 144 | if _, err = cs.QueueCoordinator(a, msg.ID, 0, Mutation{RequestID: "project_setup:" + id}); err != nil { |
| 145 | return err |
| 146 | } |
| 147 | if err = s.Audit(tx, a, "project_setup."+p.State, id); err != nil { |
| 148 | return err |
| 149 | } |
| 150 | return cs.SaveCoordinatorResult(a, "project_setup.answer", id, m, input, p.ProjectID) |
| 151 | }) |
| 152 | } |
| 153 | |
| 154 | func (e *Engine) setupContext(tx *gorm.DB, c CoordinatorClaim) (string, error) { |
| 155 | person := c.Binding.Person |
| 156 | repos, err := setup.ReportedRepositories(tx, person) |
| 157 | if err != nil { |
| 158 | return "", err |
| 159 | } |
| 160 | if len(repos) == 0 { |
| 161 | return "", nil |
| 162 | } |
| 163 | type project struct { |
| 164 | ID int64 |
| 165 | Name, Repository string |
| 166 | } |
| 167 | var projects []project |
| 168 | if err = tx.Table("projects").Select("id,name,repository").Order("id").Find(&projects).Error; err != nil { |
| 169 | return "", err |
| 170 | } |
| 171 | visible := []project{} |
| 172 | for _, p := range projects { |
| 173 | if _, err := e.CheckProject(tx, person, p.ID, c.Brief.Selection); err != nil { |
| 174 | if !errors.Is(err, e.Forbidden) && !errors.Is(err, e.NotFound) { |
| 175 | return "", err |
| 176 | } |
| 177 | repos = slices.DeleteFunc(repos, func(repo string) bool { return repo == p.Repository }) |
| 178 | continue |
| 179 | } |
| 180 | visible = append(visible, p) |
| 181 | } |
| 182 | raw, err := json.Marshal(struct { |
| 183 | ReportedRepositories []string |
| 184 | ExistingProjects []project |
| 185 | HasProjects bool |
| 186 | }{repos, visible, len(projects) > 0}) |
| 187 | return string(raw), err |
| 188 | } |
| 189 | |