Skip to content
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
feat(gcal): read video links and declined invites, add an events action
  • Loading branch information
zakiego committed Nov 24, 2026
commit c39a2da93545819b4240bf9f1a9fbf601fe49e72
49 changes: 48 additions & 1 deletion apps/server/internal/connectors/gcal/calendar.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ type Event struct {
Location string
Status string
Link string
// MeetLink is the video call's join URL (Meet or another conference
// added to the event); "" when there is none.
MeetLink string
// Declined: the owner said no to this invite.
Declined bool
}

// JSON is the CalendarEvent object.
Expand All @@ -45,6 +50,12 @@ func (e Event) JSON() *jsonx.Object {
if e.Link != "" {
o.Set("link", e.Link)
}
if e.MeetLink != "" {
o.Set("meetLink", e.MeetLink)
}
if e.Declined {
o.Set("declined", true)
}
return o
}

Expand All @@ -56,6 +67,42 @@ type rawEvent struct {
HTMLLink string `json:"htmlLink"`
Start *evTime `json:"start"`
End *evTime `json:"end"`
// HangoutLink is the Meet URL; ConferenceData covers other providers.
HangoutLink string `json:"hangoutLink"`
ConferenceData *struct {
EntryPoints []struct {
EntryPointType string `json:"entryPointType"`
URI string `json:"uri"`
} `json:"entryPoints"`
} `json:"conferenceData"`
Attendees []struct {
Self bool `json:"self"`
ResponseStatus string `json:"responseStatus"`
} `json:"attendees"`
}

// meetLink is the event's video entry point, if any.
func (e rawEvent) meetLink() string {
if e.HangoutLink != "" {
return e.HangoutLink
}
if e.ConferenceData != nil {
for _, p := range e.ConferenceData.EntryPoints {
if p.EntryPointType == "video" && p.URI != "" {
return p.URI
}
}
}
return ""
}

func (e rawEvent) declined() bool {
for _, a := range e.Attendees {
if a.Self && a.ResponseStatus == "declined" {
return true
}
}
return false
}

type evTime struct {
Expand Down Expand Up @@ -141,7 +188,7 @@ func ListEvents(ctx context.Context, in ListEventsInput, hc *http.Client) ([]Eve
}
return t.Date
}
out = append(out, Event{ID: e.ID, CalendarID: in.CalendarID, Title: title, Start: pick(e.Start), End: pick(e.End), AllDay: e.Start == nil || e.Start.DateTime == "", Location: e.Location, Status: e.Status, Link: e.HTMLLink})
out = append(out, Event{ID: e.ID, CalendarID: in.CalendarID, Title: title, Start: pick(e.Start), End: pick(e.End), AllDay: e.Start == nil || e.Start.DateTime == "", Location: e.Location, Status: e.Status, Link: e.HTMLLink, MeetLink: e.meetLink(), Declined: e.declined()})
}
return out, nil
}
Expand Down
62 changes: 62 additions & 0 deletions apps/server/internal/connectors/gcal/gcal.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package gcal

