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 access "amadan.net/oficina/agent-access.git"
5 "amadan.net/oficina/home/teammode"
6 "context"
7 "encoding/json"
8)
9
10type agentResponseKey struct{}
11type agentResponseState struct {
12 standing *teammode.Access
13 person Person
14 selected bool
15 calendars []int64
16 calendarSet map[int64]bool
17 events map[int64]int64
18}
19
20func captureAgentCalendar(ctx context.Context, id int64) {
21 if s, ok := ctx.Value(agentResponseKey{}).(*agentResponseState); ok {
22 if s.calendarSet == nil {
23 s.calendarSet = map[int64]bool{}
24 }
25 if !s.calendarSet[id] {
26 s.calendarSet[id] = true
27 s.calendars = append(s.calendars, id)
28 }
29 }
30}
31func captureAgentEvent(ctx context.Context, id, calendarID int64) {
32 if s, ok := ctx.Value(agentResponseKey{}).(*agentResponseState); ok {
33 if s.events == nil {
34 s.events = map[int64]int64{}
35 }
36 s.events[id] = calendarID
37 captureAgentCalendar(ctx, calendarID)
38 }
39}
40func (a *App) agentResponse(ctx context.Context, p access.Principal, name string, _ json.RawMessage, result any) error {
41 s, ok := ctx.Value(agentResponseKey{}).(*agentResponseState)
42 if !ok || s.person.ID == 0 || name != "" && !s.selected {
43 return access.Fail("forbidden", "Response authority is unavailable.")
44 }
45 if name != "" {
46 value, ok := result.(map[string]any)
47 if !ok {
48 return access.Fail("forbidden", "Response provenance is unavailable.")
49 }
50 switch name {
51 case "calendar_list":
52 rows, ok := value["calendars"].([]any)
53 if !ok || len(rows) != len(s.calendars) {
54 return access.Fail("forbidden", "Calendar provenance is unavailable.")
55 }
56 case "calendar_get_event", "calendar_create_event", "calendar_update_event":
57 if len(s.events) != 1 || len(s.calendars) != 1 {
58 return access.Fail("forbidden", "Event provenance is unavailable.")
59 }
60 case "calendar_list_events":
61 rows, ok := value["events"].([]any)
62 if !ok || len(rows) > 0 && len(s.calendars) == 0 {
63 return access.Fail("forbidden", "Calendar provenance is unavailable.")
64 }
65 case "calendar_free_busy":
66 rows, ok := value["busy"].([]any)
67 if !ok || len(rows) > 0 && len(s.calendars) == 0 {
68 return access.Fail("forbidden", "Calendar provenance is unavailable.")
69 }
70 default:
71 return access.Fail("forbidden", "Response provenance is unavailable.")
72 }
73 }
74 if _, err := a.agentPerson(ctx, p, a.agentTeam); err != nil {
75 return err
76 }
77 ctx = a.agentContext(ctx)
78 for id, calendarID := range s.events {
79 e, err := a.store.Event(ctx, s.person.ID, id)
80 if err != nil {
81 return agentFailure(err)
82 }
83 if (name == "calendar_update_event" || name == "calendar_create_event") && !e.Writable() {
84 return access.Fail("forbidden", "Event write access changed.")
85 }
86 if e.CalendarID != calendarID {
87 return access.Fail("forbidden", "Event access changed.")
88 }
89 }
90 for _, id := range s.calendars {
91 c, err := a.store.Calendar(ctx, s.person.ID, id)
92 if err != nil {
93 return agentFailure(err)
94 }
95 if (name == "calendar_update_event" || name == "calendar_create_event") && !CanWrite(c.Role) {
96 return access.Fail("forbidden", "Calendar write access changed.")
97 }
98 if c.ArchivedAt != nil {
99 return access.Fail("not_found", "This calendar is unavailable.")
100 }
101 }
102 return a.agentStanding(ctx, p.Subject, false)
103}
104