2023-04-17 16:14:33 +03:00
|
|
|
/**
|
|
|
|
* @typedef {object} PostLike
|
|
|
|
* @property {string} id
|
|
|
|
* @property {string} lexical
|
2023-04-17 18:14:43 +03:00
|
|
|
* @property {string} html
|
2023-04-17 16:14:33 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @typedef {object} Revision
|
|
|
|
* @property {string} post_id
|
|
|
|
* @property {string} lexical
|
|
|
|
* @property {number} created_at_ts
|
|
|
|
*/
|
|
|
|
|
|
|
|
class PostRevisions {
|
|
|
|
/**
|
|
|
|
* @param {object} deps
|
|
|
|
* @param {object} deps.config
|
|
|
|
* @param {number} deps.config.max_revisions
|
|
|
|
*/
|
|
|
|
constructor(deps) {
|
|
|
|
this.config = deps.config;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {PostLike} previous
|
|
|
|
* @param {PostLike} current
|
|
|
|
* @param {Revision[]} revisions
|
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
|
|
|
shouldGenerateRevision(previous, current, revisions) {
|
|
|
|
if (!previous) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (revisions.length === 0) {
|
|
|
|
return true;
|
|
|
|
}
|
2023-04-17 18:14:43 +03:00
|
|
|
return previous.html !== current.html;
|
2023-04-17 16:14:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {PostLike} previous
|
|
|
|
* @param {PostLike} current
|
|
|
|
* @param {Revision[]} revisions
|
|
|
|
* @returns {Promise<Revision[]>}
|
|
|
|
*/
|
|
|
|
async getRevisions(previous, current, revisions) {
|
|
|
|
if (!this.shouldGenerateRevision(previous, current, revisions)) {
|
|
|
|
return revisions;
|
|
|
|
}
|
|
|
|
|
|
|
|
const currentRevision = this.convertPostLikeToRevision(current);
|
|
|
|
|
|
|
|
if (revisions.length === 0) {
|
|
|
|
return [
|
|
|
|
currentRevision
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
return [...revisions, currentRevision].slice(-this.config.max_revisions);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {PostLike} input
|
|
|
|
* @returns {Revision}
|
|
|
|
*/
|
|
|
|
convertPostLikeToRevision(input, offset = 0) {
|
|
|
|
return {
|
|
|
|
post_id: input.id,
|
|
|
|
lexical: input.lexical,
|
|
|
|
created_at_ts: Date.now() - offset
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = PostRevisions;
|