import (
"context"
"encoding/json"
"errors"
"net/http"
"regexp"
Expand Down Expand Up @@ -220,6 +221,9 @@ func New(opts Options) *connectors.Runtime {
return kit.O("email", email, "calendars", ids)
},
Action: func(ctx context.Context, name string, input *jsonx.Object) (*jsonx.Object, error) {
if name == EventsAction {
return eventsAction(ctx, tokens, cfg, input, opts.Client, clock)
}
if name != "authUrl" {
return nil, errors.New("Unknown action " + name)
}
Expand All @@ -246,3 +250,61 @@ func New(opts Options) *connectors.Runtime {

// Runtime is the gcal connector runtime.
var Runtime = New(Options{})

// EventsAction is the server-side read the proactive engine makes: every
// event (with its id, video link and whether the owner declined) of every
// picked calendar between "from" and "to" (Unix ms). Not a model tool: the
// model reads the calendar through get_calendar.
const EventsAction = "events"

func eventsAction(ctx context.Context, tokens *TokenCache, cfg Config, input *jsonx.Object, hc *http.Client, clock func() int64) (*jsonx.Object, error) {
num := func(k string) (int64, bool) {
if input == nil {
return 0, false
}
v, ok := input.Get(k)
if !ok {
return 0, false
}
switch n := v.(type) {
case float64:
return int64(n), true
case int64:
return n, true
case int:
return int64(n), true
case json.Number:
i, err := n.Int64()
return i, err == nil
}
return 0, false
}
from, ok1 := num("from")
to, ok2 := num("to")
if !ok1 || !ok2 || to <= from {
return nil, errors.New("events needs from and to (Unix ms), to after from")
}
token, err := tokens.Get(ctx, clock())
if err != nil {
return nil, errors.New(explain(err))
}
ids := cfg.CalendarIDs
if len(ids) == 0 {
ids = []string{"primary"}
}
list := []any{}
for _, id := range ids {
evs, err := ListEvents(ctx, ListEventsInput{AccessToken: token, CalendarID: id, TimeMin: UTCISO(from), TimeMax: UTCISO(to), MaxResults: 100}, hc)
if err != nil {
var ge *GoogleError
if errors.As(err, &ge) && ge.Status == 401 {
tokens.Clear()
}
return nil, errors.New(explain(err))
}
for _, e := range evs {
list = append(list, e.JSON())
}
}
return kit.O("events", list), nil
}
24 changes: 24 additions & 0 deletions apps/server/internal/connectors/gcal/gcal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -493,3 +493,27 @@ func TestActionAuthURLUsesAppURLAndSignsTheStateForThisConnector(t *testing.T) {
t.Fatal(nonce)
}
}

func TestEventsActionReturnsIDsVideoLinksAndSkipsNothingTheEngineNeeds(t *testing.T) {
g := &google{events: []any{
map[string]any{"id": "m1", "summary": "Call", "start": map[string]any{"dateTime": "2026-11-10T13:00:00+08:00"}, "end": map[string]any{"dateTime": "2026-11-10T14:00:00+08:00"}, "hangoutLink": "https://meet.google.com/abc-defg-hij"},
map[string]any{"id": "z1", "summary": "Zoom", "start": map[string]any{"dateTime": "2026-11-10T15:00:00+08:00"}, "end": map[string]any{"dateTime": "2026-11-10T16:00:00+08:00"}, "conferenceData": map[string]any{"entryPoints": []any{map[string]any{"entryPointType": "phone", "uri": "tel:+1"}, map[string]any{"entryPointType": "video", "uri": "https://zoom.us/j/1"}}}},
map[string]any{"id": "d1", "summary": "Declined", "start": map[string]any{"dateTime": "2026-11-10T17:00:00+08:00"}, "end": map[string]any{"dateTime": "2026-11-10T18:00:00+08:00"}, "attendees": []any{map[string]any{"self": true, "responseStatus": "declined"}}},
}}
running, err := gcal.New(gcal.Options{Client: g.client(), Clock: func() int64 { return NOW }}).Start(conntest.Ctx(t, nil, "gcal", cfg(t), with(map[string]string{"refreshToken": "1//r"})))
testutil.Must(t, err)
in := jsonx.NewMap[any]()
in.Set("from", NOW)
in.Set("to", NOW+24*3600_000)
out, err := running.Action(context.Background(), gcal.EventsAction, in)
testutil.Must(t, err)
got := jsonx.Stringify(out)
for _, want := range []string{`"id":"m1"`, `"meetLink":"https://meet.google.com/abc-defg-hij"`, `"meetLink":"https://zoom.us/j/1"`, `"id":"d1"`, `"declined":true`} {
if !strings.Contains(got, want) {
t.Errorf("missing %s in %s", want, got)
}
}
if _, err := running.Action(context.Background(), gcal.EventsAction, jsonx.NewMap[any]()); err == nil {
t.Fatal("events needs a range")
}
}