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(docs): cover sonarcloud code smells #83

Merged
merged 1 commit into from
Oct 22, 2023
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
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ RUN echo "" > .env

USER bot

CMD node /app/dist/index.js $ENV_FILE
CMD node /app/dist/index.js "$ENV_FILE"
16 changes: 14 additions & 2 deletions src/helpers/cache/save-post-to-cache.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
import { BlueskyCache, Cache, MastodonCache,Platform } from '../../types/index.js';
import { BlueskyCache, Cache, MastodonCache, Platform } from '../../types/index.js';
import { updateCacheFile } from './update-cache.js';

export const savePostToCache = async (cache:Cache,tweetId: string = '', data: MastodonCache | BlueskyCache, platform: Platform) => {
interface PostToCache {
cache: Cache,
data: MastodonCache | BlueskyCache,
tweetId?: string,
platform: Platform
}

export const savePostToCache = async ({
cache,
tweetId = '',
data,
platform
}: PostToCache) => {
const alreadyExistingCachedPostData = cache[tweetId] || {};
await updateCacheFile({
...cache,
Expand Down
4 changes: 3 additions & 1 deletion src/helpers/tweet/get-tweet-id-from-permalink.ts
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
export const getTweetIdFromPermalink = (url: string): string => url.match(/\d{19}$/)?.[0] || '';
export const getTweetIdFromPermalink = (url: string): string => {
return url.match(/\d{19}$/)?.[0] ?? '';
};
2 changes: 1 addition & 1 deletion src/helpers/url/get-redirection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export const getRedirectedUrl = async(url: string): Promise<string | null> => {

if (response.status >= 300 && response.status < 400) {
const redirectedUrl = response.headers.get('location');
return redirectedUrl || null;
return redirectedUrl ?? null;
}

return null;
Expand Down
23 changes: 17 additions & 6 deletions src/services/bluesky-sender.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,10 @@ export const blueskySenderService = async (client: BskyAgent | null, post: Blues
$type: 'app.bsky.embed.recordWithMedia',
media: {
$type: 'app.bsky.embed.images',
images: mediaAttachments.length ? mediaAttachments.map(i => ({ alt: i.alt_text ?? '', image: i.data.blob.original })) : undefined
images: mediaAttachments.length ? mediaAttachments.map(i => ({
alt: i.alt_text ?? '',
image: i.data.blob.original
})) : undefined
},
record: {
$type: 'app.bsky.embed.record',
Expand Down Expand Up @@ -114,7 +117,10 @@ export const blueskySenderService = async (client: BskyAgent | null, post: Blues
if (mediaAttachments.length) {
data.embed = {
$type: 'app.bsky.embed.images',
images: mediaAttachments.length ? mediaAttachments.map(i => ({ alt: i.alt_text ?? '', image: i.data.blob.original })) : undefined
images: mediaAttachments.length ? mediaAttachments.map(i => ({
alt: i.alt_text ?? '',
image: i.data.blob.original
})) : undefined
};
}
}
Expand All @@ -125,9 +131,14 @@ export const blueskySenderService = async (client: BskyAgent | null, post: Blues
await client.post({ ...data }).then(async (createdPost) => {
log.succeed(`☁️ | post sent: ${getPostExcerpt(post.tweet.text ?? VOID)}`);
const cache = await getCache();
await savePostToCache(cache, post.tweet.id, {
cid: createdPost.cid,
rkey: createdPost.uri.match(/\/(?<rkey>\w+)$/)?.groups?.['rkey'] || ''
}, Platform.BLUESKY);
await savePostToCache({
cache,
tweetId: post.tweet.id,
data: {
cid: createdPost.cid,
rkey: createdPost.uri.match(/\/(?<rkey>\w+)$/)?.groups?.['rkey'] || ''
},
platform: Platform.BLUESKY
});
});
};
6 changes: 3 additions & 3 deletions src/services/mastodon-sender.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const BLUESKY_MEDIA_IMAGES_MAX_COUNT = 4;
* An async method in charge of handling Mastodon posts computation & uploading
*/
export const mastodonSenderService = async (client: mastodon.rest.Client | null, post: MastodonPost | null, medias: Media[], log: Ora) => {
if(!client || !post) {
if (!client || !post) {
return;
}

Expand Down Expand Up @@ -50,7 +50,7 @@ export const mastodonSenderService = async (client: mastodon.rest.Client | null,
}

// When no compatible media has been found and no text is present, skip the post
if(!mediaAttachments.length && !post.tweet.text) {
if (!mediaAttachments.length && !post.tweet.text) {
log.warn(`🦣️ | post skipped: no compatible media nor text to post (tweet: ${post.tweet.id})`);
return;
}
Expand All @@ -67,6 +67,6 @@ export const mastodonSenderService = async (client: mastodon.rest.Client | null,
.then(async (toot) => {
log.succeed(`🦣 | toot sent: ${getPostExcerpt(post.tweet.text ?? VOID)}`);
const cache = await getCache();
await savePostToCache(cache, post.tweet.id, toot.id, Platform.MASTODON);
await savePostToCache({ cache, tweetId: post.tweet.id, data: toot.id, platform: Platform.MASTODON });
});
};
47 changes: 23 additions & 24 deletions src/services/tweets-getter.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,38 +30,37 @@ export const tweetsGetterService = async (twitterClient: Scraper): Promise<Tweet
let hasRateLimit = false;
for await(const tweet of tweetsIds) {
const rateLimitTimeout = setTimeout(() => hasRateLimit = true,1000 * API_RATE_LIMIT);
if(hasRateLimit) {
if(hasRateLimit || isTweetCached(tweet, cache)) {
continue;
}
if (tweet && !isTweetCached(tweet, cache)) {
const t: Tweet = {
...tweet,
id: getTweetIdFromPermalink(tweet.id || ''),
timestamp: (tweet.timestamp ?? 0) * 1000,
text: formatTweetText(tweet)
};

// Inject quoted tweet
if (tweet.quotedStatusId) {
const quotedStatus = await twitterClient.getTweet(tweet.quotedStatusId);
if (quotedStatus) {
t.quotedStatus = quotedStatus;
}
}
const t: Tweet = {
...tweet,
id: getTweetIdFromPermalink(tweet.id || ''),
timestamp: (tweet.timestamp ?? 0) * 1000,
text: formatTweetText(tweet)
};

// Inject in reply tweet
if (tweet.inReplyToStatusId) {
const inReplyStatus = await twitterClient.getTweet(tweet.inReplyToStatusId);
if (inReplyStatus) {
t.inReplyToStatus = inReplyStatus;
}
// Inject quoted tweet
if (tweet.quotedStatusId) {
const quotedStatus = await twitterClient.getTweet(tweet.quotedStatusId);
if (quotedStatus) {
t.quotedStatus = quotedStatus;
}
}

const eligibleTweet = await getEligibleTweet(t);
if (eligibleTweet) {
tweets.unshift(eligibleTweet);
// Inject in reply tweet
if (tweet.inReplyToStatusId) {
const inReplyStatus = await twitterClient.getTweet(tweet.inReplyToStatusId);
if (inReplyStatus) {
t.inReplyToStatus = inReplyStatus;
}
}

const eligibleTweet = await getEligibleTweet(t);
if (eligibleTweet) {
tweets.unshift(eligibleTweet);
}
clearTimeout(rateLimitTimeout);
}

Expand Down
2 changes: 1 addition & 1 deletion src/types/media.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ import { ComAtprotoRepoUploadBlob } from '@atproto/api';
import { Photo, Video } from '@the-convocation/twitter-scraper/dist/tweets.js';

export type Media = Photo & { type: 'image' } | Video & { type: 'video' }
export type BlueskyMediaAttachment = ComAtprotoRepoUploadBlob.Response & { alt_text?: string | undefined}
export type BlueskyMediaAttachment = ComAtprotoRepoUploadBlob.Response & { alt_text?: string}
4 changes: 2 additions & 2 deletions src/types/post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ export type BlueskyPost = {
status: string,
username: string,
facets?: Facet[],
quotePost?: { uri: string; cid: string; value: AppBskyFeedPost.Record } | undefined
replyPost?: { uri: string; cid: string; value: AppBskyFeedPost.Record } | undefined
quotePost?: { uri: string; cid: string; value: AppBskyFeedPost.Record }
replyPost?: { uri: string; cid: string; value: AppBskyFeedPost.Record }
}

export type Post = {
Expand Down