summaryrefslogtreecommitdiff
path: root/packages/backend/src/server
diff options
context:
space:
mode:
authorHazelnoot <acomputerdog@gmail.com>2025-06-01 00:25:39 -0400
committerHazelnoot <acomputerdog@gmail.com>2025-06-01 00:25:39 -0400
commitefd378f963fbf7a5bbe724d528b62ee51cd28e5f (patch)
treefbca2f1a9e89aa24b1b17ea1c970774a5f8b6ed5 /packages/backend/src/server
parentmerge: Add configuration option for the "query is slow" warning (!1061) (diff)
downloadsharkey-efd378f963fbf7a5bbe724d528b62ee51cd28e5f.tar.gz
sharkey-efd378f963fbf7a5bbe724d528b62ee51cd28e5f.tar.bz2
sharkey-efd378f963fbf7a5bbe724d528b62ee51cd28e5f.zip
return typed relations from MastodonDataService note fetchers
Diffstat (limited to 'packages/backend/src/server')
-rw-r--r--packages/backend/src/server/api/mastodon/MastodonDataService.ts81
1 files changed, 73 insertions, 8 deletions
diff --git a/packages/backend/src/server/api/mastodon/MastodonDataService.ts b/packages/backend/src/server/api/mastodon/MastodonDataService.ts
index db257756de..73cd553b9a 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('note.reply.reply', 'replyReply');
+ if (relations.reply.renote) query.leftJoinAndSelect('note.reply.renote', 'replyRenote');
+ if (relations.reply.user) query.innerJoinAndSelect('note.reply.user', 'replyUser');
+ if (relations.reply.channel) query.leftJoinAndSelect('note.reply.channel', 'replyChannel');
+ }
+ }
+ if (relations.renote) {
+ query.leftJoinAndSelect('note.renote', 'renote');
+ if (typeof(relations.renote) === 'object') {
+ if (relations.renote.reply) query.leftJoinAndSelect('note.renote.reply', 'renoteReply');
+ if (relations.renote.renote) query.leftJoinAndSelect('note.renote.renote', 'renoteRenote');
+ if (relations.renote.user) query.innerJoinAndSelect('note.renote.user', 'renoteUser');
+ if (relations.renote.channel) query.leftJoinAndSelect('note.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;
+};