2022-10-26 19:55:08 +03:00
|
|
|
const {faker} = require('@faker-js/faker');
|
|
|
|
const {slugify} = require('@tryghost/string');
|
|
|
|
const {luck} = require('../utils/random');
|
2023-08-02 16:43:26 +03:00
|
|
|
const TableImporter = require('./TableImporter');
|
2022-11-03 14:08:03 +03:00
|
|
|
const dateToDatabaseString = require('../utils/database-date');
|
2022-10-26 19:55:08 +03:00
|
|
|
|
|
|
|
class PostsImporter extends TableImporter {
|
2023-02-16 15:11:00 +03:00
|
|
|
static table = 'posts';
|
2023-08-02 16:43:26 +03:00
|
|
|
static dependencies = ['newsletters'];
|
|
|
|
defaultQuantity = faker.datatype.number({
|
|
|
|
min: 80,
|
|
|
|
max: 120
|
|
|
|
});
|
2023-02-16 15:11:00 +03:00
|
|
|
|
2023-08-02 16:43:26 +03:00
|
|
|
type = 'post';
|
2022-10-26 19:55:08 +03:00
|
|
|
|
2023-08-02 16:43:26 +03:00
|
|
|
constructor(knex, transaction) {
|
|
|
|
super(PostsImporter.table, knex, transaction);
|
2023-02-17 19:01:50 +03:00
|
|
|
}
|
|
|
|
|
2023-08-02 16:43:26 +03:00
|
|
|
async import(quantity = this.defaultQuantity) {
|
2023-08-04 15:07:20 +03:00
|
|
|
this.newsletters = await this.transaction.select('id').from('newsletters').orderBy('sort_order');
|
2023-08-02 16:43:26 +03:00
|
|
|
|
|
|
|
await super.import(quantity);
|
2022-10-26 19:55:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
generate() {
|
|
|
|
const title = faker.lorem.sentence();
|
|
|
|
const content = faker.lorem.paragraphs(faker.datatype.number({
|
|
|
|
min: 3,
|
|
|
|
max: 10
|
|
|
|
})).split('\n');
|
|
|
|
const twoYearsAgo = new Date();
|
|
|
|
twoYearsAgo.setFullYear(twoYearsAgo.getFullYear() - 2);
|
2023-02-17 18:47:34 +03:00
|
|
|
const twoWeeksFromNow = new Date();
|
|
|
|
twoWeeksFromNow.setDate(twoWeeksFromNow.getDate() + 14);
|
|
|
|
const timestamp = faker.date.between(twoYearsAgo, twoWeeksFromNow);
|
|
|
|
const currentTime = new Date();
|
|
|
|
|
|
|
|
let status = 'published';
|
|
|
|
if (timestamp > currentTime) {
|
|
|
|
status = 'scheduled';
|
|
|
|
}
|
|
|
|
if (luck(5)) {
|
|
|
|
status = 'draft';
|
|
|
|
}
|
2023-02-17 19:01:50 +03:00
|
|
|
if (this.type === 'page') {
|
|
|
|
status = 'published';
|
|
|
|
}
|
2023-02-17 18:47:34 +03:00
|
|
|
|
2023-03-16 00:50:15 +03:00
|
|
|
const visibility = luck(85) ? 'paid' : luck(10) ? 'members' : 'public';
|
2023-02-17 18:47:34 +03:00
|
|
|
|
2022-10-26 19:55:08 +03:00
|
|
|
return {
|
|
|
|
id: faker.database.mongodbObjectId(),
|
2022-11-03 14:08:03 +03:00
|
|
|
created_at: dateToDatabaseString(timestamp),
|
2023-02-17 19:01:50 +03:00
|
|
|
created_by: '1',
|
2022-11-03 14:08:03 +03:00
|
|
|
updated_at: dateToDatabaseString(timestamp),
|
2023-02-17 18:47:34 +03:00
|
|
|
published_at: status === 'published' ? dateToDatabaseString(faker.date.soon(5, timestamp)) : null,
|
2022-10-26 19:55:08 +03:00
|
|
|
uuid: faker.datatype.uuid(),
|
|
|
|
title: title,
|
2023-02-17 19:01:50 +03:00
|
|
|
type: this.type,
|
2022-10-26 19:55:08 +03:00
|
|
|
slug: `${slugify(title)}-${faker.random.numeric(3)}`,
|
2023-02-17 18:47:34 +03:00
|
|
|
status,
|
|
|
|
visibility,
|
2022-10-26 19:55:08 +03:00
|
|
|
mobiledoc: JSON.stringify({
|
|
|
|
version: '0.3.1',
|
|
|
|
atoms: [],
|
|
|
|
cards: [],
|
|
|
|
markups: [['em']],
|
|
|
|
sections: content.map(paragraph => [
|
|
|
|
1,
|
|
|
|
'p',
|
|
|
|
[
|
|
|
|
[
|
|
|
|
0,
|
|
|
|
[],
|
|
|
|
0,
|
|
|
|
paragraph
|
|
|
|
]
|
|
|
|
]
|
|
|
|
])
|
|
|
|
}),
|
|
|
|
html: content.map(paragraph => `<p>${paragraph}</p>`).join(''),
|
|
|
|
email_recipient_filter: 'all',
|
2023-03-16 00:50:15 +03:00
|
|
|
newsletter_id: this.type === 'post' && status === 'published' && luck(90) ? (visibility === 'paid' ? this.newsletters[0].id : this.newsletters[1].id) : null
|
2022-10-26 19:55:08 +03:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = PostsImporter;
|