| 1 | package calls |
| 2 | |
| 3 | import ( |
| 4 | access "amadan.net/oficina/agent-access.git" |
| 5 | "amadan.net/oficina/home/roster" |
| 6 | "amadan.net/oficina/home/teammode" |
| 7 | "bytes" |
| 8 | "context" |
| 9 | "encoding/json" |
| 10 | "errors" |
| 11 | "gorm.io/gorm" |
| 12 | ) |
| 13 | |
| 14 | type delegatedResponseKey struct{} |
| 15 | type delegatedResponseState struct { |
| 16 | person Person |
| 17 | selected bool |
| 18 | meetings []Meeting |
| 19 | invitation *MeetingLink |
| 20 | standing *teammode.Access |
| 21 | subject string |
| 22 | host *teammode.Access |
| 23 | } |
| 24 | |
| 25 | func captureDelegatedMeeting(ctx context.Context, m Meeting) { |
| 26 | if s, ok := ctx.Value(delegatedResponseKey{}).(*delegatedResponseState); ok { |
| 27 | s.meetings = append(s.meetings, m) |
| 28 | } |
| 29 | } |
| 30 | func (a *app) delegatedResponse(ctx context.Context, p access.Principal, name string, _ json.RawMessage, result any) error { |
| 31 | s, ok := ctx.Value(delegatedResponseKey{}).(*delegatedResponseState) |
| 32 | if !ok || s.person.ID == 0 || name != "" && !s.selected { |
| 33 | return access.Fail("forbidden", "Response authority is unavailable.") |
| 34 | } |
| 35 | if name != "" { |
| 36 | value, ok := result.(map[string]any) |
| 37 | if !ok { |
| 38 | return access.Fail("forbidden", "Meeting provenance is unavailable.") |
| 39 | } |
| 40 | if name == "calls_reserve_schedule" { |
| 41 | if value["reserved"] != true || len(s.meetings) != 0 { |
| 42 | return access.Fail("forbidden", "Schedule provenance is unavailable.") |
| 43 | } |
| 44 | } else if name == "calls_list_meetings" { |
| 45 | rows, ok := value["meetings"].([]any) |
| 46 | if !ok || len(rows) != len(s.meetings) { |
| 47 | return access.Fail("forbidden", "Meeting provenance is unavailable.") |
| 48 | } |
| 49 | } else if len(s.meetings) != 1 { |
| 50 | return access.Fail("forbidden", "Meeting provenance is unavailable.") |
| 51 | } |
| 52 | } |
| 53 | if _, err := a.delegatedPerson(ctx, p, a.agentTeam); err != nil { |
| 54 | return err |
| 55 | } |
| 56 | for _, selected := range s.meetings { |
| 57 | m, ok, err := a.store.meetingBySqid(ctx, selected.Sqid) |
| 58 | if err != nil { |
| 59 | return delegatedFailure(err) |
| 60 | } |
| 61 | if !ok || m.ID != selected.ID { |
| 62 | return access.Fail("not_found", "This meeting is unavailable.") |
| 63 | } |
| 64 | } |
| 65 | if name == "calls_get_join_link" && s.invitation == nil { |
| 66 | return access.Fail("forbidden", "Invitation authority is unavailable.") |
| 67 | } |
| 68 | if selected := s.invitation; selected != nil { |
| 69 | if a.cfg.Team != nil && (s.host == nil || s.host.State() != roster.Member) { |
| 70 | return access.Fail("unavailable", "Invitation authority is unavailable.") |
| 71 | } |
| 72 | var link MeetingLink |
| 73 | err := a.store.g.WithContext(ctx).Table("meeting_links").Select("meeting_links.*").Joins("JOIN meetings ON meetings.id=meeting_links.meeting_id").Where("meeting_links.id=? AND meeting_links.meeting_id=? AND meeting_links.revoked_at IS NULL AND meetings.ended_at IS NULL", selected.ID, selected.MeetingID).Take(&link).Error |
| 74 | if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) { |
| 75 | return delegatedFailure(err) |
| 76 | } |
| 77 | if err != nil || !bytes.Equal(link.TokenSealed, selected.TokenSealed) { |
| 78 | return access.Fail("not_found", "This invitation is unavailable.") |
| 79 | } |
| 80 | } |
| 81 | if s.invitation != nil && a.cfg.Team != nil && (s.host == nil || s.host.State() != roster.Member) { |
| 82 | return access.Fail("unavailable", "Invitation authority is unavailable.") |
| 83 | } |
| 84 | return a.delegatedStanding(ctx, p.Subject, s.person, false) |
| 85 | } |
| 86 | |