2018-05-05 00:27:15 +00:00
|
|
|
'use strict';
|
|
|
|
|
2018-05-27 23:42:46 +00:00
|
|
|
const Promise = require('bluebird');
|
2018-05-05 00:27:15 +00:00
|
|
|
|
2018-07-01 01:26:26 +00:00
|
|
|
const getIndex = require('./getIndex.js');
|
2018-05-05 00:27:15 +00:00
|
|
|
const curateUser = require('../curate/user.js');
|
|
|
|
|
2021-12-17 01:04:25 +00:00
|
|
|
const logger = require('../logger')(__filename);
|
|
|
|
const limiter = require('../limiter').reddit;
|
|
|
|
|
2019-11-05 00:58:00 +00:00
|
|
|
async function getUser(username, reddit) {
|
2018-05-27 23:42:46 +00:00
|
|
|
try {
|
2021-12-17 01:04:25 +00:00
|
|
|
const user = await limiter.schedule(async () => reddit.getUser(username).fetch());
|
2018-05-27 23:42:46 +00:00
|
|
|
|
|
|
|
return curateUser(user);
|
2018-06-30 23:07:32 +00:00
|
|
|
} catch (error) {
|
2021-12-17 01:04:25 +00:00
|
|
|
logger.error(`Failed to fetch reddit user '${username}': ${error.message} (https://reddit.com/user/${username})`);
|
2018-05-27 23:42:46 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
name: username,
|
2018-06-30 23:07:32 +00:00
|
|
|
fallback: true,
|
2018-05-27 23:42:46 +00:00
|
|
|
};
|
|
|
|
}
|
2021-12-17 01:04:25 +00:00
|
|
|
}
|
2018-05-27 23:42:46 +00:00
|
|
|
|
2018-06-30 23:07:32 +00:00
|
|
|
const getPostsWrap = reddit => function getPosts(postIds, userPosts = {}) {
|
|
|
|
return Promise.reduce(postIds, (accUserPosts, postId) => Promise.resolve().then(async () => {
|
2021-12-17 01:04:25 +00:00
|
|
|
const post = await limiter.schedule(async () => reddit.getSubmission(postId).fetch());
|
2018-05-27 23:42:46 +00:00
|
|
|
|
2018-06-30 23:07:32 +00:00
|
|
|
post.direct = true;
|
2018-05-27 23:42:46 +00:00
|
|
|
|
2018-06-30 23:07:32 +00:00
|
|
|
if (accUserPosts[post.author.name]) {
|
2021-12-17 01:04:25 +00:00
|
|
|
return {
|
|
|
|
...accUserPosts,
|
|
|
|
[post.author.name]: {
|
|
|
|
...accUserPosts[post.author.name],
|
|
|
|
posts: [...accUserPosts[post.author.name].posts, post],
|
|
|
|
},
|
|
|
|
};
|
2018-06-30 23:07:32 +00:00
|
|
|
}
|
2018-05-27 23:42:46 +00:00
|
|
|
|
2018-06-30 23:07:32 +00:00
|
|
|
// don't attempt to fetch deleted user
|
|
|
|
if (post.author.name === '[deleted]') {
|
2019-11-05 00:58:00 +00:00
|
|
|
return {
|
|
|
|
...accUserPosts,
|
|
|
|
'[deleted]': {
|
|
|
|
name: '[deleted]',
|
|
|
|
deleted: true,
|
|
|
|
posts: [post],
|
2019-11-12 02:38:26 +00:00
|
|
|
indexed: {
|
|
|
|
profile: {},
|
|
|
|
original: [],
|
|
|
|
updated: [],
|
|
|
|
},
|
2019-11-05 00:58:00 +00:00
|
|
|
},
|
|
|
|
};
|
2018-06-30 23:07:32 +00:00
|
|
|
}
|
2018-05-27 23:42:46 +00:00
|
|
|
|
2018-06-30 23:07:32 +00:00
|
|
|
const user = await getUser(post.author.name, reddit);
|
2018-07-01 22:25:48 +00:00
|
|
|
const { profile, posts: indexed } = await getIndex(user);
|
2018-05-27 23:42:46 +00:00
|
|
|
|
2019-11-05 00:58:00 +00:00
|
|
|
return {
|
|
|
|
...accUserPosts,
|
|
|
|
[post.author.name]: {
|
|
|
|
...user,
|
|
|
|
posts: [post],
|
|
|
|
indexed: {
|
|
|
|
profile,
|
|
|
|
original: indexed,
|
|
|
|
updated: [],
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
2018-06-30 23:07:32 +00:00
|
|
|
}), userPosts);
|
2018-05-05 00:27:15 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = getPostsWrap;
|