Keep local user data updated

This commit is contained in:
Tetrakern 2023-08-29 14:29:23 +02:00
parent 2ac5d5c294
commit 573d48b951
2 changed files with 4 additions and 1 deletions

2
js/user.min.js vendored
View File

@ -1 +1 @@
function fcn_replaceProfileImage(e,a){const t=e.querySelector(".user-icon");if(t){const n=document.createElement("img");n.classList.add("user-profile-image"),n.src=a,t.remove(),e.appendChild(n)}}function fcn_setProfileImage(e,a=!0){e&&fcn_isValidUrl(e)&&(a&&localStorage.setItem("fcnProfileAvatar",e),_$$("a.subscriber-profile")?.forEach((a=>{fcn_replaceProfileImage(a,e)})))}function fcn_getProfileImage(){let e=localStorage.getItem("fcnProfileAvatar");fcn_isLoggedIn?(fcn_isValidUrl(e)||(e=!1),e?fcn_setProfileImage(e):fcn_getUserAvatar()):localStorage.removeItem("fcnProfileAvatar")}function fcn_getUserAvatar(){fcn_ajaxGet({action:"fictioneer_ajax_get_avatar",fcn_fast_ajax:1}).then((e=>{e.success&&fcn_setProfileImage(e.data.url)})).catch((()=>{fcn_theRoot.dataset.defaultAvatar&&fcn_setProfileImage(fcn_theRoot.dataset.defaultAvatar,!1)}))}var fcn_userData;function fcn_initializeUserData(){fcn_userData=fcn_getUserData(),fcn_fetchUserData()}function fcn_getUserData(){return fcn_parseJSON(localStorage.getItem("fcnUserData"))??{lastLoaded:0,timestamp:0,follows:!1,reminders:!1,checkmarks:!1,bookmarks:{},fingerprint:!1}}function fcn_setUserData(e){localStorage.setItem("fcnUserData",JSON.stringify(e))}function fcn_fetchUserData(){if(fcn_ajaxLimitThreshold<fcn_userData.lastLoaded){const e=new CustomEvent("fcnUserDataReady",{detail:{data:fcn_userData,time:new Date},bubbles:!1,cancelable:!0});document.dispatchEvent(e)}else fcn_ajaxGet({action:"fictioneer_ajax_get_user_data",fcn_fast_ajax:1}).then((e=>{if(e.success){(fcn_userData=e.data).lastLoaded=Date.now();const a=new CustomEvent("fcnUserDataReady",{detail:{data:e.data,time:new Date},bubbles:!0,cancelable:!1});fcn_setUserData(fcn_userData),document.dispatchEvent(a)}else{localStorage.removeItem("fcnUserData"),fcn_userData=!1;const a=new CustomEvent("fcnUserDataFailed",{detail:{response:e,time:new Date},bubbles:!0,cancelable:!1});document.dispatchEvent(a)}})).catch((e=>{localStorage.removeItem("fcnUserData"),fcn_userData=!1;const a=new CustomEvent("fcnUserDataError",{detail:{error:e,time:new Date},bubbles:!0,cancelable:!1});document.dispatchEvent(a)}))}(fcn_isLoggedIn||fcn_theRoot.dataset.ajaxAuth)&&fcn_getProfileImage(),document.addEventListener("DOMContentLoaded",(()=>{(fcn_isLoggedIn||fcn_theRoot.dataset.ajaxAuth)&&fcn_initializeUserData()}));
function fcn_replaceProfileImage(e,a){const t=e.querySelector(".user-icon");if(t){const n=document.createElement("img");n.classList.add("user-profile-image"),n.src=a,t.remove(),e.appendChild(n)}}function fcn_setProfileImage(e,a=!0){e&&fcn_isValidUrl(e)&&(a&&localStorage.setItem("fcnProfileAvatar",e),_$$("a.subscriber-profile")?.forEach((a=>{fcn_replaceProfileImage(a,e)})))}function fcn_getProfileImage(){let e=localStorage.getItem("fcnProfileAvatar");fcn_isLoggedIn?(fcn_isValidUrl(e)||(e=!1),e?fcn_setProfileImage(e):fcn_getUserAvatar()):localStorage.removeItem("fcnProfileAvatar")}function fcn_getUserAvatar(){fcn_ajaxGet({action:"fictioneer_ajax_get_avatar",fcn_fast_ajax:1}).then((e=>{e.success&&fcn_setProfileImage(e.data.url)})).catch((()=>{fcn_theRoot.dataset.defaultAvatar&&fcn_setProfileImage(fcn_theRoot.dataset.defaultAvatar,!1)}))}var fcn_userData;function fcn_initializeUserData(){fcn_userData=fcn_getUserData(),fcn_fetchUserData()}function fcn_getUserData(){return fcn_parseJSON(localStorage.getItem("fcnUserData"))??{lastLoaded:0,timestamp:0,follows:!1,reminders:!1,checkmarks:!1,bookmarks:{},fingerprint:!1}}function fcn_setUserData(e){fcn_userData=e,localStorage.setItem("fcnUserData",JSON.stringify(e))}function fcn_fetchUserData(){if(fcn_ajaxLimitThreshold<fcn_userData.lastLoaded){const e=new CustomEvent("fcnUserDataReady",{detail:{data:fcn_userData,time:new Date},bubbles:!1,cancelable:!0});document.dispatchEvent(e)}else fcn_ajaxGet({action:"fictioneer_ajax_get_user_data",fcn_fast_ajax:1}).then((e=>{if(e.success){(fcn_userData=e.data).lastLoaded=Date.now();const a=new CustomEvent("fcnUserDataReady",{detail:{data:e.data,time:new Date},bubbles:!0,cancelable:!1});fcn_setUserData(fcn_userData),document.dispatchEvent(a)}else{localStorage.removeItem("fcnUserData"),fcn_userData=!1;const a=new CustomEvent("fcnUserDataFailed",{detail:{response:e,time:new Date},bubbles:!0,cancelable:!1});document.dispatchEvent(a)}})).catch((e=>{localStorage.removeItem("fcnUserData"),fcn_userData=!1;const a=new CustomEvent("fcnUserDataError",{detail:{error:e,time:new Date},bubbles:!0,cancelable:!1});document.dispatchEvent(a)}))}(fcn_isLoggedIn||fcn_theRoot.dataset.ajaxAuth)&&fcn_getProfileImage(),document.addEventListener("DOMContentLoaded",(()=>{(fcn_isLoggedIn||fcn_theRoot.dataset.ajaxAuth)&&fcn_initializeUserData()}));

View File

@ -150,6 +150,9 @@ function fcn_getUserData() {
}
function fcn_setUserData(data) {
// Keep global updated
fcn_userData = data;
// Update local storage
localStorage.setItem('fcnUserData', JSON.stringify(data));
}