Skip to content
This repository has been archived by the owner on Nov 4, 2024. It is now read-only.

Commit

Permalink
Cleanup avatars nextcloud handling
Browse files Browse the repository at this point in the history
  • Loading branch information
the-djmaze committed Jun 24, 2024
1 parent 0b494a1 commit 5a63b02
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 39 deletions.
75 changes: 36 additions & 39 deletions plugins/avatars/avatars.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,51 +117,48 @@
*/
addEventListener('DOMContentLoaded', () => {
// rl.pluginSettingsGet('avatars', 'nextcloud');
if (parent.OC) {
const OC = () => parent.OC,
if (parent.OC?.requestToken) {
const OC = parent.OC,
nsDAV = 'DAV:',
nsNC = 'http://nextcloud.com/ns',
nsCard = 'urn:ietf:params:xml:ns:carddav',
getElementsByTagName = (parent, namespace, localName) => parent.getElementsByTagNameNS(namespace, localName),
getElementValue = (parent, namespace, localName) =>
getElementsByTagName(parent, namespace, localName)?.item(0)?.textContent,
generateUrl = path => OC().webroot + '/remote.php' + path;
if (OC().requestToken) {
fetch(generateUrl(`/dav/addressbooks/users/${OC().currentUser}/contacts/`), {
mode: 'same-origin',
cache: 'no-cache',
redirect: 'error',
credentials: 'same-origin',
method: 'REPORT',
headers: {
requesttoken: OC().requestToken,
'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>'
})
.then(response => (response.status < 400) ? response.text() : Promise.reject(new Error({ response })))
.then(text => {
const
xmlParser = new DOMParser(),
responseList = getElementsByTagName(
xmlParser.parseFromString(text, 'application/xml').documentElement,
nsDAV,
'response');
for (let i = 0; i < responseList.length; ++i) {
const item = responseList.item(i);
if (1 == getElementValue(item, nsNC, 'has-photo')) {
[...getElementValue(item, nsCard, 'address-data').matchAll(/EMAIL.*?:([^@\r\n]+@[^@\r\n]+)/g)]
.forEach(match => {
ncAvatars.set(
match[1].toLowerCase(),
getElementValue(item, nsDAV, 'href') + '?photo'
);
});
}
getElementsByTagName(parent, namespace, localName)?.item(0)?.textContent;
fetch(`${OC.webroot}/remote.php/dav/addressbooks/users/${OC.currentUser}/contacts/`, {
mode: 'same-origin',
cache: 'no-cache',
redirect: 'error',
credentials: 'same-origin',
method: 'REPORT',
headers: {
requesttoken: OC.requestToken,
'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>'
})
.then(response => (response.status < 400) ? response.text() : Promise.reject(new Error({ response })))
.then(text => {
const
xmlParser = new DOMParser(),
responseList = getElementsByTagName(
xmlParser.parseFromString(text, 'application/xml').documentElement,
nsDAV,
'response');
for (let i = 0; i < responseList.length; ++i) {
const item = responseList.item(i);
if (1 == getElementValue(item, nsNC, 'has-photo')) {
[...getElementValue(item, nsCard, 'address-data').matchAll(/EMAIL.*?:([^@\r\n]+@[^@\r\n]+)/g)]
.forEach(match => {
ncAvatars.set(
match[1].toLowerCase(),
getElementValue(item, nsDAV, 'href') + '?photo'
);
});
}
});
}
}
});
}
});

Expand Down
1 change: 1 addition & 0 deletions plugins/avatars/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ protected function configMapping() : array
if (\class_exists('OC') && isset(\OC::$server)) {
$aResult[] = \RainLoop\Plugins\Property::NewInstance('nextcloud')->SetLabel('Lookup Nextcloud Contacts')
->SetType(\RainLoop\Enumerations\PluginPropertyType::BOOL)
// ->SetAllowedInJs(true)
->SetDefaultValue(false);
}
*/
Expand Down

0 comments on commit 5a63b02

Please sign in to comment.