Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(bluesky): add missing catch; add some debug logs #171

Merged
merged 1 commit into from
Feb 5, 2024
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
75 changes: 41 additions & 34 deletions src/services/bluesky-sender.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,10 @@ export const blueskySenderService = async (
* If the tweet is long, each child chunk will reference the previous one as replyId.
*/
for (const chunk of post.chunks) {
if (DEBUG) {
console.log("bluesky post chunk: ", chunk);
}

const richText = new bsky.RichText({ text: chunk });
await richText.detectFacets(client);

Expand Down Expand Up @@ -220,43 +224,46 @@ export const blueskySenderService = async (
log.text = `☁️ | post sending: ${getPostExcerpt(post.tweet.text ?? VOID)}`;

// Post
await client.post({ ...data }).then(async (createdPost) => {
oraProgress(
log,
{ before: "☁️ | post sending: " },
chunkIndex,
post.chunks.length,
);

// Save post ID to be able to reference it while posting the next chunk.
const RKEY_REGEX = /\/(?<rkey>\w+)$/;
chunkReferences.push({
cid: createdPost.cid,
uri: createdPost.uri,
rkey: RKEY_REGEX.exec(createdPost.uri)?.groups?.["rkey"] ?? "",
});

// If this is the last chunk, save the all chunks ID to the cache.
if (chunkIndex === post.chunks.length - 1) {
log.succeed(
`☁️ | post sent: ${getPostExcerpt(post.tweet.text ?? VOID)}${
chunkReferences.length > 1
? ` (${chunkReferences.length} chunks)`
: ""
}`,
await client
.post({ ...data })
.then(async (createdPost) => {
oraProgress(
log,
{ before: "☁️ | post sending: " },
chunkIndex,
post.chunks.length,
);

await savePostToCache({
tweetId: post.tweet.id,
data: chunkReferences.map((ref) => ({
rkey: ref.rkey,
cid: ref.cid,
})),
platform: Platform.BLUESKY,
// Save post ID to be able to reference it while posting the next chunk.
const RKEY_REGEX = /\/(?<rkey>\w+)$/;
chunkReferences.push({
cid: createdPost.cid,
uri: createdPost.uri,
rkey: RKEY_REGEX.exec(createdPost.uri)?.groups?.["rkey"] ?? "",
});
}

chunkIndex++;
});
// If this is the last chunk, save the all chunks ID to the cache.
if (chunkIndex === post.chunks.length - 1) {
log.succeed(
`☁️ | post sent: ${getPostExcerpt(post.tweet.text ?? VOID)}${
chunkReferences.length > 1
? ` (${chunkReferences.length} chunks)`
: ""
}`,
);

await savePostToCache({
tweetId: post.tweet.id,
data: chunkReferences.map((ref) => ({
rkey: ref.rkey,
cid: ref.cid,
})),
platform: Platform.BLUESKY,
});
}

chunkIndex++;
})
.catch((err) => log.fail(err));
}
};
6 changes: 5 additions & 1 deletion src/services/mastodon-sender.service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { mastodon } from "masto";
import { Ora } from "ora";

import { VOID } from "../constants.js";
import { DEBUG, VOID } from "../constants.js";
import { savePostToCache } from "../helpers/cache/save-post-to-cache.js";
import { oraProgress } from "../helpers/logs/index.js";
import { getPostExcerpt } from "../helpers/post/get-post-excerpt.js";
Expand Down Expand Up @@ -92,6 +92,10 @@ export const mastodonSenderService = async (
* If the tweet is long, each child chunk will reference the previous one as replyId.
*/
for (const chunk of post.chunks) {
if (DEBUG) {
console.log("mastodon post chunk: ", chunk);
}

await client.v1.statuses
.create({
status: chunk,
Expand Down