From e976c5bc1fd3f5afd240551c534577ea4ff446aa Mon Sep 17 00:00:00 2001 From: Pavel Prichodko <14926950+prichodko@users.noreply.github.com> Date: Fri, 10 Jun 2022 13:43:48 +0200 Subject: [PATCH] add basic validateMessage function --- .../status-js/src/utils/validate-message.ts | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 packages/status-js/src/utils/validate-message.ts diff --git a/packages/status-js/src/utils/validate-message.ts b/packages/status-js/src/utils/validate-message.ts new file mode 100644 index 00000000..752b3130 --- /dev/null +++ b/packages/status-js/src/utils/validate-message.ts @@ -0,0 +1,29 @@ +import type { ChatMessage } from '~/protos/chat-message' + +// TODO?: maybe this should normalize the message? +export function validateMessage(message: ChatMessage): boolean { + if (message.messageType !== 'COMMUNITY_CHAT') { + return false + } + + switch (message.contentType) { + case 'TEXT_PLAIN': { + return message.text !== '' + } + case 'IMAGE': { + if (message.image.type === 'UNKNOWN_IMAGE_TYPE') { + return false + } + + return message.image.payload.length !== 0 + } + case 'AUDIO': { + if (message.audio.type === 'UNKNOWN_AUDIO_TYPE') { + return false + } + return message.audio.payload.length !== 0 + } + } + + return false +}