Refactored post retrieval so limit is applied per-user and ignores directly requested posts, and to start utilizing async/await.
This commit is contained in:
@@ -1,40 +1,48 @@
|
||||
'use strict';
|
||||
|
||||
const Promise = require('bluebird');
|
||||
const config = require('config');
|
||||
|
||||
const curateUser = require('../curate/user.js');
|
||||
const saveProfileDetails = require('../save/profileDetails.js');
|
||||
|
||||
function getPostsWrap(reddit, args) {
|
||||
return function getPosts(postIds) {
|
||||
return postIds.reduce((chain, postId) => {
|
||||
return chain.then(acc => {
|
||||
return reddit.getSubmission(postId).fetch().then(post => ({post, acc}));
|
||||
}).then(({post, acc}) => {
|
||||
if(acc.users[post.author.name]) {
|
||||
return {post, acc, user: acc.users[post.author.name]}
|
||||
}
|
||||
const getUser = async (username, reddit) => {
|
||||
try {
|
||||
const user = await reddit.getUser(username).fetch();
|
||||
|
||||
if(post.author.name === '[deleted]') {
|
||||
return {post, acc, user: {name: '[deleted]'}};
|
||||
}
|
||||
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 reddit.getUser(post.author.name).fetch().then(curateUser).then(saveProfileDetails).then(user => ({post, acc, user}));
|
||||
}).then(({post, acc, user}) => {
|
||||
post.user = user;
|
||||
acc.posts.push(post);
|
||||
return {
|
||||
name: username,
|
||||
fallback: true
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
// keep track of users to prevent fetching one user multiple times
|
||||
acc.users[user.name] = user;
|
||||
const getPostsWrap = (reddit, args) => {
|
||||
return function getPosts(postIds, userPosts = {}) {
|
||||
return Promise.reduce(postIds, (accUserPosts, postId) => Promise.resolve().then(async () => {
|
||||
const post = await reddit.getSubmission(postId).fetch();
|
||||
|
||||
return acc;
|
||||
});
|
||||
}, Promise.resolve({
|
||||
posts: [],
|
||||
users: {}
|
||||
})).then(({posts, users}) => {
|
||||
return posts;
|
||||
});
|
||||
post.direct = true;
|
||||
|
||||
if(accUserPosts[post.author.name]) {
|
||||
accUserPosts[post.author.name].posts = accUserPosts[post.author.name].posts.concat(post);
|
||||
|
||||
return accUserPosts;
|
||||
}
|
||||
|
||||
// don't attempt to fetch deleted user
|
||||
if(post.author.name === '[deleted]') {
|
||||
return {...accUserPosts, '[deleted]': {name: '[deleted]', deleted: true, posts: [post]}};
|
||||
}
|
||||
|
||||
const user = await getUser(post.author.name);
|
||||
|
||||
return {...accUserPosts, [post.author.name]: {...user, posts: [post]}}
|
||||
}), userPosts);
|
||||
};
|
||||
};
|
||||
|
||||
|
||||
@@ -1,35 +1,41 @@
|
||||
'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 = (username, reddit) => {
|
||||
return reddit.getUser(username).fetch().then(user => curateUser(user)).catch(error => {
|
||||
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 = (username, reddit, args) => {
|
||||
return reddit.getUser(username).getSubmissions({
|
||||
sort: args.sort,
|
||||
limit: Infinity
|
||||
}).catch(error => {
|
||||
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) => usernames => Promise.all(
|
||||
usernames.map(async username => {
|
||||
const getUserPostsWrap = (reddit, args) => usernames => Promise.props(usernames.reduce((userPosts, username) => {
|
||||
userPosts[username] = (async () => {
|
||||
const [user, posts] = await Promise.all([
|
||||
getUser(username, reddit),
|
||||
getPosts(username, reddit, args)
|
||||
@@ -46,8 +52,10 @@ const getUserPostsWrap = (reddit, args) => usernames => Promise.all(
|
||||
posts.push(...archivedPosts);
|
||||
}
|
||||
|
||||
return posts.map(post => Object.assign(post, {user}));
|
||||
})
|
||||
).then(posts => posts.flatten());
|
||||
return {...user, posts};
|
||||
})();
|
||||
|
||||
return userPosts;
|
||||
}, {}));
|
||||
|
||||
module.exports = getUserPostsWrap;
|
||||
|
||||
Reference in New Issue
Block a user