2022-08-24 21:57:34 +00:00
|
|
|
import { Brackets, In, IsNull, Not } from 'typeorm';
|
2022-02-27 02:07:39 +00:00
|
|
|
import { publishNoteStream } from '@/services/stream.js';
|
|
|
|
import renderDelete from '@/remote/activitypub/renderer/delete.js';
|
|
|
|
import renderAnnounce from '@/remote/activitypub/renderer/announce.js';
|
|
|
|
import renderUndo from '@/remote/activitypub/renderer/undo.js';
|
|
|
|
import { renderActivity } from '@/remote/activitypub/renderer/index.js';
|
|
|
|
import renderTombstone from '@/remote/activitypub/renderer/tombstone.js';
|
|
|
|
import config from '@/config/index.js';
|
|
|
|
import { User, ILocalUser, IRemoteUser } from '@/models/entities/user.js';
|
2022-08-24 21:57:34 +00:00
|
|
|
import { Note } from '@/models/entities/note.js';
|
2022-02-27 02:07:39 +00:00
|
|
|
import { Notes, Users, Instances } from '@/models/index.js';
|
|
|
|
import { notesChart, perUserNotesChart, instanceChart } from '@/services/chart/index.js';
|
2022-10-10 22:30:32 +00:00
|
|
|
import DeliverManager from '@/remote/activitypub/deliver-manager.js';
|
2022-02-27 02:07:39 +00:00
|
|
|
import { countSameRenotes } from '@/misc/count-same-renotes.js';
|
2022-08-03 11:18:33 +00:00
|
|
|
import { isPureRenote } from '@/misc/renote.js';
|
2022-04-17 12:01:02 +00:00
|
|
|
import { registerOrFetchInstanceDoc } from '../register-or-fetch-instance-doc.js';
|
2022-02-27 02:07:39 +00:00
|
|
|
import { deliverToRelays } from '../relay.js';
|
2018-05-28 05:39:46 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 投稿を削除します。
|
|
|
|
* @param user 投稿者
|
|
|
|
* @param note 投稿
|
|
|
|
*/
|
2022-08-10 22:09:29 +00:00
|
|
|
export default async function(user: { id: User['id']; uri: User['uri']; host: User['host']; }, note: Note, quiet = false): Promise<void> {
|
2018-10-07 11:08:42 +00:00
|
|
|
const deletedAt = new Date();
|
|
|
|
|
2020-02-25 22:56:32 +00:00
|
|
|
// この投稿を除く指定したユーザーによる指定したノートのリノートが存在しないとき
|
2020-02-25 22:54:35 +00:00
|
|
|
if (note.renoteId && (await countSameRenotes(user.id, note.renoteId, note.id)) === 0) {
|
2019-04-07 12:50:36 +00:00
|
|
|
Notes.decrement({ id: note.renoteId }, 'renoteCount', 1);
|
|
|
|
Notes.decrement({ id: note.renoteId }, 'score', 1);
|
2018-10-22 22:04:00 +00:00
|
|
|
}
|
|
|
|
|
2022-03-22 13:48:33 +00:00
|
|
|
if (note.replyId) {
|
|
|
|
await Notes.decrement({ id: note.replyId }, 'repliesCount', 1);
|
|
|
|
}
|
|
|
|
|
2019-02-20 16:30:21 +00:00
|
|
|
if (!quiet) {
|
2022-08-03 12:49:55 +00:00
|
|
|
publishNoteStream(note.id, 'deleted', { deletedAt });
|
2018-05-28 05:39:46 +00:00
|
|
|
|
2019-02-20 16:30:21 +00:00
|
|
|
//#region ローカルの投稿なら削除アクティビティを配送
|
2021-05-19 07:15:01 +00:00
|
|
|
if (Users.isLocalUser(user) && !note.localOnly) {
|
2022-04-17 12:01:02 +00:00
|
|
|
let renote: Note | null = null;
|
2019-09-08 02:30:44 +00:00
|
|
|
|
2020-02-13 14:08:33 +00:00
|
|
|
// if deletd note is renote
|
2022-07-31 22:05:10 +00:00
|
|
|
if (isPureRenote(note)) {
|
2022-03-26 06:34:00 +00:00
|
|
|
renote = await Notes.findOneBy({
|
2021-12-09 14:58:30 +00:00
|
|
|
id: note.renoteId,
|
2019-09-08 02:30:44 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
const content = renderActivity(renote
|
|
|
|
? renderUndo(renderAnnounce(renote.uri || `${config.url}/notes/${renote.id}`, note), user)
|
|
|
|
: renderDelete(renderTombstone(`${config.url}/notes/${note.id}`), user));
|
2019-02-20 16:30:21 +00:00
|
|
|
|
2020-07-11 15:44:31 +00:00
|
|
|
deliverToConcerned(user, note, content);
|
2018-12-11 11:36:55 +00:00
|
|
|
}
|
2020-02-13 14:08:33 +00:00
|
|
|
|
|
|
|
// also deliever delete activity to cascaded notes
|
|
|
|
const cascadingNotes = (await findCascadingNotes(note)).filter(note => !note.localOnly); // filter out local-only notes
|
|
|
|
for (const cascadingNote of cascadingNotes) {
|
|
|
|
if (!cascadingNote.user) continue;
|
|
|
|
if (!Users.isLocalUser(cascadingNote.user)) continue;
|
|
|
|
const content = renderActivity(renderDelete(renderTombstone(`${config.url}/notes/${cascadingNote.id}`), cascadingNote.user));
|
2020-07-11 15:44:31 +00:00
|
|
|
deliverToConcerned(cascadingNote.user, cascadingNote, content);
|
2020-02-13 14:08:33 +00:00
|
|
|
}
|
2019-02-20 16:30:21 +00:00
|
|
|
//#endregion
|
2018-08-18 14:56:44 +00:00
|
|
|
|
2019-02-20 16:30:21 +00:00
|
|
|
// 統計を更新
|
|
|
|
notesChart.update(note, false);
|
|
|
|
perUserNotesChart.update(user, note, false);
|
2019-02-08 07:58:57 +00:00
|
|
|
|
2019-04-07 12:50:36 +00:00
|
|
|
if (Users.isRemoteUser(user)) {
|
2019-02-20 16:30:21 +00:00
|
|
|
registerOrFetchInstanceDoc(user.host).then(i => {
|
2019-04-07 12:50:36 +00:00
|
|
|
Instances.decrement({ id: i.id }, 'notesCount', 1);
|
2019-04-08 05:29:17 +00:00
|
|
|
instanceChart.updateNote(i.host, note, false);
|
2019-02-20 16:30:21 +00:00
|
|
|
});
|
|
|
|
}
|
2019-02-08 07:58:57 +00:00
|
|
|
}
|
2020-02-13 14:08:33 +00:00
|
|
|
|
|
|
|
await Notes.delete({
|
|
|
|
id: note.id,
|
2021-12-09 14:58:30 +00:00
|
|
|
userId: user.id,
|
2020-02-13 14:08:33 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-08-10 22:09:29 +00:00
|
|
|
async function findCascadingNotes(note: Note): Promise<Note[]> {
|
2020-02-13 14:08:33 +00:00
|
|
|
const cascadingNotes: Note[] = [];
|
|
|
|
|
|
|
|
const recursive = async (noteId: string) => {
|
|
|
|
const query = Notes.createQueryBuilder('note')
|
|
|
|
.where('note.replyId = :noteId', { noteId })
|
2020-05-16 15:49:46 +00:00
|
|
|
.orWhere(new Brackets(q => {
|
|
|
|
q.where('note.renoteId = :noteId', { noteId })
|
|
|
|
.andWhere('note.text IS NOT NULL');
|
|
|
|
}))
|
2020-02-13 14:08:33 +00:00
|
|
|
.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
|
2018-05-28 05:39:46 +00:00
|
|
|
}
|
2020-07-11 15:44:31 +00:00
|
|
|
|
2022-08-10 22:09:29 +00:00
|
|
|
async function getMentionedRemoteUsers(note: Note): Promise<IRemoteUser[]> {
|
2020-07-11 15:44:31 +00:00
|
|
|
const where = [] as any[];
|
|
|
|
|
|
|
|
// mention / reply / dm
|
2022-10-16 02:20:11 +00:00
|
|
|
if (note.mentions.length > 0) {
|
2022-08-24 21:57:34 +00:00
|
|
|
where.push({
|
|
|
|
id: In(note.mentions),
|
|
|
|
// only remote users, local users are on the server and do not need to be notified
|
|
|
|
host: Not(IsNull()),
|
|
|
|
});
|
2020-07-11 15:44:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// renote / quote
|
|
|
|
if (note.renoteUserId) {
|
|
|
|
where.push({
|
2021-12-09 14:58:30 +00:00
|
|
|
id: note.renoteUserId,
|
2020-07-11 15:44:31 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (where.length === 0) return [];
|
|
|
|
|
|
|
|
return await Users.find({
|
2021-12-09 14:58:30 +00:00
|
|
|
where,
|
2020-07-11 15:44:31 +00:00
|
|
|
}) as IRemoteUser[];
|
|
|
|
}
|
|
|
|
|
2022-03-25 07:27:41 +00:00
|
|
|
async function deliverToConcerned(user: { id: ILocalUser['id']; host: null; }, note: Note, content: any) {
|
2022-10-10 22:30:32 +00:00
|
|
|
const manager = new DeliverManager(user, content);
|
|
|
|
|
2020-07-11 15:44:31 +00:00
|
|
|
const remoteUsers = await getMentionedRemoteUsers(note);
|
|
|
|
for (const remoteUser of remoteUsers) {
|
2022-10-10 22:30:32 +00:00
|
|
|
manager.addDirectRecipe(remoteUser);
|
2020-07-11 15:44:31 +00:00
|
|
|
}
|
2022-10-10 22:30:32 +00:00
|
|
|
|
2022-10-11 19:26:20 +00:00
|
|
|
if (['public', 'home', 'followers'].includes(note.visibility)) {
|
2022-10-10 22:30:32 +00:00
|
|
|
manager.addFollowersRecipe();
|
|
|
|
}
|
|
|
|
|
2022-10-11 19:26:20 +00:00
|
|
|
if (['public', 'home'].includes(note.visibility)) {
|
2022-10-10 22:30:32 +00:00
|
|
|
manager.addEveryone();
|
|
|
|
}
|
|
|
|
|
|
|
|
await manager.execute();
|
|
|
|
|
|
|
|
deliverToRelays(user, content);
|
2020-07-11 15:44:31 +00:00
|
|
|
}
|