From 930261335bf8c927f9c0eeede06bd64eb0c4843d Mon Sep 17 00:00:00 2001 From: MohammadHadi Attarieh Date: Wed, 28 Oct 2026 21:12:24 +0330 Subject: [PATCH] fix(microsoft): ignore free, cancelled and declined entries in free/busy Graph's calendarView returns every entry on the calendar; the request only $select-ed start,end, so each one counted as busy. Unlike Google's freeBusy (which applies transparency server-side) the filter has to be ours: - showAs "free" (reminders, subscribed holiday calendars, OOO notices from others) - one all-day "free" entry blocked a whole day, and in a collective that day for every other host too - isCancelled meetings still sitting on the calendar - invitations the owner declined Tentative, busy, out-of-office and working-elsewhere still block, matching Outlook's own scheduling assistant. CalDAV already skipped transparent and cancelled events. Co-Authored-By: Claude Opus 5.5 --- internal/calendar/microsoft/freebusy.go | 36 +++++++++++++-- internal/calendar/microsoft/microsoft_test.go | 44 +++++++++++++++++++ 2 files changed, 77 insertions(+), 3 deletions(-) diff --git a/internal/calendar/microsoft/freebusy.go b/internal/calendar/microsoft/freebusy.go index 2122ae18..7c8ff75b 100644 --- a/internal/calendar/microsoft/freebusy.go +++ b/internal/calendar/microsoft/freebusy.go @@ -6,6 +6,7 @@ import ( "fmt" "net/http" "net/url" + "strings" "time" "github.com/calnode/calnode/internal/slots" @@ -26,8 +27,13 @@ func (g graphDateTime) parse() (time.Time, error) { type calendarViewResp struct { Value []struct { - Start graphDateTime `json:"start"` - End graphDateTime `json:"end"` + Start graphDateTime `json:"start"` + End graphDateTime `json:"end"` + ShowAs string `json:"showAs"` + IsCancelled bool `json:"isCancelled"` + ResponseStatus *struct { + Response string `json:"response"` + } `json:"responseStatus"` } `json:"value"` NextLink string `json:"@odata.nextLink"` } @@ -73,7 +79,9 @@ func (c *Client) calendarView(ctx context.Context, hc *http.Client, base string, q := url.Values{} q.Set("startDateTime", from.UTC().Format(time.RFC3339)) q.Set("endDateTime", to.UTC().Format(time.RFC3339)) - q.Set("$select", "start,end") + // showAs/isCancelled/responseStatus decide whether an entry blocks time at all; see + // blocksTime. Graph omits any property not $select-ed, so they must be asked for. + q.Set("$select", "start,end,showAs,isCancelled,responseStatus") q.Set("$top", "200") next := base + "?" + q.Encode() @@ -101,6 +109,13 @@ func (c *Client) calendarView(ctx context.Context, hc *http.Client, base string, return nil, fmt.Errorf("microsoft: calendarView decode: %w", derr) } for _, ev := range cv.Value { + response := "" + if ev.ResponseStatus != nil { + response = ev.ResponseStatus.Response + } + if !blocksTime(ev.ShowAs, ev.IsCancelled, response) { + continue + } s, err1 := ev.Start.parse() e, err2 := ev.End.parse() if err1 != nil || err2 != nil { @@ -113,3 +128,18 @@ func (c *Client) calendarView(ctx context.Context, hc *http.Client, base string, } return out, nil } + +// blocksTime reports whether a calendarView entry makes its owner busy. calendarView lists +// every entry, unlike Google's freeBusy (which already applies transparency), so the +// filter is ours: an entry shown as "free" (reminders, all-day holidays from subscribed +// calendars, an OOO notice someone else sent), a cancelled meeting still on the calendar, +// and an invitation the owner declined all leave them available. Without this a single +// all-day "free" entry blocked a collective's whole day for every other host. Tentative, +// busy, out-of-office and working-elsewhere still block, as Outlook's own scheduling +// assistant treats them. +func blocksTime(showAs string, cancelled bool, response string) bool { + if cancelled || strings.EqualFold(response, "declined") { + return false + } + return !strings.EqualFold(showAs, "free") +} diff --git a/internal/calendar/microsoft/microsoft_test.go b/internal/calendar/microsoft/microsoft_test.go index e02f39bc..406d52b4 100644 --- a/internal/calendar/microsoft/microsoft_test.go +++ b/internal/calendar/microsoft/microsoft_test.go @@ -5,6 +5,7 @@ import ( "database/sql" "encoding/base64" "encoding/json" + "fmt" "io" "net/http" "net/http/httptest" @@ -105,6 +106,49 @@ func TestFreeBusy_parsesCalendarView(t *testing.T) { } } +// calendarView lists entries that leave their owner available; only real busy time may +// block a slot. The request must also $select the fields the filter reads. +func TestFreeBusy_skipsEntriesThatDoNotBlockTime(t *testing.T) { + c := newTestClient(t) + connect(t, c, "u1") + + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + sel := r.URL.Query().Get("$select") + for _, f := range []string{"showAs", "isCancelled", "responseStatus"} { + if !strings.Contains(sel, f) { + t.Errorf("$select=%q omits %s", sel, f) + } + } + w.Header().Set("Content-Type", "application/json") + ev := func(hour int, extra string) string { + return fmt.Sprintf(`{"start":{"dateTime":"2026-06-22T%02d:00:00.0000000","timeZone":"UTC"},`+ + `"end":{"dateTime":"2026-06-22T%02d:30:00.0000000","timeZone":"UTC"}%s}`, hour, hour, extra) + } + _, _ = w.Write([]byte(`{"value":[` + strings.Join([]string{ + ev(9, `,"showAs":"busy"`), + ev(10, `,"showAs":"free"`), + ev(11, `,"showAs":"busy","isCancelled":true`), + ev(12, `,"showAs":"tentative","responseStatus":{"response":"declined"}`), + ev(13, `,"showAs":"tentative"`), + ev(14, `,"showAs":"oof"`), + }, ",") + `]}`)) + })) + defer srv.Close() + c.apiBase = srv.URL + + iv, err := c.FreeBusy(context.Background(), "u1", time.Now(), time.Now().Add(24*time.Hour)) + if err != nil { + t.Fatalf("FreeBusy: %v", err) + } + var hours []int + for _, i := range iv { + hours = append(hours, i.Start.Hour()) + } + if fmt.Sprint(hours) != "[9 13 14]" { + t.Errorf("busy hours = %v; want [9 13 14] (busy, tentative, out of office)", hours) + } +} + func TestCreateEvent_returnsIDAndTeamsLink(t *testing.T) { c := newTestClient(t) connect(t, c, "u1")