2022-12-15 20:22:54 +03:00
|
|
|
const debug = require('@tryghost/debug')('importer:revue');
|
|
|
|
const {slugify} = require('@tryghost/string');
|
|
|
|
const papaparse = require('papaparse');
|
|
|
|
const _ = require('lodash');
|
|
|
|
|
|
|
|
const JSONToHTML = require('../lib/json-to-html');
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Build posts out of the issue and item data
|
|
|
|
*
|
|
|
|
* @param {Object} revueData
|
|
|
|
* @return {Array}
|
|
|
|
*/
|
|
|
|
const fetchPostsFromData = (revueData) => {
|
|
|
|
const itemData = JSON.parse(revueData.items);
|
|
|
|
const issueData = papaparse.parse(revueData.issues, {
|
|
|
|
header: true,
|
|
|
|
skipEmptyLines: true,
|
|
|
|
transform(value, header) {
|
|
|
|
if (header === 'id') {
|
|
|
|
return parseInt(value);
|
|
|
|
}
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
const posts = [];
|
|
|
|
|
|
|
|
issueData.data.forEach((postMeta) => {
|
|
|
|
// Convert issues to posts
|
|
|
|
if (!postMeta.subject) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const revuePostID = postMeta.id;
|
2022-12-15 22:28:48 +03:00
|
|
|
let postHTML = JSONToHTML.cleanCsvHTML(postMeta.description);
|
2022-12-15 20:22:54 +03:00
|
|
|
|
|
|
|
const postItems = _.filter(itemData, {issue_id: revuePostID});
|
|
|
|
const sortedPostItems = _.sortBy(postItems, o => o.order);
|
|
|
|
|
|
|
|
if (postItems) {
|
|
|
|
const convertedItems = JSONToHTML.itemsToHtml(sortedPostItems);
|
2022-12-15 22:28:48 +03:00
|
|
|
postHTML = `${postHTML}${convertedItems}`;
|
2022-12-15 20:22:54 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
const postDate = JSONToHTML.getPostDate(postMeta);
|
2023-01-03 23:15:32 +03:00
|
|
|
const postSlug = slugify(postMeta.subject).slice(0, 190);
|
2022-12-15 20:22:54 +03:00
|
|
|
|
|
|
|
posts.push({
|
|
|
|
comment_id: revuePostID,
|
|
|
|
title: postMeta.subject,
|
2023-01-03 23:15:32 +03:00
|
|
|
slug: postSlug,
|
2022-12-15 20:22:54 +03:00
|
|
|
status: JSONToHTML.getPostStatus(postMeta),
|
|
|
|
visibility: 'public',
|
|
|
|
created_at: postDate,
|
|
|
|
published_at: postDate,
|
|
|
|
updated_at: postDate,
|
2022-12-15 22:28:48 +03:00
|
|
|
html: postHTML
|
2022-12-15 20:22:54 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
return posts;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param {*} revueData
|
|
|
|
*/
|
|
|
|
const buildSubscriberList = (revueData) => {
|
|
|
|
const subscribers = [];
|
|
|
|
|
|
|
|
const subscriberData = papaparse.parse(revueData.subscribers, {
|
|
|
|
header: true,
|
|
|
|
skipEmptyLines: true
|
|
|
|
});
|
|
|
|
|
|
|
|
subscriberData.data.forEach((subscriber) => {
|
|
|
|
subscribers.push({
|
|
|
|
email: subscriber.email,
|
|
|
|
name: `${subscriber.first_name} ${subscriber.last_name}`.trim(),
|
2022-12-15 23:29:08 +03:00
|
|
|
created_at: subscriber.created_at,
|
|
|
|
subscribed: true
|
2022-12-15 20:22:54 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
return subscribers;
|
|
|
|
};
|
|
|
|
|
|
|
|
const RevueImporter = {
|
|
|
|
type: 'revue',
|
|
|
|
preProcess: function (importData) {
|
|
|
|
debug('preProcess');
|
|
|
|
importData.preProcessedByRevue = true;
|
|
|
|
|
|
|
|
// TODO: this should really be in doImport
|
|
|
|
// No posts to prePprocess, return early
|
|
|
|
if (!importData?.revue?.revue?.issues) {
|
|
|
|
return importData;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This processed data goes to the data importer
|
|
|
|
importData.data = {
|
|
|
|
meta: {version: '5.0.0'},
|
|
|
|
data: {}
|
|
|
|
};
|
|
|
|
|
|
|
|
importData.data.data.posts = this.importPosts(importData.revue.revue);
|
|
|
|
|
|
|
|
// No subscribers to import, we're done
|
|
|
|
if (!importData?.revue?.revue?.subscribers) {
|
|
|
|
return importData;
|
|
|
|
}
|
|
|
|
|
|
|
|
importData.data.data.revue_subscribers = this.importSubscribers(importData.revue.revue);
|
|
|
|
|
|
|
|
return importData;
|
|
|
|
},
|
|
|
|
doImport: function (importData) {
|
|
|
|
debug('doImport');
|
|
|
|
|
|
|
|
return importData;
|
|
|
|
},
|
|
|
|
|
|
|
|
importPosts: fetchPostsFromData,
|
|
|
|
importSubscribers: buildSubscriberList
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = RevueImporter;
|