diff --git a/source/js/services/memos.js b/source/js/services/memos.js index 1afaa57..40a3655 100644 --- a/source/js/services/memos.js +++ b/source/js/services/memos.js @@ -1,73 +1,95 @@ utils.jq(() => { - $(function () { - const els = document.getElementsByClassName('ds-memos'); - for (var i = 0; i < els.length; i++) { - const el = els[i]; - const api = el.getAttribute('api'); - if (api == null) { - continue; - } - const default_avatar = el.getAttribute('avatar') || def.avatar; - const limit = el.getAttribute('limit'); - const host = api.replace(/https:\/\/(.*?)\/(.*)/i, '$1'); - // layout - utils.request(el, api, function(data) { - var users = []; - const filter = el.getAttribute('user'); - if (filter && filter.length > 0) { - users = filter.split(","); - } - var hide = []; - const hideStr = el.getAttribute('hide'); - if (hideStr && hideStr.length > 0) { - hide = hideStr.split(","); - } - data.forEach((item, i) => { - if (limit && i >= limit) { - return; - } - if (item.user && item.user.login && users.length > 0) { - if (!users.includes(item.user.login)) { - return; - } - } - let date = new Date(item.createdTs * 1000) - var cell = '
'; - cell += '
'; - if (!users.length && !hide.includes('user')) { - cell += ''; - } - cell += '' + date.toLocaleString() + ''; - cell += '
'; - cell += '
'; - cell += marked.parse(item.content || ''); - var imgs = []; - for (let res of item.resourceList) { - if (res.type?.includes('image/')) { - imgs.push(res); - } - } - if (imgs.length > 0) { - cell += '
'; - for (let img of imgs) { - if (img.externalLink?.length > 0) { - cell += `
`; - } else { - cell += `
`; - } - } - cell += '
'; - } - cell += '
'; - cell += '
'; - $(el).append(cell); - }); - }); + const els = Array.from(document.getElementsByClassName('ds-memos')); + + els.forEach(el => { + const api = el.getAttribute('api'); + if (!api) return; + + const default_avatar = el.getAttribute('avatar') || def.avatar; + const limit = el.getAttribute('limit'); + const host = api.match(/https:\/\/(.*?)\/(.*)/i)[1]; + + utils.request(el, api, async data => { + let memos = versionHandlers.identify(data); + if (memos.version === "feature" )return; + + const users = el.getAttribute('user')?.split(",") || []; + const hide = el.getAttribute('hide')?.split(",") || []; + + await Promise.all(memos.data.slice(0, limit || memos.data.length).map(item => + createMemoCell(item, memos, users, hide, default_avatar, host).then(cell => $(el).append(cell)) + )); + }); + + async function createMemoCell(item, memos, users, hide, default_avatar, host) { + const versionHandler = versionHandlers[memos.version] || versionHandlers["feature"]; + return `
+
${!users.length && !hide.includes('user') ? await versionHandler.buildUser(item, memos, default_avatar) : ''} + ${versionHandler.buildDate(item).toLocaleString()}
+
${marked.parse(item.content || '')} +
${versionHandler.buildImages(item, host).join('')}
+
`; } + + // Memos版本管理 + const versionHandlers = { + "22-": { + buildUser: async (item, memos, default_avatar) => + `
${default_avatar ? `` : ''}${item.creatorName}
`, + buildDate: item => new Date(item.createdTs * 1000), + buildImages: (item, host) => (item.resourceList || []).filter(res => res.type?.includes('image/')).map(res => + `
` + ) + }, + "22+": { + buildUser: async (item, memos, default_avatar) => { + const creatorId = item?.creator.split('/')[1]; + let user = memos.users.find(user => user.id === parseInt(creatorId)); + if (!user) { + if (!memos.requests[creatorId]) { + memos.requests[creatorId] = fetch(`${memos.site}/api/v1/users/${creatorId}`) + .then(response => response.json()) + .then(data => { + if (data.username) { + user = data; + memos.users.push(data); + } else { + user = null; + } + }) + .finally(() => delete memos.requests[creatorId]); + } + await memos.requests[creatorId]; + user = memos.users.find(user => user.id === parseInt(creatorId)); + } + const name = user ? user.nickname || user.username : 'memos'; + const avatarUrl = user?.avatarUrl ? `${memos.site}${user.avatarUrl}` : default_avatar || ''; + return `
${avatarUrl ? `` : ''}${name}
`; + }, + buildDate: item => new Date(item.createTime), + buildImages: (item) => (item.resources || []).filter(res => res.type?.includes('image/')).map(res => + `
` + ) + }, + "feature": { + buildUser: async () => "memos", + buildDate: () => new Date(), + buildImages: () => [] + }, + identify: (data) => { + let memos = { version: "feature", users: [], site: api.split('/api/v1')[0], requests: {}, data: [] } + if (Array.isArray(data)) { + memos.version = "22-"; + memos.data = data; + } else if (data.memos) { + memos.version = "22+"; + memos.data = data.memos; + } else { + memos.version = "feature"; + console.log("当前Memos版本过高,请到Stellar社区反馈"); + } + return memos + } + }; }); -}); \ No newline at end of file +});