gitoria

calendar

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commitbd730759bd730759calendar mission 006: theme move (WIP): brief, report, LOG, STATUSmrebd730759/components/month-view/reminders.hl

8.4 KB

  1. // reminders.hl — THE REMINDERS (ticket calendar#4), shared by the server (events.hl checks and stores them) and the
  2. // month view in the browser (the editor rows, the notification times).
  3. // Stored as WorldAPI Event `$reminders` (https://worldapi.org/standards/event): `{ $before: 'PT30M' }` (30 minutes, or 'PT2H'
  4. // hours before the start) and `{ $days_before: 1, $time: '20:00' }` (that many days before, at that wall-clock time).
  5. // The page edits them as ROWS: { unit = 'minutes' | 'hours' | 'days' n = '30' time = '20:00' } (all text).
  6. // Event without `$reminders` = inherits the calendar's; `[]` = none.
  7. import { isText, isTime, pad, prevDay, nextDay } from './dates.hl'
  8. static maxRows = 8
  9. static defaultRows = [
  10. { unit = 'hours' n = '1' time = '09:00' }
  11. { unit = 'days' n = '1' time = '20:00' }
  12. ]
  13. // digits only, no sign, no leading noise; 1..5 digits
  14. static isCount = (s) => {
  15. if (!isText(s) || s.length < 1 || s.length > 5) { return false }
  16. let i = 0
  17. while (i < s.length) {
  18. let c = s.charCodeAt(i)
  19. if (c < 48 || c > 57) { return false }
  20. i = i + 1
  21. }
  22. return true
  23. }
  24. // rows the page sent → { error } or { reminders } (the stored `$reminders` list)
  25. static toReminders = (rows) => {
  26. if (rows == null || hlTypeName(rows) != 'Hybrid') { return { error = 'the reminders must be a list' } }
  27. if (rows.length > maxRows) { return { error = 'at most ' + maxRows + ' reminders' } }
  28. out = []
  29. let i = 0
  30. while (i < rows.length) {
  31. let r = rows[i]
  32. if (r == null || hlTypeName(r) != 'Hybrid' || !isCount(r.n)) { return { error = 'a reminder needs a whole number' } }
  33. let n = toNumber(r.n)
  34. if (r.unit == 'minutes') {
  35. if (n < 1 || n > 10080) { return { error = 'minutes must be 1 to 10080' } }
  36. let o = {}
  37. o['$before'] = 'PT' + n + 'M'
  38. out.push(o)
  39. } else if (r.unit == 'hours') {
  40. if (n < 1 || n > 168) { return { error = 'hours must be 1 to 168' } }
  41. let o = {}
  42. o['$before'] = 'PT' + n + 'H'
  43. out.push(o)
  44. } else if (r.unit == 'days') {
  45. if (n > 30) { return { error = 'days must be 0 to 30' } }
  46. if (!isTime(r.time)) { return { error = 'a day reminder needs a time' } }
  47. let o = {}
  48. o['$days_before'] = n
  49. o['$time'] = r.time
  50. out.push(o)
  51. } else { return { error = 'a reminder is minutes, hours or days before' } }
  52. i = i + 1
  53. }
  54. return { reminders = out }
  55. }
  56. // the stored `$reminders` → rows for the editor (what is not ours to read is left out)
  57. static toRows = (list) => {
  58. out = []
  59. if (list == null) { return out }
  60. let i = 0
  61. while (i < list.length) {
  62. let r = list[i]
  63. let b = r['$before']
  64. if (isText(b) && b.length > 3 && b.slice(0, 2) == 'PT') {
  65. let unit = b.slice(b.length - 1) == 'H' ? 'hours' : 'minutes'
  66. let digits = b.slice(2, b.length - 1)
  67. if ((b.slice(b.length - 1) == 'H' || b.slice(b.length - 1) == 'M') && isCount(digits)) { out.push({ unit = unit n = digits time = '09:00' }) }
  68. } else if (r['$days_before'] != null && isTime(r['$time'])) {
  69. out.push({ unit = 'days' n = '' + r['$days_before'] time = r['$time'] })
  70. }
  71. i = i + 1
  72. }
  73. return out
  74. }
  75. // a date moved by n days (n may be negative)
  76. static shift = (date, n) => {
  77. let d = date
  78. let k = n
  79. while (k > 0) { d = nextDay(d) k = k - 1 }
  80. while (k < 0) { d = prevDay(d) k = k + 1 }
  81. return d
  82. }
  83. // rows for a list in the editor: each carries its position `i` and the class that shows the time only for days
  84. static withIndex = (rows) => {
  85. out = []
  86. let i = 0
  87. while (i < rows.length) {
  88. let r = rows[i]
  89. out.push({ i = i unit = r.unit n = r.n time = r.time cls = r.unit == 'days' ? 'days' : 'other' })
  90. i = i + 1
  91. }
  92. return out
  93. }
  94. static plural = (n, one, many) => { return n + ' ' + (n == 1 ? one : many) }
  95. // one row as words: "1 hour before", "the day before at 20:00", "2 days before at 09:00", "the same day at 08:00"
  96. static describeRow = (r) => {
  97. n = toNumber(r.n)
  98. if (r.unit == 'minutes') { return plural(n, 'minute', 'minutes') + ' before' }
  99. if (r.unit == 'hours') { return plural(n, 'hour', 'hours') + ' before' }
  100. if (n == 0) { return 'the same day at ' + r.time }
  101. if (n == 1) { return 'the day before at ' + r.time }
  102. return n + ' days before at ' + r.time
  103. }
  104. static describe = (rows) => {
  105. if (rows == null || rows.length == 0) { return 'no reminders' }
  106. let out = ''
  107. let i = 0
  108. while (i < rows.length) {
  109. out = out + (i == 0 ? '' : ', ') + describeRow(rows[i])
  110. i = i + 1
  111. }
  112. return out
  113. }
  114. // ---- browser side: the time a reminder fires (epoch ms) --------------------------------------------------
  115. // The wall clock of a time zone as an offset (ms) from UTC at the instant t.
  116. static offsetOf = (zone, t) => {
  117. f = Intl.DateTimeFormat('en-US', { timeZone = zone hourCycle = 'h23' year = 'numeric' month = 'numeric' day = 'numeric' hour = 'numeric' minute = 'numeric' second = 'numeric' })
  118. parts = f.formatToParts(new Date(t))
  119. v = { year = 0 month = 1 day = 1 hour = 0 minute = 0 second = 0 }
  120. let i = 0
  121. while (i < parts.length) {
  122. let p = parts[i]
  123. if (p.type != 'literal') { v[p.type] = toNumber(p.value) }
  124. i = i + 1
  125. }
  126. return Date.UTC(v.year, v.month - 1, v.day, v.hour, v.minute, v.second) - (t - t % 1000)
  127. }
  128. // 'YYYY-MM-DDTHH:MM:SS' wall clock in `zone` ('' = the device's own zone) → the instant, epoch ms
  129. static instantOf = (wall, zone) => {
  130. y = toNumber(wall.slice(0, 4))
  131. mo = toNumber(wall.slice(5, 7))
  132. d = toNumber(wall.slice(8, 10))
  133. h = toNumber(wall.slice(11, 13))
  134. mi = toNumber(wall.slice(14, 16))
  135. if (zone == null || zone == '') { return new Date(y, mo - 1, d, h, mi, 0).getTime() }
  136. guess = Date.UTC(y, mo - 1, d, h, mi, 0)
  137. t = guess - offsetOf(zone, guess)
  138. return guess - offsetOf(zone, t)
  139. }
  140. // the words of the notification (title = the event's title)
  141. static bodyOf = (ev, r) => {
  142. at = ev.allDay ? '' : ' at ' + ev.startTime
  143. let text = ''
  144. if (r.unit == 'minutes') { text = 'Starts in ' + plural(toNumber(r.n), 'minute', 'minutes') }
  145. else if (r.unit == 'hours') { text = 'Starts in ' + plural(toNumber(r.n), 'hour', 'hours') }
  146. else {
  147. n = toNumber(r.n)
  148. text = n == 0 ? 'Today' + at : (n == 1 ? 'Tomorrow' + at : 'In ' + n + ' days' + at + ', ' + ev.startDate)
  149. }
  150. return ev.place != '' ? text + ' · ' + ev.place : text
  151. }
  152. // the words of a reminder shown LATE (its time passed while the app could not run: switched away, dropped, closed):
  153. // when the event starts, not "Starts in 1 hour"
  154. static lateBodyOf = (ev, todayText) => {
  155. let text = ''
  156. if (ev.allDay) { text = ev.startDate == todayText ? 'Today, all day' : 'All day, ' + ev.startDate }
  157. else { text = 'Starts at ' + ev.startTime + (ev.startDate == todayText ? '' : ', ' + ev.startDate) }
  158. return ev.place != '' ? text + ' · ' + ev.place : text
  159. }
  160. // the events { id title allDay startDate startTime place zone rows } → their reminders with fromMs < at < toMs:
  161. // [{ key, at, start, title, body, late }] (late = the words for showing it after its time; todayText = the browser's today)
  162. static planBetween = (events, fromMs, toMs, todayText) => {
  163. out = []
  164. let i = 0
  165. while (i < events.length) {
  166. let ev = events[i]
  167. let zone = ev.allDay ? '' : ev.zone
  168. let startWall = ev.startDate + 'T' + (ev.allDay ? '00:00' : ev.startTime) + ':00'
  169. let start = instantOf(startWall, zone)
  170. let j = 0
  171. while (j < ev.rows.length) {
  172. let r = ev.rows[j]
  173. let at = 0
  174. if (r.unit == 'minutes') { at = start - toNumber(r.n) * 60000 }
  175. else if (r.unit == 'hours') { at = start - toNumber(r.n) * 3600000 }
  176. else {
  177. let day = ev.startDate
  178. let k = toNumber(r.n)
  179. while (k > 0) { day = prevDay(day) k = k - 1 }
  180. at = instantOf(day + 'T' + r.time + ':00', zone)
  181. }
  182. if (at > fromMs && at < toMs) {
  183. out.push({ id = ev.id key = ev.id + ':' + at at = at start = start title = ev.title body = bodyOf(ev, r) late = lateBodyOf(ev, todayText) })
  184. }
  185. j = j + 1
  186. }
  187. i = i + 1
  188. }
  189. return out
  190. }
  191. // the reminders still to come within `horizon` ms of `nowMs` (the browser timers)
  192. static plan = (events, nowMs, horizon) => { return planBetween(events, nowMs, nowMs + horizon, '') }
  193. // calendar#9: the reminders whose time came after `sinceMs` and up to now while no timer could show them, for events
  194. // that have not started yet — shown at once, late; one per event (the newest of its reminders)
  195. static missed = (events, sinceMs, nowMs, todayText) => {
  196. all = planBetween(events, sinceMs, nowMs + 1, todayText)
  197. out = []
  198. let i = 0
  199. while (i < all.length) {
  200. let q = all[i]
  201. let newest = q.start > nowMs
  202. let j = 0
  203. while (j < all.length) {
  204. if (all[j].id == q.id && all[j].at > q.at) { newest = false }
  205. j = j + 1
  206. }
  207. if (newest) { out.push(q) }
  208. i = i + 1
  209. }
  210. return out
  211. }

