sharkey/src/services/note/reaction/create.ts

95 lines
2.4 KiB
TypeScript
Raw Normal View History

2018-04-07 17:05:14 +09:00
import { IUser, pack as packUser, isLocalUser, isRemoteUser } from '../../../models/user';
2018-04-08 02:30:37 +09:00
import Note, { INote, pack as packNote } from '../../../models/note';
import NoteReaction from '../../../models/note-reaction';
import { publishNoteStream } from '../../../publishers/stream';
2018-04-07 17:05:14 +09:00
import notify from '../../../publishers/notify';
import pushSw from '../../../publishers/push-sw';
2018-04-08 02:30:37 +09:00
import NoteWatching from '../../../models/note-watching';
2018-04-07 17:05:14 +09:00
import watch from '../watch';
import renderLike from '../../../remote/activitypub/renderer/like';
import { deliver } from '../../../queue';
import context from '../../../remote/activitypub/renderer/context';
2018-04-08 02:30:37 +09:00
export default async (user: IUser, note: INote, reaction: string) => new Promise(async (res, rej) => {
2018-04-07 17:05:14 +09:00
// Myself
2018-04-08 02:30:37 +09:00
if (note.userId.equals(user._id)) {
return rej('cannot react to my note');
2018-04-07 17:05:14 +09:00
}
// if already reacted
2018-04-08 02:30:37 +09:00
const exist = await NoteReaction.findOne({
noteId: note._id,
2018-04-07 17:05:14 +09:00
userId: user._id
});
if (exist !== null) {
return rej('already reacted');
}
// Create reaction
2018-04-08 02:30:37 +09:00
await NoteReaction.insert({
2018-04-07 17:05:14 +09:00
createdAt: new Date(),
2018-04-08 02:30:37 +09:00
noteId: note._id,
2018-04-07 17:05:14 +09:00
userId: user._id,
reaction
});
res();
const inc = {};
inc[`reactionCounts.${reaction}`] = 1;
// Increment reactions count
2018-04-08 02:30:37 +09:00
await Note.update({ _id: note._id }, {
2018-04-07 17:05:14 +09:00
$inc: inc
});
2018-04-08 02:30:37 +09:00
publishNoteStream(note._id, 'reacted');
2018-04-07 17:05:14 +09:00
// Notify
2018-04-08 02:30:37 +09:00
notify(note.userId, user._id, 'reaction', {
noteId: note._id,
2018-04-07 17:05:14 +09:00
reaction: reaction
});
2018-04-08 02:30:37 +09:00
pushSw(note.userId, 'reaction', {
user: await packUser(user, note.userId),
note: await packNote(note, note.userId),
2018-04-07 17:05:14 +09:00
reaction: reaction
});
// Fetch watchers
2018-04-08 02:30:37 +09:00
NoteWatching
2018-04-07 17:05:14 +09:00
.find({
2018-04-08 02:30:37 +09:00
noteId: note._id,
2018-04-07 17:05:14 +09:00
userId: { $ne: user._id }
}, {
fields: {
userId: true
}
})
.then(watchers => {
watchers.forEach(watcher => {
notify(watcher.userId, user._id, 'reaction', {
2018-04-08 02:30:37 +09:00
noteId: note._id,
2018-04-07 17:05:14 +09:00
reaction: reaction
});
});
});
// ユーザーがローカルユーザーかつ自動ウォッチ設定がオンならばこの投稿をWatchする
if (isLocalUser(user) && user.account.settings.autoWatch !== false) {
2018-04-08 02:30:37 +09:00
watch(user._id, note);
2018-04-07 17:05:14 +09:00
}
//#region 配信
2018-04-08 02:30:37 +09:00
const content = renderLike(user, note);
2018-04-07 17:05:14 +09:00
content['@context'] = context;
// リアクターがローカルユーザーかつリアクション対象がリモートユーザーの投稿なら配送
2018-04-08 02:30:37 +09:00
if (isLocalUser(user) && isRemoteUser(note._user)) {
deliver(user, content, note._user.account.inbox).save();
2018-04-07 17:05:14 +09:00
}
//#endregion
});