diff options
| author | syuilo <syuilotan@yahoo.co.jp> | 2017-03-20 04:24:19 +0900 |
|---|---|---|
| committer | syuilo <syuilotan@yahoo.co.jp> | 2017-03-20 04:24:19 +0900 |
| commit | b05bee58d28c3209d7f86a909f877c1e121c12ed (patch) | |
| tree | 96efc12c89479b63dbc339ab18bcf9dde9dd6bcc /src/api/serializers | |
| parent | [Client] :art: (diff) | |
| download | sharkey-b05bee58d28c3209d7f86a909f877c1e121c12ed.tar.gz sharkey-b05bee58d28c3209d7f86a909f877c1e121c12ed.tar.bz2 sharkey-b05bee58d28c3209d7f86a909f877c1e121c12ed.zip | |
#298
Diffstat (limited to 'src/api/serializers')
| -rw-r--r-- | src/api/serializers/notification.ts | 2 | ||||
| -rw-r--r-- | src/api/serializers/post-reaction.ts | 43 | ||||
| -rw-r--r-- | src/api/serializers/post.ts | 14 |
3 files changed, 51 insertions, 8 deletions
diff --git a/src/api/serializers/notification.ts b/src/api/serializers/notification.ts index 50952e5426..ac919dc8b0 100644 --- a/src/api/serializers/notification.ts +++ b/src/api/serializers/notification.ts @@ -51,7 +51,7 @@ export default (notification: any) => new Promise<any>(async (resolve, reject) = case 'reply': case 'repost': case 'quote': - case 'like': + case 'reaction': case 'poll_vote': // Populate post _notification.post = await serializePost(_notification.post_id, me); diff --git a/src/api/serializers/post-reaction.ts b/src/api/serializers/post-reaction.ts new file mode 100644 index 0000000000..b8807a741c --- /dev/null +++ b/src/api/serializers/post-reaction.ts @@ -0,0 +1,43 @@ +/** + * Module dependencies + */ +import * as mongo from 'mongodb'; +import deepcopy = require('deepcopy'); +import Reaction from '../models/post-reaction'; +import serializeUser from './user'; + +/** + * Serialize a reaction + * + * @param {any} reaction + * @param {any} me? + * @return {Promise<any>} + */ +export default ( + reaction: any, + me?: any +) => new Promise<any>(async (resolve, reject) => { + let _reaction: any; + + // Populate the reaction if 'reaction' is ID + if (mongo.ObjectID.prototype.isPrototypeOf(reaction)) { + _reaction = await Reaction.findOne({ + _id: reaction + }); + } else if (typeof reaction === 'string') { + _reaction = await Reaction.findOne({ + _id: new mongo.ObjectID(reaction) + }); + } else { + _reaction = deepcopy(reaction); + } + + // Rename _id to id + _reaction.id = _reaction._id; + delete _reaction._id; + + // Populate user + _reaction.user = await serializeUser(_reaction.user_id, me); + + resolve(_reaction); +}); diff --git a/src/api/serializers/post.ts b/src/api/serializers/post.ts index f459529697..3c96884dd1 100644 --- a/src/api/serializers/post.ts +++ b/src/api/serializers/post.ts @@ -4,7 +4,7 @@ import * as mongo from 'mongodb'; import deepcopy = require('deepcopy'); import Post from '../models/post'; -import Like from '../models/like'; +import Reaction from '../models/post-reaction'; import Vote from '../models/poll-vote'; import serializeApp from './app'; import serializeUser from './user'; @@ -100,18 +100,18 @@ const self = ( } } - // Check if it is liked + // Fetch my reaction if (me && opts.detail) { - const liked = await Like - .count({ + const reaction = await Reaction + .findOne({ user_id: me._id, post_id: id, deleted_at: { $exists: false } - }, { - limit: 1 }); - _post.is_liked = liked === 1; + if (reaction) { + _post.my_reaction = reaction.reaction; + } } resolve(_post); |