Skip to content
Snippets Groups Projects
Commit 079f2098 authored by syuilo's avatar syuilo
Browse files

WIP #161

parent eea09bc3
No related branches found
No related tags found
No related merge requests found
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
});
};
......@@ -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) {
......
......@@ -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);
});
import db from '../../db/mongodb';
export default db.get('post_watching') as any; // fuck type definition
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment