gitoria

calendar

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commitb99cce69b99cce69|||PR Update calendar login and logout through session pushesAtlasb99cce69/lib/events-helpers.hl

5.1 KB

  1. // lib/events-helpers.hl — what lib/events.hl looks up (calendar mission 001, code order): the size limits, what the
  2. // page sends → a WorldAPI Event record (build), a stored event → what the page shows (viewOf), the temporary ids of
  3. // events made offline, the window check of the faces and the order by start.
  4. import { isText, isDate, isTime, isZone, nextDay, prevDay, before } from './dates.hl'
  5. import { envOr } from './util.hl'
  6. import { toReminders, toRows } from './reminders.hl'
  7. static maxTitle = 200
  8. static maxPlace = 200
  9. static maxNotes = 5000
  10. // events per user (security mission SC: a signed-in user could fill the disk): CALENDAR_MAX_EVENTS
  11. static maxEvents = toNumber(envOr('CALENDAR_MAX_EVENTS', '20000'))
  12. // what the page sends: { id?, custom, reminders (rows), title, allDay, startDate, startTime, endDate, endTime, place, notes, zone }
  13. // → { error } or { record } (a WorldAPI Event without owner/id). endDate/endTime may be empty (no end); all-day: the
  14. // last day, inclusive ('' = one day).
  15. static build = (b) => {
  16. if (b == null || hlTypeName(b) != 'Hybrid') { return { error = 'the event must be an object' } }
  17. if (!isText(b.title) || b.title.trim() == '') { return { error = 'the event needs a title' } }
  18. if (b.title.length > maxTitle) { return { error = 'the title is too long (at most ' + maxTitle + ' characters)' } }
  19. if (!isText(b.place) || b.place.length > maxPlace) { return { error = 'the place is too long (at most ' + maxPlace + ' characters)' } }
  20. if (!isText(b.notes) || b.notes.length > maxNotes) { return { error = 'the notes are too long (at most ' + maxNotes + ' characters)' } }
  21. if (b.allDay != true && b.allDay != false) { return { error = 'allDay must be true or false' } }
  22. if (!isDate(b.startDate)) { return { error = 'the start date is not a date' } }
  23. if (!isZone(b.zone)) { return { error = 'the time zone is missing' } }
  24. r = {}
  25. r['$type'] = 'event'
  26. r['$version'] = 1
  27. r['$title'] = b.title.trim()
  28. if (b.notes.trim() != '') { r['$description'] = b.notes }
  29. if (b.place.trim() != '') { r['$location'] = b.place.trim() }
  30. r['$status'] = 'confirmed'
  31. r['$visibility'] = 'private'
  32. // no own reminders: `$reminders` stays out and the calendar's apply; own ones (also none at all) are written
  33. if (b.custom == true) {
  34. rem = toReminders(b.reminders)
  35. if (rem.error != null) { return { error = rem.error } }
  36. r['$reminders'] = rem.reminders
  37. }
  38. if (b.allDay) {
  39. r['$kind'] = 'allday'
  40. r['$start'] = b.startDate
  41. if (isText(b.endDate) && b.endDate != '') {
  42. if (!isDate(b.endDate)) { return { error = 'the end date is not a date' } }
  43. if (before(b.endDate, b.startDate)) { return { error = 'the end is before the start' } }
  44. if (b.endDate != b.startDate) { r['$end'] = nextDay(b.endDate) }
  45. }
  46. return { record = r }
  47. }
  48. if (!isTime(b.startTime)) { return { error = 'the start time is not a time' } }
  49. r['$kind'] = 'appointment'
  50. r['$timezone'] = b.zone
  51. r['$start'] = b.startDate + 'T' + b.startTime + ':00'
  52. hasEnd = (isText(b.endDate) && b.endDate != '') || (isText(b.endTime) && b.endTime != '')
  53. if (hasEnd) {
  54. ed = isText(b.endDate) && b.endDate != '' ? b.endDate : b.startDate
  55. if (!isDate(ed)) { return { error = 'the end date is not a date' } }
  56. if (!isTime(b.endTime)) { return { error = 'the end time is not a time' } }
  57. end = ed + 'T' + b.endTime + ':00'
  58. if (before(end, r['$start'])) { return { error = 'the end is before the start' } }
  59. r['$end'] = end
  60. }
  61. return { record = r }
  62. }
  63. // what the page shows of a stored event: first / last day (inclusive), times, texts
  64. static viewOf = (Hybrid e) => {
  65. allDay = e['$kind'] == 'allday'
  66. start = e['$start']
  67. end = e['$end']
  68. first = start.slice(0, 10)
  69. let last = first
  70. if (allDay) {
  71. if (end != null) { last = prevDay(end) }
  72. } else if (end != null) {
  73. last = end.slice(0, 10)
  74. }
  75. v = { id = e.id title = e['$title'] allDay = allDay first = first last = last
  76. startDate = first startTime = allDay ? '' : start.slice(11, 16)
  77. endDate = end == null ? '' : last endTime = allDay || end == null ? '' : end.slice(11, 16)
  78. place = e['$location'] == null ? '' : e['$location'] notes = e['$description'] == null ? '' : e['$description']
  79. start = start zone = e['$timezone'] == null ? '' : e['$timezone']
  80. custom = e['$reminders'] != null rows = e['$reminders'] == null ? [] : toRows(e['$reminders']) }
  81. return v
  82. }
  83. // an event made offline carries a temporary id `local-…` until the page reloads; the server keeps it as `ref`, so an edit
  84. // (or delete) sent with that id reaches the same event instead of making a second one
  85. static isLocalId = (id) => { return isText(id) && id.length > 6 && id.length <= 60 && id.slice(0, 6) == 'local-' }
  86. // the window is two dates the page shows; anything else is refused
  87. static validWindow = (first, last) => { return isDate(first) && isDate(last) }
  88. // the views ordered by start (a hand-written selection sort)
  89. static byStart = (Hybrid views) => {
  90. m = views.length
  91. out = []
  92. used = []
  93. let i = 0
  94. while (i < m) { used.push(false) i = i + 1 }
  95. let round = 0
  96. while (round < m) {
  97. let best = -1
  98. let j = 0
  99. while (j < m) {
  100. if (!used[j] && (best < 0 || before(views[j].start, views[best].start))) { best = j }
  101. j = j + 1
  102. }
  103. used[best] = true
  104. out.push(views[best])
  105. round = round + 1
  106. }
  107. return out
  108. }

Branches

Latest commits

  • b99cce69|||PR Update calendar login and logout through session pushesAtlas
  • 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
  • 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