fix type errors in mastodon API

This commit is contained in:
Hazelnoot 2025-04-30 11:13:46 -04:00
parent 4ea1b6aa4d
commit 6e4e4fdc33
4 changed files with 17 additions and 17 deletions

View file

@ -4,7 +4,7 @@
*/ */
import { Inject, Injectable } from '@nestjs/common'; import { Inject, Injectable } from '@nestjs/common';
import { Entity } from 'megalodon'; import { Entity, MastodonEntity } from 'megalodon';
import mfm from '@transfem-org/sfm-js'; import mfm from '@transfem-org/sfm-js';
import { MastodonNotificationType } from 'megalodon/lib/src/mastodon/notification.js'; import { MastodonNotificationType } from 'megalodon/lib/src/mastodon/notification.js';
import { NotificationType } from 'megalodon/lib/src/notification.js'; import { NotificationType } from 'megalodon/lib/src/notification.js';
@ -275,7 +275,7 @@ export class MastodonConverters {
this.getUser(p) this.getUser(p)
.then(u => this.encode(u, mentionedRemoteUsers)) .then(u => this.encode(u, mentionedRemoteUsers))
.catch(() => null))) .catch(() => null)))
.then(p => p.filter(m => m)) as Promise<Entity.Mention[]>; .then((p: Entity.Mention[]) => p.filter(m => m));
const tags = note.tags.map(tag => { const tags = note.tags.map(tag => {
return { return {
@ -327,7 +327,7 @@ export class MastodonConverters {
sensitive: status.sensitive || !!cw, sensitive: status.sensitive || !!cw,
spoiler_text: cw, spoiler_text: cw,
visibility: status.visibility, visibility: status.visibility,
media_attachments: status.media_attachments.map(a => convertAttachment(a)), media_attachments: status.media_attachments.map((a: Entity.Account) => convertAttachment(a)),
mentions: mentions, mentions: mentions,
tags: tags, tags: tags,
card: null, //FIXME card: null, //FIXME
@ -345,7 +345,7 @@ export class MastodonConverters {
public async convertConversation(conversation: Entity.Conversation, me: MiLocalUser | null): Promise<MastodonEntity.Conversation> { public async convertConversation(conversation: Entity.Conversation, me: MiLocalUser | null): Promise<MastodonEntity.Conversation> {
return { return {
id: conversation.id, id: conversation.id,
accounts: await Promise.all(conversation.accounts.map(a => this.convertAccount(a))), accounts: await Promise.all(conversation.accounts.map((a: Entity.Account) => this.convertAccount(a))),
last_status: conversation.last_status ? await this.convertStatus(conversation.last_status, me) : null, last_status: conversation.last_status ? await this.convertStatus(conversation.last_status, me) : null,
unread: conversation.unread, unread: conversation.unread,
}; };

View file

@ -4,6 +4,7 @@
*/ */
import { Injectable } from '@nestjs/common'; import { Injectable } from '@nestjs/common';
import { MastodonEntity } from 'megalodon';
import { parseTimelineArgs, TimelineArgs } from '@/server/api/mastodon/argsUtils.js'; import { parseTimelineArgs, TimelineArgs } from '@/server/api/mastodon/argsUtils.js';
import { MastodonConverters } from '@/server/api/mastodon/MastodonConverters.js'; import { MastodonConverters } from '@/server/api/mastodon/MastodonConverters.js';
import { attachMinMaxPagination } from '@/server/api/mastodon/pagination.js'; import { attachMinMaxPagination } from '@/server/api/mastodon/pagination.js';

View file

@ -9,9 +9,8 @@ import { attachMinMaxPagination, attachOffsetPagination } from '@/server/api/mas
import { MastodonConverters } from '../MastodonConverters.js'; import { MastodonConverters } from '../MastodonConverters.js';
import { parseTimelineArgs, TimelineArgs, toBoolean, toInt } from '../argsUtils.js'; import { parseTimelineArgs, TimelineArgs, toBoolean, toInt } from '../argsUtils.js';
import { ApiError } from '../../error.js'; import { ApiError } from '../../error.js';
import Account = Entity.Account;
import Status = Entity.Status;
import type { FastifyInstance } from 'fastify'; import type { FastifyInstance } from 'fastify';
import type { Entity } from 'megalodon';
interface ApiSearchMastodonRoute { interface ApiSearchMastodonRoute {
Querystring: TimelineArgs & { Querystring: TimelineArgs & {
@ -53,8 +52,8 @@ export class ApiSearchMastodon {
const { data } = await client.search(request.query.q, { type, ...query }); const { data } = await client.search(request.query.q, { type, ...query });
const response = { const response = {
...data, ...data,
accounts: await Promise.all(data.accounts.map((account: Account) => this.mastoConverters.convertAccount(account))), accounts: await Promise.all(data.accounts.map((account: Entity.Account) => this.mastoConverters.convertAccount(account))),
statuses: await Promise.all(data.statuses.map((status: Status) => this.mastoConverters.convertStatus(status, me))), statuses: await Promise.all(data.statuses.map((status: Entity.Status) => this.mastoConverters.convertStatus(status, me))),
}; };
if (type === 'hashtags') { if (type === 'hashtags') {
@ -90,8 +89,8 @@ export class ApiSearchMastodon {
const stat = !type || type === 'statuses' ? await client.search(request.query.q, { type: 'statuses', ...query }) : null; const stat = !type || type === 'statuses' ? await client.search(request.query.q, { type: 'statuses', ...query }) : null;
const tags = !type || type === 'hashtags' ? await client.search(request.query.q, { type: 'hashtags', ...query }) : null; const tags = !type || type === 'hashtags' ? await client.search(request.query.q, { type: 'hashtags', ...query }) : null;
const response = { const response = {
accounts: await Promise.all(acct?.data.accounts.map((account: Account) => this.mastoConverters.convertAccount(account)) ?? []), accounts: await Promise.all(acct?.data.accounts.map((account: Entity.Account) => this.mastoConverters.convertAccount(account)) ?? []),
statuses: await Promise.all(stat?.data.statuses.map((status: Status) => this.mastoConverters.convertStatus(status, me)) ?? []), statuses: await Promise.all(stat?.data.statuses.map((status: Entity.Status) => this.mastoConverters.convertStatus(status, me)) ?? []),
hashtags: tags?.data.hashtags ?? [], hashtags: tags?.data.hashtags ?? [],
}; };
@ -113,7 +112,7 @@ export class ApiSearchMastodon {
{ {
method: 'POST', method: 'POST',
headers: { headers: {
...request.headers as HeadersInit, ...request.headers,
'Accept': 'application/json', 'Accept': 'application/json',
'Content-Type': 'application/json', 'Content-Type': 'application/json',
}, },
@ -122,7 +121,7 @@ export class ApiSearchMastodon {
await verifyResponse(res); await verifyResponse(res);
const data = await res.json() as Status[]; const data = await res.json() as Entity.Status[];
const me = await this.clientService.getAuth(request); const me = await this.clientService.getAuth(request);
const response = await Promise.all(data.map(status => this.mastoConverters.convertStatus(status, me))); const response = await Promise.all(data.map(status => this.mastoConverters.convertStatus(status, me)));
@ -136,7 +135,7 @@ export class ApiSearchMastodon {
{ {
method: 'POST', method: 'POST',
headers: { headers: {
...request.headers as HeadersInit, ...request.headers,
'Accept': 'application/json', 'Accept': 'application/json',
'Content-Type': 'application/json', 'Content-Type': 'application/json',
}, },
@ -150,7 +149,7 @@ export class ApiSearchMastodon {
await verifyResponse(res); await verifyResponse(res);
const data = await res.json() as Account[]; const data = await res.json() as Entity.Account[];
const response = await Promise.all(data.map(async entry => { const response = await Promise.all(data.map(async entry => {
return { return {
source: 'global', source: 'global',

View file

@ -55,8 +55,8 @@ export class ApiStatusMastodon {
const { client, me } = await this.clientService.getAuthClient(_request); const { client, me } = await this.clientService.getAuthClient(_request);
const { data } = await client.getStatusContext(_request.params.id, parseTimelineArgs(_request.query)); const { data } = await client.getStatusContext(_request.params.id, parseTimelineArgs(_request.query));
const ancestors = await Promise.all(data.ancestors.map(async status => await this.mastoConverters.convertStatus(status, me))); const ancestors = await Promise.all(data.ancestors.map(async (status: Entity.Status) => await this.mastoConverters.convertStatus(status, me)));
const descendants = await Promise.all(data.descendants.map(async status => await this.mastoConverters.convertStatus(status, me))); const descendants = await Promise.all(data.descendants.map(async (status: Entity.Status) => await this.mastoConverters.convertStatus(status, me)));
const response = { ancestors, descendants }; const response = { ancestors, descendants };
reply.send(response); reply.send(response);
@ -166,7 +166,7 @@ export class ApiStatusMastodon {
if (body.in_reply_to_id && removed === '/unreact') { if (body.in_reply_to_id && removed === '/unreact') {
const id = body.in_reply_to_id; const id = body.in_reply_to_id;
const post = await client.getStatus(id); const post = await client.getStatus(id);
const react = post.data.emoji_reactions.filter(e => e.me)[0].name; const react = post.data.emoji_reactions.filter((e: Entity.Emoji) => e.me)[0].name;
const data = await client.deleteEmojiReaction(id, react); const data = await client.deleteEmojiReaction(id, react);
reply.send(data.data); reply.send(data.data);
} }