Skip to content

Instantly share code, notes, and snippets.

@astrotars
Last active July 18, 2018 17:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save astrotars/742d668016c0b1676bd27d3ce2e9d787 to your computer and use it in GitHub Desktop.
Save astrotars/742d668016c0b1676bd27d3ce2e9d787 to your computer and use it in GitHub Desktop.
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