calendar
All repositories: gitoria
5.2 KB
// lib/events.hl — THE EVENTS (ticket calendar#2). One hl:mpackdb table, storage/mpackdb/events.db, pk @id, index owner.// A record IS a WorldAPI Event (https://worldapi.org/standards/event): the `$`-keys `$type $version $kind $title// $description $start $end $timezone $location $status $visibility $created $updated`, plus `owner` (the users @id)// and the @id. Appointment: `$start`/`$end` local wall-clock `YYYY-MM-DDTHH:MM:SS` in `$timezone`. All-day:// `$kind = 'allday'`, `$start` a date, `$end` the EXCLUSIVE end date (left out for one day). Every event is// `$visibility = 'private'`: every function takes the owner's user id and reaches only that user's events.// Every write to the table is here. Looked up in lib/events-helpers.hl: build (page fields → record), viewOf (record →// what the page shows), isLocalId, byStart. The default reminders are lib/settings.hl.import { MPackDB } from 'hl:mpackdb'import { timestamp } from 'hl:time'import { storageDir, countOfList } from './util.hl'import { build, viewOf, isLocalId, byStart, maxEvents } from './events-helpers.hl'import { defaultsOf } from './settings.hl'import { before } from './dates.hl'import { describe, shift } from './reminders.hl'import { serverToday } from './clock.hl'static eventsTable = new MPackDB(file = storageDir + '/events.db', primaryKey = '@id', indexes = ['owner'])static ownEvent = (String userId, id) => {if (id == null || hlTypeName(id) != 'String' || id == '') { return null }e = eventsTable.fetch(id)if (e == null || e.owner != userId) { return null }return e}// the owner's events that touch first..last (dates), by startstatic eventsBetween = (String userId, String first, String last) => {found = eventsTable.find('owner', userId)n = countOfList(found)views = []let i = 0while (i < n) {v = viewOf(found[i])if (!before(v.last, first) && !before(last, v.first)) { views.push(v) }i = i + 1}return byStart(views)}static realId = (String userId, id) => {if (!isLocalId(id)) { return id }found = eventsTable.find('owner', userId)n = countOfList(found)let i = 0while (i < n) {if (found[i].ref == id) { return found[i].id }i = i + 1}return null}// create, or change when the fields name an event (real or temporary id)static saveEvent = (String userId, b) => {if (b != null && hlTypeName(b) == 'Hybrid' && b.id != null && b.id != '') {id = realId(userId, b.id)if (id != null) { return changeEvent(userId, id, b) }if (!isLocalId(b.id)) { return changeEvent(userId, b.id, b) }}return createEvent(userId, b)}static createEvent = (String userId, b) => {if (countOfList(eventsTable.find('owner', userId)) >= maxEvents) { return { error = 'too many events (at most ' + maxEvents + ')' } }x = build(b)if (x.error != null) { return x }r = x.recordt = timestamp()r['$created'] = tr['$updated'] = tr.owner = userIdif (isLocalId(b.id)) { r.ref = b.id }id = eventsTable.put(r)if (id == null) { return { error = 'could not store the event: ' + eventsTable.lastError() } }return { id = id }}static changeEvent = (String userId, id, b) => {old = ownEvent(userId, id)if (old == null) { return { error = 'no such event' } }x = build(b)if (x.error != null) { return x }r = x.recordr['$created'] = old['$created']r['$updated'] = timestamp()r.owner = userIdif (old.ref != null) { r.ref = old.ref }r.id = ideventsTable.update(id, r)return { id = id }}static deleteEvent = (String userId, given) => {id = realId(userId, given)old = ownEvent(userId, id)if (old == null) { return { error = 'no such event' } }eventsTable.delete(id)return { ok = true }}// ---- the reminders still to come ------------------------------------------------------------------------// the owner's events starting first..last with the reminders that apply (own, else the defaults); timed and all-daystatic upcoming = (String userId, String first, String last) => {defaults = defaultsOf(userId)found = eventsTable.find('owner', userId)n = countOfList(found)out = []let i = 0while (i < n) {v = viewOf(found[i])if (!before(v.first, first) && !before(last, v.first)) {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 }out.push(list)}i = i + 1}return out}// the events with the reminders that apply, from two days back to nine days ahead (what the browser plans its notifications from)static soonOf = (String userId) => {t = serverToday()return upcoming(userId, shift(t, -2), shift(t, 9))}// ---- what the faces answer (components/calendar.hl) -----------------------------------------------------// the events of the six weeks on screen, the defaults (text and rows) and the reminders still to comestatic calendarView = (String userId, String first, String last) => {return { events = eventsBetween(userId, first, last) defaults = describe(defaultsOf(userId)) soon = soonOf(userId) defaultRows = defaultsOf(userId) }}// the settings editor: the default reminders as rows, their text and the reminders still to comestatic settingsView = (String userId) => {return { rows = defaultsOf(userId) defaults = describe(defaultsOf(userId)) soon = soonOf(userId) defaultRows = defaultsOf(userId) }}
Branches
Latest commits
- 189c4c8c|||PR WIP calendar28: replace visual class state with View conditionsAtlas
- 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