Skip to content

Commit 605a8d0

Browse files
fix(endpoint-posts): resolve item photo
1 parent 4123bd0 commit 605a8d0

File tree

3 files changed

+39
-2
lines changed

3 files changed

+39
-2
lines changed

packages/endpoint-posts/lib/controllers/posts.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { checkScope } from "@indiekit/endpoint-micropub/lib/scope.js";
33
import { mf2tojf2 } from "@paulrobertlloyd/mf2tojf2";
44
import { endpoint } from "../endpoint.js";
55
import { statusTypes } from "../status-types.js";
6-
import { getPostStatusBadges, getPostName } from "../utils.js";
6+
import { getPostStatusBadges, getPostName, getPhotoUrl } from "../utils.js";
77

88
/**
99
* List published posts
@@ -39,7 +39,7 @@ export const postsController = async (request, response, next) => {
3939
item.id = item.uid;
4040
item.icon = item["post-type"];
4141
item.locale = application.locale;
42-
item.photo = item.photo ? item.photo[0] : false;
42+
item.photo = getPhotoUrl(publication, item);
4343
item.description = item.summary || item.content?.text;
4444
item.title = getPostName(publication, item);
4545
item.url = path.join(request.baseUrl, request.path, item.uid);

packages/endpoint-posts/lib/utils.js

+22
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,28 @@ export const getLocationProperty = (values) => {
6464
return sanitise(location);
6565
};
6666

67+
/**
68+
* Get photo URL
69+
* @param {object} publication - Publication configuration
70+
* @param {object} properties - JF2 properties
71+
* @returns {object|boolean} Photo object, with URL
72+
*/
73+
export const getPhotoUrl = (publication, properties) => {
74+
const photo = Array.isArray(properties.photo)
75+
? properties.photo[0]
76+
: properties.photo;
77+
78+
if (!photo) {
79+
return false;
80+
} else if (URL.canParse(photo.url)) {
81+
return photo;
82+
} else {
83+
return {
84+
url: new URL(photo.url, publication.me).href,
85+
};
86+
}
87+
};
88+
6789
/**
6890
* Get post status badges
6991
* @param {object} post - Post

packages/endpoint-posts/test/unit/utils.js

+15
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@ import {
55
getGeoProperty,
66
getGeoValue,
77
getLocationProperty,
8+
getPhotoUrl,
89
getPostName,
910
getPostStatusBadges,
1011
getPostUrl,
1112
getSyndicateToItems,
1213
} from "../../lib/utils.js";
1314

1415
const publication = {
16+
me: "https://website.example",
1517
postTypes: {
1618
article: {
1719
name: "Journal entry",
@@ -193,6 +195,19 @@ describe("endpoint-posts/lib/utils", () => {
193195
);
194196
});
195197

198+
it("Gets photo URL", () => {
199+
const photo = { url: "https://external.example/foo.jpg" };
200+
const photos = [{ url: "/path/to/foo.jpg" }, { url: "/path/to/bar.jpg" }];
201+
202+
assert.equal(getPhotoUrl(publication, { name: "foo" }), false);
203+
assert.deepEqual(getPhotoUrl(publication, { photo }), {
204+
url: "https://external.example/foo.jpg",
205+
});
206+
assert.deepEqual(getPhotoUrl(publication, { photo: photos }), {
207+
url: "https://website.example/path/to/foo.jpg",
208+
});
209+
});
210+
196211
it("Gets post name", () => {
197212
const post = { name: "My favourite sandwich" };
198213

0 commit comments

Comments
 (0)