gitoria

calendar

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit0c8f93640c8f9364Merge branch 'sy-sync'mre0c8f9364/lib/events.hl

5.2 KB

  1. // lib/events.hl — THE EVENTS (ticket calendar#2). One hl:mpackdb table, storage/mpackdb/events.db, pk @id, index owner.
  2. // A record IS a WorldAPI Event (https://worldapi.org/standards/event): the `$`-keys `$type $version $kind $title
  3. // $description $start $end $timezone $location $status $visibility $created $updated`, plus `owner` (the users @id)
  4. // and the @id. Appointment: `$start`/`$end` local wall-clock `YYYY-MM-DDTHH:MM:SS` in `$timezone`. All-day:
  5. // `$kind = 'allday'`, `$start` a date, `$end` the EXCLUSIVE end date (left out for one day). Every event is
  6. // `$visibility = 'private'`: every function takes the owner's user id and reaches only that user's events.
  7. // Every write to the table is here. Looked up in lib/events-helpers.hl: build (page fields → record), viewOf (record →
  8. // what the page shows), isLocalId, byStart. The default reminders are lib/settings.hl.
  9. import { MPackDB } from 'hl:mpackdb'
  10. import { timestamp } from 'hl:time'
  11. import { storageDir, countOfList } from './util.hl'
  12. import { build, viewOf, isLocalId, byStart, maxEvents } from './events-helpers.hl'
  13. import { defaultsOf } from './settings.hl'
  14. import { before } from '../components/month-view/dates.hl'
  15. import { describe, shift } from '../components/month-view/reminders.hl'
  16. import { serverToday } from '../components/month-view/clock.hl'
  17. static eventsTable = new MPackDB(file = storageDir + '/events.db', primaryKey = '@id', indexes = ['owner'])
  18. static ownEvent = (userId, id) => {
  19. if (id == null || hlTypeName(id) != 'String' || id == '') { return null }
  20. e = eventsTable.fetch(id)
  21. if (e == null || e.owner != userId) { return null }
  22. return e
  23. }
  24. // the owner's events that touch first..last (dates), by start
  25. static eventsBetween = (userId, first, last) => {
  26. found = eventsTable.find('owner', userId)
  27. n = countOfList(found)
  28. views = []
  29. let i = 0
  30. while (i < n) {
  31. let v = viewOf(found[i])
  32. if (!before(v.last, first) && !before(last, v.first)) { views.push(v) }
  33. i = i + 1
  34. }
  35. return byStart(views)
  36. }
  37. static realId = (userId, id) => {
  38. if (!isLocalId(id)) { return id }
  39. found = eventsTable.find('owner', userId)
  40. n = countOfList(found)
  41. let i = 0
  42. while (i < n) {
  43. if (found[i].ref == id) { return found[i].id }
  44. i = i + 1
  45. }
  46. return null
  47. }
  48. // create, or change when the fields name an event (real or temporary id)
  49. static saveEvent = (userId, b) => {
  50. if (b != null && hlTypeName(b) == 'Hybrid' && b.id != null && b.id != '') {
  51. id = realId(userId, b.id)
  52. if (id != null) { return changeEvent(userId, id, b) }
  53. if (!isLocalId(b.id)) { return changeEvent(userId, b.id, b) }
  54. }
  55. return createEvent(userId, b)
  56. }
  57. static createEvent = (userId, b) => {
  58. if (countOfList(eventsTable.find('owner', userId)) >= maxEvents) { return { error = 'too many events (at most ' + maxEvents + ')' } }
  59. x = build(b)
  60. if (x.error != null) { return x }
  61. r = x.record
  62. t = timestamp()
  63. r['$created'] = t
  64. r['$updated'] = t
  65. r.owner = userId
  66. if (isLocalId(b.id)) { r.ref = b.id }
  67. let id = eventsTable.put(r)
  68. if (id == null) { return { error = 'could not store the event: ' + eventsTable.lastError() } }
  69. return { id = id }
  70. }
  71. static changeEvent = (userId, id, b) => {
  72. old = ownEvent(userId, id)
  73. if (old == null) { return { error = 'no such event' } }
  74. x = build(b)
  75. if (x.error != null) { return x }
  76. r = x.record
  77. r['$created'] = old['$created']
  78. r['$updated'] = timestamp()
  79. r.owner = userId
  80. if (old.ref != null) { r.ref = old.ref }
  81. r.id = id
  82. eventsTable.update(id, r)
  83. return { id = id }
  84. }
  85. static deleteEvent = (userId, given) => {
  86. id = realId(userId, given)
  87. old = ownEvent(userId, id)
  88. if (old == null) { return { error = 'no such event' } }
  89. eventsTable.delete(id)
  90. return { ok = true }
  91. }
  92. // ---- the reminders still to come ------------------------------------------------------------------------
  93. // the owner's events starting first..last with the reminders that apply (own, else the defaults); timed and all-day
  94. static upcoming = (userId, first, last) => {
  95. defaults = defaultsOf(userId)
  96. found = eventsTable.find('owner', userId)
  97. n = countOfList(found)
  98. out = []
  99. let i = 0
  100. while (i < n) {
  101. let v = viewOf(found[i])
  102. if (!before(v.first, first) && !before(last, v.first)) {
  103. let list = { id = v.id title = v.title allDay = v.allDay startDate = v.startDate startTime = v.startTime place = v.place zone = v.zone rows = v.custom ? v.rows : defaults }
  104. out.push(list)
  105. }
  106. i = i + 1
  107. }
  108. return out
  109. }
  110. // the events with the reminders that apply, from two days back to nine days ahead (what the browser plans its notifications from)
  111. static soonOf = (userId) => {
  112. t = serverToday()
  113. return upcoming(userId, shift(t, -2), shift(t, 9))
  114. }
  115. // ---- what the faces answer (components/calendar.hl) -----------------------------------------------------
  116. // the events of the six weeks on screen, the defaults (text and rows) and the reminders still to come
  117. static calendarView = (userId, first, last) => {
  118. return { events = eventsBetween(userId, first, last) defaults = describe(defaultsOf(userId)) soon = soonOf(userId) defaultRows = defaultsOf(userId) }
  119. }
  120. // the settings editor: the default reminders as rows, their text and the reminders still to come
  121. static settingsView = (userId) => {
  122. return { rows = defaultsOf(userId) defaults = describe(defaultsOf(userId)) soon = soonOf(userId) defaultRows = defaultsOf(userId) }
  123. }

