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 "errors"
6 "os"
7 "sort"
8 "strconv"
9 "time"
10
11 "amadan.net/oficina/home/connect"
12 "amadan.net/oficina/home/connect/linkstore"
13 "amadan.net/oficina/home/dashboard"
14 "amadan.net/oficina/home/teammode"
15)
16
17func (a *App) configureDashboard(c *connect.DecodedCredentials) error {
18 team := os.Getenv("OFICINA_DASHBOARD_TEAM")
19 if team == "" {
20 return nil
21 }
22 h, e := dashboard.NewProvider(dashboard.ProviderConfig{DB: a.db.Writer(), Origin: a.cfg.Origin, Issuer: c.Issuer, Directory: c.Directory, Keys: c.Keys, Team: team, Kind: "events", Resolve: func(ctx context.Context, subject string) (int64, error) {
23 p, e := a.home.bridge.Resolve(ctx, subject)
24 if errors.Is(e, linkstore.ErrStorage) {
25 return 0, dashboard.ErrUnavailable
26 }
27 return p.ID, e
28 }, Read: a.dashboardEvents})
29 if e != nil {
30 return e
31 }
32 a.dashboard = h
33 return nil
34}
35func (a *App) dashboardEvents(ctx context.Context, id int64, subject string, now time.Time) (dashboard.Response, error) {
36 var standing *teammode.Access
37 if a.team != nil {
38 // The roster's standing for the verified subject that asked
39 // (Resolve found a link or a Home-only person for it; identity
40 // only), never anything stored (team_access.go).
41 standing = a.team.Access(ctx, subject)
42 ctx = teammode.WithAccess(ctx, standing)
43 }
44 var person Person
45 if e := a.db.G.WithContext(ctx).First(&person, id).Error; e != nil {
46 return dashboard.Response{}, e
47 }
48 zone := person.Zone
49 if zone == "" {
50 zone = "UTC"
51 }
52 loc, e := time.LoadLocation(zone)
53 if e != nil {
54 return dashboard.Response{}, e
55 }
56 local := now.In(loc)
57 from := time.Date(local.Year(), local.Month(), local.Day(), 0, 0, 0, 0, loc)
58 until := from.AddDate(0, 0, 8)
59 rows, e := a.store.EventsInRangeStrict(ctx, id, from.AddDate(0, 0, -2).Unix(), until.AddDate(0, 0, 2).Unix())
60 if e != nil {
61 return dashboard.Response{}, e
62 }
63 // Unknown standing is unavailable, not a short list: the feed is read
64 // on another origin, by a reader who never sees a view's partial
65 // notice (teammode-package.md §4, condition 3). Asked after reading,
66 // of the same Access, because a lease can lapse during it.
67 if standing != nil && standing.Partial() {
68 return dashboard.Response{}, dashboard.ErrUnavailable
69 }
70 out := dashboard.Response{Timezone: zone}
71 for _, event := range rows {
72 if event.Status == "cancelled" || (!event.AllDay && (event.OccEndUTC <= now.Unix() || event.OccStartUTC >= until.Unix())) {
73 continue
74 }
75 item := dashboard.Item{ID: strconv.FormatInt(event.ID, 10) + ":" + strconv.FormatInt(event.OccStartUTC, 10), Title: event.Title, URL: a.cfg.Origin + eventHref(event), Start: time.Unix(event.OccStartUTC, 0).UTC(), End: time.Unix(event.OccEndUTC, 0).UTC(), AllDay: event.AllDay, Calendar: event.CalendarName}
76 if event.AllDay {
77 item.StartDate = event.StartLocal
78 if event.RRule != "" && event.SeriesID == nil && event.OccurrenceLocal != "" {
79 item.StartDate = event.OccurrenceLocal
80 }
81 masterStart, e1 := time.Parse(LayoutDate, event.StartLocal)
82 masterEnd, e2 := time.Parse(LayoutDate, event.EndLocal)
83 occurrence, e3 := time.Parse(LayoutDate, item.StartDate)
84 if e1 != nil || e2 != nil || e3 != nil {
85 return dashboard.Response{}, dashboard.ErrUnavailable
86 }
87 item.EndDate = occurrence.Add(masterEnd.Sub(masterStart)).Format(LayoutDate)
88 if item.EndDate <= from.Format(LayoutDate) || item.StartDate >= until.Format(LayoutDate) {
89 continue
90 }
91
92 item.Start, _ = time.ParseInLocation(LayoutDate, item.StartDate, loc)
93 item.End, _ = time.ParseInLocation(LayoutDate, item.EndDate, loc)
94 }
95 out.Items = append(out.Items, item)
96 }
97 sort.SliceStable(out.Items, func(i, j int) bool { return out.Items[i].Start.Before(out.Items[j].Start) })
98 if len(out.Items) > dashboard.MaxItems {
99 out.Truncated = true
100 out.Items = out.Items[:dashboard.MaxItems]
101 }
102 return out, nil
103}
104