mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-04-08 02:52:39 -05:00
Added post stats service to return total posts in Explore endpoint
no issue - The explore endpoint needs to expose the total amount of published posts - To be more consistent, this PR creates a PostStats class which is exposed as `stats` method within the PostService; just like it's done with the MemberService - Moved existing method to return the date of the most recently published post into the stats service - Updated the explore service test to reflect the new return property
This commit is contained in:
parent
79d8e64e9a
commit
be45d4ebcf
6 changed files with 52 additions and 22 deletions
|
@ -46,11 +46,13 @@ module.exports = class ExploreService {
|
|||
}
|
||||
};
|
||||
|
||||
const mostRecentlyPublishedPost = await this.PostsService.getMostRecentlyPublishedPost();
|
||||
exploreProperties.most_recently_published_at = mostRecentlyPublishedPost?.get('published_at') || null;
|
||||
const mostRecentlyPublishedPost = await this.PostsService.stats.getMostRecentlyPublishedPostDate();
|
||||
const totalPostsPublished = await this.PostsService.stats.getTotalPostsPublished();
|
||||
exploreProperties.most_recently_published_at = mostRecentlyPublishedPost ?? null;
|
||||
exploreProperties.total_posts_published = totalPostsPublished ?? null;
|
||||
|
||||
const owner = await this.UserModel.findOne({role: 'Owner', status: 'all'});
|
||||
exploreProperties.owner_email = owner?.get('email') || null;
|
||||
exploreProperties.owner_email = owner?.get('email') ?? null;
|
||||
|
||||
return exploreProperties;
|
||||
}
|
||||
|
|
|
@ -8,11 +8,12 @@ const messages = {
|
|||
};
|
||||
|
||||
class PostsService {
|
||||
constructor({mega, urlUtils, models, isSet}) {
|
||||
constructor({mega, urlUtils, models, isSet, stats}) {
|
||||
this.mega = mega;
|
||||
this.urlUtils = urlUtils;
|
||||
this.models = models;
|
||||
this.isSet = isSet;
|
||||
this.stats = stats;
|
||||
}
|
||||
|
||||
async editPost(frame) {
|
||||
|
@ -76,20 +77,6 @@ class PostsService {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the most recently `published_at` post that was published or sent
|
||||
* via email
|
||||
*/
|
||||
async getMostRecentlyPublishedPost() {
|
||||
const recentlyPublishedPost = await this.models.Post.findPage({
|
||||
status: ['published', 'sent'],
|
||||
order: 'published_at DESC',
|
||||
limit: 1
|
||||
});
|
||||
|
||||
return recentlyPublishedPost?.data[0];
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates if the email should be tried to be sent out
|
||||
* @private
|
||||
|
@ -135,12 +122,16 @@ const getPostServiceInstance = () => {
|
|||
const {mega} = require('../mega');
|
||||
const labs = require('../../../shared/labs');
|
||||
const models = require('../../models');
|
||||
const PostStats = require('./stats/post-stats');
|
||||
|
||||
const postStats = new PostStats();
|
||||
|
||||
return new PostsService({
|
||||
mega: mega,
|
||||
urlUtils: urlUtils,
|
||||
models: models,
|
||||
isSet: labs.isSet.bind(labs)
|
||||
isSet: labs.isSet.bind(labs),
|
||||
stats: postStats
|
||||
});
|
||||
};
|
||||
|
||||
|
|
35
ghost/core/core/server/services/posts/stats/post-stats.js
Normal file
35
ghost/core/core/server/services/posts/stats/post-stats.js
Normal file
|
@ -0,0 +1,35 @@
|
|||
const db = require('../../../data/db');
|
||||
class PostStats {
|
||||
#db;
|
||||
|
||||
constructor() {
|
||||
this.#db = db;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the most recently `published_at` post that was published or sent
|
||||
* via email
|
||||
*/
|
||||
async getMostRecentlyPublishedPostDate() {
|
||||
const result = await this.#db.knex.select('published_at')
|
||||
.from('posts')
|
||||
.whereIn('status', ['sent', 'published'])
|
||||
.orderBy('published_at', 'desc')
|
||||
.limit(1);
|
||||
|
||||
return result?.[0]?.published_at ? new Date(result?.[0]?.published_at) : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetches count of all published posts
|
||||
*/
|
||||
async getTotalPostsPublished() {
|
||||
const [result] = await this.#db.knex('posts')
|
||||
.whereIn('status', ['sent', 'published'])
|
||||
.count('id', {as: 'total'});
|
||||
|
||||
return result.total;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = PostStats;
|
|
@ -40,6 +40,7 @@ Object {
|
|||
"livemode": false,
|
||||
},
|
||||
"total_members": 8,
|
||||
"total_posts_published": Any<Number>,
|
||||
"version": StringMatching /\\\\d\\+\\\\\\.\\\\d\\+\\\\\\.\\\\d\\+/,
|
||||
},
|
||||
}
|
||||
|
|
|
@ -1,12 +1,12 @@
|
|||
const {agentProvider, fixtureManager, matchers} = require('../../utils/e2e-framework');
|
||||
const {anyEtag, anyISODate, anyISODateTime, anyStringNumber, stringMatching} = matchers;
|
||||
const {anyEtag, anyISODate, anyISODateTime, anyStringNumber, stringMatching, anyNumber} = matchers;
|
||||
|
||||
describe('Explore API', function () {
|
||||
let agent;
|
||||
|
||||
before(async function () {
|
||||
agent = await agentProvider.getAdminAPIAgent();
|
||||
await fixtureManager.init('members');
|
||||
await fixtureManager.init('posts', 'members');
|
||||
await agent.loginAsOwner();
|
||||
});
|
||||
|
||||
|
@ -18,6 +18,7 @@ describe('Explore API', function () {
|
|||
.matchBodySnapshot({
|
||||
explore: {
|
||||
most_recently_published_at: anyISODateTime,
|
||||
total_posts_published: anyNumber,
|
||||
mrr_stats: {
|
||||
data: [{
|
||||
date: anyISODate
|
||||
|
|
|
@ -2472,4 +2472,4 @@ Object {
|
|||
|
||||
<script defer src=\\"https://cdn.jsdelivr.net/npm/@tryghost/sodo-search@~1.0/umd/sodo-search.min.js\\" data-key=\\"xyz\\" data-styles=\\"https://cdn.jsdelivr.net/npm/@tryghost/sodo-search@~1.0/umd/main.css\\" data-sodo-search=\\"http://localhost:65530/\\" crossorigin=\\"anonymous\\"></script>",
|
||||
}
|
||||
`;
|
||||
`;
|
Loading…
Add table
Reference in a new issue