summaryrefslogtreecommitdiff
path: root/src/api
diff options
context:
space:
mode:
authorsyuilo <syuilotan@yahoo.co.jp>2017-06-07 00:44:26 +0900
committersyuilo <syuilotan@yahoo.co.jp>2017-06-07 00:44:26 +0900
commit079f2098e3047c299b2e6d113efee26f1edad361 (patch)
treed27d729e7d249c3adaa5853fa845fc71a871d9a0 /src/api
parentMerge pull request #536 from syuilo/greenkeeper/ts-node-3.0.5 (diff)
downloadsharkey-079f2098e3047c299b2e6d113efee26f1edad361.tar.gz
sharkey-079f2098e3047c299b2e6d113efee26f1edad361.tar.bz2
sharkey-079f2098e3047c299b2e6d113efee26f1edad361.zip
WIP #161
Diffstat (limited to 'src/api')
-rw-r--r--src/api/common/watch-post.ts21
-rw-r--r--src/api/endpoints/posts/polls/vote.ts28
-rw-r--r--src/api/endpoints/posts/reactions/create.ts28
-rw-r--r--src/api/models/post-watching.ts3
4 files changed, 80 insertions, 0 deletions
diff --git a/src/api/common/watch-post.ts b/src/api/common/watch-post.ts
new file mode 100644
index 0000000000..9f5e7a9130
--- /dev/null
+++ b/src/api/common/watch-post.ts
@@ -0,0 +1,21 @@
+import * as mongodb from 'mongodb';
+import Watching from '../models/post-watching';
+
+export default async (me: mongodb.ObjectID, post: mongodb.ObjectID) => {
+ // if watching now
+ const exist = await Watching.findOne({
+ post_id: post,
+ user_id: me,
+ deleted_at: { $exists: false }
+ });
+
+ if (exist !== null) {
+ return;
+ }
+
+ await Watching.insert({
+ created_at: new Date(),
+ post_id: post,
+ user_id: me
+ });
+};
diff --git a/src/api/endpoints/posts/polls/vote.ts b/src/api/endpoints/posts/polls/vote.ts
index d359d7d2c3..7bd9119fcb 100644
--- a/src/api/endpoints/posts/polls/vote.ts
+++ b/src/api/endpoints/posts/polls/vote.ts
@@ -4,7 +4,9 @@
import $ from 'cafy';
import Vote from '../../../models/poll-vote';
import Post from '../../../models/post';
+import Watching from '../../../models/post-watching';
import notify from '../../../common/notify';
+import watch from '../../../common/watch-post';
import { publishPostStream } from '../../../event';
/**
@@ -75,6 +77,32 @@ module.exports = (params, user) => new Promise(async (res, rej) => {
post_id: post._id,
choice: choice
});
+
+ // Fetch watchers
+ Watching
+ .find({
+ post_id: post._id,
+ user_id: { $ne: user._id },
+ // 削除されたドキュメントは除く
+ deleted_at: { $exists: false }
+ }, {
+ fields: {
+ user_id: true
+ }
+ })
+ .then(watchers => {
+ watchers.forEach(watcher => {
+ notify(watcher.user_id, user._id, 'poll_vote', {
+ post_id: post._id,
+ choice: choice
+ });
+ });
+ });
+
+ // この投稿をWatchする
+ // TODO: ユーザーが「投票したときに自動でWatchする」設定を
+ // オフにしていた場合はしない
+ watch(user._id, post._id);
});
function findWithAttr(array, attr, value) {
diff --git a/src/api/endpoints/posts/reactions/create.ts b/src/api/endpoints/posts/reactions/create.ts
index ba5b69ada3..7cae3830e0 100644
--- a/src/api/endpoints/posts/reactions/create.ts
+++ b/src/api/endpoints/posts/reactions/create.ts
@@ -4,7 +4,9 @@
import $ from 'cafy';
import Reaction from '../../../models/post-reaction';
import Post from '../../../models/post';
+import Watching from '../../../models/post-watching';
import notify from '../../../common/notify';
+import watch from '../../../common/watch-post';
import { publishPostStream } from '../../../event';
/**
@@ -84,4 +86,30 @@ module.exports = (params, user) => new Promise(async (res, rej) => {
post_id: post._id,
reaction: reaction
});
+
+ // Fetch watchers
+ Watching
+ .find({
+ post_id: post._id,
+ user_id: { $ne: user._id },
+ // 削除されたドキュメントは除く
+ deleted_at: { $exists: false }
+ }, {
+ fields: {
+ user_id: true
+ }
+ })
+ .then(watchers => {
+ watchers.forEach(watcher => {
+ notify(watcher.user_id, user._id, 'reaction', {
+ post_id: post._id,
+ reaction: reaction
+ });
+ });
+ });
+
+ // この投稿をWatchする
+ // TODO: ユーザーが「リアクションしたときに自動でWatchする」設定を
+ // オフにしていた場合はしない
+ watch(user._id, post._id);
});
diff --git a/src/api/models/post-watching.ts b/src/api/models/post-watching.ts
new file mode 100644
index 0000000000..41d37e2703
--- /dev/null
+++ b/src/api/models/post-watching.ts
@@ -0,0 +1,3 @@
+import db from '../../db/mongodb';
+
+export default db.get('post_watching') as any; // fuck type definition