Files
wehub-resource-sync 2114b14ee0
Sync main into demo / sync (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 12:35:26 +08:00

71 lines
2.6 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import defaults from './defaults.json';
import { REDBOOK_CONSTANTS } from '../constants';
import { resolveDataTimestamp } from '../../../os/TimeService';
import { resolveCdnUrl } from '../../../os/utils/cdn';
import type { RedBookConfig } from '../types';
const ASSET_EXT_RE = /\.(jpe?g|png|webp|gif|svg|mp4|webm|avif)(\?.*)?$/i;
// RedBook 数据有两种资源源:
// 1) './images/...' / 'images/...' → 外部媒体镜像(CDN, mobilegym-data/redbook/...
// 2) 其它带 asset 扩展名的裸路径 → 仓库内 bundle 资源(/@app-assets/RedBook/...
const resolveAssetUrl = (raw: unknown): unknown => {
const s = typeof raw === 'string' ? raw : null;
if (!s) return raw;
if (s.startsWith('http') || s.startsWith('/')) return raw;
if (s.startsWith('./images/') || s.startsWith('images/')) {
return resolveCdnUrl(s, 'redbook');
}
if (!ASSET_EXT_RE.test(s)) return raw;
return `/@app-assets/RedBook/${s}`;
};
export const resolveAssetsDeep = (value: unknown): unknown => {
if (Array.isArray(value)) return value.map(resolveAssetsDeep);
if (value && typeof value === 'object') {
const obj = value as Record<string, unknown>;
const out: Record<string, unknown> = {};
for (const [k, v] of Object.entries(obj)) {
out[k] = resolveAssetsDeep(resolveAssetUrl(v));
}
return out;
}
return resolveAssetUrl(value);
};
const ts = (v: unknown) => resolveDataTimestamp(v as string | number);
function resolveAllTimestamps(data: typeof defaults) {
return {
...data,
notes: Object.fromEntries(Object.entries((data as any).notes || {}).map(([id, note]) => [
id,
{
...(note as any),
createdAt: ts((note as any).createdAt),
commentList: (note as any).commentList?.map((c: any) => ({ ...c, time: ts(c.time) })),
},
])),
comments: Object.fromEntries(Object.entries((data as any).comments || {}).map(([id, comment]) => [
id,
{ ...(comment as any), time: ts((comment as any).time) },
])),
notifications: ((data as any).notifications || []).map((notification: any) => ({
...notification,
timestamp: ts(notification.timestamp),
})),
chats: ((data as any).chats || []).map((chat: any) => ({
...chat,
lastTime: ts(chat.lastTime),
messages: (chat.messages || []).map((m: any) => ({ ...m, timestamp: ts(m.timestamp) })),
})),
};
}
const resolvedDefaults = resolveAllTimestamps(resolveAssetsDeep(defaults) as typeof defaults);
export const REDBOOK_CONFIG: RedBookConfig & typeof REDBOOK_CONSTANTS = {
...REDBOOK_CONSTANTS,
...resolvedDefaults,
} as unknown as RedBookConfig & typeof REDBOOK_CONSTANTS;