'use strict'; const config = require('config'); const dissectLink = require('../dissectLink.js'); const omit = require('object.omit'); const curatePosts = (userPosts, args) => { const processed = new Set(); return Object.values(userPosts).reduce((accPosts, user) => accPosts.concat(user.posts.reduce((accUserPosts, post, index) => { // cut-off at limit, but don't count posts requested directly by ID if(accUserPosts.length >= args.limit && !post.direct) { return accUserPosts; } const host = dissectLink(post.url); const permalink = 'https://reddit.com' + post.permalink; const ignoring = args.ignore ? args.ignore.find(prop => { return post[prop]; }) : null; if(ignoring) { console.log('\x1b[33m%s\x1b[0m', `Ignoring ${ignoring} post '${post.title}' (${permalink})`); return accUserPosts; } if(host) { if(config.fetch.avoidDuplicates && processed.has(host.id)) { console.log('\x1b[33m%s\x1b[0m', `Ignoring duplicate content '${post.url}' (cross-post, repost, or --post ID was already included) (${permalink})`); return accUserPosts; } if((args.include && !args.include.includes(host.label)) || (args.exclude && args.exclude.includes(host.label))) { console.log('\x1b[33m%s\x1b[0m', `Ignoring source '${host.label}' from post '${post.url}' (${permalink})`); return accUserPosts; } processed.add(host.id); } return accUserPosts.concat({ id: post.id, index: index, title: post.title, text: post.selftext, user: omit(user, ['posts']), permalink, url: post.url, datetime: new Date(post.created_utc * 1000), subreddit: post.subreddit.display_name, preview: post.preview ? post.preview.images.map(image => image.source) : null, host }); }, [])), []); }; module.exports = curatePosts;