From c50b32546b8cea7523105f0dbbfcc0aea01f99ae Mon Sep 17 00:00:00 2001 From: Pavel <14926950+prichodko@users.noreply.github.com> Date: Wed, 15 Jun 2022 11:08:30 +0200 Subject: [PATCH] Handle emoji content type (#275) * add containsOnly emoji helper * add emoji check when sending text message --- packages/status-js/src/client/chat.ts | 12 ++++++++++-- .../src/helpers/contains-only-emoji.test.ts | 17 +++++++++++++++++ .../src/helpers/contains-only-emoji.ts | 4 ++++ 3 files changed, 31 insertions(+), 2 deletions(-) create mode 100644 packages/status-js/src/helpers/contains-only-emoji.test.ts create mode 100644 packages/status-js/src/helpers/contains-only-emoji.ts diff --git a/packages/status-js/src/client/chat.ts b/packages/status-js/src/client/chat.ts index 7fe98c59..947d0916 100644 --- a/packages/status-js/src/client/chat.ts +++ b/packages/status-js/src/client/chat.ts @@ -302,6 +302,14 @@ export class Chat { } public sendTextMessage = async (text: string, responseTo?: string) => { + if (text === '') { + throw new Error('Text message cannot be empty') + } + + const type = containsOnlyEmoji(text) + ? ChatMessageProto.ContentType.EMOJI + : ChatMessageProto.ContentType.TEXT_PLAIN + // TODO: protos does not support optional fields :-( const payload = ChatMessageProto.encode({ clock: BigInt(Date.now()), @@ -310,8 +318,8 @@ export class Chat { responseTo: responseTo ?? '', ensName: '', chatId: this.id, - messageType: 'COMMUNITY_CHAT', - contentType: ChatMessageProto.ContentType.TEXT_PLAIN, + contentType: type, + messageType: 'COMMUNITY_CHAT' as MessageType, sticker: { hash: '', pack: 0 }, image: { type: 'JPEG', diff --git a/packages/status-js/src/helpers/contains-only-emoji.test.ts b/packages/status-js/src/helpers/contains-only-emoji.test.ts new file mode 100644 index 00000000..6c14aa02 --- /dev/null +++ b/packages/status-js/src/helpers/contains-only-emoji.test.ts @@ -0,0 +1,17 @@ +import { containsOnlyEmoji } from './contains-only-emoji' + +describe('containsOnlyEmoji', () => { + it('should be truthy', () => { + expect(containsOnlyEmoji('💩')).toBeTruthy() + expect(containsOnlyEmoji('💩💩💩💩💩💩')).toBeTruthy() + }) + + it('should be falsy', () => { + expect(containsOnlyEmoji('')).toBeFalsy() + expect(containsOnlyEmoji(' ')).toBeFalsy() + expect(containsOnlyEmoji(' 💩')).toBeFalsy() + expect(containsOnlyEmoji('💩 ')).toBeFalsy() + expect(containsOnlyEmoji('text 💩')).toBeFalsy() + expect(containsOnlyEmoji('💩 text')).toBeFalsy() + }) +}) diff --git a/packages/status-js/src/helpers/contains-only-emoji.ts b/packages/status-js/src/helpers/contains-only-emoji.ts new file mode 100644 index 00000000..60525bbc --- /dev/null +++ b/packages/status-js/src/helpers/contains-only-emoji.ts @@ -0,0 +1,4 @@ +// todo?: should ignore whitespaces with replace(/\s+/g, '').trim() +export function containsOnlyEmoji(text: string): boolean { + return /^\p{Emoji}+$/gu.test(text) +}