From 0262083bb01fe35142abb04148f3e7eb1eab365a Mon Sep 17 00:00:00 2001 From: "Fabien \"egg\" O'Carroll" Date: Sun, 12 Mar 2023 23:06:17 +0700 Subject: [PATCH] Added getMentionReport method to MentionsAPI This will be used by the mentions-report-email package to generate the data used for the report emails --- .../lib/InMemoryMentionRepository.js | 11 +++++++ ghost/webmentions/lib/MentionsAPI.js | 32 +++++++++++++++++++ ghost/webmentions/test/MentionsAPI.test.js | 25 +++++++++++++++ 3 files changed, 68 insertions(+) diff --git a/ghost/webmentions/lib/InMemoryMentionRepository.js b/ghost/webmentions/lib/InMemoryMentionRepository.js index d3d8ac8777..b901ecde4b 100644 --- a/ghost/webmentions/lib/InMemoryMentionRepository.js +++ b/ghost/webmentions/lib/InMemoryMentionRepository.js @@ -115,4 +115,15 @@ module.exports = class InMemoryMentionRepository { } }; } + + async getAll(options) { + const page = await this.getPage({ + filter: options.filter, + order: options.order, + page: null, + limit: 'all' + }); + + return page.data; + } }; diff --git a/ghost/webmentions/lib/MentionsAPI.js b/ghost/webmentions/lib/MentionsAPI.js index d3e28aaa5f..65cb1f9dfc 100644 --- a/ghost/webmentions/lib/MentionsAPI.js +++ b/ghost/webmentions/lib/MentionsAPI.js @@ -37,10 +37,16 @@ const Mention = require('./Mention'); * @typedef {PaginatedOptions | NonPaginatedOptions} GetPageOptions */ +/** + * @typedef {object} GetAllOptions + * @prop {string} [filter] A valid NQL string + */ + /** * @typedef {object} IMentionRepository * @prop {(mention: Mention) => Promise} save * @prop {(options: GetPageOptions) => Promise>} getPage + * @prop {(options: GetAllOptions) => Promise} getAll * @prop {(source: URL, target: URL) => Promise} getBySourceAndTarget */ @@ -71,6 +77,13 @@ const Mention = require('./Mention'); * @prop {string} body */ +/** + * @typedef {object} MentionReport + * @prop {Date} startDate + * @prop {Date} endDate + * @prop {Mention[]} mentions + */ + /** * @typedef {object} IWebmentionMetadata * @prop {(url: URL) => Promise} fetch @@ -100,6 +113,25 @@ module.exports = class MentionsAPI { this.#webmentionMetadata = deps.webmentionMetadata; } + /** + * @param {Date} startDate + * @param {Date} endDate + * @returns {Promise} + */ + async getMentionReport(startDate, endDate) { + const mentions = await this.#repository.getAll({ + filter: `created_at:>${startDate.toISOString()}+created_at:<${endDate.toISOString()}` + }); + + const report = { + startDate: new Date(startDate), + endDate: new Date(endDate), + mentions + }; + + return report; + } + /** * @param {object} options * @returns {Promise>} diff --git a/ghost/webmentions/test/MentionsAPI.test.js b/ghost/webmentions/test/MentionsAPI.test.js index 71e1b21686..6d7df5467d 100644 --- a/ghost/webmentions/test/MentionsAPI.test.js +++ b/ghost/webmentions/test/MentionsAPI.test.js @@ -44,6 +44,31 @@ describe('MentionsAPI', function () { sinon.restore(); }); + it('Can generate a mentions report', async function () { + const repository = new InMemoryMentionRepository(); + const api = new MentionsAPI({ + repository, + routingService: mockRoutingService, + resourceService: mockResourceService, + webmentionMetadata: mockWebmentionMetadata + }); + + const mention = await api.processWebmention({ + source: new URL('https://source.com'), + target: new URL('https://target.com'), + payload: {} + }); + + assert(mention instanceof Mention); + + const now = new Date(); + + const report = await api.getMentionReport(new Date(0), new Date()); + + assert.deepEqual(report.startDate, new Date(0)); + assert.deepEqual(report.endDate, now); + }); + it('Can list paginated mentions', async function () { const repository = new InMemoryMentionRepository(); const api = new MentionsAPI({