You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

function.any.ts 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. import { IReduxState } from '../app/types';
  2. import { GIF_DEFAULT_RATING, GIF_PREFIX } from './constants';
  3. import { IGif } from './reducer';
  4. /**
  5. * Returns the gif config.
  6. *
  7. * @param {IReduxState} state - Redux state.
  8. * @returns {Object}
  9. */
  10. export function getGifConfig(state: IReduxState) {
  11. return state['features/base/config'].giphy || {};
  12. }
  13. /**
  14. * Get the GIF display mode.
  15. *
  16. * @param {IReduxState} state - Redux state.
  17. * @returns {string}
  18. */
  19. export function getGifDisplayMode(state: IReduxState) {
  20. return getGifConfig(state).displayMode || 'all';
  21. }
  22. /**
  23. * Get the GIF audience rating.
  24. *
  25. * @param {IReduxState} state - Redux state.
  26. * @returns {string}
  27. */
  28. export function getGifRating(state: IReduxState) {
  29. return getGifConfig(state).rating || GIF_DEFAULT_RATING;
  30. }
  31. /**
  32. * Gets the URL of the GIF for the given participant or null if there's none.
  33. *
  34. * @param {IReduxState} state - Redux state.
  35. * @param {string} participantId - Id of the participant for which to remove the GIF.
  36. * @returns {Object}
  37. */
  38. export function getGifForParticipant(state: IReduxState, participantId: string): IGif {
  39. return isGifEnabled(state) ? state['features/gifs'].gifList.get(participantId) || {} : {};
  40. }
  41. /**
  42. * Returns true if a given URL is allowed to be rendered as gif and false otherwise.
  43. *
  44. * @param {string} url - The URL to be validated.
  45. * @returns {boolean} - True if a given URL is allowed to be rendered as gif and false otherwise.
  46. */
  47. export function isGifUrlAllowed(url: string) {
  48. let hostname: string | undefined;
  49. try {
  50. const urlObject = new URL(url);
  51. hostname = urlObject?.hostname;
  52. } catch (_error) {
  53. return false;
  54. }
  55. return hostname === 'i.giphy.com';
  56. }
  57. /**
  58. * Whether or not the message is a GIF message.
  59. *
  60. * @param {string} message - Message to check.
  61. * @returns {boolean}
  62. */
  63. export function isGifMessage(message = '') {
  64. const trimmedMessage = message.trim();
  65. if (!trimmedMessage.toLowerCase().startsWith(GIF_PREFIX)) {
  66. return false;
  67. }
  68. const url = extractGifURL(trimmedMessage);
  69. return isGifUrlAllowed(url);
  70. }
  71. /**
  72. * Extracts the URL from a gif message.
  73. *
  74. * @param {string} message - The message.
  75. * @returns {string} - The URL.
  76. */
  77. export function extractGifURL(message = '') {
  78. const trimmedMessage = message.trim();
  79. return trimmedMessage.substring(GIF_PREFIX.length, trimmedMessage.length - 1);
  80. }
  81. /**
  82. * Returns the url of the gif selected in the gifs menu.
  83. *
  84. * @param {Object} gif - The gif data.
  85. * @returns {boolean}
  86. */
  87. export function getGifUrl(gif?: { data?: { embed_url: string; }; embed_url?: string; }) {
  88. const embedUrl = gif?.embed_url || gif?.data?.embed_url || '';
  89. const idx = embedUrl.lastIndexOf('/');
  90. const id = embedUrl.substr(idx + 1);
  91. return `https://i.giphy.com/media/${id}/giphy.gif`;
  92. }
  93. /**
  94. * Formats the gif message.
  95. *
  96. * @param {string} url - GIF url.
  97. * @returns {string}
  98. */
  99. export function formatGifUrlMessage(url: string) {
  100. return `${GIF_PREFIX}${url}]`;
  101. }
  102. /**
  103. * Get the Giphy API Key from config.
  104. *
  105. * @param {IReduxState} state - Redux state.
  106. * @returns {string}
  107. */
  108. export function getGifAPIKey(state: IReduxState) {
  109. return getGifConfig(state).sdkKey ?? '';
  110. }
  111. /**
  112. * Returns whether or not the feature is enabled.
  113. *
  114. * @param {IReduxState} state - Redux state.
  115. * @returns {boolean}
  116. */
  117. export function isGifEnabled(state: IReduxState) {
  118. const { disableThirdPartyRequests } = state['features/base/config'];
  119. const { giphy } = state['features/base/config'];
  120. const showGiphyIntegration = state['features/dynamic-branding']?.showGiphyIntegration !== false;
  121. if (navigator.product === 'ReactNative' && window.JITSI_MEET_LITE_SDK) {
  122. return false;
  123. }
  124. return showGiphyIntegration && Boolean(!disableThirdPartyRequests && giphy?.enabled && Boolean(giphy?.sdkKey));
  125. }