2024-09-11 03:16:54 +00:00
|
|
|
'use strict';
|
|
|
|
|
2024-09-11 03:16:55 +00:00
|
|
|
const Promise = require('bluebird');
|
2024-09-11 03:16:56 +00:00
|
|
|
|
2024-09-11 03:16:56 +00:00
|
|
|
const getIndex = require('./getIndex.js');
|
2024-09-11 03:16:54 +00:00
|
|
|
const getArchivePostIds = require('../archives/getArchivePostIds.js');
|
|
|
|
const curateUser = require('../curate/user.js');
|
|
|
|
|
2024-09-11 03:16:56 +00:00
|
|
|
async function getUser(username, reddit) {
|
2024-09-11 03:16:55 +00:00
|
|
|
try {
|
|
|
|
const user = await reddit.getUser(username).fetch();
|
|
|
|
|
|
|
|
return curateUser(user);
|
2024-09-11 03:16:56 +00:00
|
|
|
} catch (error) {
|
2024-09-11 03:16:55 +00:00
|
|
|
console.log('\x1b[31m%s\x1b[0m', `Failed to fetch reddit user '${username}': ${error.message} (https://reddit.com/user/${username})`);
|
|
|
|
|
|
|
|
return {
|
|
|
|
name: username,
|
2024-09-11 03:16:56 +00:00
|
|
|
fallback: true,
|
2024-09-11 03:16:55 +00:00
|
|
|
};
|
2024-09-11 03:16:55 +00:00
|
|
|
}
|
2024-09-11 03:16:56 +00:00
|
|
|
}
|
2024-09-11 03:16:54 +00:00
|
|
|
|
2024-09-11 03:16:56 +00:00
|
|
|
async function getPosts(username, reddit, args) {
|
2024-09-11 03:16:55 +00:00
|
|
|
try {
|
2024-09-11 03:16:57 +00:00
|
|
|
const submissions = await reddit.getUser(username).getSubmissions({
|
2024-09-11 03:16:55 +00:00
|
|
|
sort: args.sort,
|
2024-09-11 03:16:56 +00:00
|
|
|
limit: Infinity,
|
2024-09-11 03:16:55 +00:00
|
|
|
});
|
2024-09-11 03:16:56 +00:00
|
|
|
|
2024-09-11 03:16:57 +00:00
|
|
|
return submissions;
|
2024-09-11 03:16:56 +00:00
|
|
|
} catch (error) {
|
2024-09-11 03:16:55 +00:00
|
|
|
console.log('\x1b[31m%s\x1b[0m', `Failed to fetch posts from reddit user '${username}': ${error.message} (https://reddit.com/user/${username})`);
|
|
|
|
|
|
|
|
return [];
|
2024-09-11 03:16:55 +00:00
|
|
|
}
|
2024-09-11 03:16:56 +00:00
|
|
|
}
|
2024-09-11 03:16:55 +00:00
|
|
|
|
2024-09-11 03:16:56 +00:00
|
|
|
async function getArchivedPosts(username, posts, reddit) {
|
|
|
|
const postIds = await getArchivePostIds(username, posts.map(post => post.id));
|
2024-09-11 03:16:55 +00:00
|
|
|
|
2024-09-11 03:16:56 +00:00
|
|
|
return Promise.all(postIds.map(postId => reddit.getSubmission(postId).fetch()));
|
|
|
|
}
|
2024-09-11 03:16:55 +00:00
|
|
|
|
2024-09-11 03:16:56 +00:00
|
|
|
function getUserPostsWrap(reddit, args) {
|
2024-09-11 03:16:56 +00:00
|
|
|
return async function getUserPosts(usernames) {
|
|
|
|
const users = await Promise.map(usernames, async (username) => {
|
2024-09-11 03:16:56 +00:00
|
|
|
const [user, posts] = await Promise.all([
|
|
|
|
getUser(username, reddit),
|
|
|
|
getPosts(username, reddit, args),
|
|
|
|
]);
|
2024-09-11 03:16:55 +00:00
|
|
|
|
2024-09-11 03:16:56 +00:00
|
|
|
const { profile, posts: indexed } = await getIndex(user);
|
2024-09-11 03:16:56 +00:00
|
|
|
|
2024-09-11 03:16:56 +00:00
|
|
|
if (args.archives) {
|
|
|
|
posts.push(...await getArchivedPosts(username, posts, reddit));
|
|
|
|
}
|
2024-09-11 03:16:55 +00:00
|
|
|
|
2024-09-11 03:16:56 +00:00
|
|
|
if (posts.length) {
|
2024-09-11 03:16:56 +00:00
|
|
|
return { ...user, posts, indexed: { profile, original: indexed, updated: [] } };
|
2024-09-11 03:16:56 +00:00
|
|
|
}
|
2024-09-11 03:16:55 +00:00
|
|
|
|
2024-09-11 03:16:56 +00:00
|
|
|
return null;
|
|
|
|
});
|
|
|
|
|
|
|
|
return users.reduce((userPosts, user) => (user ? { ...userPosts, [user.name]: user } : userPosts), {});
|
2024-09-11 03:16:56 +00:00
|
|
|
};
|
|
|
|
}
|
2024-09-11 03:16:55 +00:00
|
|
|
|
2024-09-11 03:16:54 +00:00
|
|
|
module.exports = getUserPostsWrap;
|