[feat] support memos v22+ (#505)

This commit is contained in:
钟意 2024-08-13 16:23:06 +08:00 committed by GitHub
parent 99ae3b1a48
commit 7321d7cd8d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 92 additions and 70 deletions

View File

@ -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 = '<div class="timenode" index="' + i + '">';
cell += '<div class="header">';
if (!users.length && !hide.includes('user')) {
cell += '<div class="user-info">';
if (default_avatar.length > 0) {
cell += `<img src="${default_avatar}">`;
}
cell += '<span>' + item.creatorName + '</span>';
cell += '</div>';
}
cell += '<span>' + date.toLocaleString() + '</span>';
cell += '</div>';
cell += '<div class="body">';
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 += '<div class="tag-plugin image">';
for (let img of imgs) {
if (img.externalLink?.length > 0) {
cell += `<div class="image-bg"><img src="${img.externalLink}" data-fancybox="memos"></div>`;
} else {
cell += `<div class="image-bg"><img src="https://${host}/o/r/${img.id}" data-fancybox="memos"></div>`;
}
}
cell += '</div>';
}
cell += '</div>';
cell += '</div>';
$(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 `<div class="timenode">
<div class="header">${!users.length && !hide.includes('user') ? await versionHandler.buildUser(item, memos, default_avatar) : ''}
<span>${versionHandler.buildDate(item).toLocaleString()}</span></div>
<div class="body">${marked.parse(item.content || '')}
<div class="tag-plugin image">${versionHandler.buildImages(item, host).join('')}</div>
</div></div>`;
}
// Memos版本管理
const versionHandlers = {
"22-": {
buildUser: async (item, memos, default_avatar) =>
`<div class="user-info">${default_avatar ? `<img src="${default_avatar}">` : ''}<span>${item.creatorName}</span></div>`,
buildDate: item => new Date(item.createdTs * 1000),
buildImages: (item, host) => (item.resourceList || []).filter(res => res.type?.includes('image/')).map(res =>
`<div class="image-bg"><img src="${res.externalLink || `https://${host}/o/r/${res.id}`}"></div>`
)
},
"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 `<div class="user-info">${avatarUrl ? `<img src="${avatarUrl}">` : ''}<span>${name}</span></div>`;
},
buildDate: item => new Date(item.createTime),
buildImages: (item) => (item.resources || []).filter(res => res.type?.includes('image/')).map(res =>
`<div class="image-bg"><img src="${res.externalLink || `https://${host}/o/r/${res.id}`}"></div>`
)
},
"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
}
};
});
});