'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(); 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 user = await reddit.getUser(username).getSubmissions({ sort: args.sort, limit: Infinity, }); return user; } 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) { const users = await Promise.map(usernames, async (username) => { 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; }); return users.reduce((userPosts, user) => (user ? { ...userPosts, [user.name]: user } : userPosts), {}); }; } module.exports = getUserPostsWrap;