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

94 lines
2.4 KiB
TypeScript
Raw Normal View History

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