changed: handle new, changed and deleted events.

changed: consider current and future events only
This commit is contained in:
quenousimporte 2023-05-22 17:24:59 +02:00
parent dda25e10d8
commit 9027a0eff3
1 changed files with 53 additions and 44 deletions

89
main.js
View File

@ -1213,18 +1213,19 @@ function togglepassword()
function cvdt(text) function cvdt(text)
{ {
var day = text.substr(0,8); var day = text.substr(0,8);
var time = text.substr(9,6);
return new Date( return new Date(
day.substr(0,4) + "-" + day.substr(0,4),
day.substr(4,2) + "-" + day.substr(4,2),
day.substr(6,2)); day.substr(6,2),
time.substr(0,2),
time.substr(2,2),
time.substr(4,2));
} }
function ics2json(ics) function ics2json(ics)
{ {
// check DTSTAMP if event is modified? var events = [];
var keys = ["UID", "SUMMARY", "DTSTART"];
var events = {};
ics.split("BEGIN:VEVENT").forEach(block => ics.split("BEGIN:VEVENT").forEach(block =>
{ {
var evt = {}; var evt = {};
@ -1233,25 +1234,23 @@ function ics2json(ics)
var tuple = line.split(":"); var tuple = line.split(":");
if (tuple.length > 1) if (tuple.length > 1)
{ {
var key = tuple.shift().split(";")[0]; var field = tuple.shift().split(";")[0];
var value = tuple.join(":"); var value = tuple.join(":");
if (keys.includes(key)) if (field == "DTSTART")
{
if (key == "UID")
{
events[value] = evt;
}
else if (key == "DTSTART")
{ {
evt.DTSTART = cvdt(value); evt.DTSTART = cvdt(value);
} }
else else if (field == "UID" || field == "SUMMARY")
{ {
evt[key] = value; evt[field] = value;
}
} }
} }
}); });
if (evt.UID && evt.SUMMARY && evt.DTSTART)
{
events.push(evt);
}
}); });
return events; return events;
} }
@ -1267,45 +1266,55 @@ function checkevents()
return; return;
} }
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"); var note = getnote("events.json");
if (!note) if (!note)
{ {
note = { note = {
title: "events.json", title: "events.json",
content: "{}" content: "[]"
}; };
localdata.push(note); localdata.push(note);
} }
// check new events var events = ics2json(data.ics);
var existing = JSON.parse(note.content); var existing = JSON.parse(note.content).map(e =>
var newevents = [];
Object.keys(events).forEach(uid =>
{ {
if (!existing[uid]) e.DTSTART = new Date(e.DTSTART);
return e;
});
// keep future only
events = events.filter(e => e.DTSTART >= new Date);
existing = existing.filter(e => e.DTSTART >= new Date);
// check added, deleted, changed
var newcontent = [];
events.forEach(evt =>
{ {
newevents.push(events[uid]); var previous = existing.find(e => e.UID == evt.UID);
console.log("new event found: " + events[uid].SUMMARY) if (!previous)
{
newcontent.push("new event: " + evt.DTSTART.toDateString() + " " + evt.SUMMARY);
}
else if (previous.SUMMARY != evt.SUMMARY)
{
newcontent.push("changed event: " + evt.DTSTART.toDateString() + " " + evt.SUMMARY);
} }
}); });
if (newevents.length) existing.forEach(evt =>
{ {
showtemporaryinfo("New events to check"); if (!events.find(e => e.UID == evt.UID))
{
newcontent.push("deleted event: " + evt.DTSTART.toDateString() + " " + evt.SUMMARY);
}
});
if (newcontent.length)
{
showtemporaryinfo("Calendar changes to check");
var todo = getnote("todo"); var todo = getnote("todo");
var content = todo.content; var content = todo.content;
var newcontent = [];
newevents.forEach(evt =>
{
newcontent.push("new event: " + evt.DTSTART.toDateString() + " " + evt.SUMMARY);
});
todo.content = newcontent.join("\n") + "\n" + content; todo.content = newcontent.join("\n") + "\n" + content;
// reload todo if open // reload todo if open