| 1 | package calls |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/json" |
| 6 | "errors" |
| 7 | |
| 8 | access "amadan.net/oficina/agent-access.git" |
| 9 | "amadan.net/oficina/calls/internal/calls/seal" |
| 10 | "amadan.net/oficina/home/roster" |
| 11 | "gorm.io/gorm" |
| 12 | ) |
| 13 | |
| 14 | func (a *app) delegatedList(ctx context.Context, _ access.Principal, _ Person, raw json.RawMessage) (any, error) { |
| 15 | var in struct { |
| 16 | After int64 `json:"after"` |
| 17 | Limit int `json:"limit"` |
| 18 | } |
| 19 | if json.Unmarshal(raw, &in) != nil { |
| 20 | return nil, access.Fail("invalid", "Invalid pagination fields.") |
| 21 | } |
| 22 | if in.Limit == 0 { |
| 23 | in.Limit = 50 |
| 24 | } |
| 25 | var meetings []Meeting |
| 26 | if err := a.store.g.WithContext(ctx).Where("id>?", in.After).Order("id ASC").Limit(in.Limit + 1).Find(&meetings).Error; err != nil { |
| 27 | return nil, err |
| 28 | } |
| 29 | more := len(meetings) > in.Limit |
| 30 | if more { |
| 31 | meetings = meetings[:in.Limit] |
| 32 | } |
| 33 | out := []any{} |
| 34 | var next int64 |
| 35 | for _, m := range meetings { |
| 36 | body, err := a.delegatedMeeting(ctx, m) |
| 37 | if err != nil { |
| 38 | return nil, err |
| 39 | } |
| 40 | out = append(out, body) |
| 41 | next = m.ID |
| 42 | } |
| 43 | return map[string]any{"meetings": out, "next_after": next, "has_more": more}, nil |
| 44 | } |
| 45 | func (a *app) delegatedMeeting(ctx context.Context, m Meeting) (any, error) { |
| 46 | title, err := a.store.delegatedTitle(m) |
| 47 | if err != nil { |
| 48 | return nil, err |
| 49 | } |
| 50 | captureDelegatedMeeting(ctx, m) |
| 51 | return map[string]any{"sqid": m.Sqid, "title": title, "starts_at": apiTime(m.StartsAt), "ended_at": apiTime(m.EndedAt), "created_by": a.store.creatorLabel(ctx, m.CreatedBy), "url": a.cfg.Origin + "/m/" + m.Sqid}, nil |
| 52 | } |
| 53 | func (a *app) delegatedLookup(ctx context.Context, raw json.RawMessage) (Meeting, error) { |
| 54 | var in struct { |
| 55 | Sqid string `json:"sqid"` |
| 56 | } |
| 57 | _ = json.Unmarshal(raw, &in) |
| 58 | if !validSqid(in.Sqid) { |
| 59 | return Meeting{}, access.Fail("not_found", "This meeting is unavailable.") |
| 60 | } |
| 61 | m, ok, err := a.store.meetingBySqid(ctx, in.Sqid) |
| 62 | if err != nil { |
| 63 | return m, err |
| 64 | } |
| 65 | if !ok { |
| 66 | return m, access.Fail("not_found", "This meeting is unavailable.") |
| 67 | } |
| 68 | return m, nil |
| 69 | } |
| 70 | func (a *app) delegatedGet(ctx context.Context, _ access.Principal, _ Person, raw json.RawMessage) (any, error) { |
| 71 | m, err := a.delegatedLookup(ctx, raw) |
| 72 | if err != nil { |
| 73 | return nil, err |
| 74 | } |
| 75 | return a.delegatedMeeting(ctx, m) |
| 76 | } |
| 77 | func (a *app) delegatedJoinLink(ctx context.Context, _ access.Principal, _ Person, raw json.RawMessage) (any, error) { |
| 78 | m, err := a.delegatedLookup(ctx, raw) |
| 79 | if err != nil { |
| 80 | return nil, err |
| 81 | } |
| 82 | if a.cfg.Team != nil { |
| 83 | host, standing := a.meetingStands(ctx, m) |
| 84 | if standing == roster.Unknown { |
| 85 | return nil, access.Fail("unavailable", "Meeting host standing is unavailable.") |
| 86 | } |
| 87 | if standing != roster.Member { |
| 88 | return nil, access.Fail("not_found", "This invitation is unavailable.") |
| 89 | } |
| 90 | if s, ok := ctx.Value(delegatedResponseKey{}).(*delegatedResponseState); ok { |
| 91 | s.host = host |
| 92 | } |
| 93 | } |
| 94 | // One query rechecks the meeting and link together. Reading never mints or |
| 95 | // replaces a legacy hash-only invitation. An ended meeting has no live link. |
| 96 | var link MeetingLink |
| 97 | err = a.store.g.WithContext(ctx).Table("meeting_links").Select("meeting_links.*").Joins("JOIN meetings ON meetings.id=meeting_links.meeting_id").Where("meetings.id=? AND meetings.ended_at IS NULL AND meeting_links.revoked_at IS NULL AND length(meeting_links.token_sealed)>0", m.ID).Order("meeting_links.id DESC").Take(&link).Error |
| 98 | if errors.Is(err, gorm.ErrRecordNotFound) { |
| 99 | return nil, access.Fail("not_found", "No current shareable invitation is available.") |
| 100 | } |
| 101 | if err != nil { |
| 102 | return nil, err |
| 103 | } |
| 104 | token, err := a.sealer.OpenString(seal.PurposeMeetingLink, link.TokenSealed) |
| 105 | if err != nil { |
| 106 | return nil, err |
| 107 | } |
| 108 | captureDelegatedMeeting(ctx, m) |
| 109 | if state, ok := ctx.Value(delegatedResponseKey{}).(*delegatedResponseState); ok { |
| 110 | state.invitation = &link |
| 111 | } |
| 112 | return map[string]any{"sqid": m.Sqid, "join_url": a.linkURL(token)}, nil |
| 113 | } |
| 114 | |
| 115 | // delegatedTitle keeps content opening in the store and refuses corrupt ciphertext. |
| 116 | func (s *store) delegatedTitle(m Meeting) (string, error) { |
| 117 | return s.sealer.OpenString(seal.PurposeMeetingTitle, m.TitleSealed) |
| 118 | } |
| 119 | |