From ec55071eb24fb252ffd443a32b02eb7854358f77 Mon Sep 17 00:00:00 2001 From: Laura Hausmann Date: Thu, 5 Oct 2023 02:25:13 +0200 Subject: [PATCH] [mastodon-client] GET /v1/trends/{statuses,hashtags,links} --- .../src/server/api/mastodon/endpoints/misc.ts | 29 +++++++++------ .../server/api/mastodon/endpoints/search.ts | 36 ------------------- .../src/server/api/mastodon/helpers/misc.ts | 24 +++++++++++++ 3 files changed, 43 insertions(+), 46 deletions(-) diff --git a/packages/backend/src/server/api/mastodon/endpoints/misc.ts b/packages/backend/src/server/api/mastodon/endpoints/misc.ts index fc30a3f09..cedefa9fb 100644 --- a/packages/backend/src/server/api/mastodon/endpoints/misc.ts +++ b/packages/backend/src/server/api/mastodon/endpoints/misc.ts @@ -75,21 +75,30 @@ export function setupEndpointsMisc(router: Router): void { }, ); - router.get("/v1/trends", async (ctx) => { - const BASE_URL = `${ctx.request.protocol}://${ctx.request.hostname}`; - const accessTokens = ctx.request.headers.authorization; - const client = getClient(BASE_URL, accessTokens); // we are using this here, because in private mode some info isnt - // displayed without being logged in + router.get(["/v1/trends/tags", "/v1/trends"], async (ctx) => { try { - const data = await client.getInstanceTrends(); - ctx.body = data.data; + const args = limitToInt(ctx.query); + ctx.body = await MiscHelpers.getTrendingHashtags(args.limit, args.offset); } catch (e: any) { - console.error(e); - ctx.status = 401; - ctx.body = e.response.data; + ctx.status = 500; + ctx.body = { error: e.message }; } }); + router.get("/v1/trends/statuses", async (ctx) => { + try { + const args = limitToInt(ctx.query); + ctx.body = await MiscHelpers.getTrendingStatuses(args.limit, args.offset); + } catch (e: any) { + ctx.status = 500; + ctx.body = { error: e.message }; + } + }); + + router.get("/v1/trends/links", async (ctx) => { + ctx.body = []; + }); + router.get("/v1/preferences", async (ctx) => { const BASE_URL = `${ctx.request.protocol}://${ctx.request.hostname}`; const accessTokens = ctx.request.headers.authorization; diff --git a/packages/backend/src/server/api/mastodon/endpoints/search.ts b/packages/backend/src/server/api/mastodon/endpoints/search.ts index 113c6c6fb..7e04be87f 100644 --- a/packages/backend/src/server/api/mastodon/endpoints/search.ts +++ b/packages/backend/src/server/api/mastodon/endpoints/search.ts @@ -54,40 +54,4 @@ export function setupEndpointsSearch(router: Router): void { ctx.body = {error: e.message}; } }); - router.get("/v1/trends/statuses", async (ctx) => { - const BASE_URL = `${ctx.request.protocol}://${ctx.request.hostname}`; - const accessTokens = ctx.headers.authorization; - try { - const data = await getHighlight( - BASE_URL, - ctx.request.hostname, - accessTokens, - ); - ctx.body = data.map((status) => convertStatusIds(status)); - } catch (e: any) { - console.error(e); - ctx.status = 401; - ctx.body = e.response.data; - } - }); -} - -async function getHighlight( - BASE_URL: string, - domain: string, - accessTokens: string | undefined, -) { - const accessTokenArr = accessTokens?.split(" ") ?? [null]; - const accessToken = accessTokenArr[accessTokenArr.length - 1]; - try { - const api = await axios.post(`${BASE_URL}/api/notes/featured`, { - i: accessToken, - }); - const data: MisskeyEntity.Note[] = api.data; - return data.map((note) => new Converter(BASE_URL).note(note, domain)); - } catch (e: any) { - console.log(e); - console.log(e.response.data); - return []; - } } \ No newline at end of file diff --git a/packages/backend/src/server/api/mastodon/helpers/misc.ts b/packages/backend/src/server/api/mastodon/helpers/misc.ts index 429c0d6a8..eb3a1c521 100644 --- a/packages/backend/src/server/api/mastodon/helpers/misc.ts +++ b/packages/backend/src/server/api/mastodon/helpers/misc.ts @@ -18,6 +18,7 @@ import { generateBlockQueryForUsers } from "@/server/api/common/generate-block-q import { uniqBy } from "@/prelude/array.js"; import { EmojiConverter } from "@/server/api/mastodon/converters/emoji.js"; import { populateEmojis } from "@/misc/populate-emojis.js"; +import { NoteConverter } from "@/server/api/mastodon/converters/note.js"; export class MiscHelpers { public static async getInstance(): Promise { @@ -203,4 +204,27 @@ export class MiscHelpers { ) ); } + + public static async getTrendingStatuses(limit: number = 20, offset: number = 0): Promise { + if (limit > 40) limit = 40; + const query = Notes.createQueryBuilder("note") + .addSelect("note.score") + .andWhere("note.score > 0") + .andWhere("note.createdAt > :date", { date: new Date(Date.now() - 1000 * 60 * 60 * 24) }) + .andWhere("note.visibility = 'public'") + .andWhere("note.userHost IS NULL") + .orderBy("note.score", "DESC"); + + return query + .skip(offset) + .take(limit) + .getMany() + .then(result => NoteConverter.encodeMany(result, null)); + } + + public static async getTrendingHashtags(limit: number = 10, offset: number = 0): Promise { + if (limit > 20) limit = 20; + return []; + //FIXME: This was already implemented in api/endpoints/hashtags/trend.ts, but the implementation is sketchy at best. Rewrite from scratch. + } } \ No newline at end of file