Branches

Latest commits

  • bd730759calendar mission 006: theme move (WIP): brief, report, LOG, STATUSmre
  • b117b153calendar mission 006 WIP: reminder row fields keep 44 px; e2e script flagsmre
  • 53e3b0debrowser gate: Log out through the user menu, theme colour rgb(15, 20, 25)mre
  • 90bf9e2ecalendar mission 006 WIP: theme dbe3de7, theme shell (AppHeader/MainMenu/UserMenu/Sidebar/ContentFirst), styles cut to the app's own rules, check-theme gate, e2e toolsmre
  • 3c5604f9identkit: refuse a taken ident port, cold-start deadline, abort on dead ident, clear no-code error (as notes)mre
  • 4996bbe7paths: projects moved into /media/STORAGE/projects/WorldAPI/mre
  • f5bf0803calendar mission 004: re-vendor Hybriel to the shared static build 0f396652; compose cache volume + tmpfs; notify-paths gate in deploy.sh; gate start waits 150 s; reportmre
  • eb86526fmission brief 004mre
  • 2f953b16deploy.sh: wait up to DEPLOY_URL_WAIT (120 s) for 200 after the restart, as the other apps (cold compile, hybriel#148)mre
  • 9078e1b7calendar mission 003 (2/2): report, LOG, STATUS (cause: timers only in an open, front, touched page; gate old 5/2 new 7/0; Hybriel gaps Web Push + page-shown hook, STOP)mre
  • 31ffe910calendar mission 003 (1/2): calendar#9 — missed reminders shown when the app is back, timers set again when it is shownmre
  • 26c997d3mission brief 003mre
  • c6809c76calendar mission 002: report, LOG, STATUS (test bug; fixed gate 78/0 at 00:30 05:40 12:00 19:59 20:01; 23:30 not run, quota stop)mre
  • 00f72783calendar mission 002 (2/2): TEST bug — the reminder steps get their own identity (carol): alice's all-day Vacation (20th-22nd) from the events steps added 2 reminders from the 12th 20:00 to the 19th 23:00; Tokyo check no longer skipped in the Tokyo eveningmre
  • 54fb0ce1calendar mission 002 (1/2): gate clock override GATE_CLOCK=HH:MM (this process and every page get Date shifted to that local time today)mre
  • c838ee79mission brief 002mre
  • f2bc67ecdocker-compose: init: true (hybriel#156, PID 1 ignored SIGTERM, every restart waited 10 s)mre
  • 2881119fcalendar mission 001 (4/4): code order — README layout + same-output test, STATUS, LOG, report; tests/letcount.py (sub-folders), tests/realdata-baseline.mjs, tests/realdata-compare.pymre
  • 3b2a4cd0calendar mission 001 (3/4): code order — let only where reassigned (147 dropped incl. components/month-view; 110 left: 67 reassigned, 43 loop-bound); gates 78/0 (3 of 4 runs; 1 known 'next month' flake) + 18/0, live-data run = step 2mre
  • f5ce6b1dcalendar mission 001 (2/4): code order — topics, map, thin faces: lib/util.hl, lib/events(-helpers).hl (+calendarView/settingsView/soonOf), lib/settings.hl, lib/users.hl (+userOfLoginCode, tagOf), lib/api(-helpers).hl; project.hl = map; login route takes &req/&sessions (failed-login reason now kept); gates 78/0 + 18/0mre