651 | public async like(param: any): Promise { |
652 | const { id: _id } = param |
653 | const userId = _.get(this.currentUser, '_id') |
654 | const doc = await this.model.findById(_id) |
655 | const { likes, dislikes } = doc |
656 | |
657 | |
658 | if (_.findIndex(dislikes, oid => userId.equals(oid)) !== -1) return doc |
659 | |
660 | |
661 | if (_.findIndex(likes, oid => userId.equals(oid)) !== -1) { |
662 | await this.model.findOneAndUpdate( |
663 | { _id }, |
664 | { |
665 | $pull: { likes: userId }, |
666 | $inc: { likesNum: -1, activeness: -1 } |
667 | } |
668 | ) |
669 | } else { |
670 | |
671 | await this.model.findOneAndUpdate( |
672 | { _id }, |
673 | { |
674 | $push: { likes: userId }, |
675 | $inc: { likesNum: 1, activeness: 1 } |
676 | } |
677 | ) |
678 | } |
679 | |
680 | return this.model.findById(_id) |
681 | } |