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
3// The in-app reminders list (spec §7 "In-app reminders"): GET /reminders,
4// a view, and the two dismissals, which are member writes. Unread means
5// undismissed: nothing is written on a GET.
6
7import (
8 "errors"
9 "fmt"
10 "net/http"
11 "strconv"
12 "time"
13
14 "amadan.net/oficina/estilo.git/chrome"
15 "github.com/go-chi/chi/v5"
16)
17
18// noticeListMax bounds one list. Notices are one per rule, so this is
19// only reached by somebody with a hundred unread rules.
20const noticeListMax = 100
21
22// noticeRow is one notice as the list draws it.
23type noticeRow struct {
24 ID int64
25 Title string
26 Href string
27 // When is the occurrence in the person's zone, in the past tense once
28 // it has started: "Starts Monday 2 November at 09:00", "Started …".
29 When string
30}
31
32// noticeWhen writes when a notice's occurrence is, in zone. An all-day
33// occurrence is a DATE, read from its own local key, never a UTC instant.
34func noticeWhen(n NoticeView, zone string, now int64) string {
35 verb := "Starts"
36 if n.StartUTC <= now {
37 verb = "Started"
38 }
39 if n.AllDay {
40 if d, err := time.Parse(LayoutDate, n.Local); err == nil {
41 return verb + " " + d.Format("Monday 2 January")
42 }
43 }
44 t, err := InZone(n.StartUTC, zone)
45 if err != nil {
46 return ""
47 }
48 return fmt.Sprintf("%s %s at %s", verb, t.Format("Monday 2 January"), hhmm(n.StartUTC, zone))
49}
50
51// reminders is GET /reminders.
52func (h *handlers) reminders(w http.ResponseWriter, r *http.Request) {
53 p, _ := CurrentPerson(r)
54 list, err := h.store.UnreadNotices(r.Context(), p.ID, noticeListMax)
55 if err != nil {
56 h.fail(w, r, "list reminder notices", err)
57 return
58 }
59 now := time.Now().Unix()
60 rows := make([]noticeRow, 0, len(list))
61 for _, n := range list {
62 rows = append(rows, noticeRow{ID: n.ID, Title: n.Title, Href: n.Href, When: noticeWhen(n, personZone(p), now)})
63 }
64 render(w, "reminders", h.page(r, "Reminders", map[string]any{
65 "Notices": rows,
66 "InApp": h.store.usesInApp(p),
67 }))
68}
69
70// dismissReminder is POST /reminders/{id}/dismiss.
71func (h *handlers) dismissReminder(w http.ResponseWriter, r *http.Request) {
72 p, _ := CurrentPerson(r)
73 id, err := strconv.ParseInt(chi.URLParam(r, "id"), 10, 64)
74 if err != nil {
75 h.errorPage(w, r, http.StatusNotFound, "")
76 return
77 }
78 if err := h.store.DismissNotice(r.Context(), p.ID, id, time.Now().Unix()); err != nil {
79 h.fail(w, r, "dismiss reminder", err)
80 return
81 }
82 http.Redirect(w, r, "/reminders", http.StatusSeeOther)
83}
84
85// dismissAllReminders is POST /reminders/dismiss.
86func (h *handlers) dismissAllReminders(w http.ResponseWriter, r *http.Request) {
87 p, _ := CurrentPerson(r)
88 if err := h.store.DismissAllNotices(r.Context(), p.ID, time.Now().Unix()); err != nil {
89 h.fail(w, r, "dismiss reminders", err)
90 return
91 }
92 http.Redirect(w, r, "/reminders", http.StatusSeeOther)
93}
94
95// nativeReminders is GET /api/native/reminders: the unread count and the
96// list, for the native apps. JSON has no room for a partial notice, so
97// standing that lapsed while it was built is a 503, never a short list
98// (as nativeEvents).
99func (h *handlers) nativeReminders(w http.ResponseWriter, r *http.Request) {
100 p, _ := CurrentPerson(r)
101 n, err := h.store.UnreadNoticeCount(r.Context(), p.ID)
102 if err != nil {
103 http.Error(w, "Cannot load reminders", http.StatusInternalServerError)
104 return
105 }
106 list, err := h.store.UnreadNotices(r.Context(), p.ID, noticeListMax)
107 if err != nil {
108 http.Error(w, "Cannot load reminders", http.StatusInternalServerError)
109 return
110 }
111 if requestUnknown(r) {
112 h.teamUnavailable(w, r)
113 return
114 }
115 type item struct {
116 ID int64 `json:"id"`
117 EventID int64 `json:"event_id"`
118 Title string `json:"title"`
119 Start int64 `json:"start"`
120 AllDay bool `json:"all_day"`
121 Path string `json:"path"`
122 }
123 out := make([]item, 0, len(list))
124 for _, v := range list {
125 out = append(out, item{v.ID, v.EventID, v.Title, v.StartUTC, v.AllDay, v.Href})
126 }
127 nativeJSON(w, map[string]any{"unread": n, "reminders": out})
128}
129
130// addReminderLink puts the Reminders link in a signed-in person's bar,
131// beside the sections (never on a door, which has no account menu).
132func (h *handlers) addReminderLink(r *http.Request, bar *chrome.Bar) {
133 p, ok := CurrentPerson(r)
134 if !ok || p.ID == 0 || bar.Account == nil || bar.Nav == nil {
135 return
136 }
137 if link, ok := h.reminderLink(r, p); ok {
138 bar.Nav = append(bar.Nav, link)
139 }
140}
141
142// reminderLink is the chrome's Reminders link: drawn for a person who
143// uses in-app reminders or has one unread, and otherwise absent, so a
144// person who never chose in-app sees the bar they always did. The count
145// reads on this request's standing, so under Unknown it is absent rather
146// than a zero.
147func (h *handlers) reminderLink(r *http.Request, p Person) (chrome.Link, bool) {
148 n, err := h.store.UnreadNoticeCount(r.Context(), p.ID)
149 if err != nil {
150 n = 0
151 }
152 if n == 0 && !h.store.usesInApp(p) {
153 return chrome.Link{}, false
154 }
155 label := "Reminders"
156 if n > 0 {
157 label = fmt.Sprintf("Reminders (%d)", n)
158 }
159 return chrome.Link{Href: "/reminders", Label: label, Current: r.URL.Path == "/reminders"}, true
160}
161
162func (h *handlers) addReminder(w http.ResponseWriter, r *http.Request) {
163 p, _ := CurrentPerson(r)
164 id, _ := strconv.ParseInt(chi.URLParam(r, "id"), 10, 64)
165 _ = r.ParseForm()
166 offset, _ := strconv.ParseInt(r.FormValue("offset_min"), 10, 64)
167 // channel is absent unless the form offered a choice: the person's
168 // own channel, as before there was one (spec §7 "In-app reminders").
169 if err := h.store.AddReminderOn(r.Context(), p.ID, id, offset, r.FormValue("channel")); err != nil && !NotFound(err) && !errors.Is(err, ErrNoAddress) {
170 h.fail(w, r, "add reminder", err)
171 return
172 }
173 http.Redirect(w, r, "/events/"+strconv.FormatInt(id, 10), http.StatusSeeOther)
174}
175
176func (h *handlers) removeReminder(w http.ResponseWriter, r *http.Request) {
177 p, _ := CurrentPerson(r)
178 id, _ := strconv.ParseInt(chi.URLParam(r, "id"), 10, 64)
179 _ = r.ParseForm()
180 remID, _ := strconv.ParseInt(r.FormValue("reminder_id"), 10, 64)
181 if err := h.store.RemoveReminder(r.Context(), p.ID, remID); err != nil && !NotFound(err) {
182 h.fail(w, r, "remove reminder", err)
183 return
184 }
185 http.Redirect(w, r, "/events/"+strconv.FormatInt(id, 10), http.StatusSeeOther)
186}
187