Skip to content

πŸ“¨ The ultimate agentic social media scheduling tool πŸ€–

fix: count Threads post length in UTF-8 bytes

Gilad Resisibd781c1cd87f
diff --git a/apps/frontend/src/components/launches/information.component.tsx b/apps/frontend/src/components/launches/information.component.tsx
index c734429f4..718a8eb64 100644
--- a/apps/frontend/src/components/launches/information.component.tsx
+++ b/apps/frontend/src/components/launches/information.component.tsx
@@ -1,6 +1,6 @@
'use client';
-import React, { FC, Fragment, useMemo } from 'react';
+import React, { FC, Fragment, useCallback, useMemo } from 'react';
import { useLaunchStore } from '@gitroom/frontend/components/new-launch/store';
import { useShallow } from 'zustand/react/shallow';
import clsx from 'clsx';
@@ -8,6 +8,7 @@ import SafeImage from '@gitroom/react/helpers/safe.image';
import { capitalize } from 'lodash';
import { useT } from '@gitroom/react/translation/get.transation.service.client';
import { hasLinks } from '@gitroom/helpers/utils/strip.links';
+import { countLength } from '@gitroom/helpers/utils/count.length';
const Valid: FC = () => {
return (
@@ -91,6 +92,13 @@ export const InformationComponent: FC<{
const showStripLinkWarning = stripLinkNames.length > 0;
+ const countFor = useCallback(
+ (identifier?: string) => countLength(identifier || '', text || ''),
+ [text]
+ );
+
+ const currentChars = countFor(currentIntegration?.identifier);
+
const isInternal = useMemo(() => {
if (!isGlobal) {
return [];
@@ -113,11 +121,11 @@ export const InformationComponent: FC<{
return false;
}
- if (totalChars > totalAllowedChars && !isGlobal) {
+ if (currentChars > totalAllowedChars && !isGlobal) {
return false;
}
- if (totalChars <= totalAllowedChars && !isGlobal) {
+ if (currentChars <= totalAllowedChars && !isGlobal) {
return true;
}
@@ -127,7 +135,10 @@ export const InformationComponent: FC<{
return false;
}
- return totalChars > (chars?.[p.integration.id] || 0);
+ return (
+ countFor(p.integration.identifier) >
+ (chars?.[p.integration.id] || 0)
+ );
})
) {
return false;
@@ -137,6 +148,8 @@ export const InformationComponent: FC<{
}, [
totalAllowedChars,
totalChars,
+ currentChars,
+ countFor,
isInternal,
isPicture,
chars,
@@ -152,11 +165,11 @@ export const InformationComponent: FC<{
const limits = selectedIntegrations
.map((p, index) => ({
limit: chars?.[p.integration.id] || 0,
+ count: countFor(p.integration.identifier),
isInternal: isInternal[index],
}))
.filter((item) => !item.isInternal && item.limit > 0)
- .map((item) => item.limit)
- .sort((a, b) => a - b);
+ .sort((a, b) => a.limit - b.limit);
if (!limits.length) {
return null;
@@ -164,9 +177,9 @@ export const InformationComponent: FC<{
// Find the smallest limit that hasn't been exceeded yet
// If all are exceeded, show the smallest one
- const validLimit = limits.find((limit) => totalChars <= limit);
+ const validLimit = limits.find((item) => item.count <= item.limit);
return validLimit ?? limits[0];
- }, [isGlobal, selectedIntegrations, chars, isInternal, totalChars]);
+ }, [isGlobal, selectedIntegrations, chars, isInternal, countFor]);
return (
<div
@@ -179,12 +192,12 @@ export const InformationComponent: FC<{
{!isGlobal && (
<div className={clsx("text-[10px] font-[600] flex justify-center items-center", !isValid && 'text-white')}>
- {totalChars}/{totalAllowedChars}
+ {currentChars}/{totalAllowedChars}
</div>
)}
{isGlobal && globalDisplayLimit !== null && (
<div className={clsx("text-[10px] font-[600] flex justify-center items-center", !isValid && 'text-white')}>
- {totalChars}/{globalDisplayLimit}
+ {globalDisplayLimit.count}/{globalDisplayLimit.limit}
</div>
)}
{((isGlobal && selectedIntegrations.length) || !isValid) && (
@@ -237,7 +250,8 @@ export const InformationComponent: FC<{
'whitespace-nowrap',
isInternal?.[index]
? ''
- : totalChars > (chars?.[p.integration.id] || 0)
+ : countFor(p.integration.identifier) >
+ (chars?.[p.integration.id] || 0)
? 'text-[#FF3F3F]'
: ''
)}
@@ -250,14 +264,17 @@ export const InformationComponent: FC<{
'whitespace-nowrap',
isInternal?.[index]
? ''
- : totalChars > (chars?.[p.integration.id] || 0)
+ : countFor(p.integration.identifier) >
+ (chars?.[p.integration.id] || 0)
? 'text-[#FF3F3F]'
: ''
)}
>
{isInternal?.[index]
? t('internal_edit', 'Internal Edit')
- : `${totalChars}/${chars?.[p.integration.id] || 0}`}
+ : `${countFor(p.integration.identifier)}/${
+ chars?.[p.integration.id] || 0
+ }`}
</div>
</Fragment>
))}
diff --git a/libraries/helpers/src/utils/count.length.ts b/libraries/helpers/src/utils/count.length.ts
index 4a97255f5..d44dca1b9 100644
--- a/libraries/helpers/src/utils/count.length.ts
+++ b/libraries/helpers/src/utils/count.length.ts
@@ -37,3 +37,15 @@ export const textSlicer = (
export const weightedLength = (text: string): number => {
return twitter.parseTweet(text).weightedLength;
};
+
+export const countLength = (integrationType: string, text: string): number => {
+ if (integrationType === 'x') {
+ return weightedLength(text);
+ }
+
+ if (integrationType === 'threads') {
+ return new TextEncoder().encode(text).length;
+ }
+
+ return text.length;
+};
diff --git a/libraries/nestjs-libraries/src/database/prisma/posts/posts.service.ts b/libraries/nestjs-libraries/src/database/prisma/posts/posts.service.ts
index 95277925d..0bb6abd3c 100644
--- a/libraries/nestjs-libraries/src/database/prisma/posts/posts.service.ts
+++ b/libraries/nestjs-libraries/src/database/prisma/posts/posts.service.ts
@@ -53,7 +53,7 @@ import { stripLinks } from '@gitroom/helpers/utils/strip.links';
import { validate } from 'class-validator';
import { plainToInstance } from 'class-transformer';
import { stripHtmlValidation } from '@gitroom/helpers/utils/strip.html.validation';
-import { weightedLength } from '@gitroom/helpers/utils/count.length';
+import { countLength } from '@gitroom/helpers/utils/count.length';
type PostWithConditionals = Post & {
integration?: Integration;
@@ -827,20 +827,17 @@ export class PostsService {
}
const maximumCharacters = provider.maxLength(additionalSettings, settings);
- const isX = integration.providerIdentifier === 'x';
const emptyContent = (post.value || []).some((a) => {
const strip = stripHtmlValidation('normal', a.content || '', true);
- const length = isX ? weightedLength(strip) : strip.length;
+ const length = countLength(integration.providerIdentifier, strip);
return length === 0 && (a.image || []).length === 0;
});
const tooLong = (post.value || []).some((a) => {
const strip = stripHtmlValidation('normal', a.content || '', true);
- const weighted = isX ? weightedLength(strip) : strip.length;
- const totalCharacters =
- weighted > strip.length ? weighted : strip.length;
- return totalCharacters > (maximumCharacters || 1000000);
+ const counted = countLength(integration.providerIdentifier, strip);
+ return counted > (maximumCharacters || 1000000);
});
return {