implement /api/v1/favourites

This commit is contained in:
Hazelnoot 2025-03-21 23:26:12 -04:00
parent aaf49eadee
commit 3d8930f070
4 changed files with 59 additions and 27 deletions

View file

@ -19,6 +19,7 @@ import { ApiStatusMastodon } from '@/server/api/mastodon/endpoints/status.js';
import { ApiNotificationsMastodon } from '@/server/api/mastodon/endpoints/notifications.js'; import { ApiNotificationsMastodon } from '@/server/api/mastodon/endpoints/notifications.js';
import { ApiTimelineMastodon } from '@/server/api/mastodon/endpoints/timeline.js'; import { ApiTimelineMastodon } from '@/server/api/mastodon/endpoints/timeline.js';
import { ApiSearchMastodon } from '@/server/api/mastodon/endpoints/search.js'; import { ApiSearchMastodon } from '@/server/api/mastodon/endpoints/search.js';
import { ApiError } from '@/server/api/error.js';
import { parseTimelineArgs, TimelineArgs, toBoolean } from './argsUtils.js'; import { parseTimelineArgs, TimelineArgs, toBoolean } from './argsUtils.js';
import { convertAnnouncement, convertAttachment, MastoConverters, convertRelationship } from './converters.js'; import { convertAnnouncement, convertAttachment, MastoConverters, convertRelationship } from './converters.js';
import type { Entity } from 'megalodon'; import type { Entity } from 'megalodon';
@ -231,8 +232,21 @@ export class MastodonApiServerService {
fastify.get<{ Querystring: TimelineArgs }>('/v1/favourites', async (_request, reply) => { fastify.get<{ Querystring: TimelineArgs }>('/v1/favourites', async (_request, reply) => {
const { client, me } = await this.clientService.getAuthClient(_request); const { client, me } = await this.clientService.getAuthClient(_request);
const data = await client.getFavourites(parseTimelineArgs(_request.query)); if (!me) {
const response = Promise.all(data.data.map((status) => this.mastoConverters.convertStatus(status, me))); throw new ApiError({
message: 'Credential required.',
code: 'CREDENTIAL_REQUIRED',
id: '1384574d-a912-4b81-8601-c7b1c4085df1',
httpStatusCode: 401,
});
}
const args = {
...parseTimelineArgs(_request.query),
userId: me.id,
};
const data = await client.getFavourites(args);
const response = await Promise.all(data.data.map((status) => this.mastoConverters.convertStatus(status, me)));
reply.send(response); reply.send(response);
}); });

View file

@ -691,14 +691,13 @@ export default class Misskey implements MegalodonInterface {
}) })
} }
// ======================================
// accounts/favourites
// ======================================
/** /**
* POST /api/i/favorites * POST /api/users/reactions
*/ */
public async getFavourites(options?: { limit?: number; max_id?: string; min_id?: string }): Promise<Response<Array<Entity.Status>>> { public async getReactions(userId: string, options?: { limit?: number; max_id?: string; min_id?: string }): Promise<Response<MisskeyEntity.NoteReaction[]>> {
let params = {} let params = {
userId,
};
if (options) { if (options) {
if (options.limit) { if (options.limit) {
params = Object.assign(params, { params = Object.assign(params, {
@ -716,11 +715,24 @@ export default class Misskey implements MegalodonInterface {
}) })
} }
} }
return this.client.post<Array<MisskeyAPI.Entity.Favorite>>('/api/i/favorites', params).then(res => { return this.client.post<MisskeyAPI.Entity.NoteReaction[]>('/api/users/reactions', params);
return Object.assign(res, { }
data: res.data.map(fav => MisskeyAPI.Converter.note(fav.note, this.baseUrl))
}) // ======================================
}) // accounts/favourites
// ======================================
/**
* POST /api/users/reactions
*/
public async getFavourites(options?: { limit?: number; max_id?: string; min_id?: string; userId?: string }): Promise<Response<Array<Entity.Status>>> {
const userId = options?.userId ?? (await this.verifyAccountCredentials()).data.id;
const response = await this.getReactions(userId, options);
return {
...response,
data: response.data.map(r => MisskeyAPI.Converter.note(r.note, this.baseUrl)),
};
} }
// ====================================== // ======================================

View file

@ -32,6 +32,7 @@ namespace MisskeyAPI {
export type Notification = MisskeyEntity.Notification export type Notification = MisskeyEntity.Notification
export type Poll = MisskeyEntity.Poll export type Poll = MisskeyEntity.Poll
export type Reaction = MisskeyEntity.Reaction export type Reaction = MisskeyEntity.Reaction
export type NoteReaction = MisskeyEntity.NoteReaction
export type Relation = MisskeyEntity.Relation export type Relation = MisskeyEntity.Relation
export type User = MisskeyEntity.User export type User = MisskeyEntity.User
export type UserDetail = MisskeyEntity.UserDetail export type UserDetail = MisskeyEntity.UserDetail

View file

@ -1,4 +1,5 @@
/// <reference path="user.ts" /> /// <reference path="user.ts" />
/// <reference path="note.ts" />
namespace MisskeyEntity { namespace MisskeyEntity {
export type Reaction = { export type Reaction = {
@ -7,4 +8,8 @@ namespace MisskeyEntity {
user: User user: User
type: string type: string
} }
export type NoteReaction = Reaction & {
note: Note
}
} }