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