From d15c588080a56811d19b03d054080d5be64f64f1 Mon Sep 17 00:00:00 2001 From: Mar0xy Date: Tue, 31 Oct 2023 11:31:31 +0100 Subject: [PATCH] upd: change handling of suggestions and status trends --- .../server/api/mastodon/endpoints/search.ts | 101 +++++------------- packages/megalodon/src/megalodon.ts | 2 +- 2 files changed, 30 insertions(+), 73 deletions(-) diff --git a/packages/backend/src/server/api/mastodon/endpoints/search.ts b/packages/backend/src/server/api/mastodon/endpoints/search.ts index 772ef3307..633a3d05e 100644 --- a/packages/backend/src/server/api/mastodon/endpoints/search.ts +++ b/packages/backend/src/server/api/mastodon/endpoints/search.ts @@ -1,63 +1,8 @@ -import { Converter } from 'megalodon'; -import { MastoConverters, convertAccount, convertStatus } from '../converters.js'; +import { MastoConverters } from '../converters.js'; import { limitToInt } from './timeline.js'; import type { MegalodonInterface } from 'megalodon'; import type { FastifyRequest } from 'fastify'; -async function getHighlight( - BASE_URL: string, - domain: string, - accessTokens: string | undefined, -) { - const accessTokenArr = accessTokens?.split(' ') ?? [null]; - const accessToken = accessTokenArr[accessTokenArr.length - 1]; - try { - const apicall = await fetch(`${BASE_URL}/api/notes/featured`, - { - method: 'POST', - headers: { - 'Accept': 'application/json, text/plain, */*', - 'Content-Type': 'application/json', - }, - body: JSON.stringify({ i: accessToken }), - }); - const api = await apicall.json(); - const data: MisskeyEntity.Note[] = api; - return data.map((note) => Converter.note(note, domain)); - } catch (e: any) { - console.log(e); - console.log(e.response.data); - return []; - } -} - -async function getFeaturedUser( BASE_URL: string, host: string, accessTokens: string | undefined, limit: number ) { - const accessTokenArr = accessTokens?.split(' ') ?? [null]; - const accessToken = accessTokenArr[accessTokenArr.length - 1]; - try { - const apicall = await fetch(`${BASE_URL}/api/users`, - { - method: 'POST', - headers: { - 'Accept': 'application/json, text/plain, */*', - 'Content-Type': 'application/json', - }, - body: JSON.stringify({ i: accessToken, limit, origin: 'local', sort: '+follower', state: 'alive' }), - }); - const api = await apicall.json(); - const data: MisskeyEntity.UserDetail[] = api; - return data.map((u) => { - return { - source: 'past_interactions', - account: Converter.userDetail(u, host), - }; - }); - } catch (e: any) { - console.log(e); - console.log(e.response.data); - return []; - } -} export class ApiSearchMastodon { private request: FastifyRequest; private client: MegalodonInterface; @@ -89,8 +34,8 @@ export class ApiSearchMastodon { const stat = !type || type === 'statuses' ? await this.client.search(query.q, { type: 'statuses', ...query }) : null; const tags = !type || type === 'hashtags' ? await this.client.search(query.q, { type: 'hashtags', ...query }) : null; const data = { - accounts: await Promise.all(acct?.data.accounts.map(async (account) => await this.mastoConverter.convertAccount(account)) ?? []), - statuses: await Promise.all(stat?.data.statuses.map(async (status) => await this.mastoConverter.convertStatus(status)) ?? []), + accounts: await Promise.all(acct?.data.accounts.map(async (account: any) => await this.mastoConverter.convertAccount(account)) ?? []), + statuses: await Promise.all(stat?.data.statuses.map(async (status: any) => await this.mastoConverter.convertStatus(status)) ?? []), hashtags: tags?.data.hashtags ?? [], }; return data; @@ -102,27 +47,39 @@ export class ApiSearchMastodon { public async getStatusTrends() { try { - const data = await getHighlight( - this.BASE_URL, - this.request.hostname, - this.request.headers.authorization, - ); - return data.map((status) => convertStatus(status)); + let map; + await fetch(`${this.BASE_URL}/api/notes/featured`, + { + method: 'POST', + headers: { + 'Accept': 'application/json', + 'Content-Type': 'application/json', + }, + body: JSON.stringify({}), + }) + .then(res => res.json()) + .then((data) => { + map = data.map((status: any) => this.mastoConverter.convertStatus(status)); + }); + return map; } catch (e: any) { console.error(e); - return e.response.data; + return []; } } public async getSuggestions() { try { - const data = await getFeaturedUser( - this.BASE_URL, - this.request.hostname, - this.request.headers.authorization, - (this.request.query as any).limit || 20, - ); - return data.map((suggestion) => { suggestion.account = convertAccount(suggestion.account); return suggestion; }); + const data = await fetch(`${this.BASE_URL}/api/users`, + { + method: 'POST', + headers: { + 'Accept': 'application/json', + 'Content-Type': 'application/json', + }, + body: JSON.stringify({ i: this.request.headers.authorization?.replace('Bearer ', ''), limit: (this.request.query as any).limit || 20, origin: 'local', sort: '+follower', state: 'alive' }), + }).then((res) => res.json()).then((data) => data.map(((entry: any) => { return { source: 'global', account: entry }; }))); + return Promise.all(data.map(async (suggestion: any) => { suggestion.account = await this.mastoConverter.convertAccount(suggestion.account); return suggestion; })); } catch (e: any) { console.error(e); return e.response.data; diff --git a/packages/megalodon/src/megalodon.ts b/packages/megalodon/src/megalodon.ts index 19cd5c555..e2245f7c2 100644 --- a/packages/megalodon/src/megalodon.ts +++ b/packages/megalodon/src/megalodon.ts @@ -1041,7 +1041,7 @@ export interface MegalodonInterface { * * @return Array of lists. */ - getLists(id: string): Promise>> + getLists(id?: string): Promise>> /** * Show a single list. *