Handle emoji content type (#275)

* add containsOnly emoji helper

* add emoji check when sending text message
This commit is contained in:
Pavel 2022-06-15 11:08:30 +02:00 committed by GitHub
parent 6d5d6ab498
commit 562d42b09e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 31 additions and 2 deletions

View File

@ -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',

View File

@ -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()
})
})

View File

@ -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)
}