Cleaned up entrypoint.
This commit is contained in:
41
src/sources/getPosts.js
Normal file
41
src/sources/getPosts.js
Normal file
@@ -0,0 +1,41 @@
|
||||
'use strict';
|
||||
|
||||
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]}
|
||||
}
|
||||
|
||||
if(post.author.name === '[deleted]') {
|
||||
return {post, acc, user: {name: '[deleted]'}};
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
// keep track of users to prevent fetching one user multiple times
|
||||
acc.users[user.name] = user;
|
||||
|
||||
return acc;
|
||||
});
|
||||
}, Promise.resolve({
|
||||
posts: [],
|
||||
users: {}
|
||||
})).then(({posts, users}) => {
|
||||
return posts;
|
||||
});
|
||||
};
|
||||
};
|
||||
|
||||
module.exports = getPostsWrap;
|
||||
47
src/sources/getUserPosts.js
Normal file
47
src/sources/getUserPosts.js
Normal file
@@ -0,0 +1,47 @@
|
||||
'use strict';
|
||||
|
||||
const config = require('config');
|
||||
|
||||
const getArchivePostIds = require('../archives/getArchivePostIds.js');
|
||||
const curateUser = require('../curate/user.js');
|
||||
const saveProfileDetails = require('../save/profileDetails.js');
|
||||
|
||||
function getUserPostsWrap(reddit, args) {
|
||||
return function getUserPosts(usernames) {
|
||||
return usernames.reduce((chain, username) => {
|
||||
return chain.then(accPosts => {
|
||||
return reddit.getUser(username).fetch().then(curateUser).then(saveProfileDetails).then(user => ({
|
||||
user,
|
||||
accPosts
|
||||
}));
|
||||
}).then(({user, accPosts}) => {
|
||||
return reddit.getUser(username).getSubmissions({
|
||||
sort: args.sort,
|
||||
limit: Infinity
|
||||
}).then(posts => ({
|
||||
user,
|
||||
accPosts: accPosts.concat(posts)
|
||||
}));
|
||||
}).then(({user, accPosts}) => {
|
||||
if(args.archives || config.fetch.archives.search) {
|
||||
return getArchivePostIds(username, accPosts.map(post => post.id)).then(postIds => {
|
||||
return Promise.all(postIds.map(postId => {
|
||||
return reddit.getSubmission(postId).fetch();
|
||||
}));
|
||||
}).then(archivedPosts => {
|
||||
return {
|
||||
user,
|
||||
accPosts: accPosts.concat(archivedPosts)
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
return {user, accPosts};
|
||||
}).then(({user, accPosts}) => {
|
||||
return accPosts.map(post => Object.assign(post, {user}));
|
||||
});
|
||||
}, Promise.resolve([]));
|
||||
};
|
||||
};
|
||||
|
||||
module.exports = getUserPostsWrap;
|
||||
Reference in New Issue
Block a user