improve performance of /v1/accounts/relationships

This commit is contained in:
Hazelnoot 2025-03-21 22:51:33 -04:00
parent f5be341acc
commit de26ffd60b
3 changed files with 14 additions and 12 deletions

View file

@ -154,14 +154,11 @@ export class ApiAccountMastodon {
reply.send(response); reply.send(response);
}); });
fastify.get<ApiAccountMastodonRoute & { Querystring: { id?: string | string[], 'id[]'?: string | string[] }}>('/v1/accounts/relationships', async (_request, reply) => { fastify.get<ApiAccountMastodonRoute & { Querystring: { id?: string | string[] }}>('/v1/accounts/relationships', async (_request, reply) => {
let ids = _request.query['id[]'] ?? _request.query['id'] ?? []; if (!_request.query.id) return reply.code(400).send({ error: 'BAD_REQUEST', error_description: 'Missing required property "id"' });
if (typeof ids === 'string') {
ids = [ids];
}
const client = this.clientService.getClient(_request); const client = this.clientService.getClient(_request);
const data = await client.getRelationships(ids); const data = await client.getRelationships(_request.query.id);
const response = data.data.map(relationship => convertRelationship(relationship)); const response = data.data.map(relationship => convertRelationship(relationship));
reply.send(response); reply.send(response);

View file

@ -342,7 +342,7 @@ export interface MegalodonInterface {
* @param ids Array of account IDs. * @param ids Array of account IDs.
* @return Array of Relationship. * @return Array of Relationship.
*/ */
getRelationships(ids: Array<string>): Promise<Response<Array<Entity.Relationship>>> getRelationships(ids: string | Array<string>): Promise<Response<Array<Entity.Relationship>>>
/** /**
* Search for matching accounts by username or display name. * Search for matching accounts by username or display name.
* *

View file

@ -606,11 +606,16 @@ export default class Misskey implements MegalodonInterface {
* *
* @param ids Array of account ID, for example `['1sdfag', 'ds12aa']`. * @param ids Array of account ID, for example `['1sdfag', 'ds12aa']`.
*/ */
public async getRelationships(ids: Array<string>): Promise<Response<Array<Entity.Relationship>>> { public async getRelationships(ids: string | Array<string>): Promise<Response<Array<Entity.Relationship>>> {
return Promise.all(ids.map(id => this.getRelationship(id))).then(results => ({ return this.client
...results[0], .post<MisskeyAPI.Entity.Relation[]>('/api/users/relation', {
data: results.map(r => r.data) userId: ids
})) })
.then(res => {
return Object.assign(res, {
data: res.data.map(r => MisskeyAPI.Converter.relation(r))
})
})
} }
/** /**