2017-06-06 10:44:26 -05:00
|
|
|
import * as mongodb from 'mongodb';
|
|
|
|
import Watching from '../models/post-watching';
|
|
|
|
|
2017-06-06 11:20:07 -05:00
|
|
|
export default async (me: mongodb.ObjectID, post: object) => {
|
|
|
|
// 自分の投稿はwatchできない
|
|
|
|
if (me.equals((post as any).user_id)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-06-06 10:44:26 -05:00
|
|
|
// if watching now
|
|
|
|
const exist = await Watching.findOne({
|
2017-06-06 11:20:07 -05:00
|
|
|
post_id: (post as any)._id,
|
2017-06-06 10:44:26 -05:00
|
|
|
user_id: me,
|
|
|
|
deleted_at: { $exists: false }
|
|
|
|
});
|
|
|
|
|
|
|
|
if (exist !== null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
await Watching.insert({
|
|
|
|
created_at: new Date(),
|
2017-06-06 11:20:07 -05:00
|
|
|
post_id: (post as any)._id,
|
2017-06-06 10:44:26 -05:00
|
|
|
user_id: me
|
|
|
|
});
|
|
|
|
};
|