improve mastodon note conversion and use access checks in more places (resolves #509)

This commit is contained in:
Hazelnoot 2025-01-31 12:54:18 -05:00
parent 3550ce27d5
commit 1e43162ba7
8 changed files with 222 additions and 236 deletions

View file

@ -4,10 +4,12 @@
*/
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 { ApiError } from '../error.js';
/**
* Utility service for accessing data with Mastodon semantics
@ -22,6 +24,28 @@ export class MastodonDataService {
private readonly queryService: QueryService,
) {}
/**
* 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);
if (!note) {
throw new ApiError({
message: 'No such note.',
code: 'NO_SUCH_NOTE',
id: '24fcbfc6-2e37-42b6-8388-c29b3861a08d',
kind: 'client',
httpStatusCode: 404,
});
}
return note;
}
/**
* Fetches a note in the context of the current user.
*/
public async getNote(noteId: string, me?: MiLocalUser | null): Promise<MiNote | null> {
// Root query: note + required dependencies
const query = this.notesRepository
@ -37,4 +61,24 @@ export class MastodonDataService {
return await query.getOne();
}
/**
* Checks where the current user has made a reblog / boost / pure renote of a given target note.
*/
public async hasReblog(noteId: string, me: MiLocalUser | null | undefined): Promise<boolean> {
if (!me) return false;
return await this.notesRepository.existsBy({
// Reblog of the target note by me
userId: me.id,
renoteId: noteId,
// That is pure (not a quote)
text: IsNull(),
cw: IsNull(),
replyId: IsNull(),
hasPoll: false,
fileIds: '{}',
});
}
}