Cleanup avatars nextcloud handling

This commit is contained in:
the-djmaze 2024-06-24 16:23:18 +02:00
parent 0b494a13a5
commit 5a63b02782
2 changed files with 37 additions and 39 deletions

View file

@ -117,51 +117,48 @@
*/ */
addEventListener('DOMContentLoaded', () => { addEventListener('DOMContentLoaded', () => {
// rl.pluginSettingsGet('avatars', 'nextcloud'); // rl.pluginSettingsGet('avatars', 'nextcloud');
if (parent.OC) { if (parent.OC?.requestToken) {
const OC = () => parent.OC, const OC = parent.OC,
nsDAV = 'DAV:', nsDAV = 'DAV:',
nsNC = 'http://nextcloud.com/ns', nsNC = 'http://nextcloud.com/ns',
nsCard = 'urn:ietf:params:xml:ns:carddav', nsCard = 'urn:ietf:params:xml:ns:carddav',
getElementsByTagName = (parent, namespace, localName) => parent.getElementsByTagNameNS(namespace, localName), getElementsByTagName = (parent, namespace, localName) => parent.getElementsByTagNameNS(namespace, localName),
getElementValue = (parent, namespace, localName) => getElementValue = (parent, namespace, localName) =>
getElementsByTagName(parent, namespace, localName)?.item(0)?.textContent, getElementsByTagName(parent, namespace, localName)?.item(0)?.textContent;
generateUrl = path => OC().webroot + '/remote.php' + path; fetch(`${OC.webroot}/remote.php/dav/addressbooks/users/${OC.currentUser}/contacts/`, {
if (OC().requestToken) { mode: 'same-origin',
fetch(generateUrl(`/dav/addressbooks/users/${OC().currentUser}/contacts/`), { cache: 'no-cache',
mode: 'same-origin', redirect: 'error',
cache: 'no-cache', credentials: 'same-origin',
redirect: 'error', method: 'REPORT',
credentials: 'same-origin', headers: {
method: 'REPORT', requesttoken: OC.requestToken,
headers: { 'Content-Type': 'application/xml; charset=utf-8',
requesttoken: OC().requestToken, Depth: 1
'Content-Type': 'application/xml; charset=utf-8', },
Depth: 1 body: '<x4:addressbook-query xmlns:x4="urn:ietf:params:xml:ns:carddav"><x0:prop xmlns:x0="DAV:"><x4:address-data><x4:prop name="EMAIL"/></x4:address-data><x3:has-photo xmlns:x3="http://nextcloud.com/ns"/></x0:prop></x4:addressbook-query>'
}, })
body: '<x4:addressbook-query xmlns:x4="urn:ietf:params:xml:ns:carddav"><x0:prop xmlns:x0="DAV:"><x4:address-data><x4:prop name="EMAIL"/></x4:address-data><x3:has-photo xmlns:x3="http://nextcloud.com/ns"/></x0:prop></x4:addressbook-query>' .then(response => (response.status < 400) ? response.text() : Promise.reject(new Error({ response })))
}) .then(text => {
.then(response => (response.status < 400) ? response.text() : Promise.reject(new Error({ response }))) const
.then(text => { xmlParser = new DOMParser(),
const responseList = getElementsByTagName(
xmlParser = new DOMParser(), xmlParser.parseFromString(text, 'application/xml').documentElement,
responseList = getElementsByTagName( nsDAV,
xmlParser.parseFromString(text, 'application/xml').documentElement, 'response');
nsDAV, for (let i = 0; i < responseList.length; ++i) {
'response'); const item = responseList.item(i);
for (let i = 0; i < responseList.length; ++i) { if (1 == getElementValue(item, nsNC, 'has-photo')) {
const item = responseList.item(i); [...getElementValue(item, nsCard, 'address-data').matchAll(/EMAIL.*?:([^@\r\n]+@[^@\r\n]+)/g)]
if (1 == getElementValue(item, nsNC, 'has-photo')) { .forEach(match => {
[...getElementValue(item, nsCard, 'address-data').matchAll(/EMAIL.*?:([^@\r\n]+@[^@\r\n]+)/g)] ncAvatars.set(
.forEach(match => { match[1].toLowerCase(),
ncAvatars.set( getElementValue(item, nsDAV, 'href') + '?photo'
match[1].toLowerCase(), );
getElementValue(item, nsDAV, 'href') + '?photo' });
);
});
}
} }
}); }
} });
} }
}); });

View file

@ -172,6 +172,7 @@ class AvatarsPlugin extends \RainLoop\Plugins\AbstractPlugin
if (\class_exists('OC') && isset(\OC::$server)) { if (\class_exists('OC') && isset(\OC::$server)) {
$aResult[] = \RainLoop\Plugins\Property::NewInstance('nextcloud')->SetLabel('Lookup Nextcloud Contacts') $aResult[] = \RainLoop\Plugins\Property::NewInstance('nextcloud')->SetLabel('Lookup Nextcloud Contacts')
->SetType(\RainLoop\Enumerations\PluginPropertyType::BOOL) ->SetType(\RainLoop\Enumerations\PluginPropertyType::BOOL)
// ->SetAllowedInJs(true)
->SetDefaultValue(false); ->SetDefaultValue(false);
} }
*/ */