summaryrefslogtreecommitdiff
path: root/packages/backend/src/server/api/mastodon
diff options
context:
space:
mode:
Diffstat (limited to 'packages/backend/src/server/api/mastodon')
-rw-r--r--packages/backend/src/server/api/mastodon/MastodonApiServerService.ts7
-rw-r--r--packages/backend/src/server/api/mastodon/MastodonConverters.ts8
-rw-r--r--packages/backend/src/server/api/mastodon/MastodonDataService.ts81
-rw-r--r--packages/backend/src/server/api/mastodon/endpoints/status.ts24
4 files changed, 104 insertions, 16 deletions
diff --git a/packages/backend/src/server/api/mastodon/MastodonApiServerService.ts b/packages/backend/src/server/api/mastodon/MastodonApiServerService.ts
index 74fd9d7d59..072dacf708 100644
--- a/packages/backend/src/server/api/mastodon/MastodonApiServerService.ts
+++ b/packages/backend/src/server/api/mastodon/MastodonApiServerService.ts
@@ -71,6 +71,13 @@ export class MastodonApiServerService {
done();
});
+ // Tell crawlers not to index API endpoints.
+ // https://developers.google.com/search/docs/crawling-indexing/block-indexing
+ fastify.addHook('onRequest', (request, reply, done) => {
+ reply.header('X-Robots-Tag', 'noindex');
+ done();
+ });
+
// External endpoints
this.apiAccountMastodon.register(fastify);
this.apiAppsMastodon.register(fastify);
diff --git a/packages/backend/src/server/api/mastodon/MastodonConverters.ts b/packages/backend/src/server/api/mastodon/MastodonConverters.ts
index 375ea1ef08..df8d68042a 100644
--- a/packages/backend/src/server/api/mastodon/MastodonConverters.ts
+++ b/packages/backend/src/server/api/mastodon/MastodonConverters.ts
@@ -5,7 +5,7 @@
import { Inject, Injectable } from '@nestjs/common';
import { Entity, MastodonEntity, MisskeyEntity } from 'megalodon';
-import mfm from '@transfem-org/sfm-js';
+import mfm from 'mfm-js';
import { MastodonNotificationType } from 'megalodon/lib/src/mastodon/notification.js';
import { NotificationType } from 'megalodon/lib/src/notification.js';
import { DI } from '@/di-symbols.js';
@@ -252,10 +252,10 @@ export class MastodonConverters {
return await this.convertStatus(status, me);
}
- public async convertStatus(status: Entity.Status, me: MiLocalUser | null): Promise<MastodonEntity.Status> {
+ public async convertStatus(status: Entity.Status, me: MiLocalUser | null, hints?: { note?: MiNote, user?: MiUser }): Promise<MastodonEntity.Status> {
const convertedAccount = this.convertAccount(status.account);
- const note = await this.mastodonDataService.requireNote(status.id, me);
- const noteUser = await this.getUser(status.account.id);
+ const note = hints?.note ?? await this.mastodonDataService.requireNote(status.id, me);
+ const noteUser = hints?.user ?? note.user ?? await this.getUser(status.account.id);
const mentionedRemoteUsers = JSON.parse(note.mentionedRemoteUsers);
const emojis = await this.customEmojiService.populateEmojis(note.emojis, noteUser.host ? noteUser.host : this.config.host);
diff --git a/packages/backend/src/server/api/mastodon/MastodonDataService.ts b/packages/backend/src/server/api/mastodon/MastodonDataService.ts
index db257756de..e080cb10bd 100644
--- a/packages/backend/src/server/api/mastodon/MastodonDataService.ts
+++ b/packages/backend/src/server/api/mastodon/MastodonDataService.ts
@@ -7,8 +7,8 @@ import { Inject, Injectable } from '@nestjs/common';
import { IsNull } from 'typeorm';
import { DI } from '@/di-symbols.js';
import { QueryService } from '@/core/QueryService.js';
-import type { MiNote, NotesRepository } from '@/models/_.js';
-import type { MiLocalUser } from '@/models/User.js';
+import type { MiChannel, MiNote, NotesRepository } from '@/models/_.js';
+import type { MiLocalUser, MiUser } from '@/models/User.js';
import { ApiError } from '../error.js';
/**
@@ -27,8 +27,8 @@ export class MastodonDataService {
/**
* Fetches a note in the context of the current user, and throws an exception if not found.
*/
- public async requireNote(noteId: string, me?: MiLocalUser | null): Promise<MiNote> {
- const note = await this.getNote(noteId, me);
+ public async requireNote<Rel extends NoteRelations = NoteRelations>(noteId: string, me: MiLocalUser | null | undefined, relations?: Rel): Promise<NoteWithRelations<Rel>> {
+ const note = await this.getNote(noteId, me, relations);
if (!note) {
throw new ApiError({
@@ -46,12 +46,39 @@ export class MastodonDataService {
/**
* Fetches a note in the context of the current user.
*/
- public async getNote(noteId: string, me?: MiLocalUser | null): Promise<MiNote | null> {
+ public async getNote<Rel extends NoteRelations = NoteRelations>(noteId: string, me: MiLocalUser | null | undefined, relations?: Rel): Promise<NoteWithRelations<Rel> | null> {
// Root query: note + required dependencies
const query = this.notesRepository
.createQueryBuilder('note')
- .where('note.id = :noteId', { noteId })
- .innerJoinAndSelect('note.user', 'user');
+ .where('note.id = :noteId', { noteId });
+
+ // Load relations
+ if (relations) {
+ if (relations.reply) {
+ query.leftJoinAndSelect('note.reply', 'reply');
+ if (typeof(relations.reply) === 'object') {
+ if (relations.reply.reply) query.leftJoinAndSelect('reply.reply', 'replyReply');
+ if (relations.reply.renote) query.leftJoinAndSelect('reply.renote', 'replyRenote');
+ if (relations.reply.user) query.innerJoinAndSelect('reply.user', 'replyUser');
+ if (relations.reply.channel) query.leftJoinAndSelect('reply.channel', 'replyChannel');
+ }
+ }
+ if (relations.renote) {
+ query.leftJoinAndSelect('note.renote', 'renote');
+ if (typeof(relations.renote) === 'object') {
+ if (relations.renote.reply) query.leftJoinAndSelect('renote.reply', 'renoteReply');
+ if (relations.renote.renote) query.leftJoinAndSelect('renote.renote', 'renoteRenote');
+ if (relations.renote.user) query.innerJoinAndSelect('renote.user', 'renoteUser');
+ if (relations.renote.channel) query.leftJoinAndSelect('renote.channel', 'renoteChannel');
+ }
+ }
+ if (relations.user) {
+ query.innerJoinAndSelect('note.user', 'user');
+ }
+ if (relations.channel) {
+ query.leftJoinAndSelect('note.channel', 'channel');
+ }
+ }
// Restrict visibility
this.queryService.generateVisibilityQuery(query, me);
@@ -59,7 +86,7 @@ export class MastodonDataService {
this.queryService.generateBlockedUserQueryForNotes(query, me);
}
- return await query.getOne();
+ return await query.getOne() as NoteWithRelations<Rel> | null;
}
/**
@@ -82,3 +109,41 @@ export class MastodonDataService {
});
}
}
+
+interface NoteRelations {
+ reply?: boolean | {
+ reply?: boolean;
+ renote?: boolean;
+ user?: boolean;
+ channel?: boolean;
+ };
+ renote?: boolean | {
+ reply?: boolean;
+ renote?: boolean;
+ user?: boolean;
+ channel?: boolean;
+ };
+ user?: boolean;
+ channel?: boolean;
+}
+
+type NoteWithRelations<Rel extends NoteRelations> = MiNote & {
+ reply: Rel extends { reply: false }
+ ? null
+ : null | (MiNote & {
+ reply: Rel['reply'] extends { reply: true } ? MiNote | null : null;
+ renote: Rel['reply'] extends { renote: true } ? MiNote | null : null;
+ user: Rel['reply'] extends { user: true } ? MiUser : null;
+ channel: Rel['reply'] extends { channel: true } ? MiChannel | null : null;
+ });
+ renote: Rel extends { renote: false }
+ ? null
+ : null | (MiNote & {
+ reply: Rel['renote'] extends { reply: true } ? MiNote | null : null;
+ renote: Rel['renote'] extends { renote: true } ? MiNote | null : null;
+ user: Rel['renote'] extends { user: true } ? MiUser : null;
+ channel: Rel['renote'] extends { channel: true } ? MiChannel | null : null;
+ });
+ user: Rel extends { user: true } ? MiUser : null;
+ channel: Rel extends { channel: true } ? MiChannel | null : null;
+};
diff --git a/packages/backend/src/server/api/mastodon/endpoints/status.ts b/packages/backend/src/server/api/mastodon/endpoints/status.ts
index 22b8a911ca..7a058a0ed9 100644
--- a/packages/backend/src/server/api/mastodon/endpoints/status.ts
+++ b/packages/backend/src/server/api/mastodon/endpoints/status.ts
@@ -8,6 +8,10 @@ import { Injectable } from '@nestjs/common';
import { emojiRegexAtStartToEnd } from '@/misc/emoji-regex.js';
import { parseTimelineArgs, TimelineArgs, toBoolean, toInt } from '@/server/api/mastodon/argsUtils.js';
import { MastodonClientService } from '@/server/api/mastodon/MastodonClientService.js';
+import { MastodonDataService } from '@/server/api/mastodon/MastodonDataService.js';
+import { getNoteSummary } from '@/misc/get-note-summary.js';
+import type { Packed } from '@/misc/json-schema.js';
+import { isPureRenote } from '@/misc/is-renote.js';
import { convertAttachment, convertPoll, MastodonConverters } from '../MastodonConverters.js';
import type { Entity } from 'megalodon';
import type { FastifyInstance } from 'fastify';
@@ -22,6 +26,7 @@ export class ApiStatusMastodon {
constructor(
private readonly mastoConverters: MastodonConverters,
private readonly clientService: MastodonClientService,
+ private readonly mastodonDataService: MastodonDataService,
) {}
public register(fastify: FastifyInstance): void {
@@ -29,13 +34,24 @@ export class ApiStatusMastodon {
if (!_request.params.id) return reply.code(400).send({ error: 'BAD_REQUEST', error_description: 'Missing required parameter "id"' });
const { client, me } = await this.clientService.getAuthClient(_request);
- const data = await client.getStatus(_request.params.id);
- const response = await this.mastoConverters.convertStatus(data.data, me);
+ const note = await this.mastodonDataService.requireNote(_request.params.id, me, { user: true, renote: { user: true } });
+
+ // Unpack renote for Discord, otherwise the preview breaks
+ const appearNote = (isPureRenote(note) && _request.headers['user-agent']?.match(/\bDiscordbot\//))
+ ? note.renote as NonNullable<typeof note.renote>
+ : note;
+
+ const data = await client.getStatus(appearNote.id);
+ const response = await this.mastoConverters.convertStatus(data.data, me, { note: appearNote, user: appearNote.user });
// Fixup - Discord ignores CWs and renders the entire post.
if (response.sensitive && _request.headers['user-agent']?.match(/\bDiscordbot\//)) {
- response.content = '(preview disabled for sensitive content)';
+ response.content = getNoteSummary(data.data satisfies Packed<'Note'>);
response.media_attachments = [];
+ response.in_reply_to_id = null;
+ response.in_reply_to_account_id = null;
+ response.reblog = null;
+ response.quote = null;
}
return reply.send(response);
@@ -170,7 +186,7 @@ export class ApiStatusMastodon {
const data = await client.deleteEmojiReaction(id, react);
return reply.send(data.data);
}
- if (!body.media_ids) body.media_ids = undefined;
+ body.media_ids ??= undefined;
if (body.media_ids && !body.media_ids.length) body.media_ids = undefined;
if (body.poll && !body.poll.options) {