Skip to content

Commit b3c0869

Browse files
authored
Merge pull request #2178 from nhodges/patch-1
fix(client-twitter): add mention deduplication utility
2 parents 28e9740 + 96f3d9c commit b3c0869

File tree

1 file changed

+32
-2
lines changed

1 file changed

+32
-2
lines changed

packages/client-twitter/src/utils.ts

+32-2
Original file line numberDiff line numberDiff line change
@@ -212,15 +212,18 @@ export async function sendTweet(
212212
})
213213
);
214214
}
215+
216+
const cleanChunk = deduplicateMentions(chunk.trim())
217+
215218
const result = await client.requestQueue.add(async () =>
216219
isLongTweet
217220
? client.twitterClient.sendLongTweet(
218-
chunk.trim(),
221+
cleanChunk,
219222
previousTweetId,
220223
mediaData
221224
)
222225
: client.twitterClient.sendTweet(
223-
chunk.trim(),
226+
cleanChunk,
224227
previousTweetId,
225228
mediaData
226229
)
@@ -397,6 +400,33 @@ function splitSentencesAndWords(text: string, maxLength: number): string[] {
397400
return chunks;
398401
}
399402

403+
function deduplicateMentions(paragraph: string) {
404+
// Regex to match mentions at the beginning of the string
405+
const mentionRegex = /^@(\w+)(?:\s+@(\w+))*(\s+|$)/;
406+
407+
// Find all matches
408+
const matches = paragraph.match(mentionRegex);
409+
410+
if (!matches) {
411+
return paragraph; // If no matches, return the original string
412+
}
413+
414+
// Extract mentions from the match groups
415+
let mentions = matches.slice(1).filter(Boolean) as string[];
416+
417+
// Deduplicate mentions
418+
mentions = [...new Set(mentions)];
419+
420+
// Reconstruct the string with deduplicated mentions
421+
const uniqueMentionsString = `@${mentions.join(' ')}`;
422+
423+
// Find where the mentions end in the original string
424+
const endOfMentions = paragraph.indexOf(matches[0]) + matches[0].length;
425+
426+
// Construct the result by combining unique mentions with the rest of the string
427+
return uniqueMentionsString + paragraph.slice(endOfMentions);
428+
}
429+
400430
function restoreUrls(
401431
chunks: string[],
402432
placeholderMap: Map<string, string>

0 commit comments

Comments
 (0)