Branches

Latest commits

  • 0c8f9364Merge branch 'sy-sync'mre
  • 9fab1c8fsync components.hybriel 4922f9c into the month view: prev / next 44 px targets, names formatted in UTC (a UTC+12 zone showed the wrong first weekday); the calendar keeps its own language handling (Accept-Language / browser) and the P1/C13 reminder code; gate: 44 px check (SY)mre
  • 27d3e294theme: sync from layouts 4994037 (list rows stack, td word-keep, heading code, edited tap target)mre
  • 2ac9c1bbdeploy: security gate (tests/security.mjs)mre
  • 6b0a2571merge sc-sec (calendar security) with 99-year sessionsmre
  • 92001e52sessions last 99 years (creator 2026-10-23: logins never expire; only sign out ends them)mre
  • 0f76c99dSecurity audit SC: push endpoint SSRF guard, uploadMax 0, Secure cookie, login session rotation + rate limit, event cap, nosniff, security gatemre
  • 2dd53dccP1: one reminder shows once: arm() one at a time, the page skips reminders the server already pushed; push-twice gatemre
  • 3a29760aC13 report (handoff)mre
  • 33ab66ffCalendar: notification controls move from the month view into Reminder settings (user menu), one consistent state, state read when the dialog opensmre
  • 682a5b43reports: calendar gate countsmre
  • 876d22b2reports/H1-hydrate.mdmre
  • ef0cae45tests/dst.mjs: wait for hydration instead of a fixed 2.5 s sleep before clicking #gomre
  • d9cadecatests/cdp.mjs: HL_CPU_THROTTLE=<rate> optional CPU throttling for gate runsmre
  • efa61227re-vendor Hybriel 5406d146 (hl:web batches 9+10: #72 emits while the socket is closed delivered after reconnect, browser boot linear, faster list appends; contains int 6–14 + m406)mre
  • 7808adbecalendar#12: report 011, LOG, STATUS, push-shots-testmre
  • 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