2018-05-28 00:39:46 -05:00
|
|
|
import Note, { INote } from '../../models/note';
|
2019-02-08 01:58:57 -06:00
|
|
|
import { IUser, isLocalUser, isRemoteUser } from '../../models/user';
|
2019-02-04 23:14:23 -06:00
|
|
|
import { publishNoteStream } from '../stream';
|
2018-05-28 00:39:46 -05:00
|
|
|
import renderDelete from '../../remote/activitypub/renderer/delete';
|
2019-01-30 11:29:36 -06:00
|
|
|
import { renderActivity } from '../../remote/activitypub/renderer';
|
2018-05-28 00:39:46 -05:00
|
|
|
import { deliver } from '../../queue';
|
|
|
|
import Following from '../../models/following';
|
2018-09-01 12:57:34 -05:00
|
|
|
import renderTombstone from '../../remote/activitypub/renderer/tombstone';
|
2019-02-07 13:31:33 -06:00
|
|
|
import notesChart from '../../services/chart/notes';
|
|
|
|
import perUserNotesChart from '../../services/chart/per-user-notes';
|
2018-09-01 12:57:34 -05:00
|
|
|
import config from '../../config';
|
2018-10-18 16:29:25 -05:00
|
|
|
import NoteUnread from '../../models/note-unread';
|
|
|
|
import read from './read';
|
2018-10-22 17:04:00 -05:00
|
|
|
import DriveFile from '../../models/drive-file';
|
2019-02-08 01:58:57 -06:00
|
|
|
import { registerOrFetchInstanceDoc } from '../register-or-fetch-instance-doc';
|
|
|
|
import Instance from '../../models/instance';
|
|
|
|
import instanceChart from '../../services/chart/instance';
|
2018-05-28 00:39:46 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 投稿を削除します。
|
|
|
|
* @param user 投稿者
|
|
|
|
* @param note 投稿
|
|
|
|
*/
|
2019-02-20 10:30:21 -06:00
|
|
|
export default async function(user: IUser, note: INote, quiet = false) {
|
2018-10-07 06:08:42 -05:00
|
|
|
const deletedAt = new Date();
|
|
|
|
|
2018-05-28 00:39:46 -05:00
|
|
|
await Note.update({
|
|
|
|
_id: note._id,
|
|
|
|
userId: user._id
|
|
|
|
}, {
|
|
|
|
$set: {
|
2018-10-07 06:08:42 -05:00
|
|
|
deletedAt: deletedAt,
|
2018-05-28 00:39:46 -05:00
|
|
|
text: null,
|
2018-06-12 15:24:44 -05:00
|
|
|
tags: [],
|
2018-09-05 05:32:46 -05:00
|
|
|
fileIds: [],
|
2019-01-26 02:47:56 -06:00
|
|
|
renoteId: null,
|
2018-07-26 13:46:12 -05:00
|
|
|
poll: null,
|
2018-09-10 00:48:19 -05:00
|
|
|
geo: null,
|
|
|
|
cw: null
|
2018-05-28 00:39:46 -05:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-01-26 02:47:56 -06:00
|
|
|
if (note.renoteId) {
|
|
|
|
Note.update({ _id: note.renoteId }, {
|
|
|
|
$inc: {
|
|
|
|
renoteCount: -1,
|
|
|
|
score: -1
|
|
|
|
},
|
|
|
|
$pull: {
|
|
|
|
_quoteIds: note._id
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-10-18 16:29:25 -05:00
|
|
|
// この投稿が関わる未読通知を削除
|
|
|
|
NoteUnread.find({
|
|
|
|
noteId: note._id
|
|
|
|
}).then(unreads => {
|
2018-12-11 05:36:55 -06:00
|
|
|
for (const unread of unreads) {
|
2018-10-18 16:29:25 -05:00
|
|
|
read(unread.userId, unread.noteId);
|
2018-12-11 05:36:55 -06:00
|
|
|
}
|
2018-10-18 16:29:25 -05:00
|
|
|
});
|
|
|
|
|
2018-10-22 17:04:00 -05:00
|
|
|
// ファイルが添付されていた場合ドライブのファイルの「このファイルが添付された投稿一覧」プロパティからこの投稿を削除
|
|
|
|
if (note.fileIds) {
|
2018-12-11 05:36:55 -06:00
|
|
|
for (const fileId of note.fileIds) {
|
2018-10-22 17:04:00 -05:00
|
|
|
DriveFile.update({ _id: fileId }, {
|
|
|
|
$pull: {
|
|
|
|
'metadata.attachedNoteIds': note._id
|
|
|
|
}
|
|
|
|
});
|
2018-12-11 05:36:55 -06:00
|
|
|
}
|
2018-10-22 17:04:00 -05:00
|
|
|
}
|
|
|
|
|
2019-02-20 10:30:21 -06:00
|
|
|
if (!quiet) {
|
|
|
|
publishNoteStream(note._id, 'deleted', {
|
|
|
|
deletedAt: deletedAt
|
2018-05-28 00:39:46 -05:00
|
|
|
});
|
|
|
|
|
2019-02-20 10:30:21 -06:00
|
|
|
//#region ローカルの投稿なら削除アクティビティを配送
|
|
|
|
if (isLocalUser(user)) {
|
|
|
|
const content = renderActivity(renderDelete(renderTombstone(`${config.url}/notes/${note._id}`), user));
|
|
|
|
|
|
|
|
const followings = await Following.find({
|
|
|
|
followeeId: user._id,
|
|
|
|
'_follower.host': { $ne: null }
|
|
|
|
});
|
|
|
|
|
|
|
|
for (const following of followings) {
|
|
|
|
deliver(user, content, following._follower.inbox);
|
|
|
|
}
|
2018-12-11 05:36:55 -06:00
|
|
|
}
|
2019-02-20 10:30:21 -06:00
|
|
|
//#endregion
|
2018-08-18 09:56:44 -05:00
|
|
|
|
2019-02-20 10:30:21 -06:00
|
|
|
// 統計を更新
|
|
|
|
notesChart.update(note, false);
|
|
|
|
perUserNotesChart.update(user, note, false);
|
2019-02-08 01:58:57 -06:00
|
|
|
|
2019-02-20 10:30:21 -06:00
|
|
|
if (isRemoteUser(user)) {
|
|
|
|
registerOrFetchInstanceDoc(user.host).then(i => {
|
|
|
|
Instance.update({ _id: i._id }, {
|
|
|
|
$inc: {
|
|
|
|
notesCount: -1
|
|
|
|
}
|
|
|
|
});
|
2019-02-08 01:58:57 -06:00
|
|
|
|
2019-02-20 10:30:21 -06:00
|
|
|
instanceChart.updateNote(i.host, false);
|
|
|
|
});
|
|
|
|
}
|
2019-02-08 01:58:57 -06:00
|
|
|
}
|
2018-05-28 00:39:46 -05:00
|
|
|
}
|