Added page resource type to Mentions (#16302)

closes https://github.com/TryGhost/Team/issues/2572

The mentions browse output previously only showed resource info if the
mentioned resource type was a `post`.
Additionally, the `resource_type` column basically defaulted to `post`
regardless whether it was a page or in fact a post.

With this change we now have `resource_type` wired in to correctly
determine if the mentioned url was a page or a post.
This commit is contained in:
Ronald Langeveld 2023-02-21 13:02:47 +08:00 committed by GitHub
parent 066db4c46f
commit ba0c99d6fb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 27 additions and 6 deletions

View File

@ -49,6 +49,7 @@ module.exports = class BookshelfMentionRepository {
timestamp: model.get('created_at'),
payload,
resourceId: model.get('resource_id'),
resourceType: model.get('resource_type'),
sourceTitle: model.get('source_title'),
sourceSiteTitle: model.get('source_site_title'),
sourceAuthor: model.get('source_author'),
@ -106,7 +107,7 @@ module.exports = class BookshelfMentionRepository {
source_favicon: mention.sourceFavicon?.href,
target: mention.target.href,
resource_id: mention.resourceId?.toHexString(),
resource_type: mention.resourceId ? 'post' : null,
resource_type: mention.resourceType,
payload: mention.payload ? JSON.stringify(mention.payload) : null,
deleted: Mention.isDeleted(mention),
verified: mention.verified

View File

@ -37,6 +37,12 @@ module.exports = class ResourceService {
id: ObjectID.createFromHexString(resource.data.id)
};
}
if (resource?.config?.type === 'pages') {
return {
type: 'page',
id: ObjectID.createFromHexString(resource.data.id)
};
}
return {
type: null,
id: null

View File

@ -70,17 +70,15 @@ module.exports = {
if (!id) {
return null;
}
const post = await models.Post.findOne({id: id.toHexString()});
if (!post) {
return null;
}
return {
id: id,
name: post.get('title'),
type: 'post'
type: post.get('type')
};
}
}

View File

@ -58,6 +58,12 @@ module.exports = class Mention {
return this.#resourceId;
}
/** @type {string | null} */
#resourceType;
get resourceType() {
return this.#resourceType;
}
/** @type {string} */
#sourceTitle;
get sourceTitle() {
@ -147,6 +153,7 @@ module.exports = class Mention {
timestamp: this.timestamp,
payload: this.payload,
resourceId: this.resourceId,
resourceType: this.resourceType,
sourceTitle: this.sourceTitle,
sourceSiteTitle: this.sourceSiteTitle,
sourceAuthor: this.sourceAuthor,
@ -165,6 +172,7 @@ module.exports = class Mention {
this.#timestamp = data.timestamp;
this.#payload = data.payload;
this.#resourceId = data.resourceId;
this.#resourceType = data.resourceType;
this.#verified = data.verified;
}
@ -237,6 +245,12 @@ module.exports = class Mention {
}
}
/** @type {string | null} */
let resourceType = null;
if (data.resourceType) {
resourceType = data.resourceType;
}
const mention = new Mention({
id,
source,
@ -244,6 +258,7 @@ module.exports = class Mention {
timestamp,
payload,
resourceId,
resourceType,
verified
});

View File

@ -160,7 +160,6 @@ module.exports = class MentionsAPI {
}
const resourceInfo = await this.#resourceService.getByURL(webmention.target);
let metadata;
try {
metadata = await this.#webmentionMetadata.fetch(webmention.source);
@ -187,7 +186,8 @@ module.exports = class MentionsAPI {
target: webmention.target,
timestamp: new Date(),
payload: webmention.payload,
resourceId: resourceInfo.type === 'post' ? resourceInfo.id : null,
resourceId: resourceInfo.id ? resourceInfo.id.toHexString() : null,
resourceType: resourceInfo.type,
sourceTitle: metadata.title,
sourceSiteTitle: metadata.siteTitle,
sourceAuthor: metadata.author,

View File

@ -21,6 +21,7 @@ describe('Mention', function () {
'timestamp',
'payload',
'resourceId',
'resourceType',
'sourceTitle',
'sourceSiteTitle',
'sourceAuthor',