Handle emoji content type (#275)
* add containsOnly emoji helper * add emoji check when sending text message
This commit is contained in:
parent
98e340a39a
commit
c50b32546b
|
@ -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',
|
||||
|
|
|
@ -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()
|
||||
})
|
||||
})
|
|
@ -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)
|
||||
}
|
Loading…
Reference in New Issue