feat(misskey): add avatar to user timeline (#18104)

This commit is contained in:
Tsuyumi25
2025-01-12 13:04:16 +00:00
committed by GitHub
parent 26acb31419
commit c13de97b78
3 changed files with 15 additions and 11 deletions
+1 -1
View File
@@ -51,7 +51,7 @@ export interface MisskeyNote {
}
// https://github.com/misskey-dev/misskey/blob/d2e8dc4fe3c6e90e68001ed1f092d4e3d2454283/packages/backend/src/models/json-schema/user.ts
interface MisskeyUser {
export interface MisskeyUser {
id: string;
name: string | null;
username: string;
+4 -3
View File
@@ -1,4 +1,4 @@
import { Route, ViewType } from '@/types';
import { Route, ViewType, Data } from '@/types';
import utils from './utils';
import { config } from '@/config';
import ConfigNotFoundError from '@/errors/types/config-not-found';
@@ -38,7 +38,7 @@ Examples:
handler,
};
async function handler(ctx) {
async function handler(ctx): Promise<Data> {
const username = ctx.req.param('username');
const [, pureUsername, site] = username.match(/@?(\w+)@(\w+\.\w+)/) || [];
if (!pureUsername || !site) {
@@ -57,7 +57,7 @@ async function handler(ctx) {
throw new InvalidParameterError('withRenotes and mediaOnly cannot both be true.');
}
const { accountData } = await utils.getUserTimelineByUsername(pureUsername, site, {
const { accountData, avatarUrl } = await utils.getUserTimelineByUsername(pureUsername, site, {
withRenotes,
mediaOnly,
});
@@ -65,6 +65,7 @@ async function handler(ctx) {
return {
title: `User timeline for ${username} on ${site}`,
link: `https://${site}/@${pureUsername}`,
image: avatarUrl || '',
item: utils.parseNotes(accountData, site),
};
}
+10 -7
View File
@@ -7,7 +7,7 @@ import path from 'node:path';
import cache from '@/utils/cache';
import got from '@/utils/got';
import { MisskeyNote } from './types';
import { MisskeyNote, MisskeyUser } from './types';
const allowSiteList = ['misskey.io', 'madost.one', 'mk.nixnet.social'];
@@ -71,7 +71,7 @@ async function getUserTimelineByUsername(username, site, { withRenotes = false,
const searchUrl = `https://${site}/api/users/search-by-username-and-host`;
const cacheUid = `misskey_username/${site}/${username}`;
const accountId = await cache.tryGet(cacheUid, async () => {
const userData = (await cache.tryGet(cacheUid, async () => {
const searchResponse = await got({
method: 'post',
url: searchUrl,
@@ -82,13 +82,16 @@ async function getUserTimelineByUsername(username, site, { withRenotes = false,
limit: 1,
},
});
const userData = searchResponse.data.find((item) => item.username === username);
const user = searchResponse.data.find((item) => item.username === username);
if (!userData) {
if (!user) {
throw new Error(`username ${username} not found`);
}
return userData.id;
});
return user;
})) as MisskeyUser;
const accountId = userData.id;
const avatarUrl = userData.avatarUrl;
// https://misskey.io/api-doc#tag/users/operation/users___notes
const usernotesUrl = `https://${site}/api/users/notes`;
@@ -106,7 +109,7 @@ async function getUserTimelineByUsername(username, site, { withRenotes = false,
},
});
const accountData = usernotesResponse.data;
return { site, accountId, accountData };
return { site, accountId, accountData, avatarUrl };
}
export default { parseNotes, getUserTimelineByUsername, allowSiteList };