Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
36 changes: 33 additions & 3 deletions internal/calendar/microsoft/freebusy.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"net/http"
"net/url"
"strings"
"time"

"github.com/calnode/calnode/internal/slots"
Expand All @@ -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"`
}
Expand Down Expand Up @@ -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()

Expand Down Expand Up @@ -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 {
Expand All @@ -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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The parity claim here doesn't hold: Microsoft's availabilityView maps workingElsewhere to 0 ("free or working elsewhere"), so Outlook's scheduling view treats it as available. Because blocksTime only special-cases free, a full-day "Working elsewhere" entry (a common WFH marker) still blocks that host's entire day — the exact symptom #188 reports for other all-day entries.

Technical details
# Treat workingElsewhere consistently with Outlook (or fix the comment)

## Affected sites
- internal/calendar/microsoft/freebusy.go:138 — comment asserts working-elsewhere blocks "as Outlook's own scheduling assistant treats them".
- internal/calendar/microsoft/freebusy.go:144 — `return !strings.EqualFold(showAs, "free")` keeps `workingElsewhere` in the blocking set.
- internal/calendar/microsoft/microsoft_test.go:127-134 — no `workingElsewhere` case, so the decision is not pinned by a test.

## Evidence
- https://learn.microsoft.com/en-us/graph/api/resources/scheduleinformation?view=graph-rest-1.0 — `availabilityView`: `0`= free or working elsewhere, `1`= tentative, `2`= busy, `3`= out of office.

## Required outcome
- Either treat `workingElsewhere` as non-blocking (same as `free`) so availability matches Outlook, or keep the conservative behaviour deliberately and correct the comment's rationale.
- Add `workingElsewhere` (and ideally `unknown`) to `TestFreeBusy_skipsEntriesThatDoNotBlockTime` so whichever choice is made stays locked in.

// 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")
}
44 changes: 44 additions & 0 deletions internal/calendar/microsoft/microsoft_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"database/sql"
"encoding/base64"
"encoding/json"
"fmt"
"io"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -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")
Expand Down
Loading