Improved cache handling of remote avatars (delayed or not)

This commit is contained in:
the-djmaze 2024-07-08 18:26:08 +02:00
parent 10450dea8b
commit 57d1021d51
2 changed files with 36 additions and 35 deletions

View file

@ -76,6 +76,8 @@
if (rl.pluginSettingsGet('avatars', 'delay')) { if (rl.pluginSettingsGet('avatars', 'delay')) {
queue.push([msg, fn]); queue.push([msg, fn]);
runQueue(); runQueue();
} else if (msg.avatar) {
fn(getAvatarUrl(msg));
} }
}, },
runQueue = (() => { runQueue = (() => {
@ -88,6 +90,8 @@
item[1](url); item[1](url);
item = queue.shift(); item = queue.shift();
continue; continue;
} else if (item[0].avatar) {
item[1](getAvatarUrl(item[0]));
} else if (!avatars.has(uid)) { } else if (!avatars.has(uid)) {
let from = item[0].from[0]; let from = item[0].from[0];
rl.pluginRemoteRequest((iError, data) => { rl.pluginRemoteRequest((iError, data) => {
@ -104,7 +108,6 @@
bimiSelector: getBimiSelector(item[0]), bimiSelector: getBimiSelector(item[0]),
email: from.email email: from.email
}); });
break;
} }
} }
runQueue(); runQueue();
@ -112,10 +115,22 @@
} }
}).debounce(1000); }).debounce(1000);
// addEventListener('DOMContentLoaded', () => {
/**
* Modify templates
*/
getEl('MailMessageList').content.querySelectorAll('.messageCheckbox')
.forEach(el => el.append(Element.fromHTML(`<img class="fromPic" data-bind="fromPic:$data" loading="lazy">`)));
const messageItemHeader = getEl(templateId).content.querySelector('.messageItemHeader');
if (messageItemHeader) {
messageItemHeader.prepend(Element.fromHTML(
`<img class="fromPic" data-bind="visible: viewUserPicVisible, attr: {'src': viewUserPic() }" loading="lazy">`
));
}
/** /**
* Loads images from Nextcloud contacts * Loads images from Nextcloud contacts
*/ */
addEventListener('DOMContentLoaded', () => {
// rl.pluginSettingsGet('avatars', 'nextcloud'); // rl.pluginSettingsGet('avatars', 'nextcloud');
if (parent.OC?.requestToken) { if (parent.OC?.requestToken) {
const OC = parent.OC, const OC = parent.OC,
@ -160,24 +175,21 @@
} }
}); });
} }
}); // });
/**
* Used by MailMessageList
*/
ko.bindingHandlers.fromPic = { ko.bindingHandlers.fromPic = {
init: (element, self, dummy, msg) => { init: (element, self, dummy, msg) => {
try { try {
if (msg?.from?.[0]) { if (msg?.from?.[0]) {
let url = getAvatar(msg), let url = getAvatar(msg),
from = msg.from[0],
fn = url=>{element.src = url}; fn = url=>{element.src = url};
if (url) { if (url) {
fn(url); fn(url);
} else if (msg.avatar) { } else if (msg.avatar?.startsWith('data:')) {
if (msg.avatar?.startsWith('data:')) {
fn(msg.avatar); fn(msg.avatar);
} else {
element.onerror = () => setIdenticon(from, fn);
fn(getAvatarUrl(msg));
}
} else { } else {
addQueue(msg, fn); addQueue(msg, fn);
} }
@ -190,17 +202,6 @@
addEventListener('rl-view-model.create', e => { addEventListener('rl-view-model.create', e => {
if (templateId === e.detail.viewModelTemplateID) { if (templateId === e.detail.viewModelTemplateID) {
const
template = getEl(templateId),
messageItemHeader = template.content.querySelector('.messageItemHeader');
if (messageItemHeader) {
messageItemHeader.prepend(Element.fromHTML(
`<img class="fromPic" data-bind="visible: viewUserPicVisible, attr: {'src': viewUserPic() }" loading="lazy">`
));
}
let view = e.detail; let view = e.detail;
view.viewUserPic = ko.observable(''); view.viewUserPic = ko.observable('');
view.viewUserPicVisible = ko.observable(false); view.viewUserPicVisible = ko.observable(false);
@ -226,11 +227,6 @@
} }
}); });
} }
if ('MailMessageList' === e.detail.viewModelTemplateID) {
getEl('MailMessageList').content.querySelectorAll('.messageCheckbox')
.forEach(el => el.append(Element.fromHTML(`<img class="fromPic" data-bind="fromPic:$data" loading="lazy">`)));
}
}); });
})(window.rl); })(window.rl);

View file

@ -10,8 +10,8 @@ class AvatarsPlugin extends \RainLoop\Plugins\AbstractPlugin
NAME = 'Avatars', NAME = 'Avatars',
AUTHOR = 'SnappyMail', AUTHOR = 'SnappyMail',
URL = 'https://snappymail.eu/', URL = 'https://snappymail.eu/',
VERSION = '1.18', VERSION = '1.19',
RELEASE = '2024-06-24', RELEASE = '2024-07-08',
REQUIRED = '2.33.0', REQUIRED = '2.33.0',
CATEGORY = 'Contacts', CATEGORY = 'Contacts',
LICENSE = 'MIT', LICENSE = 'MIT',
@ -28,7 +28,12 @@ class AvatarsPlugin extends \RainLoop\Plugins\AbstractPlugin
$this->addJs("{$identicon}.js"); $this->addJs("{$identicon}.js");
} }
// https://github.com/the-djmaze/snappymail/issues/714 // https://github.com/the-djmaze/snappymail/issues/714
if ($this->Config()->Get('plugin', 'service', true) || !$this->Config()->Get('plugin', 'delay', true)) { if ($this->Config()->Get('plugin', 'service', true)
// || !$this->Config()->Get('plugin', 'delay', true)
|| $this->Config()->Get('plugin', 'gravatar', false)
|| $this->Config()->Get('plugin', 'bimi', false)
|| $this->Config()->Get('plugin', 'favicon', false)
) {
$this->addHook('json.after-message', 'JsonMessage'); $this->addHook('json.after-message', 'JsonMessage');
$this->addHook('json.after-messagelist', 'JsonMessageList'); $this->addHook('json.after-messagelist', 'JsonMessageList');
} }
@ -69,8 +74,8 @@ class AvatarsPlugin extends \RainLoop\Plugins\AbstractPlugin
$mFrom = $mFrom->jsonSerialize(); $mFrom = $mFrom->jsonSerialize();
} }
if (\is_array($mFrom)) { if (\is_array($mFrom)) {
if (!$this->Config()->Get('plugin', 'delay', true) if (/*!$this->Config()->Get('plugin', 'delay', true)
&& ($this->Config()->Get('plugin', 'gravatar', false) && */($this->Config()->Get('plugin', 'gravatar', false)
|| ($this->Config()->Get('plugin', 'bimi', false) && 'pass' == $mFrom['dkimStatus']) || ($this->Config()->Get('plugin', 'bimi', false) && 'pass' == $mFrom['dkimStatus'])
|| ($this->Config()->Get('plugin', 'favicon', false) && 'pass' == $mFrom['dkimStatus']) || ($this->Config()->Get('plugin', 'favicon', false) && 'pass' == $mFrom['dkimStatus'])
) )