62 lines
1.8 KiB
JavaScript
62 lines
1.8 KiB
JavaScript
'use strict';
|
|
|
|
const Promise = require('bluebird');
|
|
const config = require('config');
|
|
const getArchivePostIds = require('../archives/getArchivePostIds.js');
|
|
const curateUser = require('../curate/user.js');
|
|
const saveProfileDetails = require('../save/profileDetails.js');
|
|
|
|
const getUser = async (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
|
|
};
|
|
}
|
|
};
|
|
|
|
const getPosts = async (username, reddit, args) => {
|
|
try {
|
|
return await reddit.getUser(username).getSubmissions({
|
|
sort: args.sort,
|
|
limit: Infinity
|
|
});
|
|
} 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 [];
|
|
}
|
|
};
|
|
|
|
const getUserPostsWrap = (reddit, args) => users => Promise.props(Object.entries(users).reduce((userPosts, [username, user]) => {
|
|
userPosts[username] = (async () => {
|
|
const [user, posts] = await Promise.all([
|
|
getUser(username, reddit),
|
|
getPosts(username, reddit, args)
|
|
]);
|
|
|
|
if(user) {
|
|
saveProfileDetails(user);
|
|
}
|
|
|
|
if(args.archives) {
|
|
const postIds = await getArchivePostIds(username, posts.map(post => post.id));
|
|
const archivedPosts = await Promise.all(postIds.map(postId => reddit.getSubmission(postId).fetch()));
|
|
|
|
posts.push(...archivedPosts);
|
|
}
|
|
|
|
return {...user, posts};
|
|
})();
|
|
|
|
return userPosts;
|
|
}, {}));
|
|
|
|
module.exports = getUserPostsWrap;
|