mirror of
https://github.com/caiwx86/small-packages.git
synced 2026-11-01 14:13:56 +08:00
1 line
7.1 KiB
JavaScript
Generated
1 line
7.1 KiB
JavaScript
Generated
"use strict";"require baseclass";const POLL_INTERVAL_MS=432e5,MAX_NOTICES=20,MAX_TITLE_CHARS=120,MAX_BODY_CHARS=4e3,MAX_REASON_CHARS=1e3,MAX_VERSION_CHARS=64,ID_RE=/^[A-Za-z0-9]{1,32}$/,LANG_RE=/^[a-z]{2}(-[a-z]{2,4})?$/,LEVELS=["info","warning","critical"],BADGE_LEVELS=["warning","critical"],AUDIENCES=["all","creators"],COMPAT_STATES=["unsupported","deprecated"],HTTPS_PREFIX="https://",LUCI_PATH_RE=/^admin\/[A-Za-z0-9_\-./?=&%#]*$/,URL_FORBIDDEN_RE=/[\u0000-\u0020\u007f\\]/,STAMP_RE=/^(\d{4})-(\d{2})-(\d{2})(?:[ T](\d{2}):(\d{2})(?::(\d{2}))?)?/,SUNSET_DATE_RE=/^\d{4}-\d{2}-\d{2}(?=$|[ T])/,GROUP_ACTION="action",GROUP_UPDATES="updates",SOURCE_ROUTER="router",SOURCE_SHARES="shares",SOURCE_STORE="store",FEED_KEY="feed:missing",LOCAL_KEY_PREFIX="feed:",DERIVED_KEY_MARK=":",AGE_STEPS=[["week",6048e5],["day",864e5],["hour",36e5],["minute",6e4]],isObject=e=>null!==e&&"object"==typeof e&&!Array.isArray(e),charCount=e=>Array.from(e).length,isTitle=e=>"string"==typeof e&&""!==e.trim()&&charCount(e)<=120,isBody=e=>"string"==typeof e&&charCount(e)<=4e3,cleanI18n=e=>{const t={};return isObject(e)?(Object.keys(e).forEach(s=>{const r=e[s];if(!LANG_RE.test(s)||!isObject(r))return;const n={};isTitle(r.title)&&(n.title=r.title),isBody(r.body)&&""!==r.body&&(n.body=r.body),Object.keys(n).length&&(t[s]=n)}),t):t},stampOf=e=>{const t="string"==typeof e?STAMP_RE.exec(e):null;return t?t[0]:""},cleanNotice=e=>isObject(e)&&"string"==typeof e.id&&ID_RE.test(e.id)&&LEVELS.includes(e.level)&&AUDIENCES.includes(e.audience)&&isTitle(e.title)&&isBody(e.body)?{id:e.id,level:e.level,audience:e.audience,title:e.title,body:e.body,url:"string"==typeof e.url?e.url:"",i18n:cleanI18n(e.i18n),at:stampOf(e.starts_at)||stampOf(e.at)||stampOf(e.created_at)}:null,stringList=e=>Array.isArray(e)?e.filter(e=>"string"==typeof e):[],ownConfigs=e=>isObject(e)&&Array.isArray(e.configs)?e.configs.filter(e=>isObject(e)&&"string"==typeof e.id&&ID_RE.test(e.id)):[],isReason=e=>"string"==typeof e&&charCount(e)<=1e3,versionOf=e=>{const t=isObject(e)?e.installed_version:null;return"string"==typeof t&&charCount(t)<=64?t:""},nameOf=e=>"string"==typeof e.name?e.name:"",shortHash=e=>{let t=2166136261;for(let s=0;s<e.length;s+=1)t^=e.charCodeAt(s),t=Math.imul(t,16777619);return(t>>>0).toString(36)};return baseclass.extend({POLL_INTERVAL_MS:432e5,MAX_NOTICES:20,GROUP_ACTION:"action",GROUP_UPDATES:"updates",SOURCE_ROUTER:"router",SOURCE_SHARES:"shares",SOURCE_STORE:"store",FEED_KEY:FEED_KEY,localize(e,t){const s=isObject(e)?e:{},r=isObject(s.i18n)?s.i18n:{},n=String(null==t?"":t).toLowerCase().replace(/_/g,"-"),i=[r[n],r[n.split("-")[0]],s],o=e=>{const t=i.find(t=>isObject(t)&&"string"==typeof t[e]&&""!==t[e]);return t?t[e]:""};return{title:o("title"),body:o("body")}},safeUrl(e){if("string"!=typeof e||URL_FORBIDDEN_RE.test(e))return"";if(LUCI_PATH_RE.test(e))return e.includes("..")||e.includes("//")?"":e;if(!e.startsWith("https://"))return"";try{const t=new URL(e);return"https:"===t.protocol&&t.hostname?t.username||t.password?"":e:""}catch(e){return""}},isExternalUrl:e=>"string"==typeof e&&e.startsWith("https://"),present(e,t){const s=this.localize(e,t),r=this.safeUrl(e?.url);return{title:s.title,body:s.body,url:r,external:this.isExternalUrl(r)}},sanitize(e){if(!Array.isArray(e))return[];const t=[];for(const s of e){if(t.length>=20)break;const e=cleanNotice(s);e&&!t.some(t=>t.id===e.id)&&t.push(e)}return t},isDue:(e,t)=>!Number.isFinite(e)||e<=0||!Number.isFinite(t)||t<e||t-e>=432e5,creatorAffected(e){if(!isObject(e))return!1;const t=isObject(e.compat_summary)?e.compat_summary:{},s=e=>Number.isFinite(t[e])?t[e]:0;return s("deprecated")+s("unsupported")>0||(Array.isArray(e.configs)?e.configs:[]).some(e=>{if(!isObject(e)||"removed"===e.status)return!1;const t=isObject(e.compat)?e.compat.state:null;return"string"==typeof t&&"ok"!==t})},hasShares:e=>ownConfigs(e).length>0,compatBadge(e){const t=e?.compat?.state;return COMPAT_STATES.includes(t)?t:null},sunsetDate(e){const t=e?.compat?.sunset_at,s="string"==typeof t?SUNSET_DATE_RE.exec(t):null;return s?s[0]:""},compatCounts(e){const t={deprecated:0,unsupported:0,sunset:""};return ownConfigs(e).filter(e=>"removed"!==e.status).forEach(e=>{const s=this.compatBadge(e);s&&(t[s]+=1);const r="deprecated"===s?this.sunsetDate(e):"";r&&(!t.sunset||r<t.sunset)&&(t.sunset=r)}),isObject(e)&&isObject(e.compat_summary)&&["deprecated","unsupported"].forEach(s=>{const r=e.compat_summary[s];t[s]=Number.isInteger(r)&&r>0?r:0}),t},shortHash:shortHash,derive(e){const t=ownConfigs(e),s=t.filter(e=>"removed"!==e.status),r=[];return COMPAT_STATES.forEach(e=>{const t=s.filter(t=>this.compatBadge(t)===e);if(!t.length)return;const n=t.map(e=>this.sunsetDate(e)).filter(e=>e).sort();r.push({key:"compat:"+e+":"+shortHash(t.map(e=>e.id).sort().join(",")),group:"action",source:"shares",kind:"compat",state:e,level:"critical",names:t.map(nameOf),sunset:"deprecated"===e&&n.length?n[0]:""})}),s.filter(e=>"rejected"===e.assets_status).forEach(e=>r.push({key:"rejected:"+e.id,group:"action",source:"shares",kind:"rejected",level:"warning",name:nameOf(e),reason:isReason(e.assets_reject_reason)?e.assets_reject_reason:""})),t.filter(e=>"removed"===e.status).forEach(e=>r.push({key:"removed:"+e.id,group:"action",source:"shares",kind:"removed",level:"warning",name:nameOf(e)})),r},localState(e){const t=isObject(e)&&isObject(e.feed)?e.feed:null;if(!t||"string"!=typeof t.pm||""===t.pm||"unknown"===t.pm)return null;const s=isObject(e.versions)?e.versions:{};return{feedMissing:!0!==t.configured,theme:versionOf(s.theme),app:versionOf(s.config)}},deriveLocal:e=>isObject(e)&&!0===e.feedMissing?[{key:FEED_KEY,group:"action",source:"router",kind:"feed",level:"warning",theme:"string"==typeof e.theme?e.theme:"",app:"string"==typeof e.app?e.app:""}]:[],inbox(e,t,s){const r=stringList(s&&s.read),n=stringList(s&&s.done),i=this.creatorAffected(t),o=this.sanitize(isObject(e)?e.notices:null).filter(e=>"creators"!==e.audience||i).map(e=>({key:e.id,group:"updates",source:"store",kind:"notice",level:e.level,at:e.at,notice:e}));return this.deriveLocal(isObject(e)?e.local:null).concat(this.derive(t),o).filter(e=>!n.includes(e.key)).map(e=>Object.assign(e,{unread:!r.includes(e.key)}))},unreadCount:e=>(Array.isArray(e)?e:[]).filter(e=>e&&e.unread).length,badgeCount(e,t,s){return isObject(e)?this.inbox(e,t,s).filter(e=>e.unread&&("action"===e.group||BADGE_LEVELS.includes(e.level))).length:0},withKey(e,t){const s=stringList(e);return"string"!=typeof t||s.includes(t)?s:s.concat(t)},withoutKey:(e,t)=>stringList(e).filter(e=>e!==t),withKeys(e,t){return(Array.isArray(t)?t:[]).reduce((e,t)=>this.withKey(e,t&&t.key),stringList(e))},pruneKeys(e,t,s){const r=stringList(t);return stringList(e).filter((e,t,n)=>n.indexOf(e)===t&&(r.includes(e)||(e=>e.startsWith("feed:")?!(s&&s.local):e.includes(":")?!(s&&s.me):!(s&&s.feed))(e)))},stampMs(e){const t="string"==typeof e?STAMP_RE.exec(e):null;if(!t)return null;const[s,r,n,i,o,a]=t.slice(1).map(e=>Number(e||0)),c=Date.UTC(s,r-1,n,i,o,a);return Number.isFinite(c)?c:null},ageOf(e,t){if(!Number.isFinite(e)||!Number.isFinite(t))return null;const s=Math.max(0,t-e),r=AGE_STEPS.find(e=>s>=e[1]);return r?{value:-Math.floor(s/r[1]),unit:r[0]}:{value:0,unit:"minute"}}}); |