Files
2026-11-08 11:57:35 +08:00

1 line
5.9 KiB
JavaScript
Generated

"use strict";"require baseclass";"require rpc";"require utils.notices as notices";const CACHE_KEY="aurora.hub.list",ME_CACHE_KEY="aurora.hub.me",NOTICES_CACHE_KEY="aurora.hub.notices",INBOX_READ_KEY="aurora.hub.inboxRead",INBOX_DONE_KEY="aurora.hub.inboxDone",NOTICES_CHECKED_KEY="aurora.hub.noticesChecked",HUB_BASE="https://themes.eamonxg.fun",HUB_TIMEOUT_MS=15e3,HUB_NOT_FOUND="invalid_id",CLIENT_SCHEMA=1,hubFetch=e=>{const t=new AbortController,a=setTimeout(()=>t.abort(),15e3);return fetch(HUB_BASE+e,{signal:t.signal,headers:{Accept:"application/json"}}).then(e=>404===e.status?{result:1,error:"invalid_id"}:e.ok?e.json().then(e=>({result:0,data:e})):{result:1,error:"hub_unreachable"}).catch(()=>({result:1,error:"hub_unreachable"})).finally(()=>clearTimeout(a))},putAssetBytes=(e,t,a,r)=>new Promise(o=>{const l=new XMLHttpRequest;l.open("PUT",e,!0),l.setRequestHeader("Authorization","Bearer "+t),l.setRequestHeader("Content-Type","application/octet-stream"),l.timeout=12e4,l.upload.onprogress=e=>{e.lengthComputable&&r&&r(e.loaded,e.total)},l.onload=()=>o(l.status>=200&&l.status<300),l.onerror=()=>o(!1),l.ontimeout=()=>o(!1),l.send(a)}),ASSET_PATH_RE=/^\/assets\/[A-Za-z0-9]{1,32}\/[a-z0-9_]{1,32}$/,makeCache=(e,t)=>({getStale(){try{const t=localStorage.getItem(e);return t?JSON.parse(t).value??null:null}catch(e){return null}},set(a){try{localStorage.setItem(e,JSON.stringify({timestamp:Date.now(),value:a}))}catch(e){console.error("Failed to cache hub "+t+" data:",e)}},clear(){try{localStorage.removeItem(e)}catch(e){}}}),readStored=(e,t)=>{try{const a=JSON.parse(localStorage.getItem(e));return null==a?t:a}catch(e){return t}},writeStored=(e,t)=>{try{localStorage.setItem(e,JSON.stringify(t))}catch(e){}},patchSnapshot=(e,t)=>{const a=e.getStale(),r=Object.assign({notices:notices.sanitize(a&&a.notices),local:a&&a.local||null},t);return e.set(r),r};return baseclass.extend({listCache:makeCache(CACHE_KEY,"list"),meCache:makeCache(ME_CACHE_KEY,"me"),noticesCache:makeCache(NOTICES_CACHE_KEY,"notices"),CLIENT_SCHEMA:1,callHubList(e,t){const a="new"===e?"new":"hot",r=Number.isInteger(t)&&t>0?t:1;return hubFetch("/api/v1/themes/aurora/configs?sort="+a+"&page="+r+"&schema=1")},callHubNotices:()=>hubFetch("/api/v1/notices?theme=aurora&schema=1").then(e=>"invalid_id"===e.error?{result:0,data:{notices:[]}}:e),noticesCheckedAt(){const e=readStored(NOTICES_CHECKED_KEY,0);return Number.isFinite(e)?e:0},markNoticesChecked(e){writeStored(NOTICES_CHECKED_KEY,e)},inboxState(){const e=e=>{const t=readStored(e,[]);return Array.isArray(t)?t.filter(e=>"string"==typeof e):[]};return{read:e(INBOX_READ_KEY),done:e(INBOX_DONE_KEY)}},setInboxState(e){writeStored(INBOX_READ_KEY,e.read),writeStored(INBOX_DONE_KEY,e.done)},setNoticesLocal(e){return patchSnapshot(this.noticesCache,{local:e})},refreshNotices(e){const t=e||{},a=void 0===t.me?null:Promise.resolve(t.me).catch(()=>null);this.markNoticesChecked(Date.now());const r=L.resolveDefault(this.callGetInitData(),null).then(e=>notices.localState(e));return Promise.all([this.callHubNotices(),r]).then(([e,t])=>{const r=this.noticesCache.getStale(),o=t||r&&r.local||null,l={feed:Boolean(e&&0===e.result),me:!1,local:Boolean(t)},s={notices:notices.sanitize(l.feed?e.data&&e.data.notices:r&&r.notices),local:o},c=s.notices,n=this.meCache.getStale();return(a||(()=>l.feed&&(c.some(e=>"creators"===e.audience)||notices.hasShares(n))?L.resolveDefault(this.callHubMe(),null).then(e=>{const t=e&&0===e.result&&e.data?e.data:null;return t&&this.meCache.set(t),t}):Promise.resolve(null))()).then(e=>{this.noticesCache.set(s);const t=notices.inbox(s,e||n,null).map(e=>e.key),a=this.inboxState();return l.me=Boolean(e),this.setInboxState({read:notices.pruneKeys(a.read,t,l),done:notices.pruneKeys(a.done,t,l)}),s})})},callHubGet:e=>/^[A-Za-z0-9]+$/.test(String(e||""))?hubFetch("/api/v1/themes/aurora/configs/"+e):Promise.resolve({result:1,error:"invalid_id"}),hubAssetUrl:e=>"string"==typeof e&&ASSET_PATH_RE.test(e)?HUB_BASE+e:"",callHubApply:rpc.declare({object:"luci.aurora",method:"hub_apply",params:["id"]}),callGetHubStatus:rpc.declare({object:"luci.aurora",method:"get_hub_status",params:["job_id"]}),callHubRestore:rpc.declare({object:"luci.aurora",method:"hub_restore_backup"}),callHubLocalState:rpc.declare({object:"luci.aurora",method:"hub_local_state"}),callHubShareBegin:rpc.declare({object:"luci.aurora",method:"hub_share_begin",params:["name","description","target_id"]}),callHubShareCommit:rpc.declare({object:"luci.aurora",method:"hub_share_commit",params:["draft_id"]}),publishCurrentConfig(e){const t=e||{},a=e=>{"function"==typeof t.onProgress&&t.onProgress(e)};return a({phase:"begin"}),L.resolveDefault(this.callHubShareBegin(t.name,t.description||"",t.targetId||""),null).then(e=>{if(!e||0!==e.result)return{result:1,error:e&&e.error||"hub_unreachable"};if(e.duplicate)return{result:0,id:e.id,duplicate:!0};const t=e.assets||[];let r=0;const o=()=>{if(r>=t.length)return a({phase:"commit"}),L.resolveDefault(this.callHubShareCommit(e.draft_id),null).then(e=>e&&0===e.result?{result:0,id:e.id,assets:t.length}:{result:1,error:e&&e.error||"hub_unreachable"});const l=t[r],s={kind:l.kind,index:r+1,count:t.length};return r+=1,fetch(l.src).then(e=>e.ok?e.blob():null).catch(()=>null).then(e=>e?(a({phase:"upload",loaded:0,total:e.size,...s}),putAssetBytes(HUB_BASE+l.url,l.ticket,e,(e,t)=>a({phase:"upload",loaded:e,total:t,...s})).then(e=>e?o():{result:1,error:"asset_upload_failed"})):{result:1,error:"asset_unreadable"})};return o()})},callHubSetNickname:rpc.declare({object:"luci.aurora",method:"hub_set_nickname",params:["nickname"]}),callGetInitData:rpc.declare({object:"luci.aurora",method:"get_init_data"}),callAddFeed:rpc.declare({object:"luci.aurora",method:"add_feed"}),callHubMe:rpc.declare({object:"luci.aurora",method:"hub_me"}),callHubExportKey:rpc.declare({object:"luci.aurora",method:"hub_export_key"}),callHubImportKey:rpc.declare({object:"luci.aurora",method:"hub_import_key",params:["key"]}),callHubDelete:rpc.declare({object:"luci.aurora",method:"hub_delete",params:["id"]})});