From b47570729f3917fccdb5c2106a7f6fff63abc834 Mon Sep 17 00:00:00 2001 From: quenousimporte Date: Mon, 24 Apr 2023 09:30:46 +0200 Subject: [PATCH] added: feature to check new events from a public ics file --- handler.php | 8 ++++ main.js | 106 ++++++++++++++++++++++++++++++++++++++++++++++++++++ readme.md | 2 +- 3 files changed, 115 insertions(+), 1 deletion(-) diff --git a/handler.php b/handler.php index a0e4285..22fe12c 100644 --- a/handler.php +++ b/handler.php @@ -2,6 +2,8 @@ // global settings $datafile = '../data/data.json'; +$icsfile = 'ics url'; + $password = ''; // check authent @@ -37,6 +39,12 @@ else if (isset($_POST['action'])) } break; + case 'cal': + $result = array(); + $result["ics"] = file_get_contents($icsfile); + echo json_encode($result); + break; + default: echo '{"error": "unknown action ' . $action . '"}'; break; diff --git a/main.js b/main.js index 6734aa9..c91c98a 100644 --- a/main.js +++ b/main.js @@ -1208,6 +1208,7 @@ function init() localdata = data; window.localStorage.setItem("remote", JSON.stringify(data)); loadlast(); + checkevents(); }) .catch(remotecallfailed); } @@ -1239,6 +1240,111 @@ function togglepassword() password.focus(); } +function cvdt(text) +{ + var day = text.substr(0,8); + return new Date( + day.substr(0,4) + "-" + + day.substr(4,2) + "-" + + day.substr(6,2)); +} + +function ics2json(ics) +{ + // check DTSTAMP if event is modified? + + var keys = ["UID", "SUMMARY", "DTSTART"]; + var events = {}; + ics.split("BEGIN:VEVENT").forEach(block => + { + var evt = {}; + block.split("\r\n").forEach(line => + { + var tuple = line.split(":"); + if (tuple.length > 1) + { + var key = tuple.shift().split(";")[0]; + var value = tuple.join(":"); + if (keys.includes(key)) + { + if (key == "UID") + { + events[value] = evt; + } + else if (key == "DTSTART") + { + evt.DTSTART = cvdt(value); + } + else + { + evt[key] = value; + } + } + } + }); + }); + return events; +} + +function checkevents() +{ + queryremote({action: "cal"}) + .then(data => + { + var events = ics2json(data.ics); + + // todo: keep future only + /*var today = new Date(); + today.setHours(0,0,0,0); + events = events.filter(e => e.DTSTART >= today);*/ + + var note = getnote("events.json"); + if (!note) + { + note = { + title: "events.json", + content: "{}" + }; + localdata.push(note); + } + + // check new events + var existing = JSON.parse(note.content); + var newevents = []; + Object.keys(events).forEach(uid => + { + if (!existing[uid]) + { + newevents.push(events[uid]); + console.log("new event found: " + events[uid].SUMMARY) + } + }); + + if (newevents.length) + { + showtemporaryinfo("New events to check"); + var todo = getnote("todo"); + var content = todo.content; + var newcontent = ""; + newevents.forEach(evt => + { + newcontent += "new event: " + evt.DTSTART.toDateString() + " " + evt.SUMMARY + "\n"; + }); + todo.content = newcontent + "---\n" + content; + + // reload todo if open + if (currentnote == todo) + { + bindfile(todo); + } + + note.content = JSON.stringify(events); + datachanged(); + } + }) + .catch(remotecallfailed); +} + function queryremote(params) { return new Promise( (apply, failed) => { diff --git a/readme.md b/readme.md index 11a2fd4..b358482 100644 --- a/readme.md +++ b/readme.md @@ -2,7 +2,7 @@ ## getting started -navigate to index.html from your own web server or local computer, or try https://notes.ouvaton.org. +launch index.html from your web server or local computer, or try https://notes.ouvaton.org. your notes are stored in your browser local storage.