diff --git a/js/follows.min.js b/js/follows.min.js index 14051b23..2f6c258d 100644 --- a/js/follows.min.js +++ b/js/follows.min.js @@ -1 +1 @@ -const fcn_desktopFollowList=_$$$("follow-menu-scroll"),fcn_mobileFollowList=_$$$("mobile-menu-follows-list"),fcn_followsMenuItem=_$$$("follow-menu-button");var fcn_userFollowsTimeout,fcn_follows;function fcn_initializeFollows(o){const t=o.detail.data.follows;!1!==t&&(fcn_follows=t,fcn_updateFollowsView(),localStorage.removeItem("fcnBookshelfContent"))}function fcn_toggleFollow(o){const t=fcn_getUserData();if(fcn_follows&&t.follows){if(localStorage.removeItem("fcnBookshelfContent"),JSON.stringify(fcn_follows.data[o])!==JSON.stringify(t.follows.data[o]))return fcn_follows=t.follows,fcn_showNotification(__("Follows re-synchronized.","fictioneer")),void fcn_updateFollowsView();fcn_follows.data.hasOwnProperty(o)?delete fcn_follows.data[o]:fcn_follows.data[o]={story_id:parseInt(o),timestamp:Date.now()},t.follows.data[o]=fcn_follows.data[o],t.lastLoaded=0,fcn_setUserData(t),fcn_updateFollowsView(),clearTimeout(fcn_userFollowsTimeout),fcn_userFollowsTimeout=setTimeout((()=>{fcn_ajaxPost({action:"fictioneer_ajax_toggle_follow",fcn_fast_ajax:1,story_id:o,set:fcn_follows.data.hasOwnProperty(o)}).then((o=>{o.data.error&&fcn_showNotification(o.data.error,5,"warning")})).catch((o=>{429===o.status?fcn_showNotification(__("Slow down.","fictioneer"),3,"warning"):o.status&&o.statusText&&fcn_showNotification(`${o.status}: ${o.statusText}`,5,"warning")}))}),fictioneer_ajax.post_debounce_rate)}}function fcn_updateFollowsView(){const o=fcn_getUserData();if(!fcn_follows||!o.follows)return;_$$(".button-follow-story").forEach((o=>{o.classList.toggle("_followed",fcn_follows?.data.hasOwnProperty(o.dataset.storyId))})),_$$(".card").forEach((o=>{o.classList.toggle("has-follow",fcn_follows?.data.hasOwnProperty(o.dataset.storyId))}));const t=parseInt(fcn_follows.new)>0;_$$(".mark-follows-read, .follows-alert-number, .mobile-menu-button").forEach((o=>{o.classList.toggle("_new",t),t>0&&(o.dataset.newCount=fcn_follows.new)}))}function fcn_setupFollowsHTML(){fcn_followsMenuItem.classList.contains("_loaded")||fcn_ajaxGet({action:"fictioneer_ajax_get_follows_notifications",fcn_fast_ajax:1}).then((o=>{o.data.html&&(fcn_desktopFollowList.innerHTML=o.data.html,fcn_mobileFollowList.innerHTML=o.data.html)})).catch((o=>{429===o.status?fcn_showNotification(__("Slow down.","fictioneer"),3,"warning"):o.status&&o.statusText&&fcn_showNotification(`${o.status}: ${o.statusText}`,5,"warning"),fcn_desktopFollowList.remove(),fcn_mobileFollowList.remove()})).then((()=>{fcn_followsMenuItem.classList.add("_loaded")}))}function fcn_markFollowsRead(){if(!fcn_followsMenuItem.classList.contains("_new")||!fcn_followsMenuItem.classList.contains("_loaded"))return;_$$(".mark-follows-read, .follows-alert-number, .follow-item, .mobile-menu-button").forEach((o=>{o.classList.remove("_new")}));const o=fcn_getUserData();o.new=0,o.lastLoaded=0,fcn_setUserData(o),fcn_ajaxPost({action:"fictioneer_ajax_mark_follows_read",fcn_fast_ajax:1}).catch((o=>{o.status&&o.statusText&&fcn_showNotification(`${o.status}: ${o.statusText}`,5,"warning")}))}document.addEventListener("fcnUserDataReady",(o=>{fcn_initializeFollows(o)})),fcn_followsMenuItem?.addEventListener("mouseover",(()=>{fcn_setupFollowsHTML()}),{once:!0}),fcn_followsMenuItem?.addEventListener("focus",(()=>{fcn_setupFollowsHTML()}),{once:!0}),_$('.mobile-menu__frame-button[data-frame-target="follows"]')?.addEventListener("click",(()=>{fcn_setupFollowsHTML()}),{once:!0}),_$$(".button-follow-story").forEach((o=>{o.addEventListener("click",(o=>{fcn_toggleFollow(o.currentTarget.dataset.storyId)}))})),_$$(".mark-follows-read").forEach((o=>{o.addEventListener("click",(()=>{fcn_markFollowsRead()}))})); \ No newline at end of file +const fcn_desktopFollowList=_$$$("follow-menu-scroll"),fcn_mobileFollowList=_$$$("mobile-menu-follows-list"),fcn_followsMenuItem=_$$$("follow-menu-button");var fcn_userFollowsTimeout,fcn_follows;function fcn_initializeFollows(o){const t=o.detail.data.follows;!1!==t&&(fcn_follows=t,fcn_updateFollowsView(),localStorage.removeItem("fcnBookshelfContent"))}function fcn_toggleFollow(o){const t=fcn_getUserData();if(fcn_follows&&t.follows){if(localStorage.removeItem("fcnBookshelfContent"),JSON.stringify(fcn_follows.data[o])!==JSON.stringify(t.follows.data[o]))return fcn_follows=t.follows,fcn_showNotification(__("Follows re-synchronized.","fictioneer")),void fcn_updateFollowsView();fcn_follows.data.hasOwnProperty(o)?delete fcn_follows.data[o]:fcn_follows.data[o]={story_id:parseInt(o),timestamp:Date.now()},t.follows.data[o]=fcn_follows.data[o],t.lastLoaded=0,fcn_setUserData(t),fcn_updateFollowsView(),clearTimeout(fcn_userFollowsTimeout),fcn_userFollowsTimeout=setTimeout((()=>{fcn_ajaxPost({action:"fictioneer_ajax_toggle_follow",fcn_fast_ajax:1,story_id:o,set:fcn_follows.data.hasOwnProperty(o)}).then((o=>{o.data.error&&fcn_showNotification(o.data.error,5,"warning")})).catch((o=>{429===o.status?fcn_showNotification(__("Slow down.","fictioneer"),3,"warning"):o.status&&o.statusText&&fcn_showNotification(`${o.status}: ${o.statusText}`,5,"warning")}))}),fictioneer_ajax.post_debounce_rate)}}function fcn_updateFollowsView(){const o=fcn_getUserData();if(!fcn_follows||!o.follows)return;_$$(".button-follow-story").forEach((o=>{o.classList.toggle("_followed",fcn_follows?.data.hasOwnProperty(o.dataset.storyId))})),_$$(".card").forEach((o=>{o.classList.toggle("has-follow",fcn_follows?.data.hasOwnProperty(o.dataset.storyId))}));const t=parseInt(fcn_follows.new)>0;_$$(".mark-follows-read, .follows-alert-number, .mobile-menu-button").forEach((o=>{o.classList.toggle("_new",t),t>0&&(o.dataset.newCount=fcn_follows.new)}))}function fcn_setupFollowsHTML(){fcn_followsMenuItem.classList.contains("_loaded")||fcn_ajaxGet({action:"fictioneer_ajax_get_follows_notifications",fcn_fast_ajax:1}).then((o=>{o.data.html&&(fcn_desktopFollowList.innerHTML=o.data.html,fcn_mobileFollowList.innerHTML=o.data.html,!1===fcn_getUserData().loggedIn&&(fcn_prepareLogin(),fcn_fetchUserData()))})).catch((o=>{429===o.status?fcn_showNotification(__("Slow down.","fictioneer"),3,"warning"):o.status&&o.statusText&&fcn_showNotification(`${o.status}: ${o.statusText}`,5,"warning"),fcn_desktopFollowList.remove(),fcn_mobileFollowList.remove()})).then((()=>{fcn_followsMenuItem.classList.add("_loaded")}))}function fcn_markFollowsRead(){if(!fcn_followsMenuItem.classList.contains("_new")||!fcn_followsMenuItem.classList.contains("_loaded"))return;_$$(".mark-follows-read, .follows-alert-number, .follow-item, .mobile-menu-button").forEach((o=>{o.classList.remove("_new")}));const o=fcn_getUserData();o.new=0,o.lastLoaded=0,fcn_setUserData(o),fcn_ajaxPost({action:"fictioneer_ajax_mark_follows_read",fcn_fast_ajax:1}).catch((o=>{o.status&&o.statusText&&fcn_showNotification(`${o.status}: ${o.statusText}`,5,"warning")}))}document.addEventListener("fcnUserDataReady",(o=>{fcn_initializeFollows(o)})),fcn_followsMenuItem?.addEventListener("mouseover",(()=>{fcn_setupFollowsHTML()}),{once:!0}),fcn_followsMenuItem?.addEventListener("focus",(()=>{fcn_setupFollowsHTML()}),{once:!0}),_$('.mobile-menu__frame-button[data-frame-target="follows"]')?.addEventListener("click",(()=>{fcn_setupFollowsHTML()}),{once:!0}),_$$(".button-follow-story").forEach((o=>{o.addEventListener("click",(o=>{fcn_toggleFollow(o.currentTarget.dataset.storyId)}))})),_$$(".mark-follows-read").forEach((o=>{o.addEventListener("click",(()=>{fcn_markFollowsRead()}))})); \ No newline at end of file diff --git a/js/user.min.js b/js/user.min.js index 75680d44..6e596701 100644 --- a/js/user.min.js +++ b/js/user.min.js @@ -1 +1 @@ -function fcn_replaceProfileImage(e,t){const a=e.querySelector(".user-icon");if(a){const n=document.createElement("img");n.classList.add("user-profile-image"),n.src=t,a.remove(),e.appendChild(n)}}function fcn_setProfileImage(e,t=!0){e&&fcn_isValidUrl(e)&&(t&&localStorage.setItem("fcnProfileAvatar",e),_$$("a.subscriber-profile")?.forEach((t=>{fcn_replaceProfileImage(t,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)}))}function fcn_getUserData(){return fcn_parseJSON(localStorage.getItem("fcnUserData"))??{lastLoaded:0,timestamp:0,loggedIn:"pending",follows:!1,reminders:!1,checkmarks:!1,bookmarks:{},fingerprint:!1}}function fcn_setUserData(e){localStorage.setItem("fcnUserData",JSON.stringify(e))}function fcn_fetchUserData(){let e=fcn_getUserData();if(fcn_isLoggedIn&&!1===e.loggedIn&&(fcn_prepareLogin(),e=fcn_getUserData()),fcn_ajaxLimitThreshold{if(e.success){let t=fcn_getUserData();t=e.data,t.lastLoaded=Date.now(),fcn_setUserData(t);const a=new CustomEvent("fcnUserDataReady",{detail:{data:e.data,time:new Date},bubbles:!0,cancelable:!1});document.dispatchEvent(a)}else{const t=fcn_getUserData();t.lastLoaded=Date.now(),t.loggedIn=!1,fcn_setUserData(t);const a=new CustomEvent("fcnUserDataFailed",{detail:{response:e,time:new Date},bubbles:!0,cancelable:!1});document.dispatchEvent(a)}})).catch((e=>{localStorage.removeItem("fcnUserData");const t=new CustomEvent("fcnUserDataError",{detail:{error:e,time:new Date},bubbles:!0,cancelable:!1});document.dispatchEvent(t)}))}document.addEventListener("DOMContentLoaded",(()=>{fcn_isLoggedIn&&!fcn_theRoot.dataset.ajaxAuth&&fcn_getProfileImage()})),document.addEventListener("DOMContentLoaded",(()=>{fcn_isLoggedIn&&!fcn_theRoot.dataset.ajaxAuth&&fcn_fetchUserData()})); \ No newline at end of file +function fcn_replaceProfileImage(e,t){const a=e.querySelector(".user-icon");if(a){const n=document.createElement("img");n.classList.add("user-profile-image"),n.src=t,a.remove(),e.appendChild(n)}}function fcn_setProfileImage(e,t=!0){e&&fcn_isValidUrl(e)&&(t&&localStorage.setItem("fcnProfileAvatar",e),_$$("a.subscriber-profile")?.forEach((t=>{fcn_replaceProfileImage(t,e)})),!1===fcn_getUserData().loggedIn&&fcn_prepareLogin())}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)}))}function fcn_getUserData(){return fcn_parseJSON(localStorage.getItem("fcnUserData"))??{lastLoaded:0,timestamp:0,loggedIn:"pending",follows:!1,reminders:!1,checkmarks:!1,bookmarks:{},fingerprint:!1}}function fcn_setUserData(e){localStorage.setItem("fcnUserData",JSON.stringify(e))}function fcn_fetchUserData(){let e=fcn_getUserData();if(fcn_isLoggedIn&&!1===e.loggedIn&&(fcn_prepareLogin(),e=fcn_getUserData()),fcn_ajaxLimitThreshold{if(e.success){let t=fcn_getUserData();t=e.data,t.lastLoaded=Date.now(),fcn_setUserData(t);const a=new CustomEvent("fcnUserDataReady",{detail:{data:e.data,time:new Date},bubbles:!0,cancelable:!1});document.dispatchEvent(a)}else{const t=fcn_getUserData();t.lastLoaded=Date.now(),t.loggedIn=!1,fcn_setUserData(t);const a=new CustomEvent("fcnUserDataFailed",{detail:{response:e,time:new Date},bubbles:!0,cancelable:!1});document.dispatchEvent(a)}})).catch((e=>{localStorage.removeItem("fcnUserData");const t=new CustomEvent("fcnUserDataError",{detail:{error:e,time:new Date},bubbles:!0,cancelable:!1});document.dispatchEvent(t)}))}document.addEventListener("DOMContentLoaded",(()=>{fcn_isLoggedIn&&!fcn_theRoot.dataset.ajaxAuth&&fcn_getProfileImage()})),document.addEventListener("DOMContentLoaded",(()=>{fcn_isLoggedIn&&!fcn_theRoot.dataset.ajaxAuth&&fcn_fetchUserData()})); \ No newline at end of file diff --git a/src/js/application.js b/src/js/application.js index c666f65f..42a4fd4d 100644 --- a/src/js/application.js +++ b/src/js/application.js @@ -122,7 +122,7 @@ function fcn_cleanupGuestView() { _$$$('fictioneer-ajax-nonce')?.remove(); _$$('.only-moderators, .only-admins, .only-authors, .only-editors, .chapter-group__list-item-checkmark').forEach(element => { - element.remove() + element.remove(); }); } diff --git a/src/js/follows.js b/src/js/follows.js index 82593e68..db23c559 100644 --- a/src/js/follows.js +++ b/src/js/follows.js @@ -191,6 +191,12 @@ function fcn_setupFollowsHTML() { if (response.data.html) { fcn_desktopFollowList.innerHTML = response.data.html; fcn_mobileFollowList.innerHTML = response.data.html; + + // Use opportunity to fix broken login state + if (fcn_getUserData().loggedIn === false) { + fcn_prepareLogin(); + fcn_fetchUserData(); + } } }) .catch(error => { diff --git a/src/js/user.js b/src/js/user.js index 3a4ba8ff..66281a2c 100644 --- a/src/js/user.js +++ b/src/js/user.js @@ -49,10 +49,15 @@ function fcn_setProfileImage(avatar, save = true) { localStorage.setItem('fcnProfileAvatar', avatar); } - // replace user icon with avatar + // Replace user icon with avatar _$$('a.subscriber-profile')?.forEach(element => { fcn_replaceProfileImage(element, avatar); }); + + // Use opportunity to fix broken login state + if (fcn_getUserData().loggedIn === false) { + fcn_prepareLogin(); + } } /** @@ -171,7 +176,7 @@ function fcn_setUserData(data) { function fcn_fetchUserData() { let currentUserData = fcn_getUserData(); - // Fix wrong login state + // Fix broken login state if (fcn_isLoggedIn && currentUserData.loggedIn === false) { fcn_prepareLogin(); // Remove outdated data from web storage currentUserData = fcn_getUserData();