2023-08-25 19:12:49 +00:00
|
|
|
# SPDX-FileCopyrightText: ☭ Emery Hemingway
|
|
|
|
# SPDX-License-Identifier: Unlicense
|
|
|
|
|
|
|
|
import std/[json, sequtils, tables, uri]
|
2024-03-15 12:22:44 +00:00
|
|
|
import preserves, preserves/jsonhooks, syndicate, syndicate/relays
|
2023-08-25 19:12:49 +00:00
|
|
|
|
|
|
|
import ./icalendar_components
|
|
|
|
|
|
|
|
type BootArgs {.preservesDictionary.} = object
|
|
|
|
dataspace: Cap
|
|
|
|
|
|
|
|
proc importCalendar(location: string): Component =
|
|
|
|
let uri = parseUri(location)
|
|
|
|
if uri.scheme != "file":
|
|
|
|
raiseAssert """only supported calendar schema is "file" ignoring """ & location
|
|
|
|
else:
|
|
|
|
var
|
|
|
|
js = parseFile(uri.path)
|
2024-03-15 12:22:44 +00:00
|
|
|
pr = jsonhooks.toPreservesHook(js)
|
|
|
|
if not result.fromPreserves(pr):
|
2023-08-25 19:12:49 +00:00
|
|
|
# TODO: convert directly from JsonNode
|
|
|
|
raise newException(ValueError, "Preserves was not a valid iCalendar component")
|
|
|
|
if result.label != "vcalendar":
|
|
|
|
raise newException(ValueError, "Preserves was not a valid vcalendar @component")
|
|
|
|
|
2024-03-15 12:23:50 +00:00
|
|
|
proc toPreserveHook(prop: Property): Value =
|
2023-08-25 19:12:49 +00:00
|
|
|
initRecord(prop.label,
|
2024-03-15 12:23:50 +00:00
|
|
|
prop.parameters.toPreserves,
|
|
|
|
prop.`type`.toPreserves,
|
|
|
|
prop.values.toPreserves,
|
2023-08-25 19:12:49 +00:00
|
|
|
)
|
|
|
|
|
2024-03-15 12:23:50 +00:00
|
|
|
proc toPreserveHook(comp: Component): Value =
|
2023-08-25 19:12:49 +00:00
|
|
|
initRecord(comp.label,
|
2024-03-15 12:23:50 +00:00
|
|
|
comp.properties.toPreserves,
|
|
|
|
comp.components.toPreserves,
|
2023-08-25 19:12:49 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
proc serve(turn: var Turn; url: string): Cap =
|
|
|
|
let ds = newDataspace(turn)
|
|
|
|
let calendar = importCalendar(url)
|
|
|
|
run(turn.facet) do (turn: var Turn):
|
|
|
|
for prop in calendar.properties:
|
|
|
|
discard publish(turn, ds, prop)
|
|
|
|
for comp in calendar.components:
|
|
|
|
discard publish(turn, ds, comp)
|
|
|
|
ds
|
|
|
|
|
|
|
|
runActor("icalendar_actor") do (root: Cap; turn: var Turn):
|
2024-03-15 12:22:44 +00:00
|
|
|
connectStdio(turn, root)
|
2023-08-25 19:12:49 +00:00
|
|
|
stderr.writeLine "connected stdio to ", root
|
2024-03-15 12:22:44 +00:00
|
|
|
during(turn, root, ?:BootArgs) do (ds: Cap):
|
2023-08-25 19:12:49 +00:00
|
|
|
stderr.writeLine "got dataspace at ", ds
|
|
|
|
during(turn, ds, ?Observe(pattern: !CalendarDataspace) ?? {0: grabLit()}) do (url: string):
|
|
|
|
let cap = serve(turn, url)
|
|
|
|
stderr.writeLine "serving ", url, " at ", cap
|
2024-03-15 12:23:50 +00:00
|
|
|
discard publish(turn, ds, initRecord("calendar", url.toPreserves, cap.embed))
|