(rl => {
// if (rl.settings.get('Nextcloud'))
const templateId = 'MailMessageView';
addEventListener('rl-view-model.create', e => {
if (templateId === e.detail.viewModelTemplateID) {
const
template = document.getElementById(templateId),
cfg = rl.settings.get('Nextcloud'),
attachmentsControls = template.content.querySelector('.attachmentsControls'),
msgMenu = template.content.querySelector('#more-view-dropdown-id + menu');
if (attachmentsControls) {
attachmentsControls.append(Element.fromHTML(`
✖
💾
`));
// https://github.com/nextcloud/calendar/issues/4684
if (cfg.CalDAV) {
attachmentsControls.append(Element.fromHTML(`
`));
}
}
/*
// https://github.com/the-djmaze/snappymail/issues/592
if (cfg.CalDAV) {
const attachmentsPlace = template.content.querySelector('.attachmentsPlace');
attachmentsPlace.after(Element.fromHTML(`
Summary | |
Organizer | |
Start | |
End | |
Transparency | |
| |
`));
}
*/
if (msgMenu) {
msgMenu.append(Element.fromHTML(`
`));
}
let view = e.detail;
view.saveNextcloudError = ko.observable(false).extend({ falseTimeout: 7000 });
view.saveNextcloudLoading = ko.observable(false);
view.saveNextcloud = () => {
const
hashes = (view.message()?.attachments || [])
.map(item => item?.checked() /*&& !item?.isLinked()*/ ? item.download : '')
.filter(v => v);
if (hashes.length) {
view.saveNextcloudLoading(true);
rl.nextcloud.selectFolder().then(folder => {
if (folder) {
rl.fetchJSON('./?/Json/&q[]=/0/', {}, {
Action: 'AttachmentsActions',
target: 'nextcloud',
hashes: hashes,
NcFolder: folder
})
.then(result => {
view.saveNextcloudLoading(false);
if (result?.Result) {
// success
} else {
view.saveNextcloudError(true);
}
})
.catch(() => {
view.saveNextcloudLoading(false);
view.saveNextcloudError(true);
});
} else {
view.saveNextcloudLoading(false);
}
});
}
};
view.nextcloudSaveMsg = () => {
rl.nextcloud.selectFolder().then(folder => {
let msg = view.message();
folder && rl.pluginRemoteRequest(
(iError, data) => {
console.dir({
iError:iError,
data:data
});
},
'NextcloudSaveMsg',
{
'msgHash': msg.requestHash,
'folder': folder,
'filename': msg.subject()
}
);
});
};
view.nextcloudICS = ko.observable(null);
view.nextcloudSaveICS = () => {
let VEVENT = view.nextcloudICS();
VEVENT && rl.nextcloud.selectCalendar()
.then(href => href && rl.nextcloud.calendarPut(href, VEVENT));
}
/**
* TODO
*/
view.message.subscribe(msg => {
view.nextcloudICS(null);
if (msg && cfg.CalDAV) {
// let ics = msg.attachments.find(attachment => 'application/ics' == attachment.mimeType);
let ics = msg.attachments.find(attachment => 'text/calendar' == attachment.mimeType);
if (ics && ics.download) {
// fetch it and parse the VEVENT
rl.fetch(ics.linkDownload())
.then(response => (response.status < 400) ? response.text() : Promise.reject(new Error({ response })))
.then(text => {
let VEVENT,
VALARM,
multiple = ['ATTACH','ATTENDEE','CATEGORIES','COMMENT','CONTACT','EXDATE',
'EXRULE','RSTATUS','RELATED','RESOURCES','RDATE','RRULE'],
lines = text.split(/\r?\n/),
i = lines.length;
while (i--) {
let line = lines[i];
if (VEVENT) {
while (line.startsWith(' ') && i--) {
line = lines[i] + line.slice(1);
}
if (line.startsWith('END:VALARM')) {
VALARM = {};
continue;
} else if (line.startsWith('BEGIN:VALARM')) {
VEVENT.VALARM || (VEVENT.VALARM = []);
VEVENT.VALARM.push(VALARM);
VALARM = null;
continue;
} else if (line.startsWith('BEGIN:VEVENT')) {
break;
}
line = line.match(/^([^:;]+)[:;](.+)$/);
if (line) {
if (VALARM) {
VALARM[line[1]] = line[2];
} else if (multiple.includes(line[1]) || 'X-' == line[1].slice(0,2)) {
VEVENT[line[1]] || (VEVENT[line[1]] = []);
VEVENT[line[1]].push(line[2]);
} else {
VEVENT[line[1]] = line[2];
}
}
} else if (line.startsWith('END:VEVENT')) {
VEVENT = {};
}
}
// METHOD:REPLY || METHOD:REQUEST
// console.dir({VEVENT:VEVENT});
if (VEVENT) {
VEVENT.rawText = text;
VEVENT.isCancelled = () => VEVENT.STATUS?.includes('CANCELLED');
VEVENT.isConfirmed = () => VEVENT.STATUS?.includes('CONFIRMED');
VEVENT.shouldReply = () => VEVENT.METHOD?.includes('REPLY');
console.dir({
isCancelled: VEVENT.isCancelled(),
shouldReply: VEVENT.shouldReply()
});
view.nextcloudICS(VEVENT);
}
});
}
}
});
}
});
})(window.rl);