| 1 | package calls |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "database/sql" |
| 6 | "encoding/json" |
| 7 | "strconv" |
| 8 | "strings" |
| 9 | "time" |
| 10 | |
| 11 | access "amadan.net/oficina/agent-access.git" |
| 12 | "github.com/google/uuid" |
| 13 | "gorm.io/gorm" |
| 14 | ) |
| 15 | |
| 16 | type delegatedCreateInput struct { |
| 17 | Team string `json:"team"` |
| 18 | ClientID string `json:"client_id"` |
| 19 | Title string `json:"title"` |
| 20 | StartsAt string `json:"starts_at"` |
| 21 | } |
| 22 | |
| 23 | func (a *app) delegatedCreate(ctx context.Context, p access.Principal, person Person, raw json.RawMessage) (any, error) { |
| 24 | var in delegatedCreateInput |
| 25 | if json.Unmarshal(raw, &in) != nil { |
| 26 | return nil, access.Fail("invalid", "Invalid meeting input.") |
| 27 | } |
| 28 | id, err := uuid.Parse(in.ClientID) |
| 29 | if err != nil || id == uuid.Nil || id.String() != in.ClientID { |
| 30 | return nil, access.Fail("invalid", "Supply a nonzero UUID client_id.") |
| 31 | } |
| 32 | if strings.TrimSpace(in.Title) == "" || titleTooLong(in.Title) { |
| 33 | return nil, access.Fail("invalid", "Supply a meeting title within the length limit.") |
| 34 | } |
| 35 | var starts *time.Time |
| 36 | if in.StartsAt != "" { |
| 37 | v, err := time.Parse(time.RFC3339, in.StartsAt) |
| 38 | if err != nil { |
| 39 | return nil, access.Fail("invalid", "Supply starts_at as RFC3339 with an explicit offset.") |
| 40 | } |
| 41 | v = v.UTC() |
| 42 | starts = &v |
| 43 | } |
| 44 | in.ClientID = id.String() |
| 45 | coordinates, _ := json.Marshal([]string{a.cfg.Home.Issuer, a.agentIssuer, p.Subject, p.Client, in.Team, in.ClientID}) |
| 46 | payload, _ := json.Marshal(in) |
| 47 | key, err := a.sealer.Fingerprint("connection.receipt.v1", string(coordinates)) |
| 48 | if err != nil { |
| 49 | return nil, err |
| 50 | } |
| 51 | content, err := a.sealer.Fingerprint("connection.content.v1", string(payload)) |
| 52 | if err != nil { |
| 53 | return nil, err |
| 54 | } |
| 55 | var meeting Meeting |
| 56 | err = a.store.g.WithContext(ctx).Transaction(func(tx *gorm.DB) error { |
| 57 | if e := a.bindDelegated(tx); e != nil { |
| 58 | return e |
| 59 | } |
| 60 | if e := a.checkDelegatedWriter(ctx, tx, p, person); e != nil { |
| 61 | return e |
| 62 | } |
| 63 | if err := tx.Exec("INSERT INTO calls_agent_meetings(coordinate,content) VALUES(?,?) ON CONFLICT(coordinate) DO NOTHING", key, content).Error; err != nil { |
| 64 | return err |
| 65 | } |
| 66 | current := *a.store |
| 67 | current.g = tx |
| 68 | operator, ok, err := current.personByID(ctx, person.ID) |
| 69 | if err != nil { |
| 70 | return err |
| 71 | } |
| 72 | if !ok || operator.Email != person.Email { |
| 73 | return access.Fail("forbidden", "This account cannot access Calls.") |
| 74 | } |
| 75 | if err := a.delegatedStanding(ctx, p.Subject, operator, false); err != nil { |
| 76 | return err |
| 77 | } |
| 78 | var saved string |
| 79 | var mid sql.NullInt64 |
| 80 | if err = tx.Raw("SELECT content,meeting_id FROM calls_agent_meetings WHERE coordinate=?", key).Row().Scan(&saved, &mid); err != nil { |
| 81 | return err |
| 82 | } |
| 83 | if saved != content { |
| 84 | return access.Fail("conflict", "This client_id was already used with different meeting fields.") |
| 85 | } |
| 86 | if mid.Valid { |
| 87 | if err = tx.Where("id=?", mid.Int64).Take(&meeting).Error; err == gorm.ErrRecordNotFound { |
| 88 | return access.Fail("not_found", "This meeting is unavailable.") |
| 89 | } |
| 90 | if err != nil { |
| 91 | return err |
| 92 | } |
| 93 | return a.checkDelegatedWriter(ctx, tx, p, person) |
| 94 | } |
| 95 | actor, err := current.delegatedActor(ctx, p, a.cfg.Home.Issuer, a.agentIssuer, in.Team, operator) |
| 96 | if err != nil { |
| 97 | return err |
| 98 | } |
| 99 | meeting, _, err = current.createMeeting(ctx, strings.TrimSpace(in.Title), starts, actor) |
| 100 | if err != nil { |
| 101 | return err |
| 102 | } |
| 103 | if e := tx.Exec("UPDATE calls_agent_meetings SET meeting_id=? WHERE coordinate=?", meeting.ID, key).Error; e != nil { |
| 104 | return e |
| 105 | } |
| 106 | return a.checkDelegatedWriter(ctx, tx, p, person) |
| 107 | }) |
| 108 | if err != nil { |
| 109 | return nil, err |
| 110 | } |
| 111 | captureDelegatedMeeting(ctx, meeting) |
| 112 | return map[string]any{"sqid": meeting.Sqid, "url": a.cfg.Origin + "/m/" + meeting.Sqid, "invitations_sent": false}, nil |
| 113 | } |
| 114 | func (s *store) delegatedActor(ctx context.Context, p access.Principal, homeIssuer, issuer, team string, operator Person) (string, error) { |
| 115 | raw, _ := json.Marshal([]string{homeIssuer, issuer, p.Subject, p.Client, team, strconv.FormatInt(operator.ID, 10)}) |
| 116 | key, err := s.sealer.Fingerprint("connection.actor.v1", string(raw)) |
| 117 | if err != nil { |
| 118 | return "", err |
| 119 | } |
| 120 | label, err := s.sealer.SealString("connection.client.v1", p.Client) |
| 121 | if err != nil { |
| 122 | return "", err |
| 123 | } |
| 124 | err = s.g.WithContext(ctx).Exec("INSERT INTO calls_connections(coordinate,operator_person_id,client_sealed) VALUES(?,?,?) ON CONFLICT(coordinate) DO NOTHING", key, operator.ID, label).Error |
| 125 | return "connection:" + key, err |
| 126 | } |
| 127 | func (s *store) delegatedCreator(ctx context.Context, key string) string { |
| 128 | var operatorID int64 |
| 129 | var sealed []byte |
| 130 | if err := s.g.WithContext(ctx).Raw("SELECT operator_person_id,client_sealed FROM calls_connections WHERE coordinate=?", key).Row().Scan(&operatorID, &sealed); err != nil { |
| 131 | return "Oficina agent" |
| 132 | } |
| 133 | label, err := s.sealer.OpenString("connection.client.v1", sealed) |
| 134 | if err != nil { |
| 135 | return "Oficina agent" |
| 136 | } |
| 137 | operator, ok, err := s.personByID(ctx, operatorID) |
| 138 | if err != nil || !ok { |
| 139 | return "Oficina agent" |
| 140 | } |
| 141 | return label + "~" + operator.Email |
| 142 | } |
| 143 | |