gitoria

calendar

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commita753b0aba753b0ab|||PR move shared calendar logic to libAtlasa753b0ab/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 './dates.hl'
  15. import { describe, shift } from './reminders.hl'
  16. import { serverToday } from './clock.hl'
  17. static eventsTable = new MPackDB(file = storageDir + '/events.db', primaryKey = '@id', indexes = ['owner'])
  18. static ownEvent = (String 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 = (String userId, String first, String last) => {
  26. found = eventsTable.find('owner', userId)
  27. n = countOfList(found)
  28. views = []
  29. let i = 0
  30. while (i < n) {
  31. 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 = (String 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 = (String 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 = (String 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. 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 = (String 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 = (String 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 = (String userId, String first, String 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. v = viewOf(found[i])
  102. if (!before(v.first, first) && !before(last, v.first)) {
  103. 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 = (String 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 = (String userId, String first, String 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 = (String userId) => {
  122. return { rows = defaultsOf(userId) defaults = describe(defaultsOf(userId)) soon = soonOf(userId) defaultRows = defaultsOf(userId) }
  123. }

Branches

Latest commits

  • a753b0ab|||PR move shared calendar logic to libAtlas
  • b32a921dMerge branch 'vendor-day72' into mainAtlas
  • 60a0dd19|||PR Hybriel day72 (00c9dbb8) instead of the withdrawn day73Atlas
  • 53f5464dMerge branch 'calendar26-day73' into mainAtlas
  • 338499ec|||PR Keep reminder settings open during editing and rejected savesAtlas
  • ad9e8d59|||PR Use framework page and popover state, Hybriel day73Atlas
  • cab224a1Re-vendor calendar to verified Hybriel day73Atlas
  • f9b8f918Merge branch 'calendar-vendor-day61' into mainAtlas
  • 66341793|||PR Vendor Hybriel day61Atlas
  • 36fc596bMerge branch 'vendor-day59' into mainAtlas
  • b37cc1da|||PR Vendor Hybriel day59Atlas
  • 05665102Merge branch 'vendor-day56' into mainAtlas
  • 687296fb|||PR Vendor Hybriel day56Atlas
  • a6166d4eMerge branch 'hl-standard-3' into mainAtlas
  • 6d0eb3b0|||PR Calendar day add buttons only when signed in (Kunibert minor)Atlas
  • add4b499|||PR Make calendar day actions keyboard accessibleAtlas
  • 79e49b4dMerge branch 'hl-standard' into mainAtlas
  • e9fec7bb|||PR HL-STANDARD clean-up: typed parameters, plain locals, by-reference cacheAtlas
  • eff166a4Re-vendor complete Hybriel plugins and runtime to day44mre
  • cef81394Wait for the DST fixture page instead of an unused socketmre