2022-09-17 18:27:08 +00:00
|
|
|
import { Brackets, In } from 'typeorm';
|
|
|
|
import { Injectable, Inject } from '@nestjs/common';
|
2023-02-13 06:50:22 +00:00
|
|
|
import type { User, LocalUser, RemoteUser } from '@/models/entities/User.js';
|
2022-09-17 18:27:08 +00:00
|
|
|
import type { Note, IMentionedRemoteUsers } from '@/models/entities/Note.js';
|
2022-09-20 20:33:11 +00:00
|
|
|
import type { InstancesRepository, NotesRepository, UsersRepository } from '@/models/index.js';
|
2022-09-17 18:27:08 +00:00
|
|
|
import { RelayService } from '@/core/RelayService.js';
|
|
|
|
import { FederatedInstanceService } from '@/core/FederatedInstanceService.js';
|
|
|
|
import { DI } from '@/di-symbols.js';
|
2022-09-20 20:33:11 +00:00
|
|
|
import type { Config } from '@/config.js';
|
2022-09-17 18:27:08 +00: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 01:16:03 +00: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 06:03:09 +00:00
|
|
|
import { bindThis } from '@/decorators.js';
|
2023-03-24 06:43:36 +00:00
|
|
|
import { MetaService } from '@/core/MetaService.js';
|
2022-09-17 18:27:08 +00:00
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class NoteDeleteService {
|
|
|
|
constructor(
|
|
|
|
@Inject(DI.config)
|
|
|
|
private config: Config,
|
|
|
|
|
|
|
|
@Inject(DI.usersRepository)
|
|
|
|
private usersRepository: UsersRepository,
|
|
|
|
|
|
|
|
@Inject(DI.notesRepository)
|
|
|
|
private notesRepository: NotesRepository,
|
|
|
|
|
|
|
|
@Inject(DI.instancesRepository)
|
|
|
|
private instancesRepository: InstancesRepository,
|
|
|
|
|
|
|
|
private userEntityService: UserEntityService,
|
2022-09-20 17:19:49 +00:00
|
|
|
private noteEntityService: NoteEntityService,
|
2023-02-04 01:02:03 +00:00
|
|
|
private globalEventService: GlobalEventService,
|
2022-09-17 18:27:08 +00:00
|
|
|
private relayService: RelayService,
|
|
|
|
private federatedInstanceService: FederatedInstanceService,
|
|
|
|
private apRendererService: ApRendererService,
|
|
|
|
private apDeliverManagerService: ApDeliverManagerService,
|
2023-03-24 06:43:36 +00:00
|
|
|
private metaService: MetaService,
|
2022-09-17 18:27:08 +00:00
|
|
|
private notesChart: NotesChart,
|
|
|
|
private perUserNotesChart: PerUserNotesChart,
|
|
|
|
private instanceChart: InstanceChart,
|
|
|
|
) {}
|
|
|
|
|
|
|
|
/**
|
2022-09-20 17:19:49 +00:00
|
|
|
* 投稿を削除します。
|
|
|
|
* @param user 投稿者
|
|
|
|
* @param note 投稿
|
|
|
|
*/
|
2022-12-21 01:23:03 +00:00
|
|
|
async delete(user: { id: User['id']; uri: User['uri']; host: User['host']; isBot: User['isBot']; }, note: Note, quiet = false) {
|
2022-09-17 18:27:08 +00:00
|
|
|
const deletedAt = new Date();
|
|
|
|
|
|
|
|
// この投稿を除く指定したユーザーによる指定したノートのリノートが存在しないとき
|
|
|
|
if (note.renoteId && (await this.noteEntityService.countSameRenotes(user.id, note.renoteId, note.id)) === 0) {
|
|
|
|
this.notesRepository.decrement({ id: note.renoteId }, 'renoteCount', 1);
|
2022-12-21 01:23:03 +00:00
|
|
|
if (!user.isBot) this.notesRepository.decrement({ id: note.renoteId }, 'score', 1);
|
2022-09-17 18:27:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (note.replyId) {
|
|
|
|
await this.notesRepository.decrement({ id: note.replyId }, 'repliesCount', 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!quiet) {
|
2023-02-04 01:02:03 +00:00
|
|
|
this.globalEventService.publishNoteStream(note.id, 'deleted', {
|
2022-09-17 18:27:08 +00:00
|
|
|
deletedAt: deletedAt,
|
|
|
|
});
|
|
|
|
|
|
|
|
//#region ローカルの投稿なら削除アクティビティを配送
|
|
|
|
if (this.userEntityService.isLocalUser(user) && !note.localOnly) {
|
|
|
|
let renote: Note | null = null;
|
|
|
|
|
|
|
|
// if deletd note is renote
|
|
|
|
if (note.renoteId && note.text == null && !note.hasPoll && (note.fileIds == null || note.fileIds.length === 0)) {
|
|
|
|
renote = await this.notesRepository.findOneBy({
|
|
|
|
id: note.renoteId,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-02-12 09:47:30 +00:00
|
|
|
const content = this.apRendererService.addContext(renote
|
2022-09-17 18:27:08 +00: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 18:11:50 +00:00
|
|
|
this.deliverToConcerned(user, note, content);
|
2022-09-17 18:27:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// also deliever delete activity to cascaded notes
|
2022-09-18 18:11:50 +00:00
|
|
|
const cascadingNotes = (await this.findCascadingNotes(note)).filter(note => !note.localOnly); // filter out local-only notes
|
2022-09-17 18:27:08 +00:00
|
|
|
for (const cascadingNote of cascadingNotes) {
|
|
|
|
if (!cascadingNote.user) continue;
|
|
|
|
if (!this.userEntityService.isLocalUser(cascadingNote.user)) continue;
|
2023-02-12 09:47:30 +00:00
|
|
|
const content = this.apRendererService.addContext(this.apRendererService.renderDelete(this.apRendererService.renderTombstone(`${this.config.url}/notes/${cascadingNote.id}`), cascadingNote.user));
|
2022-09-18 18:11:50 +00:00
|
|
|
this.deliverToConcerned(cascadingNote.user, cascadingNote, content);
|
2022-09-17 18:27:08 +00:00
|
|
|
}
|
|
|
|
//#endregion
|
|
|
|
|
2023-03-24 06:43:36 +00:00
|
|
|
const meta = await this.metaService.fetch();
|
|
|
|
|
2022-09-17 18:27:08 +00:00
|
|
|
this.notesChart.update(note, false);
|
2023-03-24 06:43:36 +00:00
|
|
|
if (meta.enableChartsForRemoteUser || (user.host == null)) {
|
|
|
|
this.perUserNotesChart.update(user, note, false);
|
|
|
|
}
|
2022-09-17 18:27:08 +00:00
|
|
|
|
|
|
|
if (this.userEntityService.isRemoteUser(user)) {
|
2023-01-03 00:32:36 +00:00
|
|
|
this.federatedInstanceService.fetch(user.host).then(i => {
|
2022-09-17 18:27:08 +00:00
|
|
|
this.instancesRepository.decrement({ id: i.id }, 'notesCount', 1);
|
|
|
|
this.instanceChart.updateNote(i.host, note, false);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
await this.notesRepository.delete({
|
|
|
|
id: note.id,
|
|
|
|
userId: user.id,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-12-04 06:03:09 +00:00
|
|
|
@bindThis
|
2022-09-18 18:11:50 +00:00
|
|
|
private async findCascadingNotes(note: Note) {
|
2022-09-17 18:27:08 +00:00
|
|
|
const cascadingNotes: Note[] = [];
|
|
|
|
|
|
|
|
const recursive = async (noteId: string) => {
|
|
|
|
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();
|
|
|
|
for (const reply of replies) {
|
|
|
|
cascadingNotes.push(reply);
|
|
|
|
await recursive(reply.id);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
await recursive(note.id);
|
|
|
|
|
|
|
|
return cascadingNotes.filter(note => note.userHost === null); // filter out non-local users
|
|
|
|
}
|
|
|
|
|
2022-12-04 06:03:09 +00:00
|
|
|
@bindThis
|
2022-09-18 18:11:50 +00:00
|
|
|
private async getMentionedRemoteUsers(note: Note) {
|
2022-09-17 18:27:08 +00: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-02-13 06:50:22 +00:00
|
|
|
}) as RemoteUser[];
|
2022-09-17 18:27:08 +00:00
|
|
|
}
|
|
|
|
|
2022-12-04 06:03:09 +00:00
|
|
|
@bindThis
|
2023-02-13 06:50:22 +00:00
|
|
|
private async deliverToConcerned(user: { id: LocalUser['id']; host: null; }, note: Note, content: any) {
|
2022-09-17 18:27:08 +00:00
|
|
|
this.apDeliverManagerService.deliverToFollowers(user, content);
|
|
|
|
this.relayService.deliverToRelays(user, content);
|
2022-09-18 18:11:50 +00:00
|
|
|
const remoteUsers = await this.getMentionedRemoteUsers(note);
|
2022-09-17 18:27:08 +00:00
|
|
|
for (const remoteUser of remoteUsers) {
|
|
|
|
this.apDeliverManagerService.deliverToUser(user, content, remoteUser);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|