summaryrefslogtreecommitdiff
path: root/src/services
diff options
context:
space:
mode:
authorsyuilo <syuilotan@yahoo.co.jp>2018-04-07 17:05:14 +0900
committersyuilo <syuilotan@yahoo.co.jp>2018-04-07 17:05:14 +0900
commitc5f23bce7864bc1cffb82b54f62e019c4c04137d (patch)
tree8ec7313f4ec333bcb4ca3e19a5106c602740870c /src/services
parentRefactor (diff)
downloadmisskey-c5f23bce7864bc1cffb82b54f62e019c4c04137d.tar.gz
misskey-c5f23bce7864bc1cffb82b54f62e019c4c04137d.tar.bz2
misskey-c5f23bce7864bc1cffb82b54f62e019c4c04137d.zip
Implement like
Diffstat (limited to 'src/services')
-rw-r--r--src/services/post/reaction/create.ts94
1 files changed, 94 insertions, 0 deletions
diff --git a/src/services/post/reaction/create.ts b/src/services/post/reaction/create.ts
index e69de29bb2..c26efcfc75 100644
--- a/src/services/post/reaction/create.ts
+++ b/src/services/post/reaction/create.ts
@@ -0,0 +1,94 @@
+import { IUser, pack as packUser, isLocalUser, isRemoteUser } from '../../../models/user';
+import Post, { IPost, pack as packPost } from '../../../models/post';
+import PostReaction from '../../../models/post-reaction';
+import { publishPostStream } from '../../../publishers/stream';
+import notify from '../../../publishers/notify';
+import pushSw from '../../../publishers/push-sw';
+import PostWatching from '../../../models/post-watching';
+import watch from '../watch';
+import renderLike from '../../../remote/activitypub/renderer/like';
+import { deliver } from '../../../queue';
+import context from '../../../remote/activitypub/renderer/context';
+
+export default async (user: IUser, post: IPost, reaction: string) => new Promise(async (res, rej) => {
+ // Myself
+ if (post.userId.equals(user._id)) {
+ return rej('cannot react to my post');
+ }
+
+ // if already reacted
+ const exist = await PostReaction.findOne({
+ postId: post._id,
+ userId: user._id
+ });
+
+ if (exist !== null) {
+ return rej('already reacted');
+ }
+
+ // Create reaction
+ await PostReaction.insert({
+ createdAt: new Date(),
+ postId: post._id,
+ userId: user._id,
+ reaction
+ });
+
+ res();
+
+ const inc = {};
+ inc[`reactionCounts.${reaction}`] = 1;
+
+ // Increment reactions count
+ await Post.update({ _id: post._id }, {
+ $inc: inc
+ });
+
+ publishPostStream(post._id, 'reacted');
+
+ // Notify
+ notify(post.userId, user._id, 'reaction', {
+ postId: post._id,
+ reaction: reaction
+ });
+
+ pushSw(post.userId, 'reaction', {
+ user: await packUser(user, post.userId),
+ post: await packPost(post, post.userId),
+ reaction: reaction
+ });
+
+ // Fetch watchers
+ PostWatching
+ .find({
+ postId: post._id,
+ userId: { $ne: user._id }
+ }, {
+ fields: {
+ userId: true
+ }
+ })
+ .then(watchers => {
+ watchers.forEach(watcher => {
+ notify(watcher.userId, user._id, 'reaction', {
+ postId: post._id,
+ reaction: reaction
+ });
+ });
+ });
+
+ // ユーザーがローカルユーザーかつ自動ウォッチ設定がオンならばこの投稿をWatchする
+ if (isLocalUser(user) && user.account.settings.autoWatch !== false) {
+ watch(user._id, post);
+ }
+
+ //#region 配信
+ const content = renderLike(user, post);
+ content['@context'] = context;
+
+ // リアクターがローカルユーザーかつリアクション対象がリモートユーザーの投稿なら配送
+ if (isLocalUser(user) && isRemoteUser(post._user)) {
+ deliver(user, content, post._user.account.inbox).save();
+ }
+ //#endregion
+});