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.

functions.js 2.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import { showOverflowDrawer } from '../toolbox/functions.web';
  2. import { GIF_PREFIX } from './constants';
  3. /**
  4. * Gets the URL of the GIF for the given participant or null if there's none.
  5. *
  6. * @param {Object} state - Redux state.
  7. * @param {string} participantId - Id of the participant for which to remove the GIF.
  8. * @returns {Object}
  9. */
  10. export function getGifForParticipant(state, participantId) {
  11. return isGifEnabled(state) ? state['features/gifs'].gifList.get(participantId) || {} : {};
  12. }
  13. /**
  14. * Whether or not the message is a GIF message.
  15. *
  16. * @param {string} message - Message to check.
  17. * @returns {boolean}
  18. */
  19. export function isGifMessage(message) {
  20. return message.trim().toLowerCase()
  21. .startsWith(GIF_PREFIX);
  22. }
  23. /**
  24. * Returns the visibility state of the gifs menu.
  25. *
  26. * @param {Object} state - The state of the application.
  27. * @returns {boolean}
  28. */
  29. export function isGifsMenuOpen(state) {
  30. const overflowDrawer = showOverflowDrawer(state);
  31. const { drawerVisible, menuOpen } = state['features/gifs'];
  32. return overflowDrawer ? drawerVisible : menuOpen;
  33. }
  34. /**
  35. * Returns the url of the gif selected in the gifs menu.
  36. *
  37. * @param {Object} gif - The gif data.
  38. * @returns {boolean}
  39. */
  40. export function getGifUrl(gif) {
  41. const embedUrl = gif?.embed_url || gif?.data?.embed_url || '';
  42. const idx = embedUrl.lastIndexOf('/');
  43. const id = embedUrl.substr(idx + 1);
  44. return `https://i.giphy.com/media/${id}/giphy.gif`;
  45. }
  46. /**
  47. * Formats the gif message.
  48. *
  49. * @param {string} url - GIF url.
  50. * @returns {string}
  51. */
  52. export function formatGifUrlMessage(url) {
  53. return `${GIF_PREFIX}${url}]`;
  54. }
  55. /**
  56. * Get the Giphy API Key from config.
  57. *
  58. * @param {Object} state - Redux state.
  59. * @returns {string}
  60. */
  61. export function getGifAPIKey(state) {
  62. return state['features/base/config']?.giphy?.sdkKey;
  63. }
  64. /**
  65. * Returns whether or not the feature is enabled.
  66. *
  67. * @param {Object} state - Redux state.
  68. * @returns {boolean}
  69. */
  70. export function isGifEnabled(state) {
  71. const { disableThirdPartyRequests } = state['features/base/config'];
  72. const { giphy } = state['features/base/config'];
  73. return !disableThirdPartyRequests && giphy?.enabled && Boolean(giphy?.sdkKey);
  74. }
  75. /**
  76. * Get the GIF display mode.
  77. *
  78. * @param {Object} state - Redux state.
  79. * @returns {string}
  80. */
  81. export function getGifDisplayMode(state) {
  82. const { giphy } = state['features/base/config'];
  83. return giphy?.displayMode || 'all';
  84. }