2019-02-05 14:14:23 +09:00
|
|
|
import { publishNoteStream } from '../stream';
|
2018-05-28 14:39:46 +09:00
|
|
|
import renderDelete from '../../remote/activitypub/renderer/delete';
|
2019-09-08 11:30:44 +09:00
|
|
|
import renderAnnounce from '../../remote/activitypub/renderer/announce';
|
|
|
|
import renderUndo from '../../remote/activitypub/renderer/undo';
|
2019-01-31 02:29:36 +09:00
|
|
|
import { renderActivity } from '../../remote/activitypub/renderer';
|
2018-09-02 02:57:34 +09:00
|
|
|
import renderTombstone from '../../remote/activitypub/renderer/tombstone';
|
|
|
|
import config from '../../config';
|
2019-02-08 16:58:57 +09:00
|
|
|
import { registerOrFetchInstanceDoc } from '../register-or-fetch-instance-doc';
|
2019-04-07 21:50:36 +09:00
|
|
|
import { User } from '../../models/entities/user';
|
|
|
|
import { Note } from '../../models/entities/note';
|
2019-11-09 18:51:54 +09:00
|
|
|
import { Notes, Users, Instances } from '../../models';
|
2019-04-07 21:50:36 +09:00
|
|
|
import { notesChart, perUserNotesChart, instanceChart } from '../chart';
|
2019-11-09 18:51:54 +09:00
|
|
|
import { deliverToFollowers } from '../../remote/activitypub/deliver-manager';
|
2020-02-26 07:56:32 +09:00
|
|
|
import { countSameRenotes } from '../../misc/count-same-renotes';
|
2020-05-10 18:42:31 +09:00
|
|
|
import { deliverToRelays } from '../relay';
|
2018-05-28 14:39:46 +09:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 投稿を削除します。
|
|
|
|
* @param user 投稿者
|
|
|
|
* @param note 投稿
|
|
|
|
*/
|
2019-04-07 21:50:36 +09:00
|
|
|
export default async function(user: User, note: Note, quiet = false) {
|
2018-10-07 20:08:42 +09:00
|
|
|
const deletedAt = new Date();
|
|
|
|
|
2020-02-26 07:56:32 +09:00
|
|
|
// この投稿を除く指定したユーザーによる指定したノートのリノートが存在しないとき
|
2020-02-26 07:54:35 +09:00
|
|
|
if (note.renoteId && (await countSameRenotes(user.id, note.renoteId, note.id)) === 0) {
|
2019-04-07 21:50:36 +09:00
|
|
|
Notes.decrement({ id: note.renoteId }, 'renoteCount', 1);
|
|
|
|
Notes.decrement({ id: note.renoteId }, 'score', 1);
|
2018-10-23 07:04:00 +09:00
|
|
|
}
|
|
|
|
|
2019-02-21 01:30:21 +09:00
|
|
|
if (!quiet) {
|
2019-04-07 21:50:36 +09:00
|
|
|
publishNoteStream(note.id, 'deleted', {
|
2019-02-21 01:30:21 +09:00
|
|
|
deletedAt: deletedAt
|
2018-05-28 14:39:46 +09:00
|
|
|
});
|
|
|
|
|
2019-02-21 01:30:21 +09:00
|
|
|
//#region ローカルの投稿なら削除アクティビティを配送
|
2019-04-07 21:50:36 +09:00
|
|
|
if (Users.isLocalUser(user)) {
|
2019-09-08 11:30:44 +09:00
|
|
|
let renote: Note | undefined;
|
|
|
|
|
2020-02-13 23:08:33 +09:00
|
|
|
// if deletd note is renote
|
2019-09-08 11:30:44 +09:00
|
|
|
if (note.renoteId && note.text == null && !note.hasPoll && (note.fileIds == null || note.fileIds.length == 0)) {
|
|
|
|
renote = await Notes.findOne({
|
|
|
|
id: note.renoteId
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
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-21 01:30:21 +09:00
|
|
|
|
2019-11-09 18:51:54 +09:00
|
|
|
deliverToFollowers(user, content);
|
2020-05-10 18:42:31 +09:00
|
|
|
deliverToRelays(user, content);
|
2018-12-11 20:36:55 +09:00
|
|
|
}
|
2020-02-13 23:08:33 +09: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));
|
|
|
|
deliverToFollowers(cascadingNote.user, content);
|
|
|
|
}
|
2019-02-21 01:30:21 +09:00
|
|
|
//#endregion
|
2018-08-18 23:56:44 +09:00
|
|
|
|
2019-02-21 01:30:21 +09:00
|
|
|
// 統計を更新
|
|
|
|
notesChart.update(note, false);
|
|
|
|
perUserNotesChart.update(user, note, false);
|
2019-02-08 16:58:57 +09:00
|
|
|
|
2019-04-07 21:50:36 +09:00
|
|
|
if (Users.isRemoteUser(user)) {
|
2019-02-21 01:30:21 +09:00
|
|
|
registerOrFetchInstanceDoc(user.host).then(i => {
|
2019-04-07 21:50:36 +09:00
|
|
|
Instances.decrement({ id: i.id }, 'notesCount', 1);
|
2019-04-08 14:29:17 +09:00
|
|
|
instanceChart.updateNote(i.host, note, false);
|
2019-02-21 01:30:21 +09:00
|
|
|
});
|
|
|
|
}
|
2019-02-08 16:58:57 +09:00
|
|
|
}
|
2020-02-13 23:08:33 +09:00
|
|
|
|
|
|
|
await Notes.delete({
|
|
|
|
id: note.id,
|
|
|
|
userId: user.id
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
async function findCascadingNotes(note: Note) {
|
|
|
|
const cascadingNotes: Note[] = [];
|
|
|
|
|
|
|
|
const recursive = async (noteId: string) => {
|
|
|
|
const query = Notes.createQueryBuilder('note')
|
|
|
|
.where('note.replyId = :noteId', { noteId })
|
|
|
|
.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 14:39:46 +09:00
|
|
|
}
|