upd: change handling of suggestions and status trends

This commit is contained in:
Mar0xy 2023-10-31 11:31:31 +01:00
parent 20a8bb0467
commit d15c588080
No known key found for this signature in database
GPG key ID: 56569BBE47D2C828
2 changed files with 30 additions and 73 deletions

View file

@ -1,63 +1,8 @@
import { Converter } from 'megalodon'; import { MastoConverters } from '../converters.js';
import { MastoConverters, convertAccount, convertStatus } from '../converters.js';
import { limitToInt } from './timeline.js'; import { limitToInt } from './timeline.js';
import type { MegalodonInterface } from 'megalodon'; import type { MegalodonInterface } from 'megalodon';
import type { FastifyRequest } from 'fastify'; 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 { export class ApiSearchMastodon {
private request: FastifyRequest; private request: FastifyRequest;
private client: MegalodonInterface; 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 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 tags = !type || type === 'hashtags' ? await this.client.search(query.q, { type: 'hashtags', ...query }) : null;
const data = { const data = {
accounts: await Promise.all(acct?.data.accounts.map(async (account) => await this.mastoConverter.convertAccount(account)) ?? []), 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) => await this.mastoConverter.convertStatus(status)) ?? []), statuses: await Promise.all(stat?.data.statuses.map(async (status: any) => await this.mastoConverter.convertStatus(status)) ?? []),
hashtags: tags?.data.hashtags ?? [], hashtags: tags?.data.hashtags ?? [],
}; };
return data; return data;
@ -102,27 +47,39 @@ export class ApiSearchMastodon {
public async getStatusTrends() { public async getStatusTrends() {
try { try {
const data = await getHighlight( let map;
this.BASE_URL, await fetch(`${this.BASE_URL}/api/notes/featured`,
this.request.hostname, {
this.request.headers.authorization, method: 'POST',
); headers: {
return data.map((status) => convertStatus(status)); '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) { } catch (e: any) {
console.error(e); console.error(e);
return e.response.data; return [];
} }
} }
public async getSuggestions() { public async getSuggestions() {
try { try {
const data = await getFeaturedUser( const data = await fetch(`${this.BASE_URL}/api/users`,
this.BASE_URL, {
this.request.hostname, method: 'POST',
this.request.headers.authorization, headers: {
(this.request.query as any).limit || 20, 'Accept': 'application/json',
); 'Content-Type': 'application/json',
return data.map((suggestion) => { suggestion.account = convertAccount(suggestion.account); return suggestion; }); },
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) { } catch (e: any) {
console.error(e); console.error(e);
return e.response.data; return e.response.data;

View file

@ -1041,7 +1041,7 @@ export interface MegalodonInterface {
* *
* @return Array of lists. * @return Array of lists.
*/ */
getLists(id: string): Promise<Response<Array<Entity.List>>> getLists(id?: string): Promise<Response<Array<Entity.List>>>
/** /**
* Show a single list. * Show a single list.
* *