2023-01-23 15:47:10 +03:00
|
|
|
const TableImporter = require('./base');
|
|
|
|
const {faker} = require('@faker-js/faker');
|
|
|
|
const generateEvents = require('../utils/event-generator');
|
|
|
|
const {luck} = require('../utils/random');
|
|
|
|
const dateToDatabaseString = require('../utils/database-date');
|
|
|
|
|
|
|
|
class WebMentionsImporter extends TableImporter {
|
2023-02-16 15:11:00 +03:00
|
|
|
static table = 'mentions';
|
|
|
|
|
2023-01-23 15:47:10 +03:00
|
|
|
constructor(knex, {baseUrl}) {
|
2023-02-16 15:11:00 +03:00
|
|
|
super(WebMentionsImporter.table, knex);
|
2023-01-23 15:47:10 +03:00
|
|
|
|
|
|
|
this.baseUrl = baseUrl;
|
|
|
|
}
|
|
|
|
|
|
|
|
setImportOptions({model, amount}) {
|
|
|
|
this.model = model;
|
|
|
|
|
|
|
|
// Most web mentions published soon after publication date
|
|
|
|
const startDate = new Date(this.model.published_at);
|
|
|
|
// End date should be either 1 year after post is published, or current date, whichever is first
|
|
|
|
const endDate = new Date(Math.min(new Date().valueOf(), new Date(startDate).setFullYear(startDate.getFullYear() + 1)));
|
|
|
|
this.timestamps = generateEvents({
|
|
|
|
shape: 'ease-out',
|
|
|
|
trend: 'negative',
|
|
|
|
total: amount,
|
|
|
|
startTime: startDate,
|
|
|
|
endTime: endDate
|
|
|
|
}).sort();
|
|
|
|
}
|
|
|
|
|
|
|
|
generate() {
|
|
|
|
if (luck(50)) {
|
|
|
|
// 50/50 chance of having a web mention
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
const id = faker.database.mongodbObjectId();
|
|
|
|
const timestamp = this.timestamps.shift();
|
|
|
|
|
|
|
|
const author = `${faker.name.fullName()}`;
|
|
|
|
return {
|
|
|
|
id,
|
|
|
|
source: `${faker.internet.url()}/${faker.helpers.slugify(`${faker.word.adjective()} ${faker.word.noun()}`).toLowerCase()}`,
|
|
|
|
source_title: faker.lorem.sentence(5),
|
|
|
|
source_site_title: `${author}'s ${faker.word.noun()}`,
|
|
|
|
source_excerpt: faker.lorem.paragraph(),
|
|
|
|
source_author: author,
|
2023-02-13 16:16:24 +03:00
|
|
|
source_featured_image: `https://api.dicebear.com/5.x/shapes/png?size=256&seed=${id}`,
|
|
|
|
source_favicon: `https://api.dicebear.com/5.x/bottts/png?size=32&seed=${id}`,
|
2023-01-23 15:47:10 +03:00
|
|
|
target: `${this.baseUrl}${this.model.slug}/`,
|
|
|
|
resource_id: this.model.id,
|
|
|
|
resource_type: 'post', // TODO: Randomise resource type - should also include pages
|
|
|
|
created_at: dateToDatabaseString(timestamp),
|
|
|
|
payload: JSON.stringify({
|
|
|
|
// TODO: Add some random payload
|
2023-02-09 13:29:13 +03:00
|
|
|
}),
|
|
|
|
deleted: Math.floor(Math.random() * 2) ? true : false
|
2023-01-23 15:47:10 +03:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = WebMentionsImporter;
|