fix(gifs): trim the message before extracting the URL.

This commit is contained in:
Hristo Terezov
2024-08-27 17:14:29 -05:00
parent 697ede207b
commit 32f9f8ba92
4 changed files with 28 additions and 12 deletions

View File

@@ -4,8 +4,7 @@ import { connect } from 'react-redux';
import { IReduxState } from '../../../../app/types';
import GifMessage from '../../../../chat/components/web/GifMessage';
import { GIF_PREFIX } from '../../../../gifs/constants';
import { isGifEnabled, isGifMessage } from '../../../../gifs/functions.web';
import { extractGifURL, isGifEnabled, isGifMessage } from '../../../../gifs/functions.web';
import Linkify from './Linkify';
@@ -55,7 +54,7 @@ class Message extends Component<IProps> {
// check if the message is a GIF
if (gifEnabled && isGifMessage(text)) {
const url = text.substring(GIF_PREFIX.length, text.length - 1);
const url = extractGifURL(text);
content.push(<GifMessage
key = { url }

View File

@@ -1,7 +1,7 @@
import React from 'react';
import { Image, ImageStyle, View } from 'react-native';
import { GIF_PREFIX } from '../../../gifs/constants';
import { extractGifURL } from '../../../gifs/function.any';
import styles from './styles';
@@ -14,7 +14,7 @@ interface IProps {
}
const GifMessage = ({ message }: IProps) => {
const url = message.substring(GIF_PREFIX.length, message.length - 1);
const url = extractGifURL(message);
return (<View
id = 'gif-message'

View File

@@ -24,8 +24,7 @@ import MiddlewareRegistry from '../base/redux/MiddlewareRegistry';
import StateListenerRegistry from '../base/redux/StateListenerRegistry';
import { playSound, registerSound, unregisterSound } from '../base/sounds/actions';
import { addGif } from '../gifs/actions';
import { GIF_PREFIX } from '../gifs/constants';
import { getGifDisplayMode, isGifEnabled, isGifMessage } from '../gifs/function.any';
import { extractGifURL, getGifDisplayMode, isGifEnabled, isGifMessage } from '../gifs/function.any';
import { showMessageNotification } from '../notifications/actions';
import { NOTIFICATION_TIMEOUT_TYPE } from '../notifications/constants';
import { resetNbUnreadPollsMessages } from '../polls/actions';
@@ -351,7 +350,7 @@ function _onConferenceMessageReceived(store: IStore,
* @returns {void}
*/
function _handleGifMessageReceived(store: IStore, participantId: string, message: string) {
const url = message.substring(GIF_PREFIX.length, message.length - 1);
const url = extractGifURL(message);
store.dispatch(addGif(participantId, url));
}

View File

@@ -71,11 +71,29 @@ export function isGifUrlAllowed(url: string) {
* @param {string} message - Message to check.
* @returns {boolean}
*/
export function isGifMessage(message: string) {
const url = message.substring(GIF_PREFIX.length, message.length - 1);
export function isGifMessage(message = '') {
const trimmedMessage = message.trim();
if (!trimmedMessage.toLowerCase().startsWith(GIF_PREFIX)) {
return false;
}
const url = extractGifURL(trimmedMessage);
return isGifUrlAllowed(url);
}
/**
* Extracts the URL from a gif message.
*
* @param {string} message - The message.
* @returns {string} - The URL.
*/
export function extractGifURL(message = '') {
const trimmedMessage = message.trim();
return trimmedMessage.substring(GIF_PREFIX.length, trimmedMessage.length - 1);
return message.trim().toLowerCase()
.startsWith(GIF_PREFIX) && isGifUrlAllowed(url);
}
/**