Skip to content

Commit

Permalink
fix(endpoint-posts): resolve item photo
Browse files Browse the repository at this point in the history
  • Loading branch information
paulrobertlloyd committed Apr 10, 2024
1 parent 4123bd0 commit 605a8d0
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 2 deletions.
4 changes: 2 additions & 2 deletions packages/endpoint-posts/lib/controllers/posts.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { checkScope } from "@indiekit/endpoint-micropub/lib/scope.js";
import { mf2tojf2 } from "@paulrobertlloyd/mf2tojf2";
import { endpoint } from "../endpoint.js";
import { statusTypes } from "../status-types.js";
import { getPostStatusBadges, getPostName } from "../utils.js";
import { getPostStatusBadges, getPostName, getPhotoUrl } from "../utils.js";

/**
* List published posts
Expand Down Expand Up @@ -39,7 +39,7 @@ export const postsController = async (request, response, next) => {
item.id = item.uid;
item.icon = item["post-type"];
item.locale = application.locale;
item.photo = item.photo ? item.photo[0] : false;
item.photo = getPhotoUrl(publication, item);
item.description = item.summary || item.content?.text;
item.title = getPostName(publication, item);
item.url = path.join(request.baseUrl, request.path, item.uid);
Expand Down
22 changes: 22 additions & 0 deletions packages/endpoint-posts/lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,28 @@ export const getLocationProperty = (values) => {
return sanitise(location);
};

/**
* Get photo URL
* @param {object} publication - Publication configuration
* @param {object} properties - JF2 properties
* @returns {object|boolean} Photo object, with URL
*/
export const getPhotoUrl = (publication, properties) => {
const photo = Array.isArray(properties.photo)
? properties.photo[0]
: properties.photo;

if (!photo) {
return false;
} else if (URL.canParse(photo.url)) {
return photo;
} else {
return {
url: new URL(photo.url, publication.me).href,
};
}
};

/**
* Get post status badges
* @param {object} post - Post
Expand Down
15 changes: 15 additions & 0 deletions packages/endpoint-posts/test/unit/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ import {
getGeoProperty,
getGeoValue,
getLocationProperty,
getPhotoUrl,
getPostName,
getPostStatusBadges,
getPostUrl,
getSyndicateToItems,
} from "../../lib/utils.js";

const publication = {
me: "https://website.example",
postTypes: {
article: {
name: "Journal entry",
Expand Down Expand Up @@ -193,6 +195,19 @@ describe("endpoint-posts/lib/utils", () => {
);
});

it("Gets photo URL", () => {
const photo = { url: "https://external.example/foo.jpg" };
const photos = [{ url: "/path/to/foo.jpg" }, { url: "/path/to/bar.jpg" }];

assert.equal(getPhotoUrl(publication, { name: "foo" }), false);
assert.deepEqual(getPhotoUrl(publication, { photo }), {
url: "https://external.example/foo.jpg",
});
assert.deepEqual(getPhotoUrl(publication, { photo: photos }), {
url: "https://website.example/path/to/foo.jpg",
});
});

it("Gets post name", () => {
const post = { name: "My favourite sandwich" };

Expand Down

0 comments on commit 605a8d0

Please sign in to comment.