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.

actions.any.ts 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. import { IStore } from '../app/types';
  2. import { getCurrentConference } from '../base/conference/functions';
  3. import { getLocalParticipant } from '../base/participants/functions';
  4. import { IParticipant } from '../base/participants/types';
  5. import { LOBBY_CHAT_INITIALIZED } from '../lobby/constants';
  6. import {
  7. ADD_MESSAGE,
  8. CLEAR_MESSAGES,
  9. CLOSE_CHAT,
  10. EDIT_MESSAGE,
  11. REMOVE_LOBBY_CHAT_PARTICIPANT,
  12. SEND_MESSAGE,
  13. SET_IS_POLL_TAB_FOCUSED,
  14. SET_LOBBY_CHAT_ACTIVE_STATE,
  15. SET_LOBBY_CHAT_RECIPIENT,
  16. SET_PRIVATE_MESSAGE_RECIPIENT
  17. } from './actionTypes';
  18. /**
  19. * Adds a chat message to the collection of messages.
  20. *
  21. * @param {Object} messageDetails - The chat message to save.
  22. * @param {string} messageDetails.displayName - The displayName of the
  23. * participant that authored the message.
  24. * @param {boolean} messageDetails.hasRead - Whether or not to immediately mark
  25. * the message as read.
  26. * @param {string} messageDetails.message - The received message to display.
  27. * @param {string} messageDetails.messageType - The kind of message, such as
  28. * "error" or "local" or "remote".
  29. * @param {string} messageDetails.timestamp - A timestamp to display for when
  30. * the message was received.
  31. * @param {string} messageDetails.isReaction - Whether or not the
  32. * message is a reaction message.
  33. * @returns {{
  34. * type: ADD_MESSAGE,
  35. * displayName: string,
  36. * hasRead: boolean,
  37. * message: string,
  38. * messageType: string,
  39. * timestamp: string,
  40. * isReaction: boolean
  41. * }}
  42. */
  43. export function addMessage(messageDetails: Object) {
  44. return {
  45. type: ADD_MESSAGE,
  46. ...messageDetails
  47. };
  48. }
  49. /**
  50. * Edits an existing chat message.
  51. *
  52. * @param {Object} message - The chat message to edit/override. The messages will be matched from the state
  53. * comparing the messageId.
  54. * @returns {{
  55. * type: EDIT_MESSAGE,
  56. * message: Object
  57. * }}
  58. */
  59. export function editMessage(message: Object) {
  60. return {
  61. type: EDIT_MESSAGE,
  62. message
  63. };
  64. }
  65. /**
  66. * Clears the chat messages in Redux.
  67. *
  68. * @returns {{
  69. * type: CLEAR_MESSAGES
  70. * }}
  71. */
  72. export function clearMessages() {
  73. return {
  74. type: CLEAR_MESSAGES
  75. };
  76. }
  77. /**
  78. * Action to signal the closing of the chat dialog.
  79. *
  80. * @returns {{
  81. * type: CLOSE_CHAT
  82. * }}
  83. */
  84. export function closeChat() {
  85. return {
  86. type: CLOSE_CHAT
  87. };
  88. }
  89. /**
  90. * Sends a chat message to everyone in the conference.
  91. *
  92. * @param {string} message - The chat message to send out.
  93. * @param {boolean} ignorePrivacy - True if the privacy notification should be ignored.
  94. * @returns {{
  95. * type: SEND_MESSAGE,
  96. * ignorePrivacy: boolean,
  97. * message: string
  98. * }}
  99. */
  100. export function sendMessage(message: string, ignorePrivacy = false) {
  101. return {
  102. type: SEND_MESSAGE,
  103. ignorePrivacy,
  104. message
  105. };
  106. }
  107. /**
  108. * Initiates the sending of a private message to the supplied participant.
  109. *
  110. * @param {IParticipant} participant - The participant to set the recipient to.
  111. * @returns {{
  112. * participant: IParticipant,
  113. * type: SET_PRIVATE_MESSAGE_RECIPIENT
  114. * }}
  115. */
  116. export function setPrivateMessageRecipient(participant?: Object) {
  117. return {
  118. participant,
  119. type: SET_PRIVATE_MESSAGE_RECIPIENT
  120. };
  121. }
  122. /**
  123. * Set the value of _isPollsTabFocused.
  124. *
  125. * @param {boolean} isPollsTabFocused - The new value for _isPollsTabFocused.
  126. * @returns {Function}
  127. */
  128. export function setIsPollsTabFocused(isPollsTabFocused: boolean) {
  129. return {
  130. isPollsTabFocused,
  131. type: SET_IS_POLL_TAB_FOCUSED
  132. };
  133. }
  134. /**
  135. * Initiates the sending of messages between a moderator and a lobby attendee.
  136. *
  137. * @param {Object} lobbyChatInitializedInfo - The information about the attendee and the moderator
  138. * that is going to chat.
  139. *
  140. * @returns {Function}
  141. */
  142. export function onLobbyChatInitialized(lobbyChatInitializedInfo: { attendee: IParticipant; moderator: IParticipant; }) {
  143. return async (dispatch: IStore['dispatch'], getState: IStore['getState']) => {
  144. const state = getState();
  145. const conference = getCurrentConference(state);
  146. const lobbyLocalId = conference.myLobbyUserId();
  147. if (!lobbyLocalId) {
  148. return;
  149. }
  150. if (lobbyChatInitializedInfo.moderator.id === lobbyLocalId) {
  151. dispatch({
  152. type: SET_LOBBY_CHAT_RECIPIENT,
  153. participant: lobbyChatInitializedInfo.attendee,
  154. open: true
  155. });
  156. }
  157. if (lobbyChatInitializedInfo.attendee.id === lobbyLocalId) {
  158. return dispatch({
  159. type: SET_LOBBY_CHAT_RECIPIENT,
  160. participant: lobbyChatInitializedInfo.moderator,
  161. open: false
  162. });
  163. }
  164. };
  165. }
  166. /**
  167. * Sets the lobby room's chat active state.
  168. *
  169. * @param {boolean} value - The active state.
  170. *
  171. * @returns {Object}
  172. */
  173. export function setLobbyChatActiveState(value: boolean) {
  174. return {
  175. type: SET_LOBBY_CHAT_ACTIVE_STATE,
  176. payload: value
  177. };
  178. }
  179. /**
  180. * Removes lobby type messages.
  181. *
  182. * @param {boolean} removeLobbyChatMessages - Should remove messages from chat (works only for accepted users).
  183. * If not specified, it will delete all lobby messages.
  184. *
  185. * @returns {Object}
  186. */
  187. export function removeLobbyChatParticipant(removeLobbyChatMessages?: boolean) {
  188. return {
  189. type: REMOVE_LOBBY_CHAT_PARTICIPANT,
  190. removeLobbyChatMessages
  191. };
  192. }
  193. /**
  194. * Handles initial setup of lobby message between
  195. * Moderator and participant.
  196. *
  197. * @param {string} participantId - The participant id.
  198. *
  199. * @returns {Object}
  200. */
  201. export function handleLobbyChatInitialized(participantId: string) {
  202. return async (dispatch: IStore['dispatch'], getState: IStore['getState']) => {
  203. if (!participantId) {
  204. return;
  205. }
  206. const state = getState();
  207. const conference = state['features/base/conference'].conference;
  208. const { knockingParticipants } = state['features/lobby'];
  209. const { lobbyMessageRecipient } = state['features/chat'];
  210. const me = getLocalParticipant(state);
  211. const lobbyLocalId = conference?.myLobbyUserId();
  212. if (lobbyMessageRecipient && lobbyMessageRecipient.id === participantId) {
  213. return dispatch(setLobbyChatActiveState(true));
  214. }
  215. const attendee = knockingParticipants.find(p => p.id === participantId);
  216. if (attendee && attendee.chattingWithModerator === lobbyLocalId) {
  217. return dispatch({
  218. type: SET_LOBBY_CHAT_RECIPIENT,
  219. participant: attendee,
  220. open: true
  221. });
  222. }
  223. if (!attendee) {
  224. return;
  225. }
  226. const payload = { type: LOBBY_CHAT_INITIALIZED,
  227. moderator: {
  228. ...me,
  229. name: 'Moderator',
  230. id: lobbyLocalId
  231. },
  232. attendee };
  233. // notify attendee privately.
  234. conference?.sendLobbyMessage(payload, attendee.id);
  235. // notify other moderators.
  236. return conference?.sendLobbyMessage(payload);
  237. };
  238. }