ripunzel/src/save/writeToIndex.js

77 lines
2.2 KiB
JavaScript

'use strict';
const config = require('config');
const yaml = require('js-yaml');
const interpolate = require('../interpolate');
// const textToStream = require('./textToStream');
const save = require('./save');
const logger = require('../logger')(__filename);
function curatePosts(newPosts, indexedPosts) {
// console.log(indexedPosts, 'new');
const newIds = new Set(newPosts.map((post) => post.id));
const uniqueIndexedPosts = indexedPosts.filter((post) => !newIds.has(post.id));
const combinedPosts = newPosts.concat(uniqueIndexedPosts);
// console.log(combinedPosts);
return combinedPosts;
}
async function writeToIndex(posts, profilePaths, user, args) {
const filepath = interpolate(config.library.index.file, null, null, null, null, user, false);
const now = new Date();
const newAndUpdatedEntries = posts.concat(user.indexed.updated, args.indexIgnored ? user.indexed.ignored : []).map((post) => {
const entryPost = {
id: post.id,
subreddit: post.subreddit,
permalink: post.permalink,
url: post.url,
hostId: post.host.id,
date: post.datetime,
indexed: now,
score: post.score,
title: post.title,
hash: post.hash,
phash: post.phash,
content: post.content,
};
if (post.previewFallback) {
entryPost.preview = true;
}
return entryPost;
});
const data = {
updated: new Date(),
profile: {
image: profilePaths.image,
description: profilePaths.description,
},
posts: curatePosts(newAndUpdatedEntries, user.indexed.original),
};
if (!data.profile.image && !data.profile.description && !data.posts.length) {
return false;
}
try {
const yamlIndex = yaml.safeDump(data, { skipInvalid: true });
const saved = await save(filepath, Buffer.from(yamlIndex, 'utf8'));
logger.info(`Saved index with ${posts.length} new posts for ${user.name}`);
return saved;
} catch (error) {
logger.error(`Could not save index for ${user.name}: ${error.message}`);
return null;
}
}
module.exports = writeToIndex;