Created
September 14, 2018 12:09
-
-
Save vanya2h/f5e0c2b055b6675133e2290f562ec19e to your computer and use it in GitHub Desktop.
Find user feed posts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const yesterday = Date.now() - 1000*60*60*24; | |
const getUserFeed = (userId, params) => User.findById(userId) | |
.populate(USER_ENTRY_SUBSCRIPTIONS) | |
.then(user => | |
Post.find({ | |
[POST_ENTRY_CREATED]: { $gt: yesterday }, | |
[`${POST_ENTRY_AUTHOR}.${ENTITY_ENTRY_ITEM}`]: { $in: [ | |
...user[USER_ENTRY_SUBSCRIPTIONS][USER_SUBSCRIPTIONS_ENTRY_USERS], | |
...user[USER_ENTRY_SUBSCRIPTIONS][USER_SUBSCRIPTIONS_ENTRY_BLOGS] | |
] } | |
}, { | |
...params, | |
sort: { [POST_ENTRY_LIKES]: -1 }, | |
}) | |
.then((posts) => { | |
if (posts.length > 0) { | |
return posts; | |
} | |
return Post.find({ | |
[`${POST_ENTRY_AUTHOR}.${ENTITY_ENTRY_ITEM}`]: { $in: [ | |
...user[USER_ENTRY_SUBSCRIPTIONS][USER_SUBSCRIPTIONS_ENTRY_USERS], | |
...user[USER_ENTRY_SUBSCRIPTIONS][USER_SUBSCRIPTIONS_ENTRY_BLOGS] | |
] | |
}, { | |
...params, | |
sort: { [POST_ENTRY_CREATED]: -1 }, | |
}) | |
}) | |
}) | |
.catch(error => Promise.reject(error)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment