| 1 | package conversation |
| 2 | |
| 3 | import ( |
| 4 | "amadan.net/oficina/equipo/internal/equipo/team" |
| 5 | "gorm.io/gorm" |
| 6 | ) |
| 7 | |
| 8 | // RevokeConnectionAt runs in the connection mutation's existing transaction. |
| 9 | // A pending containment remains pending until the runner supplies its proof. |
| 10 | func (s *Application) RevokeConnectionAt(tx *gorm.DB, a Actor, id string) error { |
| 11 | if _, err := s.Conversation(tx).deps.Authorize(tx, a); err != nil { |
| 12 | return err |
| 13 | } |
| 14 | var bindings []BindingRecord |
| 15 | if err := tx.Where("person=? AND connection_id=?", a.Address, id).Find(&bindings).Error; err != nil { |
| 16 | return err |
| 17 | } |
| 18 | for _, b := range bindings { |
| 19 | var turns []TurnRecord |
| 20 | if err := tx.Where("binding_id=? AND state NOT IN ('superseded','fenced')", b.ID).Find(&turns).Error; err != nil { |
| 21 | return err |
| 22 | } |
| 23 | for _, turn := range turns { |
| 24 | if turn.JobID > 0 { |
| 25 | if s.Core == nil { |
| 26 | return s.Unavailable |
| 27 | } |
| 28 | scope := team.Scope{Account: team.AccountID(b.TeamID), Job: team.JobID(turn.JobID)} |
| 29 | job, err := s.Core.GetJob(tx.Statement.Context, scope) |
| 30 | if err != nil { |
| 31 | return err |
| 32 | } |
| 33 | if !job.State.Terminal() { |
| 34 | if _, err = s.Core.Cancel(tx.Statement.Context, scope, a.Address, "connection.revoke"); err != nil { |
| 35 | return err |
| 36 | } |
| 37 | } |
| 38 | } |
| 39 | if turn.State == "delivered" { |
| 40 | continue |
| 41 | } |
| 42 | state := "waiting_connection" |
| 43 | if turn.AttemptID > 0 { |
| 44 | state = "uncertain" |
| 45 | } |
| 46 | if err := tx.Model(&TurnRecord{}).Where("id=?", turn.ID).Select("state", "revision").Updates(map[string]any{"state": state, "revision": gorm.Expr("revision+1")}).Error; err != nil { |
| 47 | return err |
| 48 | } |
| 49 | } |
| 50 | if err := s.Audit(tx, a, "coordinator.connection_revoked", b.ID); err != nil { |
| 51 | return err |
| 52 | } |
| 53 | } |
| 54 | return nil |
| 55 | } |
| 56 | |