MCPcopy Create free account
hub / github.com/chavyleung/scripts / apiAddAppSubRaw

Function apiAddAppSubRaw

chavy.box.js:770–822  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

768}
769
770async function apiAddAppSubRaw() {
771 // 直接粘贴 JSON 内容添加订阅(非远程 URL)
772 // body: { json: '<订阅JSON字符串>', id, name }
773 const req = $.toObj($request.body) || {}
774 const rawText = typeof req.json === 'string' ? req.json : ''
775
776 // 体积上限,避免撑爆存储 / 拖垮客户端
777 const MAX_RAW_BYTES = 512 * 1024
778 if (rawText.length === 0) {
779 $.json = { code: -1, message: '订阅内容为空' }
780 return
781 }
782 if (rawText.length > MAX_RAW_BYTES) {
783 $.json = { code: -1, message: '订阅内容过大' }
784 return
785 }
786
787 // 严格解析:必须是合法 JSON 对象,含 id,且 apps 为数组
788 const sub = $.toObj(rawText)
789 if (!sub || typeof sub !== 'object' || Array.isArray(sub)) {
790 $.json = { code: -1, message: '订阅内容不是合法 JSON 对象' }
791 return
792 }
793 if (!sub.hasOwnProperty('id') || !sub.id) {
794 $.json = { code: -1, message: '订阅缺少 id 字段' }
795 return
796 }
797 if (!Array.isArray(sub.apps)) {
798 $.json = { code: -1, message: '订阅 apps 字段格式错误' }
799 return
800 }
801
802 // 无远程地址:用订阅自带 id 作 key(客户端 appSubCaches / appsubs 均按 url 索引,
803 // 故这里让 url 字段等于 id,两处对齐即可,不引入任何伪协议前缀)
804 const id = req.id || sub.id
805
806 // 写入订阅缓存(与 reloadAppSubCache 一致的结构)
807 const subcaches = getAppSubCaches()
808 subcaches[id] = sub
809 subcaches[id].url = id
810 subcaches[id].updateTime = new Date()
811 if (req.name) subcaches[id].name = req.name
812 $.setjson(subcaches, $.KEY_app_subCaches)
813
814 // 写入订阅引用列表(按 id 去重,避免重复粘贴产生多条)
815 const usercfgs = getUserCfgs()
816 usercfgs.appsubs = usercfgs.appsubs.filter((e) => e && e.id !== id)
817 usercfgs.appsubs.push({ url: id, enable: true, id })
818 $.setjson(usercfgs, $.KEY_usercfgs)
819
820 $.log(`添加订阅(粘贴), 成功! id=${id}`)
821 $.json = getBoxData()
822}
823
824async function apiDeleteAppSub() {
825 const sub = $.toObj($request.body)

Callers

nothing calls this directly

Calls 6

getAppSubCachesFunction · 0.70
getUserCfgsFunction · 0.70
getBoxDataFunction · 0.70
toObjMethod · 0.45
setjsonMethod · 0.45
logMethod · 0.45

Tested by

no test coverage detected