2017-06-06 15:44:26 +00:00
|
|
|
import * as mongodb from 'mongodb';
|
2018-04-02 08:11:14 +00:00
|
|
|
import Watching from '../models/post-watching';
|
2017-06-06 15:44:26 +00:00
|
|
|
|
2017-06-06 16:20:07 +00:00
|
|
|
export default async (me: mongodb.ObjectID, post: object) => {
|
|
|
|
// 自分の投稿はwatchできない
|
2018-03-29 05:48:47 +00:00
|
|
|
if (me.equals((post as any).userId)) {
|
2017-06-06 16:20:07 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-06-06 15:44:26 +00:00
|
|
|
// if watching now
|
|
|
|
const exist = await Watching.findOne({
|
2018-03-29 05:48:47 +00:00
|
|
|
postId: (post as any)._id,
|
|
|
|
userId: me,
|
|
|
|
deletedAt: { $exists: false }
|
2017-06-06 15:44:26 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
if (exist !== null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
await Watching.insert({
|
2018-03-29 05:48:47 +00:00
|
|
|
createdAt: new Date(),
|
|
|
|
postId: (post as any)._id,
|
|
|
|
userId: me
|
2017-06-06 15:44:26 +00:00
|
|
|
});
|
|
|
|
};
|