'use strict'; const Promise = require('bluebird'); const getIndex = require('./getIndex.js'); const getArchivePostIds = require('../archives/getArchivePostIds.js'); const curateUser = require('../curate/user.js'); async function getUser(username, reddit) { try { const user = await reddit.getUser(username).fetch(); console.log(`Fetched user profile for '${username}' (https://reddit.com/user/${username})`); return curateUser(user); } catch (error) { console.log('\x1b[31m%s\x1b[0m', `Failed to fetch reddit user '${username}': ${error.message} (https://reddit.com/user/${username})`); return { name: username, fallback: true, }; } } async function getPosts(username, reddit, args) { try { const submissions = await reddit .getUser(username) .getSubmissions({ sort: args.sort, limit: Infinity, }); console.log(`Fetched ${submissions.length} submissions for '${username}' (https://reddit.com/user/${username})`); return submissions; } catch (error) { console.log('\x1b[31m%s\x1b[0m', `Failed to fetch posts from reddit user '${username}': ${error.message} (https://reddit.com/user/${username})`); return []; } } async function getArchivedPosts(username, posts, reddit) { const postIds = await getArchivePostIds(username, posts.map(post => post.id)); return Promise.all(postIds.map(postId => reddit.getSubmission(postId).fetch())); } function getUserPostsWrap(reddit, args) { return async function getUserPosts(usernames) { try { const users = await Promise.map(usernames, async (username) => { try { const [user, posts] = await Promise.all([ getUser(username, reddit), getPosts(username, reddit, args), ]); const { profile, posts: indexed } = await getIndex(user); if (args.archives) { posts.push(...await getArchivedPosts(username, posts, reddit)); } if (posts.length) { return { ...user, posts, indexed: { profile, original: indexed, updated: [], }, }; } return null; } catch (error) { console.log(username, error); return null; } }, { concurrency: 5, }); return users.reduce( (userPosts, user) => (user ? { ...userPosts, [user.name]: user, } : userPosts ), {}, ); } catch (error) { console.log(error); throw error; } }; } module.exports = getUserPostsWrap;