Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added links API #15446

Merged
merged 10 commits into from
Sep 22, 2022
Merged

Added links API #15446

merged 10 commits into from
Sep 22, 2022

Conversation

allouis
Copy link
Contributor

@allouis allouis commented Sep 20, 2022

This expose the /links endpoint on the Admin API, which is filterable by Post ID.

We also add in memory repository implementations, just because they exist locally. I don't think we should necessarily merge them with this PR, but it is what I was building this API against initially.

Missing tests & some small bug fixes


return {
data: links,
meta: {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure about hardcoding this here, should maybe come from the getLinks method?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lets keep it in the endpoint implementation for now. The getLinks method shouldn't really know the endpoint structure I think

@codecov
Copy link

codecov bot commented Sep 20, 2022

Codecov Report

Base: 53.33% // Head: 53.28% // Decreases project coverage by -0.04% ⚠️

Coverage data is based on head (6aa1764) compared to base (98fc808).
Patch coverage: 28.57% of modified lines in pull request are covered.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #15446      +/-   ##
==========================================
- Coverage   53.33%   53.28%   -0.05%     
==========================================
  Files        1418     1419       +1     
  Lines       89465    89662     +197     
  Branches     9650     9656       +6     
==========================================
+ Hits        47713    47774      +61     
- Misses      40797    40937     +140     
+ Partials      955      951       -4     
Impacted Files Coverage Δ
ghost/admin/app/controllers/posts/analytics.js 0.00% <0.00%> (ø)
...ervices/link-redirection/LinkRedirectRepository.js 0.00% <0.00%> (ø)
...ore/core/server/services/link-redirection/index.js 0.00% <0.00%> (ø)
ghost/core/core/server/models/link-redirect.js 24.61% <9.37%> (-14.78%) ⬇️
.../core/server/services/link-click-tracking/index.js 21.42% <11.11%> (-1.43%) ⬇️
...ervices/link-click-tracking/LinkClickRepository.js 39.28% <22.22%> (-9.44%) ⬇️
...services/link-click-tracking/PostLinkRepository.js 50.00% <37.14%> (-17.86%) ⬇️
ghost/link-tracking/lib/FullPostLink.js 61.11% <61.11%> (ø)
...host/link-tracking/lib/LinkClickTrackingService.js 59.84% <77.77%> (+2.83%) ⬆️
ghost/link-redirects/lib/LinkRedirectsService.js 55.55% <100.00%> (+0.49%) ⬆️
... and 5 more

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report at Codecov.
📢 Do you have feedback about the report comment? Let us know in this issue.

for (const model of collection.models) {
result.push(new LinkRedirect({
id: model.id,
from: new URL(model.get('from'), this.#urlUtils.getSiteUrl()),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to correctly work with subdirectories, we need to trim the leading / from the from property

result.push(new LinkRedirect({
id: model.id,
from: new URL(model.get('from'), this.#urlUtils.getSiteUrl()),
to: new URL(this.#urlUtils.transformReadyToAbsolute(model.get('to')))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

transformReadyToAbsolute has moved to the model layer for now because the activity feed also needs to return these. Ideally the activity feed would also use the repository pattern, but with all the relations that would become difficult (because we need click event models, member models, link models and post models) unless we just ignore the whole bookshelf relations and start wiring up the relations manually using repositories. Then do a call to the LinkClickRepository to get the 'events' (and where you need to pass in a repository for members, links and posts), which returns a different kind of object that includes the member, link and posts. Curious to know your opinion on this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah cool, good to know, thanks!

The plan is to still keep all of the DB code in the models for now, so that everything is in one place for pulling out/improving/replacing in future. I think the repository pattern serves us best in new code so we can easily stub and mock the data layer in development, as well as typing all of our code, we don't need to update the activity feed stuff just yet I don't think :)

Comment on lines 75 to 102
const postLinks = await this.#postLinkRepository.getAll({
filter: options.filter
});

if (postLinks.length === 0) {
return data;
}

const links = await this.#linkRedirectService.getAll({
filter: `id:[${postLinks.map(x => x.link_id.toHexString())}]`
});

for (const link of links) {
const events = await this.#linkClickRepository.getAll({
filter: `link_id:[${link.link_id.toHexString()}]`
});

const result = {
id: link.link_id.toHexString(),
url: link.to.toString(),
post_id: postLinks.find((postLink) => {
return postLink.link_id.equals(link.link_id);
}).post_id.toHexString(),
click_events: events
};

data.push(result);
}
Copy link
Contributor

@SimonBackx SimonBackx Sep 22, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO this is where the repository pattern is lacking. We have to do this.#linkRedirectService.getAll, but this is returning the same database models in the implementation as this.#postLinkRepository.getAll. And we are fetching all the events of all the links in a loop. I'm not sure why we need to return all the event when we are only going to display the total count.

@SimonBackx SimonBackx marked this pull request as ready for review September 22, 2022 11:23
@SimonBackx
Copy link
Contributor

SimonBackx commented Sep 22, 2022

I moved the in memory repositories to a different PR for now: #15455

@SimonBackx SimonBackx changed the title [WIP] Links API Added links API Sep 22, 2022
@SimonBackx SimonBackx merged commit 5fcf509 into TryGhost:main Sep 22, 2022
@allouis allouis deleted the link-redirects-and-tracking branch September 26, 2022 10:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants