Last active
July 18, 2018 17:20
-
-
Save astrotars/742d668016c0b1676bd27d3ce2e9d787 to your computer and use it in GitHub Desktop.
This file contains 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
async function getContentFeed(req, res, type, model) { | |
const limit = req.query.per_page || 10; | |
const offset = req.query.page * limit || 0; | |
// retrieve articles from stream | |
const response = await getStreamClient() | |
.feed(`user_${type}`, req.params.userId) | |
.get({ limit, offset }); | |
// split on a ":" value and return the second value (article id) | |
let articleIDs = response.results.map(r => { | |
return r.foreign_id.split(':')[1]; | |
}); | |
// do a fancy $in query with the article id from the previous call | |
let articles = await model.find({ _id: { $in: articleIDs } }); | |
let articleLookup = {}; | |
// assign article ids to the article value | |
for (let a of articles) { | |
articleLookup[a._id] = a; | |
} | |
let sortedArticles = []; | |
for (let r of response.results) { | |
let articleID = r.foreign_id.split(':')[1]; | |
let article = articleLookup[articleID]; | |
if (!article) { | |
console.log( | |
`Failed to load article ${articleID} specified in feed user_${type}:${ | |
req.params.userId | |
}`, | |
); | |
continue; | |
} | |
sortedArticles.push(article); | |
} | |
// return articles as response | |
res.json(sortedArticles); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment