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 98e340a39a
commit c50b32546b
No known key found for this signature in database
GPG Key ID: 0EB8D75C775AB6F1
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) => { 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 :-( // TODO: protos does not support optional fields :-(
const payload = ChatMessageProto.encode({ const payload = ChatMessageProto.encode({
clock: BigInt(Date.now()), clock: BigInt(Date.now()),
@ -310,8 +318,8 @@ export class Chat {
responseTo: responseTo ?? '', responseTo: responseTo ?? '',
ensName: '', ensName: '',
chatId: this.id, chatId: this.id,
messageType: 'COMMUNITY_CHAT', contentType: type,
contentType: ChatMessageProto.ContentType.TEXT_PLAIN, messageType: 'COMMUNITY_CHAT' as MessageType,
sticker: { hash: '', pack: 0 }, sticker: { hash: '', pack: 0 },
image: { image: {
type: 'JPEG', 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)
}