2
|
1 import { addHours, endOfToday, endOfTomorrow, format, isAfter, isBefore, isToday, isTomorrow, isWithinInterval, parseISO, startOfToday } from "date-fns";
|
11
|
2 import { html, LitElement, TemplateResult } from "lit";
|
2
|
3 import { customElement, property } from "lit/decorators.js";
|
|
4 import { sortBy } from "lodash";
|
|
5 import { DataFactory, NamedNode, Parser, Quad_Predicate, Quad_Subject, Store, Term } from "n3";
|
|
6 import { hideFeeds, hideTitles } from "./private";
|
|
7 import { shared } from "./shared";
|
|
8 const { namedNode } = DataFactory;
|
|
9 export { WeekGuide } from "./WeekGuide";
|
|
10 const EV = "http://bigasterisk.com/event#";
|
|
11 const RDF = "http://www.w3.org/1999/02/22-rdf-syntax-ns#";
|
|
12
|
11
|
13 const es = new EventSource("http://localhost:8005/events");
|
|
14 es.addEventListener("message", (msg) => {
|
|
15 (document.querySelector("#time") as HTMLElement).innerText = JSON.parse(msg.data).time.label;
|
|
16 });
|
4
|
17
|
11
|
18 function notifyWebScraperOnAllDomChanges() {
|
|
19 function sendPostRequest(data: any) {
|
|
20 fetch("https://example.com/api", {
|
|
21 method: "POST",
|
|
22 headers: { "Content-Type": "application/json" },
|
|
23 body: JSON.stringify(data),
|
|
24 })
|
|
25 .then((response) => {
|
|
26 if (!response.ok) {
|
|
27 throw new Error("Network response was not ok");
|
|
28 }
|
|
29 return response.json();
|
|
30 })
|
|
31 .then((data) => {
|
|
32 console.log("POST request successful");
|
|
33 })
|
|
34 .catch((error) => {
|
|
35 console.error("There was a problem with the POST request:", error);
|
|
36 });
|
|
37 }
|
|
38
|
|
39 function handleDomChanges(mutationsList: MutationRecord[], observer: MutationObserver): void {
|
|
40 sendPostRequest({ domChanges: mutationsList });
|
|
41 }
|
|
42
|
|
43 const observer = new MutationObserver(handleDomChanges);
|
|
44 observer.observe(document.body, { subtree: true, childList: true, attributes: true });
|
4
|
45 }
|
|
46
|
2
|
47 function getLiteral(store: Store, graph: Term, subj: Quad_Subject, pred: Quad_Predicate, missing: string | null): string {
|
|
48 let out = null;
|
|
49 store.getObjects(subj, pred, graph).forEach((attr) => {
|
|
50 out = attr.value;
|
|
51 });
|
|
52 if (!out) {
|
|
53 if (missing === null) {
|
|
54 throw new Error();
|
|
55 }
|
|
56 return missing;
|
|
57 }
|
|
58 return out;
|
|
59 }
|
|
60
|
|
61 class DisplayEvent {
|
|
62 constructor(private store: Store, private graph: Term, public uri: Quad_Subject) {}
|
|
63 get title(): string {
|
|
64 return getLiteral(this.store, this.graph, this.uri, namedNode(EV + "title"), "(unnamed)");
|
|
65 }
|
|
66 get start(): string {
|
|
67 return getLiteral(this.store, this.graph, this.uri, namedNode(EV + "start"), null);
|
|
68 }
|
|
69 get feed(): NamedNode {
|
|
70 return namedNode(getLiteral(this.store, this.graph, this.uri, namedNode(EV + "feed"), null));
|
|
71 }
|
|
72 shortDate(): TemplateResult {
|
|
73 const t = parseISO(this.start);
|
|
74 return html`<span class="d">${format(t, "EEE, MMM d,")}</span> <span class="t">${format(t, "HH:mm")}</span>`;
|
|
75 }
|
|
76 show(): boolean {
|
|
77 const now = new Date();
|
|
78 const t = parseISO(this.start);
|
|
79
|
|
80 const start = startOfToday();
|
|
81 let end = endOfToday();
|
|
82 if (isAfter(now, addHours(startOfToday(), 18))) {
|
|
83 end = endOfTomorrow();
|
|
84 }
|
|
85
|
|
86 return isWithinInterval(t, { start, end }) && !hideTitles.has(this.title) && !hideFeeds.has(this.feed.value);
|
|
87 }
|
|
88 toHtml(): TemplateResult {
|
|
89 return html`
|
|
90 <li>
|
|
91 <span class="date">${this.shortDate()}</span> ${this.title}
|
|
92 <!--${this.feed}-->
|
|
93 </li>
|
|
94 `;
|
|
95 }
|
|
96 }
|
|
97
|
|
98 @customElement("fd-upcoming-events")
|
|
99 export class UpcomingEvents extends LitElement {
|
|
100 @property() evs: DisplayEvent[];
|
|
101 constructor() {
|
|
102 super();
|
|
103 this.evs = [];
|
|
104 this.load();
|
|
105 setInterval(this.load.bind(this), 5 * 60 * 1000);
|
|
106 }
|
|
107
|
|
108 async load() {
|
|
109 const store = new Store();
|
11
|
110 const r = await fetch(
|
|
111 "/gcalendarwatch/graph/calendar/upcoming",
|
|
112
|
|
113 {
|
|
114 method: "GET",
|
|
115 headers: {
|
|
116 Accept: "application/json",
|
|
117 "X-Pomerium-Authorization": document.cookie.substring(document.cookie.indexOf("=") + 1),
|
|
118 },
|
|
119 }
|
2
|
120 );
|
|
121 const n3txt = await r.text();
|
|
122 const parser = new Parser({ format: "application/trig" });
|
|
123 parser.parse(n3txt, (error, quad, prefixes) => {
|
|
124 if (quad) {
|
|
125 store.addQuad(quad);
|
|
126 } else {
|
|
127 const graph = namedNode(EV + "gcalendar");
|
|
128 this.evs = [];
|
|
129 store.getSubjects(namedNode(RDF + "type"), namedNode(EV + "Event"), graph).forEach((ev: Quad_Subject) => {
|
|
130 const de = new DisplayEvent(store, graph, ev);
|
|
131 if (de.show()) {
|
|
132 this.evs = [...this.evs, de];
|
|
133 }
|
|
134 });
|
|
135 this.evs = sortBy(this.evs, "start");
|
|
136 }
|
|
137 });
|
|
138 }
|
|
139 static styles = [
|
|
140 shared,
|
|
141 ];
|
|
142 render() {
|
|
143 return html`
|
|
144 <h1 data-text="Calendar">Calendar</h1>
|
|
145 <ol>
|
|
146 ${this.evs.map((d) => d.toHtml())}
|
|
147 </ol>
|
|
148 `;
|
|
149 }
|
|
150 }
|