Skip to content

Commit

Permalink
parsing caldav
Browse files Browse the repository at this point in the history
  • Loading branch information
strukturart committed Feb 17, 2024
1 parent 0aa1589 commit 1f65575
Show file tree
Hide file tree
Showing 5 changed files with 143 additions and 161 deletions.
171 changes: 74 additions & 97 deletions application/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,7 @@ async function load_caldav(callback = false) {

let cache_caldav = async function () {
closing_prohibited = true;
let data_to_store;

// Load data from every account
for (const item of accounts) {
Expand All @@ -518,7 +519,7 @@ let cache_caldav = async function () {
calendar: calendars[i],
});
//cache data
let data_to_store = {
data_to_store = {
displayName: calendars[i].displayName,
syncToken: calendars[i].syncToken,
ctag: calendars[i].ctag,
Expand All @@ -542,6 +543,7 @@ let cache_caldav = async function () {
closing_prohibited = false;

side_toaster('Data synchronized', 3000);
return data_to_store;
} catch (e) {
console.log(e);
closing_prohibited = false;
Expand Down Expand Up @@ -756,43 +758,21 @@ export let create_caldav = async function (
event.url = result.url;
event.isCaldav = true;
event.id = calendar_id;
event.calendar_name = calendar_name;
document.querySelector('.loading-spinner').style.display = 'none';
cache_caldav().then((e) => {});

return event;
} catch (e) {
console.log(e);
}

if (
'serviceWorker' in navigator &&
navigator.serviceWorker.controller
) {
collectionOfData = {
data: event_data,
etag: event.etag,
url: event.url,
};
navigator.serviceWorker.controller.postMessage({
type: 'parse',
t: collectionOfData,
e: event.id,
callback: false,
store: false,
});
}

} else {
document.querySelector('.loading-spinner').style.display = 'none';

cache_caldav().then(() => {});
} else {
side_toaster(
'There was a problem saving, please try again later.',
5000
);
setTimeout(function () {
try {
sort_array(events, 'dateStartUnix', 'number');
} catch (e) {
console.log(e);
}
}, 5000);
}
}
} catch (e) {
Expand Down Expand Up @@ -855,7 +835,8 @@ export let delete_caldav = async function (etag, url, account_id, uid) {
);
setTimeout(function () {
try {
sort_array(events, 'dateStartUnix', 'number');
sort_array(parsed_events, 'dateStartUnix', 'number');
style_calendar_cell(currentYear, currentMonth);
} catch (e) {
console.log(e);
}
Expand Down Expand Up @@ -1088,7 +1069,7 @@ const event_check = function (date) {
new Date(a.dateStart).getTime() - new Date(b.dateStart).getTime()
);

for (let i = 1; i < sortedparsed_events.length; i++) {
for (let i = 1; i < sortedEvents.length; i++) {
const prevEventEndTime = new Date(sortedEvents[i - 1].dateEnd).getTime();
const currentEventStartTime = new Date(
sortedEvents[i].dateStart
Expand Down Expand Up @@ -1803,7 +1784,6 @@ var page_events = {
class: 'flex justify-content-spacearound',
oncreate: function (vnode) {
vnode.dom.style.display = 'none';
console.log(events);
},
onfocus: () => {
window.scroll({
Expand Down Expand Up @@ -2441,7 +2421,7 @@ export let page_options = {
parsed_events.length > 0
? [
m('option', { value: '-' }, '-'),
...events
...parsed_events
.map((e) => e.CATEGORIES)
.filter(
(category, index, array) =>
Expand Down Expand Up @@ -3944,25 +3924,6 @@ let delete_account = function () {
});
};

//load indexedDB
/*
localforage
.getItem('events')
.then(function (value) {
if (value != null) {
events = value;
try {
style_calendar_cell(currentYear, currentMonth);
} catch (e) {
console.log(e);
}
} else {
}
})
.catch(function (err) {});
*/

localforage
.getItem('subscriptions')
.then(function (s) {
Expand Down Expand Up @@ -4336,8 +4297,6 @@ let store_event = function (db_id, cal_name) {
'event-notification-time'
).value;

let bn = notification_time;

let calc_notification;
if (notification_time !== 'none') {
calc_notification = new Date(convert_dt_start);
Expand Down Expand Up @@ -4374,7 +4333,7 @@ let store_event = function (db_id, cal_name) {
'LAST-MODIFIED':
';TZID=' + settings.timezone + convert_ics_date(new Date()),
CLASS: 'PRIVATE',
DTSTAMP: ';TZID=' + settings.timezone + convert_ics_date(convert_dt_start),
DTSTAMP: ';TZID=' + settings.timezone + convert_ics_date(new Date()),
DTSTART:
';TZID=' + settings.timezone + convert_ics_date(convert_dt_start, allDay),
DTEND:
Expand Down Expand Up @@ -4433,6 +4392,7 @@ let store_event = function (db_id, cal_name) {
event.DTEND +
'\nDTSTAMP' +
event.DTSTAMP +
'\nCLASS:Private' +
'\nLOCATION:' +
event.LOCATION +
'\nDESCRIPTION:' +
Expand All @@ -4441,19 +4401,19 @@ let store_event = function (db_id, cal_name) {
event.CATEGORIES +
'\nEND:VEVENT\nEND:VCALENDAR';

if (event.id == 'local-id') {
if (event.RRULE != '') {
event.DTEND =
';TZID=' + settings.timezone + convert_ics_date(rrule_dt_end, allDay);
}
if (event.RRULE != '') {
event.DTEND =
';TZID=' + settings.timezone + convert_ics_date(rrule_dt_end, allDay);
}

if (event.alarm !== 'none') {
event.BEGIN = 'VALARM';
event['TRIGGER;VALUE=DATE-TIME'] = notification_time;
event.ACTION = 'AUDIO';
event.END = 'VALARM';
}
if (event.alarm !== 'none') {
event.BEGIN = 'VALARM';
event['TRIGGER;VALUE=DATE-TIME'] = notification_time;
event.ACTION = 'AUDIO';
event.END = 'VALARM';
}

if (event.id == 'local-id') {
try {
dd = dd.trim();

Expand Down Expand Up @@ -4481,28 +4441,17 @@ let store_event = function (db_id, cal_name) {
}
} else {
//caldav
//rrule event should end on the same day, but rrule.until should set the end date

if (event.RRULE != '') {
event.DTEND =
';TZID=' + settings.timezone + convert_ics_date(rrule_dt_end, allDay);
}

if (event.alarm !== 'none') {
event.BEGIN = 'VALARM';
event['TRIGGER;VALUE=DATE-TIME'] = notification_time;
event.ACTION = 'AUDIO';
event.END = 'VALARM';
}

let alarm = ''; // Initialize alarm as an empty string
let alarm = '';

if (notification_time !== null && notification_time !== 'none') {
alarm = `\nBEGIN:VALARM\nTRIGGER;VALUE=DATE-TIME:${notification_time}\nACTION:AUDIO\nEND:VALARM`;
}

let event_data =
'BEGIN:VCALENDAR\nVERSION:2.0\nPRODID:-//ZContent.net//Greg Calendar 1.0//EN\nCALSCALE:GREGORIAN\nBEGIN:VEVENT\nSUMMARY:' +
'BEGIN:VCALENDAR\nVERSION:2.0\nPRODID:-//ZContent.net//Greg Calendar 1.0//EN\nCALSCALE:GREGORIAN\nX-WR-CALNAME:' +
cal_name +
'\nBEGIN:VEVENT\nSUMMARY:' +
event.SUMMARY +
'\nUID:' +
event.UID +
Expand All @@ -4514,6 +4463,7 @@ let store_event = function (db_id, cal_name) {
event.DTEND +
'\nDTSTAMP' +
event.DTSTAMP +
'\nCLASS:Private' +
'\nLOCATION:' +
event.LOCATION +
'\nDESCRIPTION:' +
Expand All @@ -4529,11 +4479,27 @@ let store_event = function (db_id, cal_name) {
event_data = event_data.replace('RRULE:null', '');
event_data = event_data.replace('RRULE:', '');
}
// Remove empty lines
event_data = event_data.replace(/\bDESCRIPTION:[^\S\n]*\n/g, '');
event_data = event_data.replace(/\bLOCATION:[^\S\n]*\n/g, '');
event_data = event_data.replace(/\bCATEGORIES:[^\S\n]*\n/g, '');
event_data = event_data.replace(/^\s*[\r\n]/gm, '');

event_data = event_data.trim();

create_caldav(event_data, event.id, cal_name, event, event.UID).then(() => {
get_last_view();
create_caldav(event_data, db_id, cal_name, event, event.UID).then((e) => {
try {
navigator.serviceWorker.controller.postMessage({
type: 'parse',
t: { uid: e.UID, data: dd, url: e.url, etag: e.etag },
e: e.calendar_name,
callback: false,
store: false,
});
get_last_view();
} catch (e) {
console.log('send to sw');
}
});
}
};
Expand Down Expand Up @@ -5202,24 +5168,28 @@ function shortpress_action(param) {
}

if (m == 'filter-last-modified') {
sort_array(events, 'lastmod', 'date', 'asc').then(() => {
sort_array(parsed_events, 'lastmod', 'date', 'asc').then(() => {
document.querySelector('#search').focus();
set_tabindex();
});
}

if (m == 'filter-asc') {
sort_array(events, 'dateStartUnix', 'date', 'asc').then(() => {
document.querySelector('#search').focus();
set_tabindex();
});
sort_array(parsed_events, 'dateStartUnix', 'date', 'asc').then(
() => {
document.querySelector('#search').focus();
set_tabindex();
}
);
}

if (m == 'filter-desc') {
sort_array(events, 'dateStartUnix', 'date', 'desc').then(() => {
document.querySelector('#search').focus();
set_tabindex();
});
sort_array(parsed_events, 'dateStartUnix', 'date', 'desc').then(
() => {
document.querySelector('#search').focus();
set_tabindex();
}
);
}
document.querySelector('#filter-menu').style.display = 'none';
document.querySelectorAll('#filter-menu button').forEach((e) => {
Expand Down Expand Up @@ -5387,7 +5357,7 @@ channel.addEventListener('message', (event) => {
}

//callback from Google OAuth
//ugly method to open a new window, because a window from sw clients.open can no longer be closed
//ugly method to open a new window, because a window from sw clients.open can not be closed

if (event.data.oauth_success) {
const l = event.data.oauth_success;
Expand All @@ -5403,22 +5373,25 @@ channel.addEventListener('message', (event) => {
} catch (e) {}

if (
event.data.content !== false &&
event.data.content.parsed_data !== false &&
(calendar_not_visible.length === 0 ||
calendar_not_visible.indexOf(
event.data.content.parsed_data.calendar_name
) === -1)
) {
parsed_events.push(event.data.content.parsed_data);

//notify user when data stored

if (event.data.content.callback) {
side_toaster('event imported', 5000);
}
//store data
//when importing data
//When importing, the data is first parsed and then the RAW and parsed data are returned
if (event.data.content.raw_data) {
local_account.data.push({
UID: uid(),
UID: uid(32),
data: event.data.content.raw_data,
});

Expand All @@ -5431,6 +5404,8 @@ channel.addEventListener('message', (event) => {
console.log(e);
});
}
} else {
console.log('something is wrong' + event.data.content);
}

if (interval_is_running == false) {
Expand All @@ -5440,6 +5415,7 @@ channel.addEventListener('message', (event) => {
if (event.data.action == 'error') {
try {
document.getElementById('icon-waiting').style.visibility = 'hidden';
side_toaster('something is wrong', 3000);
} catch (e) {}
}
});
Expand All @@ -5456,12 +5432,13 @@ let interval = () => {
running = false;
interval_is_running = false;
style_calendar_cell(currentYear, currentMonth);
sort_array(events, 'dateStartUnix', 'number');
sort_array(parsed_events, 'dateStartUnix', 'number');
console.log('done');
//store parsed data in events array
//to load the data when starting the app
//only parse the raw data again if has changments
/*
localforage.setItem('events', events).then(() => {
localforage.setItem('parsed_events', 'parsed_events').then(() => {
console.log(events);
});
*/
Expand Down
2 changes: 1 addition & 1 deletion application/assets/css/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ kbd {
#intro kbd {
border: 2px solid pink;
box-shadow: 2px 2px pink;
font-size: 0.85em;
font-size: 0.6em;
line-height: 0.85em;
display: inline-block;
font-weight: 600;
Expand Down
2 changes: 1 addition & 1 deletion application/assets/js/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export let autocomplete = function (e, key) {

if (parsed_events.length == 0) return;

let matches = events.filter(function (val, i) {
let matches = parsed_events.filter(function (val, i) {
if (parsed_events[i][key].indexOf(e) >= 0) return parsed_events[i];
});

Expand Down
2 changes: 1 addition & 1 deletion application/manifest.webapp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "2.2.627",
"version": "2.2.6299",
"name": "greg",
"id": "greg",
"categories": ["utilities"],
Expand Down
Loading

0 comments on commit 1f65575

Please sign in to comment.