Skip to content

Commit

Permalink
refactor(syndicator-mastodon): use getCanonicalUrl from util package
Browse files Browse the repository at this point in the history
  • Loading branch information
paulrobertlloyd committed Jul 4, 2023
1 parent fa6119c commit d8c8688
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 38 deletions.
6 changes: 3 additions & 3 deletions packages/syndicator-mastodon/lib/mastodon.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/* eslint-disable camelcase */
import { isSameOrigin } from "@indiekit/util";
import { getCanonicalUrl, isSameOrigin } from "@indiekit/util";
import axios from "axios";
import megalodon from "megalodon";
import { createStatus, getAbsoluteUrl, getStatusIdFromUrl } from "./utils.js";
import { createStatus, getStatusIdFromUrl } from "./utils.js";

export const mastodon = ({ accessToken, characterLimit, serverUrl }) => ({
client() {
Expand Down Expand Up @@ -59,7 +59,7 @@ export const mastodon = ({ accessToken, characterLimit, serverUrl }) => ({
}

try {
const mediaUrl = getAbsoluteUrl(url, me);
const mediaUrl = getCanonicalUrl(url, me);
const response = await axios(mediaUrl, {
responseType: "stream",
});
Expand Down
15 changes: 0 additions & 15 deletions packages/syndicator-mastodon/lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,21 +69,6 @@ export const createStatus = (properties, options = {}) => {
return parameters;
};

/**
* Get absolute URL
* @param {string} string - URL or path
* @param {string} me - Publication URL
* @returns {string} Absolute URL
*/
export const getAbsoluteUrl = (string, me) => {
try {
return new URL(string).href;
} catch {
const absoluteUrl = path.posix.join(me, string);
return new URL(absoluteUrl).href;
}
};

/**
* Get status ID from Mastodon status URL
* @param {string} url Mastodon status URL
Expand Down
8 changes: 4 additions & 4 deletions packages/syndicator-mastodon/tests/unit/mastodon.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ test.serial("Throws error fetching media to upload", async (t) => {
await t.throwsAsync(
mastodon(t.context.options).uploadMedia(
t.context.media,
t.context.publication
t.context.publication.me
),
{
message: /Not found/,
Expand All @@ -130,7 +130,7 @@ test.failing("Uploads media and returns a media id", async (t) => {

const result = await mastodon(t.context.options).uploadMedia(
t.context.media,
t.context.publication
t.context.publication.me
);

t.is(result, "1234567890987654321");
Expand All @@ -148,7 +148,7 @@ test.failing("Throws error uploading media", async (t) => {
await t.throwsAsync(
mastodon(t.context.options).uploadMedia(
t.context.media,
t.context.publication
t.context.publication.me
),
{
message: "Request failed with status code 404",
Expand All @@ -159,7 +159,7 @@ test.failing("Throws error uploading media", async (t) => {
test("Returns false passing an object to media upload function", async (t) => {
const result = await mastodon(t.context.options).uploadMedia(
{ foo: "bar" },
t.context.publication
t.context.publication.me
);

t.falsy(result);
Expand Down
16 changes: 0 additions & 16 deletions packages/syndicator-mastodon/tests/unit/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { IndiekitError } from "@indiekit/error";
import { getFixture } from "@indiekit-test/fixtures";
import {
createStatus,
getAbsoluteUrl,
getStatusIdFromUrl,
htmlToStatusText,
} from "../../lib/utils.js";
Expand Down Expand Up @@ -121,21 +120,6 @@ test("Creates a status with a photo", (t) => {
t.deepEqual(result.media_ids, ["1", "2", "3", "4"]);
});

test("Gets absolute URL", (t) => {
const result = getAbsoluteUrl(
`${t.context.me}/media/photo.jpg`,
t.context.me
);

t.is(result, `${t.context.me}/media/photo.jpg`);
});

test("Gets absolute URL by prepending publication URL", (t) => {
const result = getAbsoluteUrl("/media/photo.jpg", t.context.me);

t.is(result, `${t.context.me}/media/photo.jpg`);
});

test("Gets status ID from Mastodon permalink", (t) => {
const result = getStatusIdFromUrl(
"https://mastodon.example/@username/1234567890987654321"
Expand Down

0 comments on commit d8c8688

Please sign in to comment.