Building user posts object after fetching user to ensure user fetched posts and directly fetched posts are added to the same user key. Refactor to make better use of functions. Moved profile detail saving call to content fetch. No longer attempting and failing to save profile details for deleted users (directory would not exist).
This commit is contained in:
@@ -1,61 +1,64 @@
|
||||
'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) => {
|
||||
async function getUser(username, reddit) {
|
||||
try {
|
||||
const user = await reddit.getUser(username).fetch();
|
||||
|
||||
return curateUser(user);
|
||||
} catch(error) {
|
||||
} 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
|
||||
fallback: true,
|
||||
};
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
const getPosts = async (username, reddit, args) => {
|
||||
async function getPosts(username, reddit, args) {
|
||||
try {
|
||||
return await reddit.getUser(username).getSubmissions({
|
||||
const user = await reddit.getUser(username).getSubmissions({
|
||||
sort: args.sort,
|
||||
limit: Infinity
|
||||
limit: Infinity,
|
||||
});
|
||||
} catch(error) {
|
||||
|
||||
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 [];
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
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)
|
||||
]);
|
||||
async function getArchivedPosts(username, posts, reddit) {
|
||||
const postIds = await getArchivePostIds(username, posts.map(post => post.id));
|
||||
|
||||
if(user) {
|
||||
saveProfileDetails(user);
|
||||
}
|
||||
return Promise.all(postIds.map(postId => reddit.getSubmission(postId).fetch()));
|
||||
}
|
||||
|
||||
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()));
|
||||
function getUserPostsWrap(reddit, args) {
|
||||
return function getUserPosts(usernames) {
|
||||
return Promise.props(usernames.reduce(async (userPosts, username) => {
|
||||
const [user, posts] = await Promise.all([
|
||||
getUser(username, reddit),
|
||||
getPosts(username, reddit, args),
|
||||
]);
|
||||
|
||||
posts.push(...archivedPosts);
|
||||
}
|
||||
if (args.archives) {
|
||||
posts.push(...await getArchivedPosts(username, posts, reddit));
|
||||
}
|
||||
|
||||
return {...user, posts};
|
||||
})();
|
||||
if (posts.length) {
|
||||
return { ...userPosts, [user.name]: { ...user, posts } };
|
||||
}
|
||||
|
||||
return userPosts;
|
||||
}, {}));
|
||||
return userPosts;
|
||||
}, {}));
|
||||
};
|
||||
}
|
||||
|
||||
module.exports = getUserPostsWrap;
|
||||
|
||||
Reference in New Issue
Block a user