egirlskey/src/services/note/delete.ts

95 lines
2.4 KiB
TypeScript
Raw Normal View History

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