ripunzel/src/save/writeToIndex.js

48 lines
1.2 KiB
JavaScript

'use strict';
const config = require('config');
const fs = require('fs-extra');
const yaml = require('js-yaml');
const interpolate = require('../interpolate.js');
async function writeToIndex(posts, profilePaths, user) {
const filename = interpolate(config.library.index.file, user, null, false);
const now = new Date();
const newAndUpdatedEntries = posts.concat(user.indexed.updated).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,
title: post.title,
};
if (post.previewFallback) {
entryPost.preview = true;
}
return entryPost;
});
const data = {
profile: {
image: profilePaths.image,
description: profilePaths.description,
},
posts: newAndUpdatedEntries.concat(user.indexed.original),
};
if (!data.profile.image && !data.profile.description && !data.posts.length) {
return;
}
return fs.writeFile(filename, yaml.safeDump(data));
}
module.exports = writeToIndex;