Skip to content

Commit

Permalink
v1.3.6 fix permalinks
Browse files Browse the repository at this point in the history
  • Loading branch information
codybrom committed Sep 23, 2024
1 parent f76e53e commit 501a140
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 26 deletions.
37 changes: 11 additions & 26 deletions mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,17 +114,9 @@ export async function createThreadsContainer(
}

const data = JSON.parse(responseText);
console.log(`Created container: ${data}`);

// If getPermalink is true, fetch the permalink
if (request.getPermalink) {
const threadData = await getSingleThread(data.id, request.accessToken);
return {
id: data.id,
permalink: threadData.permalink || "",
};
} else {
return data.id;
}
return data.id;
} catch (error) {
// Access error message safely
const errorMessage =
Expand Down Expand Up @@ -385,22 +377,6 @@ export async function publishThreadsContainer(

const publishData = await publishResponse.json();

if (getPermalink) {
const mediaId = publishData.id;
const permalinkUrl = `${THREADS_API_BASE_URL}/${mediaId}?fields=permalink&access_token=${accessToken}`;
const permalinkResponse = await fetch(permalinkUrl);

if (permalinkResponse.ok) {
const permalinkData = await permalinkResponse.json();
return {
id: mediaId,
permalink: permalinkData.permalink,
};
} else {
throw new Error("Failed to fetch permalink");
}
}

// Check container status
let status = await checkContainerStatus(containerId, accessToken);
let attempts = 0;
Expand All @@ -426,6 +402,14 @@ export async function publishThreadsContainer(
);
}

if (getPermalink) {
const threadData = await getSingleThread(publishData.id, accessToken);
return {
id: publishData.id,
permalink: threadData.permalink || "",
};
}

return publishData.id;
} catch (error) {
if (error instanceof Error) {
Expand All @@ -434,6 +418,7 @@ export async function publishThreadsContainer(
throw error;
}
}

/**
* Serves HTTP requests to create and publish Threads posts.
*
Expand Down
51 changes: 51 additions & 0 deletions mod_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,57 @@ Deno.test("Threads API", async (t) => {
);
teardownMockAPI();
});

await t.step("should return permalink when requested", async () => {
setupMockAPI();
const userId = "12345";
const accessToken = "token";
const containerId = await createThreadsContainer({
userId,
accessToken,
mediaType: "TEXT",
text: "Test post with permalink",
});

const result = await publishThreadsContainer(
userId,
accessToken,
typeof containerId === "string" ? containerId : containerId.id,
true // Request permalink
);

if (typeof result === "string") {
throw new Error("Expected an object with permalink, but got a string");
} else {
assertEquals(typeof result, "object");
assertEquals(typeof result.id, "string");
assertEquals(typeof result.permalink, "string");
assertEquals(result.permalink.startsWith("https://"), true);
}
teardownMockAPI();
});

await t.step("should not return permalink when not requested", async () => {
setupMockAPI();
const userId = "12345";
const accessToken = "token";
const containerId = await createThreadsContainer({
userId,
accessToken,
mediaType: "TEXT",
text: "Test post without permalink",
});

const result = await publishThreadsContainer(
userId,
accessToken,
typeof containerId === "string" ? containerId : containerId.id,
false // Don't request permalink
);

assertEquals(typeof result, "string");
teardownMockAPI();
});
});

await t.step("createCarouselItem", async (t) => {
Expand Down

0 comments on commit 501a140

Please sign in to comment.