calendar
All repositories: gitoria
7.9 KB
// reminders.hl — THE REMINDERS (ticket calendar#4), shared by the server (events.hl checks and stores them) and the// month view in the browser (the editor rows, the notification times).// Stored as WorldAPI Event `$reminders` (https://worldapi.org/standards/event): `{ $before: 'PT30M' }` (30 minutes, or 'PT2H'// hours before the start) and `{ $days_before: 1, $time: '20:00' }` (that many days before, at that wall-clock time).// The page edits them as ROWS: { unit = 'minutes' | 'hours' | 'days' n = '30' time = '20:00' } (all text).// Event without `$reminders` = inherits the calendar's; `[]` = none.import { fromLocal } from 'hl:time'import { isText, isTime, pad, prevDay, nextDay } from './dates.hl'static maxRows = 8static defaultRows = [{ unit = 'hours' n = '1' time = '09:00' }{ unit = 'days' n = '1' time = '20:00' }]// digits only, no sign, no leading noise; 1..5 digitsstatic isCount = (s) => {if (!isText(s) || s.length < 1 || s.length > 5) { return false }let i = 0while (i < s.length) {let c = s.charCodeAt(i)if (c < 48 || c > 57) { return false }i = i + 1}return true}// rows the page sent → { error } or { reminders } (the stored `$reminders` list)static toReminders = (rows) => {if (rows == null || hlTypeName(rows) != 'Hybrid') { return { error = 'the reminders must be a list' } }if (rows.length > maxRows) { return { error = 'at most ' + maxRows + ' reminders' } }out = []let i = 0while (i < rows.length) {let r = rows[i]if (r == null || hlTypeName(r) != 'Hybrid' || !isCount(r.n)) { return { error = 'a reminder needs a whole number' } }let n = toNumber(r.n)if (r.unit == 'minutes') {if (n < 1 || n > 10080) { return { error = 'minutes must be 1 to 10080' } }let o = {}o['$before'] = 'PT' + n + 'M'out.push(o)} else if (r.unit == 'hours') {if (n < 1 || n > 168) { return { error = 'hours must be 1 to 168' } }let o = {}o['$before'] = 'PT' + n + 'H'out.push(o)} else if (r.unit == 'days') {if (n > 30) { return { error = 'days must be 0 to 30' } }if (!isTime(r.time)) { return { error = 'a day reminder needs a time' } }let o = {}o['$days_before'] = no['$time'] = r.timeout.push(o)} else { return { error = 'a reminder is minutes, hours or days before' } }i = i + 1}return { reminders = out }}// the stored `$reminders` → rows for the editor (what is not ours to read is left out)static toRows = (list) => {out = []if (list == null) { return out }let i = 0while (i < list.length) {let r = list[i]let b = r['$before']if (isText(b) && b.length > 3 && b.slice(0, 2) == 'PT') {let unit = b.slice(b.length - 1) == 'H' ? 'hours' : 'minutes'let digits = b.slice(2, b.length - 1)if ((b.slice(b.length - 1) == 'H' || b.slice(b.length - 1) == 'M') && isCount(digits)) { out.push({ unit = unit n = digits time = '09:00' }) }} else if (r['$days_before'] != null && isTime(r['$time'])) {out.push({ unit = 'days' n = '' + r['$days_before'] time = r['$time'] })}i = i + 1}return out}// a date moved by n days (n may be negative)static shift = (date, n) => {let d = datelet k = nwhile (k > 0) { d = nextDay(d) k = k - 1 }while (k < 0) { d = prevDay(d) k = k + 1 }return d}// rows for a list in the editor: each carries its position `i` and the class that shows the time only for daysstatic withIndex = (rows) => {out = []let i = 0while (i < rows.length) {let r = rows[i]out.push({ i = i unit = r.unit n = r.n time = r.time cls = r.unit == 'days' ? 'days' : 'other' })i = i + 1}return out}static plural = (n, one, many) => { return n + ' ' + (n == 1 ? one : many) }// 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"static describeRow = (r) => {n = toNumber(r.n)if (r.unit == 'minutes') { return plural(n, 'minute', 'minutes') + ' before' }if (r.unit == 'hours') { return plural(n, 'hour', 'hours') + ' before' }if (n == 0) { return 'the same day at ' + r.time }if (n == 1) { return 'the day before at ' + r.time }return n + ' days before at ' + r.time}static describe = (rows) => {if (rows == null || rows.length == 0) { return 'no reminders' }let out = ''let i = 0while (i < rows.length) {out = out + (i == 0 ? '' : ', ') + describeRow(rows[i])i = i + 1}return out}// ---- the time a reminder fires (epoch ms): hl:time, the same zone database in the browser (Intl) and on the server -------// 'YYYY-MM-DDTHH:MM:SS' wall clock in the IANA zone `zone` → the instant, epoch ms (fold: the earlier instant, gap: moved forward)static instantOf = (wall, zone) => {return fromLocal({ year = toNumber(wall.slice(0, 4)) month = toNumber(wall.slice(5, 7)) day = toNumber(wall.slice(8, 10)) hour = toNumber(wall.slice(11, 13)) minute = toNumber(wall.slice(14, 16)) }, zone)}// the words of the notification (title = the event's title)static bodyOf = (ev, r) => {at = ev.allDay ? '' : ' at ' + ev.startTimelet text = ''if (r.unit == 'minutes') { text = 'Starts in ' + plural(toNumber(r.n), 'minute', 'minutes') }else if (r.unit == 'hours') { text = 'Starts in ' + plural(toNumber(r.n), 'hour', 'hours') }else {n = toNumber(r.n)text = n == 0 ? 'Today' + at : (n == 1 ? 'Tomorrow' + at : 'In ' + n + ' days' + at + ', ' + ev.startDate)}return ev.place != '' ? text + ' · ' + ev.place : text}// the words of a reminder shown LATE (its time passed while the app could not run: switched away, dropped, closed):// when the event starts, not "Starts in 1 hour"static lateBodyOf = (ev, todayText) => {let text = ''if (ev.allDay) { text = ev.startDate == todayText ? 'Today, all day' : 'All day, ' + ev.startDate }else { text = 'Starts at ' + ev.startTime + (ev.startDate == todayText ? '' : ', ' + ev.startDate) }return ev.place != '' ? text + ' · ' + ev.place : text}// the events { id title allDay startDate startTime place zone rows } → their reminders with fromMs < at < toMs:// [{ key, at, start, title, body, late }] (late = the words for showing it after its time; todayText = the device's today; an// all-day event's day is read in `deviceZone`, the device's IANA zone; the server passes the zone the device registered)static planBetween = (events, fromMs, toMs, todayText, deviceZone) => {out = []let i = 0while (i < events.length) {let ev = events[i]let zone = ev.allDay ? deviceZone : ev.zonelet startWall = ev.startDate + 'T' + (ev.allDay ? '00:00' : ev.startTime) + ':00'let start = instantOf(startWall, zone)let j = 0while (j < ev.rows.length) {let r = ev.rows[j]let at = 0if (r.unit == 'minutes') { at = start - toNumber(r.n) * 60000 }else if (r.unit == 'hours') { at = start - toNumber(r.n) * 3600000 }else {let day = ev.startDatelet k = toNumber(r.n)while (k > 0) { day = prevDay(day) k = k - 1 }at = instantOf(day + 'T' + r.time + ':00', zone)}if (at > fromMs && at < toMs) {out.push({ id = ev.id key = ev.id + ':' + at at = at start = start title = ev.title body = bodyOf(ev, r) late = lateBodyOf(ev, todayText) })}j = j + 1}i = i + 1}return out}// the reminders still to come within `horizon` ms of `nowMs` (the browser timers)static plan = (events, nowMs, horizon, deviceZone) => { return planBetween(events, nowMs, nowMs + horizon, '', deviceZone) }// calendar#9: the reminders whose time came after `sinceMs` and up to now while no timer could show them, for events// that have not started yet — shown at once, late; one per event (the newest of its reminders)static missed = (events, sinceMs, nowMs, todayText, deviceZone) => {all = planBetween(events, sinceMs, nowMs + 1, todayText, deviceZone)out = []let i = 0while (i < all.length) {let q = all[i]let newest = q.start > nowMslet j = 0while (j < all.length) {if (all[j].id == q.id && all[j].at > q.at) { newest = false }j = j + 1}if (newest) { out.push(q) }i = i + 1}return out}
Branches
Latest commits
- df2e4fa0calendar#12: test push rate limit (1 per 30 s per device), owner-only gate checksmre
- acb957cfcalendar#12: Send a test notification on a subscribed device (one push in ~10 s, owner's own device), push gate part 4mre
- 0b222eb8tests: e2e-theme CDP port default from E2E_THEME_CDP_PORT (8756 unchanged)mre
- 8b8a5abare-vendor Hybriel 6f8a0689 (int 6–11: streaming cursors, #203 index heal + close() fix, fd403 listDir/cursor fds, hl:time browser side, hl:web batches 5+6)mre
- a69b27f9docker-compose: nofile 65536 (Hybriel int 6+ boot opened >1024 files in the tracker)mre
- 1a97edfdcalendar mission 010: reminder rows at 390 px (the 7-column month grid no longer catches the reminder lists), gate checks, settings-shots, reportmre
- 61b21404calendar mission 009: reminder times on hl:time in the browser (lib/push-plan.hl and the Intl code removed), DST gate tests/dst.mjs, E2E script, reportmre
- 875353c1calendar mission 009: re-vendor Hybriel master 022a3ac1 (int 6 6b396322 hl:time browser side, int 7 65e8ec21; bin sha256 6e0f77d7, plugins whole)mre
- ce16a4efdeploy: backup copies index files first and *.mpack data last (hot-copy skew, hybriel #203)mre
- f490bc8cdeploy: Web Push gate tests/push.mjs (PUSH_GATE_PORT)mre
- 3af29fc8calendar mission 008 (A8): report, README, LOG, push-shots.mjsmre
- f91f3663calendar mission 008 (A8): push only asked of the browser when the app has the VAPID settings (pushAvailable); real-FCM E2E scripts (push-real-app.mjs, push-real.mjs)mre
- bf571badcalendar mission 008 (A8): Turn off on this device (unsubscribes the browser, server drops the device, choice kept), subscribe/refresh when the app is shown again; push gate part 3mre
- 50985708calendar mission 008 (A7): Web Push report, README, mission, LOGmre
- ee318774calendar mission 008 (A7): push-real.mjs, push sent log linemre
- ebda0fd1calendar mission 008 (A7): server-side reminder plan on hl:time (push-plan.hl; hl:time fromLocal in a browser static is refused: repro .scratch/hltime-client), push gate 9/9mre
- d063dad3calendar mission 008 (A7): Web Push (WIP): lib/push.hl subscriptions + sender clock, faces calPushSave/calPushOff, button subscribes, VAPID tool, tests/push.mjs; instantOf on hl:timemre
- cf175c80calendar mission 008 (A7): re-vendor Hybriel master 0e3c9830 (bin 9d957fed, plugins whole: hl:crypto Web Push primitives, hl:web sendPush)mre
- 9bf3c38bcalendar mission 008: re-vendor Hybriel f9833544 (gates old = new); Web Push stopped: hl:crypto half missing on mastermre
- 186f969bcalendar mission 007: signed-in gate for the first-pointer race (fails on 3c5604f), README theme section, reportmre