snappymail/plugins/avatars/avatars.min.js
2022-11-29 15:56:21 +01:00

1 line
2.8 KiB
JavaScript

(e=>{const t=[],a=new Map,s=new Map,r="MailMessageView",i=e=>{let t=e.from[0];return`${"pass"==t.dkimStatus?1:0}/${t.email.toLowerCase()}`},o=e=>s.get(e.from[0].email.toLowerCase())||a.get(i(e)),n=(()=>{let s=t.shift();for(;s;){let r=o(s[0]);if(!r){let t=s[0].from[0];e.pluginRemoteRequest(((e,o)=>{!e&&o?.Result.type?(r=`data:${o.Result.type};base64,${o.Result.data}`,a.set(i(s[0]),r),s[1](r)):window.identiconSvg&&window.identiconSvg(t.email).then((e=>{a.set(i(s[0]),e),s[1](e)})),n()}),"Avatar",{bimi:"pass"==t.dkimStatus?1:0,email:t.email});break}s[1](r),s=t.shift()}}).debounce(1e3);addEventListener("DOMContentLoaded",(()=>{if(parent.OC){const e=()=>parent.OC,t="DAV:",a="http://nextcloud.com/ns",r="urn:ietf:params:xml:ns:carddav",i=(e,t,a)=>e.getElementsByTagNameNS(t,a),o=(e,t,a)=>i(e,t,a)?.item(0)?.textContent,n=t=>e().webroot+"/remote.php"+t;e().requestToken&&fetch(n(`/dav/addressbooks/users/${e().currentUser}/contacts/`),{mode:"same-origin",cache:"no-cache",redirect:"error",credentials:"same-origin",method:"REPORT",headers:{requesttoken:e().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((e=>e.status<400?e.text():Promise.reject(new Error({response:e})))).then((e=>{const n=new DOMParser,d=i(n.parseFromString(e,"application/xml").documentElement,t,"response");for(let e=0;e<d.length;++e){const i=d.item(e);1==o(i,a,"has-photo")&&[...o(i,r,"address-data").matchAll(/EMAIL.*?:([^@\r\n]+@[^@\r\n]+)/g)].forEach((e=>{s.set(e[1].toLowerCase(),o(i,t,"href")+"?photo")}))}}))}})),ko.bindingHandlers.fromPic={init:(e,a,s,r)=>{if(r){let a=o(r),s=t=>{e.src=t};if(a)s(a);else if(r.avatar){let t="pass"==r.from[0].dkimStatus?1:0;window.identiconSvg&&(e.onerror=()=>window.identiconSvg(r.from[0].email).then(s)),s(`?Avatar/${t}/${r.avatar}`)}else t.push([r,s]),n()}}},addEventListener("rl-view-model.create",(e=>{if(r===e.detail.viewModelTemplateID){const a=document.getElementById(r).content.querySelector(".messageItemHeader");a&&a.prepend(Element.fromHTML('<img class="fromPic" data-bind="visible: viewUserPicVisible, attr: {\'src\': viewUserPic() }" loading="lazy">'));let s=e.detail;s.viewUserPic=ko.observable(""),s.viewUserPicVisible=ko.observable(!1),s.message.subscribe((e=>{if(s.viewUserPicVisible(!1),e){let a=o(e),r=e=>{s.viewUserPic(e),s.viewUserPicVisible(!0)};if(a)r(a);else if(e.avatar){r(`?Avatar/${"pass"==e.from[0].dkimStatus?1:0}/${e.avatar}`)}else t.push([e,r]),n()}}))}"MailMessageList"===e.detail.viewModelTemplateID&&document.getElementById("MailMessageList").content.querySelector(".messageCheckbox").append(Element.fromHTML('<img class="fromPic" data-bind="fromPic:$data" loading="lazy">'))}))})(window.rl);