Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions packages/endpoint-micropub/lib/jf2.js
Original file line number Diff line number Diff line change
Expand Up @@ -301,13 +301,24 @@ export const getSyndicateToProperty = (properties, syndicationTargets) => {
return;
}

// Converting mf2 to JF2 leaves this property in one of three shapes: an
// array for several targets, a string for one, and an empty *object* for
// none. Calling `.includes` on it directly threw a TypeError and failed the
// whole post for any client sending `"mp-syndicate-to": []`, which is what
// an unticked syndication list produces.
const syndicateTo = properties["mp-syndicate-to"];
const requested = Array.isArray(syndicateTo)
? syndicateTo
: typeof syndicateTo === "string"
? [syndicateTo]
: [];

const property = [];

for (const target of syndicationTargets) {
const { uid } = target.info;
const syndicateTo = properties["mp-syndicate-to"];

if (syndicateTo?.includes(uid)) {
if (requested.includes(uid)) {
property.push(uid);
}
}
Expand Down
27 changes: 27 additions & 0 deletions packages/endpoint-micropub/test/unit/jf2.js
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,33 @@ describe("endpoint-micropub/lib/jf2", () => {
assert.deepEqual(result, ["https://example.website/"]);
});

it("Doesn’t throw when `mp-syndicate-to` is not an array", () => {
// Converting mf2 to JF2 turns an empty array into an empty object, so a
// client that sends `"mp-syndicate-to": []` — an unticked syndication
// list — arrives here with a value that has no `.includes`
const syndicationTargets = [{ info: { uid: "https://example.website/" } }];

for (const value of [{}, 1, true]) {
const result = getSyndicateToProperty(
{ "mp-syndicate-to": value },
syndicationTargets,
);

assert.equal(result, undefined);
}
});

it("Adds syndication target given as a string", () => {
// A single target collapses to a string during mf2 to JF2 conversion
const syndicationTargets = [{ info: { uid: "https://example.website/" } }];
const result = getSyndicateToProperty(
{ "mp-syndicate-to": "https://example.website/" },
syndicationTargets,
);

assert.deepEqual(result, ["https://example.website/"]);
});

it("Doesn’t add unused syndication target", () => {
const properties = {};
const syndicationTargets = [
Expand Down