cleanup Mastodon API (resolves #804 and #865, partially resolves #492)

* Fix TS errors and warnings
* Fix ESLint errors and warnings
* Fix property typos in various places
* Fix property data conversion
* Add missing entity properties
* Normalize logging and reduce spam
* Check for missing request parameters
* Allow mastodon API to work with local debugging
* Safer error handling
* Fix quote-post detection
This commit is contained in:
Hazelnoot 2025-01-31 02:46:38 -05:00
parent 2c2fb8a692
commit 16f483d273
13 changed files with 1275 additions and 1147 deletions

View file

@ -4,87 +4,86 @@
*/
import { MastoConverters } from '../converters.js';
import { limitToInt } from './timeline.js';
import { parseTimelineArgs, TimelineArgs } from '../timelineArgs.js';
import Account = Entity.Account;
import Status = Entity.Status;
import type { MegalodonInterface } from 'megalodon';
import type { FastifyRequest } from 'fastify';
export class ApiSearchMastodon {
private request: FastifyRequest;
private client: MegalodonInterface;
private BASE_URL: string;
constructor(request: FastifyRequest, client: MegalodonInterface, BASE_URL: string, private mastoConverter: MastoConverters) {
this.request = request;
this.client = client;
this.BASE_URL = BASE_URL;
export interface ApiSearchMastodonRoute {
Querystring: TimelineArgs & {
type?: 'accounts' | 'hashtags' | 'statuses';
q?: string;
}
}
export class ApiSearchMastodon {
constructor(
private readonly request: FastifyRequest<ApiSearchMastodonRoute>,
private readonly client: MegalodonInterface,
private readonly BASE_URL: string,
private readonly mastoConverter: MastoConverters,
) {}
public async SearchV1() {
try {
const query: any = limitToInt(this.request.query as any);
const type = query.type || '';
const data = await this.client.search(query.q, { type: type, ...query });
return data.data;
} catch (e: any) {
console.error(e);
return e.response.data;
}
if (!this.request.query.q) throw new Error('Missing required property "q"');
const query = parseTimelineArgs(this.request.query);
const data = await this.client.search(this.request.query.q, { type: this.request.query.type, ...query });
return data.data;
}
public async SearchV2() {
try {
const query: any = limitToInt(this.request.query as any);
const type = query.type;
const acct = !type || type === 'accounts' ? await this.client.search(query.q, { type: 'accounts', ...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 data = {
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;
} catch (e: any) {
console.error(e);
return e.response.data;
}
if (!this.request.query.q) throw new Error('Missing required property "q"');
const query = parseTimelineArgs(this.request.query);
const type = this.request.query.type;
const acct = !type || type === 'accounts' ? await this.client.search(this.request.query.q, { type: 'accounts', ...query }) : null;
const stat = !type || type === 'statuses' ? await this.client.search(this.request.query.q, { type: 'statuses', ...query }) : null;
const tags = !type || type === 'hashtags' ? await this.client.search(this.request.query.q, { type: 'hashtags', ...query }) : null;
return {
accounts: await Promise.all(acct?.data.accounts.map(async (account: Account) => await this.mastoConverter.convertAccount(account)) ?? []),
statuses: await Promise.all(stat?.data.statuses.map(async (status: Status) => await this.mastoConverter.convertStatus(status)) ?? []),
hashtags: tags?.data.hashtags ?? [],
};
}
public async getStatusTrends() {
try {
const data = 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 => data.map((status: any) => this.mastoConverter.convertStatus(status)));
return data;
} catch (e: any) {
console.error(e);
return [];
}
return 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 => data.map((status: Status) => this.mastoConverter.convertStatus(status)));
}
public async getSuggestions() {
try {
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: parseInt((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 [];
}
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: parseTimelineArgs(this.request.query).limit ?? 20,
origin: 'local',
sort: '+follower',
state: 'alive',
}),
})
.then((res) => res.json())
.then((data: Account[]) => data.map((entry => ({
source: 'global',
account: entry,
}))));
return Promise.all(data.map(async suggestion => {
suggestion.account = await this.mastoConverter.convertAccount(suggestion.account);
return suggestion;
}));
}
}