gitoria

calendar

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commitf20313aaf20313aa|||PR bound calendar context and recover state across account changes and resyncsAtlasf20313aa/lib/events.hl

5.9 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 { windowFor } from './month-grid.hl'
  15. import { validMonth } from './dates.hl'
  16. import { before } from './dates.hl'
  17. import { describe, shift } from './reminders.hl'
  18. import { serverToday } from './clock.hl'
  19. static eventsTable = new MPackDB(file = storageDir + '/events.db', primaryKey = '@id', indexes = ['owner'])
  20. static ownEvent = (String userId, id) => {
  21. if (id == null || hlTypeName(id) != 'String' || id == '') { return null }
  22. e = eventsTable.fetch(id)
  23. if (e == null || e.owner != userId) { return null }
  24. return e
  25. }
  26. // the owner's events that touch first..last (dates), by start
  27. static eventsBetween = (String userId, String first, String last) => {
  28. found = eventsTable.find('owner', userId)
  29. n = countOfList(found)
  30. views = []
  31. let i = 0
  32. while (i < n) {
  33. v = viewOf(found[i])
  34. if (!before(v.last, first) && !before(last, v.first)) { views.push(v) }
  35. i = i + 1
  36. }
  37. return byStart(views)
  38. }
  39. static realId = (String userId, id) => {
  40. if (!isLocalId(id)) { return id }
  41. found = eventsTable.find('owner', userId)
  42. n = countOfList(found)
  43. let i = 0
  44. while (i < n) {
  45. if (found[i].ref == id) { return found[i].id }
  46. i = i + 1
  47. }
  48. return null
  49. }
  50. // create, or change when the fields name an event (real or temporary id)
  51. static saveEvent = (String userId, b) => {
  52. if (b != null && hlTypeName(b) == 'Hybrid' && b.id != null && b.id != '') {
  53. id = realId(userId, b.id)
  54. if (id != null) { return changeEvent(userId, id, b) }
  55. if (!isLocalId(b.id)) { return changeEvent(userId, b.id, b) }
  56. }
  57. return createEvent(userId, b)
  58. }
  59. static createEvent = (String userId, b) => {
  60. if (countOfList(eventsTable.find('owner', userId)) >= maxEvents) { return { error = 'too many events (at most ' + maxEvents + ')' } }
  61. x = build(b)
  62. if (x.error != null) { return x }
  63. r = x.record
  64. t = timestamp()
  65. r['$created'] = t
  66. r['$updated'] = t
  67. r.owner = userId
  68. if (isLocalId(b.id)) { r.ref = b.id }
  69. id = eventsTable.put(r)
  70. if (id == null) { return { error = 'could not store the event: ' + eventsTable.lastError() } }
  71. return { id = id }
  72. }
  73. static changeEvent = (String userId, id, b) => {
  74. old = ownEvent(userId, id)
  75. if (old == null) { return { error = 'no such event' } }
  76. x = build(b)
  77. if (x.error != null) { return x }
  78. r = x.record
  79. r['$created'] = old['$created']
  80. r['$updated'] = timestamp()
  81. r.owner = userId
  82. if (old.ref != null) { r.ref = old.ref }
  83. r.id = id
  84. eventsTable.update(id, r)
  85. return { id = id }
  86. }
  87. static deleteEvent = (String userId, given) => {
  88. id = realId(userId, given)
  89. old = ownEvent(userId, id)
  90. if (old == null) { return { error = 'no such event' } }
  91. eventsTable.delete(id)
  92. return { ok = true id = id ref = old.ref }
  93. }
  94. // ---- the reminders still to come ------------------------------------------------------------------------
  95. // the owner's events starting first..last with the reminders that apply (own, else the defaults); timed and all-day
  96. static upcoming = (String userId, String first, String last) => {
  97. defaults = defaultsOf(userId)
  98. found = eventsTable.find('owner', userId)
  99. n = countOfList(found)
  100. out = []
  101. let i = 0
  102. while (i < n) {
  103. v = viewOf(found[i])
  104. if (!before(v.first, first) && !before(last, v.first)) {
  105. 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 }
  106. out.push(list)
  107. }
  108. i = i + 1
  109. }
  110. return out
  111. }
  112. // the events with the reminders that apply, from two days back to nine days ahead (what the browser plans its notifications from)
  113. static soonOf = (String userId) => {
  114. t = serverToday()
  115. return upcoming(userId, shift(t, -2), shift(t, 9))
  116. }
  117. // ---- what the faces answer (components/calendar.hl) -----------------------------------------------------
  118. // the events of the six weeks on screen, the defaults (text and rows) and the reminders still to come
  119. static calendarView = (String userId, String first, String last) => {
  120. return { events = eventsBetween(userId, first, last) defaults = describe(defaultsOf(userId)) soon = soonOf(userId) defaultRows = defaultsOf(userId) }
  121. }
  122. // the settings editor: the default reminders as rows, their text and the reminders still to come
  123. static settingsView = (String userId) => {
  124. return { rows = defaultsOf(userId) defaults = describe(defaultsOf(userId)) soon = soonOf(userId) defaultRows = defaultsOf(userId) }
  125. }
  126. static monthDataFor = (String userId, String address) => {
  127. day = serverToday()
  128. window = windowFor(address, day)
  129. return calendarView(userId, window.first, window.last)
  130. }
  131. static contextFor = (String userId, String day) => {
  132. return { events = eventsBetween(userId, shift(day, -42), shift(day, 42)) soon = upcoming(userId, shift(day, -2), shift(day, 9)) defaults = describe(defaultsOf(userId)) defaultRows = defaultsOf(userId) }
  133. }
  134. static eventDelta = (String userId, String id) => {
  135. record = ownEvent(userId, id)
  136. if (record == null) { return [{ =id deleted = true }] }
  137. value = viewOf(record)
  138. value.ref = record.ref
  139. return [value]
  140. }

Branches

Latest commits

  • f20313aa|||PR bound calendar context and recover state across account changes and resyncsAtlas
  • 651752c0|||PR run the browser gate once per deployAtlas
  • e24e28a1|||PR preserve calendar state across month addresses and split dialogsAtlas
  • 384d3d23|||PR give calendar months their own addressAtlas
  • fff2f537Merge branch 'calendar31-lib' into mainAtlas
  • 07cde3dc|||PR keep worker records out of the repo; README vendor is day72Atlas
  • 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