From 17cfdcd3a9640618b043eed06904f289fd18f767 Mon Sep 17 00:00:00 2001 From: Elena Baidakova Date: Wed, 19 Oct 2022 15:21:43 +0400 Subject: [PATCH] Updated feedback buttons url (#15655) closes TryGhost/Team#2080 - If the post was published and emailed the link leads the user to the post. - If the post was just emailed the link leads the user to the home page. --- .../lib/AudienceFeedbackService.js | 11 ++++- .../test/AudienceFeedbackService.test.js | 45 +++++++++++++++++++ ghost/audience-feedback/test/hello.test.js | 10 ----- .../services/audience-feedback/index.js | 2 + 4 files changed, 57 insertions(+), 11 deletions(-) create mode 100644 ghost/audience-feedback/test/AudienceFeedbackService.test.js delete mode 100644 ghost/audience-feedback/test/hello.test.js diff --git a/ghost/audience-feedback/lib/AudienceFeedbackService.js b/ghost/audience-feedback/lib/AudienceFeedbackService.js index fbd1da0f74..66240fd416 100644 --- a/ghost/audience-feedback/lib/AudienceFeedbackService.js +++ b/ghost/audience-feedback/lib/AudienceFeedbackService.js @@ -1,13 +1,17 @@ class AudienceFeedbackService { /** @type URL */ #baseURL; + /** @type {Object} */ + #urlService; /** * @param {object} deps * @param {object} deps.config * @param {URL} deps.config.baseURL + * @param {object} deps.urlService */ constructor(deps) { this.#baseURL = deps.config.baseURL; + this.#urlService = deps.urlService; } /** * @param {string} uuid @@ -15,7 +19,12 @@ class AudienceFeedbackService { * @param {0 | 1} score */ buildLink(uuid, postId, score) { - const url = new URL(this.#baseURL); + let postUrl = this.#urlService.getUrlByResourceId(postId, {absolute: true}); + + if (postUrl.match(/\/404\//)) { + postUrl = this.#baseURL; + } + const url = new URL(postUrl); url.searchParams.set('action', 'feedback'); url.searchParams.set('post', postId); url.searchParams.set('uuid', uuid); diff --git a/ghost/audience-feedback/test/AudienceFeedbackService.test.js b/ghost/audience-feedback/test/AudienceFeedbackService.test.js new file mode 100644 index 0000000000..fb88c4a825 --- /dev/null +++ b/ghost/audience-feedback/test/AudienceFeedbackService.test.js @@ -0,0 +1,45 @@ +const assert = require('assert'); +const {AudienceFeedbackService} = require('../index'); + +describe('audienceFeedbackService', function () { + it('exported', function () { + assert.equal(require('../index').AudienceFeedbackService, AudienceFeedbackService); + }); + + const mockData = { + uuid: '7b11de3c-dff9-4563-82ae-a281122d201d', + postId: '634fc3901e0a291855d8b135', + postTitle: 'somepost', + score: 1 + }; + + describe('build link', function () { + it('Can build link to post', async function () { + const instance = new AudienceFeedbackService({ + urlService: { + getUrlByResourceId: () => `https://localhost:2368/${mockData.postTitle}/` + }, + config: { + baseURL: new URL('https://localhost:2368') + } + }); + const link = instance.buildLink(mockData.uuid, mockData.postId, mockData.score); + const expectedLink = `https://localhost:2368/${mockData.postTitle}/?action=feedback&post=${mockData.postId}&uuid=${mockData.uuid}&score=${mockData.score}`; + assert.equal(link.href, expectedLink); + }); + + it('Can build link to home page if post wasn\'t published', async function () { + const instance = new AudienceFeedbackService({ + urlService: { + getUrlByResourceId: () => `https://localhost:2368/${mockData.postTitle}/404/` + }, + config: { + baseURL: new URL('https://localhost:2368') + } + }); + const link = instance.buildLink(mockData.uuid, mockData.postId, mockData.score); + const expectedLink = `https://localhost:2368/?action=feedback&post=${mockData.postId}&uuid=${mockData.uuid}&score=${mockData.score}`; + assert.equal(link.href, expectedLink); + }); + }); +}); diff --git a/ghost/audience-feedback/test/hello.test.js b/ghost/audience-feedback/test/hello.test.js deleted file mode 100644 index 85d69d1e08..0000000000 --- a/ghost/audience-feedback/test/hello.test.js +++ /dev/null @@ -1,10 +0,0 @@ -// Switch these lines once there are useful utils -// const testUtils = require('./utils'); -require('./utils'); - -describe('Hello world', function () { - it('Runs a test', function () { - // TODO: Write me! - 'hello'.should.eql('hello'); - }); -}); diff --git a/ghost/core/core/server/services/audience-feedback/index.js b/ghost/core/core/server/services/audience-feedback/index.js index 0eda9c0c75..39ea79bc17 100644 --- a/ghost/core/core/server/services/audience-feedback/index.js +++ b/ghost/core/core/server/services/audience-feedback/index.js @@ -1,4 +1,5 @@ const urlUtils = require('../../../shared/url-utils'); +const urlService = require('../../services/url'); const FeedbackRepository = require('./FeedbackRepository'); class AudienceFeedbackServiceWrapper { @@ -22,6 +23,7 @@ class AudienceFeedbackServiceWrapper { // Expose the service this.service = new AudienceFeedbackService({ + urlService, config: { baseURL: new URL(urlUtils.urlFor('home', true)) }