50 lines
1.5 KiB
JavaScript
50 lines
1.5 KiB
JavaScript
'use strict';
|
|
|
|
const Promise = require('bluebird');
|
|
const config = require('config');
|
|
|
|
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 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();
|
|
|
|
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);
|
|
};
|
|
};
|
|
|
|
module.exports = getPostsWrap;
|