sharkey/src/services/note/delete.ts

69 lines
1.7 KiB
TypeScript
Raw Normal View History

2018-05-28 14:39:46 +09:00
import Note, { INote } from '../../models/note';
import { IUser, isLocalUser } from '../../models/user';
2018-07-07 19:19:00 +09:00
import { publishNoteStream } from '../../stream';
2018-05-28 14:39:46 +09:00
import renderDelete from '../../remote/activitypub/renderer/delete';
import pack from '../../remote/activitypub/renderer';
import { deliver } from '../../queue';
import Following from '../../models/following';
2018-09-02 02:57:34 +09:00
import renderTombstone from '../../remote/activitypub/renderer/tombstone';
2018-10-22 04:30:27 +09:00
import { notesStats, perUserNotesStats } from '../stats';
2018-09-02 02:57:34 +09:00
import config from '../../config';
2018-10-19 06:29:25 +09:00
import NoteUnread from '../../models/note-unread';
import read from './read';
2018-05-28 14:39:46 +09:00
/**
* 稿
* @param user 稿
* @param note 稿
*/
export default async function(user: IUser, note: INote) {
const deletedAt = new Date();
2018-05-28 14:39:46 +09:00
await Note.update({
_id: note._id,
userId: user._id
}, {
$set: {
deletedAt: deletedAt,
2018-05-28 14:39:46 +09:00
text: null,
2018-06-13 05:24:44 +09:00
tags: [],
2018-09-05 19:32:46 +09:00
fileIds: [],
2018-07-27 03:46:12 +09:00
poll: null,
2018-09-10 14:48:19 +09:00
geo: null,
cw: null
2018-05-28 14:39:46 +09:00
}
});
publishNoteStream(note._id, 'deleted', {
deletedAt: deletedAt
});
2018-05-28 14:39:46 +09:00
2018-10-19 06:29:25 +09:00
// この投稿が関わる未読通知を削除
NoteUnread.find({
noteId: note._id
}).then(unreads => {
unreads.forEach(unread => {
read(unread.userId, unread.noteId);
});
});
2018-05-28 14:39:46 +09:00
//#region ローカルの投稿なら削除アクティビティを配送
if (isLocalUser(user)) {
2018-09-02 02:57:34 +09:00
const content = pack(renderDelete(renderTombstone(`${config.url}/notes/${note._id}`), user));
2018-05-28 14:39:46 +09:00
const followings = await Following.find({
followeeId: user._id,
'_follower.host': { $ne: null }
});
followings.forEach(following => {
deliver(user, content, following._follower.inbox);
});
}
//#endregion
2018-08-18 23:56:44 +09:00
// 統計を更新
2018-10-21 15:08:07 +09:00
notesStats.update(note, false);
2018-10-22 04:30:27 +09:00
perUserNotesStats.update(user, note, false);
2018-05-28 14:39:46 +09:00
}