diff --git a/apps/frontend/src/components/launches/general.preview.component.tsx b/apps/frontend/src/components/launches/general.preview.component.tsx index 33cb17e8..403293c8 100644 --- a/apps/frontend/src/components/launches/general.preview.component.tsx +++ b/apps/frontend/src/components/launches/general.preview.component.tsx @@ -16,7 +16,16 @@ export const GeneralPreviewComponent: FC<{ const mediaDir = useMediaDirectory(); const renderContent = topValue.map((p) => { - const newContent = stripHtmlValidation('normal', p.content, true); + const newContent = stripHtmlValidation( + 'normal', + p.content.replace( + /([.\s\S]*?)<\/span>/gi, + (match, match1, match2) => { + return `[[[${match2}]]]`; + } + ), + true + ); const { start, end } = textSlicer( integration?.identifier || '', @@ -27,21 +36,13 @@ export const GeneralPreviewComponent: FC<{ const finalValue = newContent .slice(start, end) - .replace(/(@.+?)(\s)/gi, (match, match1, match2) => { - return `${match1.trim()}${match2}`; - }) - .replace(/@\[(.+?)]\((.+?)\)/gi, (match, name, id) => { - return `@${name}`; + .replace(/\[\[\[([.\s\S]*?)]]]/, (match, match1) => { + return `${match1}`; }) + `` + - newContent - .slice(end) - .replace(/(@.+?)(\s)/gi, (match, match1, match2) => { - return `${match1.trim()}${match2}`; - }) - .replace(/@\[(.+?)]\((.+?)\)/gi, (match, name, id) => { - return `@${name}`; - }) + + newContent.slice(end).replace(/\[\[\[([.\s\S]*?)]]]/, (match, match1) => { + return `${match1}`; + }) + ``; return { text: finalValue, images: p.image }; @@ -110,10 +111,7 @@ export const GeneralPreviewComponent: FC<{
string ): string => { if (type === 'html') { - return striptags(value, [ + return striptags(convertMention(value, convertMentionFunction), [ 'ul', 'ol', 'li', @@ -153,28 +153,31 @@ export const stripHtmlValidation = ( if (type === 'markdown') { return striptags( - value - .replace(/

([.\s\S]*?)<\/h1>/g, (match, p1) => { - return `

# ${p1}

\n`; - }) - .replace(/

([.\s\S]*?)<\/h2>/g, (match, p1) => { - return `

## ${p1}

\n`; - }) - .replace(/

([.\s\S]*?)<\/h3>/g, (match, p1) => { - return `

### ${p1}

\n`; - }) - .replace(/([.\s\S]*?)<\/u>/g, (match, p1) => { - return `__${p1}__`; - }) - .replace(/([.\s\S]*?)<\/strong>/g, (match, p1) => { - return `**${p1}**`; - }) - .replace(/([.\s\S]*?)<\/li.*?>/gm, (match, p1) => { - return `
  • - ${p1.replace(/\n/gm, '')}
  • `; - }) - .replace(/

    ([.\s\S]*?)<\/p>/g, (match, p1) => { - return `

    ${p1}

    \n`; - }) + convertMention( + value + .replace(/

    ([.\s\S]*?)<\/h1>/g, (match, p1) => { + return `

    # ${p1}

    \n`; + }) + .replace(/

    ([.\s\S]*?)<\/h2>/g, (match, p1) => { + return `

    ## ${p1}

    \n`; + }) + .replace(/

    ([.\s\S]*?)<\/h3>/g, (match, p1) => { + return `

    ### ${p1}

    \n`; + }) + .replace(/([.\s\S]*?)<\/u>/g, (match, p1) => { + return `__${p1}__`; + }) + .replace(/([.\s\S]*?)<\/strong>/g, (match, p1) => { + return `**${p1}**`; + }) + .replace(/([.\s\S]*?)<\/li.*?>/gm, (match, p1) => { + return `
  • - ${p1.replace(/\n/gm, '')}
  • `; + }) + .replace(/

    ([.\s\S]*?)<\/p>/g, (match, p1) => { + return `

    ${p1}

    \n`; + }), + convertMentionFunction + ) ); } @@ -222,7 +225,7 @@ export const convertMention = ( } return value.replace( - /(.*?)<\/span>/gi, + /([.\s\S]*?)<\/span>/gi, (match, id, name) => { return `` + process(id, name) + ``; } diff --git a/libraries/nestjs-libraries/src/integrations/social/discord.provider.ts b/libraries/nestjs-libraries/src/integrations/social/discord.provider.ts index eb16a962..74da1f99 100644 --- a/libraries/nestjs-libraries/src/integrations/social/discord.provider.ts +++ b/libraries/nestjs-libraries/src/integrations/social/discord.provider.ts @@ -6,6 +6,7 @@ import { } from '@gitroom/nestjs-libraries/integrations/social/social.integrations.interface'; import { makeId } from '@gitroom/nestjs-libraries/services/make.is'; import { SocialAbstract } from '@gitroom/nestjs-libraries/integrations/social.abstract'; +import { Integration } from '@prisma/client'; export class DiscordProvider extends SocialAbstract implements SocialProvider { identifier = 'discord'; @@ -131,7 +132,6 @@ export class DiscordProvider extends SocialAbstract implements SocialProvider { postDetails: PostDetails[] ): Promise { let channel = postDetails[0].settings.channel; - console.log(postDetails[0].message); if (postDetails.length > 1) { const { id: threadId } = await ( await fetch( @@ -159,7 +159,9 @@ export class DiscordProvider extends SocialAbstract implements SocialProvider { form.append( 'payload_json', JSON.stringify({ - content: post.message, + content: post.message.replace(/\[\[\[(@\d.*?)]]]/g, (match, p1) => { + return `<${p1}>`; + }), attachments: post.media?.map((p, index) => ({ id: index, description: `Picture ${index}`, @@ -219,4 +221,33 @@ export class DiscordProvider extends SocialAbstract implements SocialProvider { name, }; } + + override async mention( + token: string, + data: { query: string }, + id: string, + integration: Integration + ) { + const list = await ( + await fetch( + `https://discord.com/api/guilds/${id}/members/search?query=${data.query}`, + { + headers: { + Authorization: `Bot ${process.env.DISCORD_BOT_TOKEN_ID}`, + 'Content-Type': 'application/json', + }, + } + ) + ).json(); + + return list.map((p: any) => ({ + id: String(p.user.id), + label: p.user.global_name || p.user.username, + image: `https://cdn.discordapp.com/avatars/${p.user.id}/${p.user.avatar}.png`, + })); + } + + mentionFormat(idOrHandle: string, name: string) { + return `[[[@${idOrHandle}]]]`; + } }