oficina / calendar Public

Clone
git clone https://amadan.net/oficina/calendar

Plain git — no account needed to clone.

Download

Download this file

1package calendar
2
3import (
4 "context"
5 "encoding/json"
6 "strconv"
7 "time"
8
9 access "amadan.net/oficina/agent-access.git"
10)
11
12func (a *App) agentCalendars(ctx context.Context, _ access.Principal, p Person, _ json.RawMessage) (any, error) {
13 cals, err := a.store.Calendars(ctx, p.ID)
14 if err != nil {
15 return nil, err
16 }
17 out := []map[string]any{}
18 for _, c := range cals {
19 captureAgentCalendar(ctx, c.ID)
20 out = append(out, map[string]any{"id": c.ID, "name": c.Name, "kind": c.Kind, "role": c.Role})
21 }
22 return map[string]any{"calendars": out}, nil
23}
24func (a *App) agentGetEvent(ctx context.Context, _ access.Principal, p Person, raw json.RawMessage) (any, error) {
25 var in struct {
26 ID int64 `json:"id"`
27 }
28 if json.Unmarshal(raw, &in) != nil {
29 return nil, access.Fail("invalid", "Invalid event ID.")
30 }
31 e, err := a.store.Event(ctx, p.ID, in.ID)
32 if err != nil {
33 return nil, err
34 }
35 c, err := a.store.Calendar(ctx, p.ID, e.CalendarID)
36 if err != nil {
37 return nil, err
38 }
39 if c.ArchivedAt != nil {
40 return nil, ErrNotFound
41 }
42 // An override shares its series's attendee rows, as in AttachAttendees.
43 attendees, err := a.store.AttendeesFor(ctx, occurrenceOwner(e))
44 if err != nil {
45 return nil, err
46 }
47 captureAgentEvent(ctx, e.ID, e.CalendarID)
48 return agentAttendeeFields(map[string]any{"id": e.ID, "calendar_id": e.CalendarID, "title": agentClip(e.Title, MaxTitleLen), "location": agentClip(e.Location, MaxLocationLen), "notes": e.Notes, "all_day": e.AllDay, "start_local": e.StartLocal, "end_local": e.EndLocal, "tz": e.TZ, "start_utc": e.StartUTC, "end_utc": e.EndUTC, "sequence": e.Sequence, "kind": e.Kind, "status": e.Status, "url": a.cfg.Origin + "/events/" + strconv.FormatInt(e.ID, 10)}, attendees), nil
49}
50func (a *App) agentBusy(ctx context.Context, _ access.Principal, p Person, raw json.RawMessage) (any, error) {
51 var in struct{ Start, End string }
52 if json.Unmarshal(raw, &in) != nil {
53 return nil, access.Fail("invalid", "Invalid time range.")
54 }
55 start, e1 := time.Parse(time.RFC3339, in.Start)
56 end, e2 := time.Parse(time.RFC3339, in.End)
57 if e1 != nil || e2 != nil || !start.Before(end) || end.Sub(start) > 31*24*time.Hour {
58 return nil, access.Fail("invalid", "Supply a positive RFC3339 time range of at most 31 days, with explicit offsets.")
59 }
60 spans, calendars, err := a.store.busyWithProvenance(a.store.g.WithContext(ctx), p, start.Unix(), end.Unix(), nil)
61 if err != nil {
62 return nil, err
63 }
64 for _, id := range calendars {
65 captureAgentCalendar(ctx, id)
66 }
67 if len(spans) > 10000 {
68 return nil, access.Fail("too_large", "Use a smaller time range.")
69 }
70 out := []map[string]string{}
71 for _, span := range spans {
72 from, to := max(span.StartUTC, start.Unix()), min(span.EndUTC, end.Unix())
73 if from >= to {
74 continue
75 }
76 out = append(out, map[string]string{"start": time.Unix(from, 0).UTC().Format(time.RFC3339), "end": time.Unix(to, 0).UTC().Format(time.RFC3339)})
77 }
78 return map[string]any{"tz": p.Zone, "busy": out}, nil
79}
80