added: feature to check new events from a public ics file

This commit is contained in:
quenousimporte 2023-04-24 09:30:46 +02:00
parent 0956b09a5b
commit b47570729f
3 changed files with 115 additions and 1 deletions

View File

@ -2,6 +2,8 @@
// global settings // global settings
$datafile = '../data/data.json'; $datafile = '../data/data.json';
$icsfile = 'ics url';
$password = ''; $password = '';
// check authent // check authent
@ -37,6 +39,12 @@ else if (isset($_POST['action']))
} }
break; break;
case 'cal':
$result = array();
$result["ics"] = file_get_contents($icsfile);
echo json_encode($result);
break;
default: default:
echo '{"error": "unknown action ' . $action . '"}'; echo '{"error": "unknown action ' . $action . '"}';
break; break;

106
main.js
View File

@ -1208,6 +1208,7 @@ function init()
localdata = data; localdata = data;
window.localStorage.setItem("remote", JSON.stringify(data)); window.localStorage.setItem("remote", JSON.stringify(data));
loadlast(); loadlast();
checkevents();
}) })
.catch(remotecallfailed); .catch(remotecallfailed);
} }
@ -1239,6 +1240,111 @@ function togglepassword()
password.focus(); 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) function queryremote(params)
{ {
return new Promise( (apply, failed) => { return new Promise( (apply, failed) => {

View File

@ -2,7 +2,7 @@
## getting started ## 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. your notes are stored in your browser local storage.