Accounting for indexed posts.

This commit is contained in:
DebaucheryLibrarian 2024-09-11 05:16:56 +02:00
parent 029351f228
commit 86002ef00b
4 changed files with 13 additions and 14 deletions

View File

@ -25,8 +25,8 @@ function curatePost(acc, post, user, index, processed, args) {
hash: hashPost(post),
};
if (user.indexed.find(entry => entry.id === post.id)) {
return { ...acc, indexed: { ...acc.indexed, [post.id]: curatedPost } };
if (user.indexed.original.find(entry => entry.id === post.id)) {
return { ...acc, indexedUpdated: [...acc.indexedUpdated, curatedPost] };
}
// cut-off at limit, but don't count posts requested directly by ID
@ -73,16 +73,16 @@ function curatePost(acc, post, user, index, processed, args) {
const curatePosts = (userPosts, args) => Object.values(userPosts).reduce((accPosts, user) => {
const processed = new Set();
const { posts, indexed } = user.posts.reduce((accUserPosts, post, index) =>
curatePost(accUserPosts, post, user, index, processed, args), { posts: [], indexed: {} });
const { posts, indexedUpdated } = user.posts.reduce((accUserPosts, post, index) =>
curatePost(accUserPosts, post, user, index, processed, args), { posts: [], indexedUpdated: [] });
const indexedLength = Object.keys(indexed).length;
if (indexedLength > 0) {
console.log('\x1b[33m%s\x1b[0m', `Ignoring ${indexedLength} indexed posts for '${user.name}'`);
if (indexedUpdated.length > 0) {
console.log('\x1b[33m%s\x1b[0m', `Ignoring ${indexedUpdated.length} indexed posts for '${user.name}'`);
}
return { ...accPosts, [user.name]: { ...user, posts } };
const indexedOriginal = user.indexed.original.filter(entry => !indexedUpdated.find(post => post.id === entry.id));
return { ...accPosts, [user.name]: { ...user, posts, indexed: { original: indexedOriginal, updated: indexedUpdated } } };
}, {});
module.exports = curatePosts;

View File

@ -5,7 +5,6 @@ const path = require('path');
const url = require('url');
const dateFns = require('date-fns');
const mime = require('mime-types');
const dotty = require('dotty');
function interpolate(pattern, user, post, item, strip = true, dateFormat = config.library.dateFormat) {
const vars = {

View File

@ -12,8 +12,8 @@ async function writeToIndex(posts, user) {
// Individual posts are wrapped in [] to get a YAML array value for each individual item, allowing them to be joined manually with a newline
// between each entry to improve human readability of the index while maintaining a valid YAML list
const oldEntries = user.indexed.map(entry => yaml.safeDump([entry]));
const newEntries = posts.map(post => yaml.safeDump([{
const originalEntries = user.indexed.original.map(entry => yaml.safeDump([entry]));
const newAndUpdatedEntries = posts.concat(user.indexed.updated).map(post => yaml.safeDump([{
id: post.id,
subreddit: post.subreddit,
permalink: post.permalink,
@ -24,7 +24,7 @@ async function writeToIndex(posts, user) {
title: post.title,
}]));
const entries = newEntries.concat(oldEntries).join('\n');
const entries = newAndUpdatedEntries.concat(originalEntries).join('\n');
return fs.writeFile(filename, entries);
}

View File

@ -74,7 +74,7 @@ function getUserPostsWrap(reddit, args) {
}
if (posts.length) {
return { ...user, posts, indexed };
return { ...user, posts, indexed: { original: indexed, updated: [] } };
}
return null;