2023-07-27 07:31:52 +02:00
|
|
|
/*
|
2024-02-13 16:59:27 +01:00
|
|
|
* SPDX-FileCopyrightText: syuilo and misskey-project
|
2023-07-27 07:31:52 +02:00
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2024-09-30 05:24:22 +02:00
|
|
|
import { Brackets, In, Not } from 'typeorm';
|
2022-09-17 20:27:08 +02:00
|
|
|
import { Injectable, Inject } from '@nestjs/common';
|
2023-09-20 04:33:36 +02:00
|
|
|
import type { MiUser, MiLocalUser, MiRemoteUser } from '@/models/User.js';
|
|
|
|
import type { MiNote, IMentionedRemoteUsers } from '@/models/Note.js';
|
2024-09-30 05:24:22 +02:00
|
|
|
import { LatestNote } from '@/models/LatestNote.js';
|
|
|
|
import type { InstancesRepository, LatestNotesRepository, NotesRepository, UsersRepository } from '@/models/_.js';
|
2022-09-17 20:27:08 +02:00
|
|
|
import { RelayService } from '@/core/RelayService.js';
|
|
|
|
import { FederatedInstanceService } from '@/core/FederatedInstanceService.js';
|
|
|
|
import { DI } from '@/di-symbols.js';
|
2022-09-20 22:33:11 +02:00
|
|
|
import type { Config } from '@/config.js';
|
2022-09-17 20:27:08 +02:00
|
|
|
import NotesChart from '@/core/chart/charts/notes.js';
|
|
|
|
import PerUserNotesChart from '@/core/chart/charts/per-user-notes.js';
|
|
|
|
import InstanceChart from '@/core/chart/charts/instance.js';
|
|
|
|
import { GlobalEventService } from '@/core/GlobalEventService.js';
|
2022-12-04 02:16:03 +01:00
|
|
|
import { ApRendererService } from '@/core/activitypub/ApRendererService.js';
|
|
|
|
import { ApDeliverManagerService } from '@/core/activitypub/ApDeliverManagerService.js';
|
|
|
|
import { UserEntityService } from '@/core/entities/UserEntityService.js';
|
|
|
|
import { NoteEntityService } from '@/core/entities/NoteEntityService.js';
|
2022-12-04 07:03:09 +01:00
|
|
|
import { bindThis } from '@/decorators.js';
|
2023-03-24 07:43:36 +01:00
|
|
|
import { MetaService } from '@/core/MetaService.js';
|
2023-07-08 14:31:38 +02:00
|
|
|
import { SearchService } from '@/core/SearchService.js';
|
2023-09-23 11:28:16 +02:00
|
|
|
import { ModerationLogService } from '@/core/ModerationLogService.js';
|
2024-04-14 03:23:48 +02:00
|
|
|
import { isQuote, isRenote } from '@/misc/is-renote.js';
|
2022-09-17 20:27:08 +02:00
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class NoteDeleteService {
|
|
|
|
constructor(
|
|
|
|
@Inject(DI.config)
|
|
|
|
private config: Config,
|
|
|
|
|
|
|
|
@Inject(DI.usersRepository)
|
|
|
|
private usersRepository: UsersRepository,
|
|
|
|
|
|
|
|
@Inject(DI.notesRepository)
|
|
|
|
private notesRepository: NotesRepository,
|
|
|
|
|
2024-09-30 05:24:22 +02:00
|
|
|
@Inject(DI.latestNotesRepository)
|
|
|
|
private latestNotesRepository: LatestNotesRepository,
|
|
|
|
|
2022-09-17 20:27:08 +02:00
|
|
|
@Inject(DI.instancesRepository)
|
|
|
|
private instancesRepository: InstancesRepository,
|
|
|
|
|
|
|
|
private userEntityService: UserEntityService,
|
2022-09-20 19:19:49 +02:00
|
|
|
private noteEntityService: NoteEntityService,
|
2023-02-04 02:02:03 +01:00
|
|
|
private globalEventService: GlobalEventService,
|
2022-09-17 20:27:08 +02:00
|
|
|
private relayService: RelayService,
|
|
|
|
private federatedInstanceService: FederatedInstanceService,
|
|
|
|
private apRendererService: ApRendererService,
|
|
|
|
private apDeliverManagerService: ApDeliverManagerService,
|
2023-03-24 07:43:36 +01:00
|
|
|
private metaService: MetaService,
|
2023-07-08 14:31:38 +02:00
|
|
|
private searchService: SearchService,
|
2023-09-23 11:28:16 +02:00
|
|
|
private moderationLogService: ModerationLogService,
|
2022-09-17 20:27:08 +02:00
|
|
|
private notesChart: NotesChart,
|
|
|
|
private perUserNotesChart: PerUserNotesChart,
|
|
|
|
private instanceChart: InstanceChart,
|
|
|
|
) {}
|
2023-07-08 00:08:16 +02:00
|
|
|
|
2022-09-17 20:27:08 +02:00
|
|
|
/**
|
2022-09-20 19:19:49 +02:00
|
|
|
* 投稿を削除します。
|
|
|
|
* @param user 投稿者
|
|
|
|
* @param note 投稿
|
|
|
|
*/
|
2023-09-23 11:28:16 +02:00
|
|
|
async delete(user: { id: MiUser['id']; uri: MiUser['uri']; host: MiUser['host']; isBot: MiUser['isBot']; }, note: MiNote, quiet = false, deleter?: MiUser) {
|
2022-09-17 20:27:08 +02:00
|
|
|
const deletedAt = new Date();
|
2023-07-08 14:31:38 +02:00
|
|
|
const cascadingNotes = await this.findCascadingNotes(note);
|
2022-09-17 20:27:08 +02:00
|
|
|
|
|
|
|
if (note.replyId) {
|
|
|
|
await this.notesRepository.decrement({ id: note.replyId }, 'repliesCount', 1);
|
|
|
|
}
|
|
|
|
|
2023-10-15 19:43:57 +02:00
|
|
|
if (note.renoteId && note.text == null && !note.hasPoll && (note.fileIds == null || note.fileIds.length === 0)) {
|
|
|
|
await this.notesRepository.findOneBy({ id: note.renoteId }).then(async (renote) => {
|
|
|
|
if (!renote) return;
|
|
|
|
if (renote.userId !== user.id) await this.notesRepository.decrement({ id: renote.id }, 'renoteCount', 1);
|
|
|
|
});
|
2023-10-15 02:09:38 +02:00
|
|
|
}
|
|
|
|
|
2022-09-17 20:27:08 +02:00
|
|
|
if (!quiet) {
|
2023-02-04 02:02:03 +01:00
|
|
|
this.globalEventService.publishNoteStream(note.id, 'deleted', {
|
2022-09-17 20:27:08 +02:00
|
|
|
deletedAt: deletedAt,
|
|
|
|
});
|
|
|
|
|
|
|
|
//#region ローカルの投稿なら削除アクティビティを配送
|
|
|
|
if (this.userEntityService.isLocalUser(user) && !note.localOnly) {
|
2023-08-16 10:51:28 +02:00
|
|
|
let renote: MiNote | null = null;
|
2022-09-17 20:27:08 +02:00
|
|
|
|
2023-10-30 05:48:22 +01:00
|
|
|
// if deleted note is renote
|
2024-04-14 03:23:48 +02:00
|
|
|
if (isRenote(note) && !isQuote(note)) {
|
2022-09-17 20:27:08 +02:00
|
|
|
renote = await this.notesRepository.findOneBy({
|
|
|
|
id: note.renoteId,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-02-12 10:47:30 +01:00
|
|
|
const content = this.apRendererService.addContext(renote
|
2022-09-17 20:27:08 +02:00
|
|
|
? this.apRendererService.renderUndo(this.apRendererService.renderAnnounce(renote.uri ?? `${this.config.url}/notes/${renote.id}`, note), user)
|
|
|
|
: this.apRendererService.renderDelete(this.apRendererService.renderTombstone(`${this.config.url}/notes/${note.id}`), user));
|
|
|
|
|
2022-09-18 20:11:50 +02:00
|
|
|
this.deliverToConcerned(user, note, content);
|
2022-09-17 20:27:08 +02:00
|
|
|
}
|
|
|
|
|
2024-08-12 07:03:16 +02:00
|
|
|
// also deliver delete activity to cascaded notes
|
2023-07-08 14:31:38 +02:00
|
|
|
const federatedLocalCascadingNotes = (cascadingNotes).filter(note => !note.localOnly && note.userHost == null); // filter out local-only notes
|
|
|
|
for (const cascadingNote of federatedLocalCascadingNotes) {
|
2022-09-17 20:27:08 +02:00
|
|
|
if (!cascadingNote.user) continue;
|
|
|
|
if (!this.userEntityService.isLocalUser(cascadingNote.user)) continue;
|
2023-02-12 10:47:30 +01:00
|
|
|
const content = this.apRendererService.addContext(this.apRendererService.renderDelete(this.apRendererService.renderTombstone(`${this.config.url}/notes/${cascadingNote.id}`), cascadingNote.user));
|
2022-09-18 20:11:50 +02:00
|
|
|
this.deliverToConcerned(cascadingNote.user, cascadingNote, content);
|
2022-09-17 20:27:08 +02:00
|
|
|
}
|
|
|
|
//#endregion
|
|
|
|
|
2023-03-24 07:43:36 +01:00
|
|
|
const meta = await this.metaService.fetch();
|
|
|
|
|
2022-09-17 20:27:08 +02:00
|
|
|
this.notesChart.update(note, false);
|
2023-03-24 07:43:36 +01:00
|
|
|
if (meta.enableChartsForRemoteUser || (user.host == null)) {
|
|
|
|
this.perUserNotesChart.update(user, note, false);
|
|
|
|
}
|
2022-09-17 20:27:08 +02:00
|
|
|
|
2023-10-21 12:48:39 +02:00
|
|
|
if (note.renoteId && note.text) {
|
2023-10-21 12:40:08 +02:00
|
|
|
// Decrement notes count (user)
|
|
|
|
this.decNotesCountOfUser(user);
|
2023-10-21 12:48:39 +02:00
|
|
|
} else if (!note.renoteId) {
|
2023-10-21 12:40:08 +02:00
|
|
|
// Decrement notes count (user)
|
|
|
|
this.decNotesCountOfUser(user);
|
|
|
|
}
|
|
|
|
|
2022-09-17 20:27:08 +02:00
|
|
|
if (this.userEntityService.isRemoteUser(user)) {
|
2023-03-24 11:08:08 +01:00
|
|
|
this.federatedInstanceService.fetch(user.host).then(async i => {
|
2023-10-21 12:48:39 +02:00
|
|
|
if (note.renoteId && note.text) {
|
2023-10-21 12:40:08 +02:00
|
|
|
this.instancesRepository.decrement({ id: i.id }, 'notesCount', 1);
|
2023-10-21 12:48:39 +02:00
|
|
|
} else if (!note.renoteId) {
|
2023-10-21 12:40:08 +02:00
|
|
|
this.instancesRepository.decrement({ id: i.id }, 'notesCount', 1);
|
|
|
|
}
|
2023-03-24 11:08:08 +01:00
|
|
|
if ((await this.metaService.fetch()).enableChartsForFederatedInstances) {
|
|
|
|
this.instanceChart.updateNote(i.host, note, false);
|
|
|
|
}
|
2022-09-17 20:27:08 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-08 14:31:38 +02:00
|
|
|
for (const cascadingNote of cascadingNotes) {
|
|
|
|
this.searchService.unindexNote(cascadingNote);
|
|
|
|
}
|
|
|
|
this.searchService.unindexNote(note);
|
|
|
|
|
2022-09-17 20:27:08 +02:00
|
|
|
await this.notesRepository.delete({
|
|
|
|
id: note.id,
|
|
|
|
userId: user.id,
|
|
|
|
});
|
2023-09-23 11:28:16 +02:00
|
|
|
|
2024-09-30 05:24:22 +02:00
|
|
|
await this.updateLatestNote(note);
|
|
|
|
|
2023-09-23 11:28:16 +02:00
|
|
|
if (deleter && (note.userId !== deleter.id)) {
|
2023-09-25 03:29:12 +02:00
|
|
|
const user = await this.usersRepository.findOneByOrFail({ id: note.userId });
|
2023-09-23 11:28:16 +02:00
|
|
|
this.moderationLogService.log(deleter, 'deleteNote', {
|
|
|
|
noteId: note.id,
|
|
|
|
noteUserId: note.userId,
|
2023-09-25 03:29:12 +02:00
|
|
|
noteUserUsername: user.username,
|
|
|
|
noteUserHost: user.host,
|
2023-09-23 11:28:16 +02:00
|
|
|
note: note,
|
|
|
|
});
|
|
|
|
}
|
2022-09-17 20:27:08 +02:00
|
|
|
}
|
|
|
|
|
2023-10-21 12:40:08 +02:00
|
|
|
@bindThis
|
|
|
|
private decNotesCountOfUser(user: { id: MiUser['id']; }) {
|
|
|
|
this.usersRepository.createQueryBuilder().update()
|
|
|
|
.set({
|
|
|
|
updatedAt: new Date(),
|
|
|
|
notesCount: () => '"notesCount" - 1',
|
|
|
|
})
|
|
|
|
.where('id = :id', { id: user.id })
|
|
|
|
.execute();
|
|
|
|
}
|
|
|
|
|
2022-12-04 07:03:09 +01:00
|
|
|
@bindThis
|
2023-08-16 10:51:28 +02:00
|
|
|
private async findCascadingNotes(note: MiNote): Promise<MiNote[]> {
|
|
|
|
const recursive = async (noteId: string): Promise<MiNote[]> => {
|
2022-09-17 20:27:08 +02:00
|
|
|
const query = this.notesRepository.createQueryBuilder('note')
|
|
|
|
.where('note.replyId = :noteId', { noteId })
|
|
|
|
.orWhere(new Brackets(q => {
|
|
|
|
q.where('note.renoteId = :noteId', { noteId })
|
|
|
|
.andWhere('note.text IS NOT NULL');
|
|
|
|
}))
|
|
|
|
.leftJoinAndSelect('note.user', 'user');
|
|
|
|
const replies = await query.getMany();
|
2023-07-06 04:25:46 +02:00
|
|
|
|
|
|
|
return [
|
|
|
|
replies,
|
|
|
|
...await Promise.all(replies.map(reply => recursive(reply.id))),
|
|
|
|
].flat();
|
2022-09-17 20:27:08 +02:00
|
|
|
};
|
2023-07-06 04:25:46 +02:00
|
|
|
|
2023-08-16 10:51:28 +02:00
|
|
|
const cascadingNotes: MiNote[] = await recursive(note.id);
|
2022-09-17 20:27:08 +02:00
|
|
|
|
2023-07-08 14:31:38 +02:00
|
|
|
return cascadingNotes;
|
2022-09-17 20:27:08 +02:00
|
|
|
}
|
|
|
|
|
2022-12-04 07:03:09 +01:00
|
|
|
@bindThis
|
2023-08-16 10:51:28 +02:00
|
|
|
private async getMentionedRemoteUsers(note: MiNote) {
|
2022-09-17 20:27:08 +02:00
|
|
|
const where = [] as any[];
|
|
|
|
|
|
|
|
// mention / reply / dm
|
|
|
|
const uris = (JSON.parse(note.mentionedRemoteUsers) as IMentionedRemoteUsers).map(x => x.uri);
|
|
|
|
if (uris.length > 0) {
|
|
|
|
where.push(
|
|
|
|
{ uri: In(uris) },
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
// renote / quote
|
|
|
|
if (note.renoteUserId) {
|
|
|
|
where.push({
|
|
|
|
id: note.renoteUserId,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (where.length === 0) return [];
|
|
|
|
|
|
|
|
return await this.usersRepository.find({
|
|
|
|
where,
|
2023-08-16 10:51:28 +02:00
|
|
|
}) as MiRemoteUser[];
|
2022-09-17 20:27:08 +02:00
|
|
|
}
|
|
|
|
|
2022-12-04 07:03:09 +01:00
|
|
|
@bindThis
|
2023-08-16 10:51:28 +02:00
|
|
|
private async deliverToConcerned(user: { id: MiLocalUser['id']; host: null; }, note: MiNote, content: any) {
|
2022-09-17 20:27:08 +02:00
|
|
|
this.apDeliverManagerService.deliverToFollowers(user, content);
|
|
|
|
this.relayService.deliverToRelays(user, content);
|
2022-09-18 20:11:50 +02:00
|
|
|
const remoteUsers = await this.getMentionedRemoteUsers(note);
|
2022-09-17 20:27:08 +02:00
|
|
|
for (const remoteUser of remoteUsers) {
|
|
|
|
this.apDeliverManagerService.deliverToUser(user, content, remoteUser);
|
|
|
|
}
|
|
|
|
}
|
2024-09-30 05:24:22 +02:00
|
|
|
|
|
|
|
private async updateLatestNote(note: MiNote) {
|
|
|
|
// If it's a DM, then it can't possibly be the latest note so we can safely skip this.
|
|
|
|
if (note.visibility === 'specified') return;
|
|
|
|
|
|
|
|
// Find the newest remaining note for the user
|
|
|
|
const nextLatest = await this.notesRepository
|
|
|
|
.createQueryBuilder()
|
|
|
|
.select()
|
|
|
|
.where({
|
|
|
|
userId: note.userId,
|
|
|
|
visibility: Not('specified'),
|
|
|
|
})
|
|
|
|
.orderBy({ id: 'DESC' })
|
|
|
|
.getOne();
|
|
|
|
if (!nextLatest) return;
|
|
|
|
|
|
|
|
// Record it as the latest
|
|
|
|
const latestNote = new LatestNote({
|
|
|
|
userId: note.userId,
|
|
|
|
noteId: nextLatest.id,
|
|
|
|
});
|
|
|
|
|
|
|
|
// We use an upsert because this deleted note might not have been the newest.
|
|
|
|
// In that case, the latest note may already be populated for this user.
|
|
|
|
// We want postgres to do nothing instead of replacing the value or returning an error.
|
|
|
|
await this.latestNotesRepository.upsert(latestNote, {
|
|
|
|
conflictPaths: ['userId'],
|
|
|
|
skipUpdateIfNoValuesChanged: true,
|
|
|
|
});
|
|
|
|
}
|
2022-09-17 20:27:08 +02:00
|
|
|
}
|