您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

actions.js 10.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. /* global interfaceConfig */
  2. import throttle from 'lodash/throttle';
  3. import { showNotification } from '../../notifications';
  4. import {
  5. DOMINANT_SPEAKER_CHANGED,
  6. KICK_PARTICIPANT,
  7. MUTE_REMOTE_PARTICIPANT,
  8. PARTICIPANT_DISPLAY_NAME_CHANGED,
  9. PARTICIPANT_ID_CHANGED,
  10. PARTICIPANT_JOINED,
  11. PARTICIPANT_LEFT,
  12. PARTICIPANT_UPDATED,
  13. PIN_PARTICIPANT
  14. } from './actionTypes';
  15. import { MAX_DISPLAY_NAME_LENGTH } from './constants';
  16. import { getLocalParticipant } from './functions';
  17. /**
  18. * Create an action for when dominant speaker changes.
  19. *
  20. * @param {string} id - Participant's ID.
  21. * @returns {{
  22. * type: DOMINANT_SPEAKER_CHANGED,
  23. * participant: {
  24. * id: string
  25. * }
  26. * }}
  27. */
  28. export function dominantSpeakerChanged(id) {
  29. return {
  30. type: DOMINANT_SPEAKER_CHANGED,
  31. participant: {
  32. id
  33. }
  34. };
  35. }
  36. /**
  37. * Create an action for removing a participant from the conference.
  38. *
  39. * @param {string} id - Participant's ID.
  40. * @returns {{
  41. * type: KICK_PARTICIPANT,
  42. * id: string
  43. * }}
  44. */
  45. export function kickParticipant(id) {
  46. return {
  47. type: KICK_PARTICIPANT,
  48. id
  49. };
  50. }
  51. /**
  52. * Creates an action to signal the connection status of the local participant
  53. * has changed.
  54. *
  55. * @param {string} connectionStatus - The current connection status of the local
  56. * participant, as enumerated by the library's participantConnectionStatus
  57. * constants.
  58. * @returns {Function}
  59. */
  60. export function localParticipantConnectionStatusChanged(connectionStatus) {
  61. return (dispatch, getState) => {
  62. const participant = getLocalParticipant(getState);
  63. if (participant) {
  64. return dispatch(participantConnectionStatusChanged(
  65. participant.id,
  66. connectionStatus));
  67. }
  68. };
  69. }
  70. /**
  71. * Action to signal that the ID of local participant has changed. It happens
  72. * when the local participant joins a new conference or leaves an existing
  73. * conference.
  74. *
  75. * @param {string} id - New ID for local participant.
  76. * @returns {Function}
  77. */
  78. export function localParticipantIdChanged(id) {
  79. return (dispatch, getState) => {
  80. const participant = getLocalParticipant(getState);
  81. if (participant) {
  82. return dispatch({
  83. type: PARTICIPANT_ID_CHANGED,
  84. newValue: id,
  85. oldValue: participant.id
  86. });
  87. }
  88. };
  89. }
  90. /**
  91. * Action to signal that a local participant has joined.
  92. *
  93. * @param {Participant} participant={} - Information about participant.
  94. * @returns {{
  95. * type: PARTICIPANT_JOINED,
  96. * participant: Participant
  97. * }}
  98. */
  99. export function localParticipantJoined(participant = {}) {
  100. return participantJoined({
  101. ...participant,
  102. local: true
  103. });
  104. }
  105. /**
  106. * Action to remove a local participant.
  107. *
  108. * @returns {Function}
  109. */
  110. export function localParticipantLeft() {
  111. return (dispatch, getState) => {
  112. const participant = getLocalParticipant(getState);
  113. if (participant) {
  114. return dispatch(participantLeft(participant.id));
  115. }
  116. };
  117. }
  118. /**
  119. * Action to signal the role of the local participant has changed. It can happen
  120. * when the participant has joined a conference, even before a non-default local
  121. * id has been set, or after a moderator leaves.
  122. *
  123. * @param {string} role - The new role of the local participant.
  124. * @returns {Function}
  125. */
  126. export function localParticipantRoleChanged(role) {
  127. return (dispatch, getState) => {
  128. const participant = getLocalParticipant(getState);
  129. if (participant) {
  130. return dispatch(participantRoleChanged(participant.id, role));
  131. }
  132. };
  133. }
  134. /**
  135. * Create an action for muting another participant in the conference.
  136. *
  137. * @param {string} id - Participant's ID.
  138. * @returns {{
  139. * type: MUTE_REMOTE_PARTICIPANT,
  140. * id: string
  141. * }}
  142. */
  143. export function muteRemoteParticipant(id) {
  144. return {
  145. type: MUTE_REMOTE_PARTICIPANT,
  146. id
  147. };
  148. }
  149. /**
  150. * Action to update a participant's connection status.
  151. *
  152. * @param {string} id - Participant's ID.
  153. * @param {string} connectionStatus - The new connection status of the
  154. * participant.
  155. * @returns {{
  156. * type: PARTICIPANT_UPDATED,
  157. * participant: {
  158. * connectionStatus: string,
  159. * id: string
  160. * }
  161. * }}
  162. */
  163. export function participantConnectionStatusChanged(id, connectionStatus) {
  164. return {
  165. type: PARTICIPANT_UPDATED,
  166. participant: {
  167. connectionStatus,
  168. id
  169. }
  170. };
  171. }
  172. /**
  173. * Action to signal that a participant's display name has changed.
  174. *
  175. * @param {string} id - The id of the participant being changed.
  176. * @param {string} displayName - The new display name.
  177. * @returns {{
  178. * type: PARTICIPANT_DISPLAY_NAME_CHANGED,
  179. * id: string,
  180. * name: string
  181. * }}
  182. */
  183. export function participantDisplayNameChanged(id, displayName = '') {
  184. // FIXME Do not use this action over participantUpdated. This action exists
  185. // as a a bridge for local name updates. Once other components responsible
  186. // for updating the local user's display name are in react/redux, this
  187. // action should be replaceable with the participantUpdated action.
  188. return {
  189. type: PARTICIPANT_DISPLAY_NAME_CHANGED,
  190. id,
  191. name: displayName.substr(0, MAX_DISPLAY_NAME_LENGTH)
  192. };
  193. }
  194. /**
  195. * Action to signal that a participant has joined.
  196. *
  197. * @param {Participant} participant - Information about participant.
  198. * @returns {{
  199. * type: PARTICIPANT_JOINED,
  200. * participant: Participant
  201. * }}
  202. */
  203. export function participantJoined(participant) {
  204. return {
  205. type: PARTICIPANT_JOINED,
  206. participant
  207. };
  208. }
  209. /**
  210. * Action to signal that a participant has left.
  211. *
  212. * @param {string} id - Participant's ID.
  213. * @returns {{
  214. * type: PARTICIPANT_LEFT,
  215. * participant: {
  216. * id: string
  217. * }
  218. * }}
  219. */
  220. export function participantLeft(id) {
  221. return {
  222. type: PARTICIPANT_LEFT,
  223. participant: {
  224. id
  225. }
  226. };
  227. }
  228. /**
  229. * Action to signal that a participant's presence status has changed.
  230. *
  231. * @param {string} id - Participant's ID.
  232. * @param {string} presence - Participant's new presence status.
  233. * @returns {{
  234. * type: PARTICIPANT_UPDATED,
  235. * participant: {
  236. * id: string,
  237. * presence: string
  238. * }
  239. * }}
  240. */
  241. export function participantPresenceChanged(id, presence) {
  242. return participantUpdated({
  243. id,
  244. presence
  245. });
  246. }
  247. /**
  248. * Action to signal that a participant's role has changed.
  249. *
  250. * @param {string} id - Participant's ID.
  251. * @param {PARTICIPANT_ROLE} role - Participant's new role.
  252. * @returns {{
  253. * type: PARTICIPANT_UPDATED,
  254. * participant: {
  255. * id: string,
  256. * role: PARTICIPANT_ROLE
  257. * }
  258. * }}
  259. */
  260. export function participantRoleChanged(id, role) {
  261. return participantUpdated({
  262. id,
  263. role
  264. });
  265. }
  266. /**
  267. * Action to signal that some of participant properties has been changed.
  268. *
  269. * @param {Participant} participant={} - Information about participant. To
  270. * identify the participant the object should contain either property id with
  271. * value the id of the participant or property local with value true (if the
  272. * local participant hasn't joined the conference yet).
  273. * @returns {{
  274. * type: PARTICIPANT_UPDATED,
  275. * participant: Participant
  276. * }}
  277. */
  278. export function participantUpdated(participant = {}) {
  279. return {
  280. type: PARTICIPANT_UPDATED,
  281. participant
  282. };
  283. }
  284. /**
  285. * Create an action which pins a conference participant.
  286. *
  287. * @param {string|null} id - The ID of the conference participant to pin or null
  288. * if none of the conference's participants are to be pinned.
  289. * @returns {{
  290. * type: PIN_PARTICIPANT,
  291. * participant: {
  292. * id: string
  293. * }
  294. * }}
  295. */
  296. export function pinParticipant(id) {
  297. return {
  298. type: PIN_PARTICIPANT,
  299. participant: {
  300. id
  301. }
  302. };
  303. }
  304. /**
  305. * An array of names of participants that have joined the conference. The array
  306. * is replaced with an empty array as notifications are displayed.
  307. *
  308. * @private
  309. * @type {string[]}
  310. */
  311. let joinedParticipantsNames = [];
  312. /**
  313. * A throttled internal function that takes the internal list of participant
  314. * names, {@code joinedParticipantsNames}, and triggers the display of a
  315. * notification informing of their joining.
  316. *
  317. * @private
  318. * @type {Function}
  319. */
  320. const _throttledNotifyParticipantConnected = throttle(dispatch => {
  321. const joinedParticipantsCount = joinedParticipantsNames.length;
  322. let notificationProps;
  323. if (joinedParticipantsCount >= 3) {
  324. notificationProps = {
  325. titleArguments: {
  326. name: joinedParticipantsNames[0],
  327. count: joinedParticipantsCount - 1
  328. },
  329. titleKey: 'notify.connectedThreePlusMembers'
  330. };
  331. } else if (joinedParticipantsCount === 2) {
  332. notificationProps = {
  333. titleArguments: {
  334. first: joinedParticipantsNames[0],
  335. second: joinedParticipantsNames[1]
  336. },
  337. titleKey: 'notify.connectedTwoMembers'
  338. };
  339. } else if (joinedParticipantsCount) {
  340. notificationProps = {
  341. titleArguments: {
  342. name: joinedParticipantsNames[0]
  343. },
  344. titleKey: 'notify.connectedOneMember'
  345. };
  346. }
  347. if (notificationProps) {
  348. dispatch(
  349. showNotification(notificationProps, 2500));
  350. }
  351. joinedParticipantsNames = [];
  352. }, 500, { leading: false });
  353. /**
  354. * Queues the display of a notification of a participant having connected to
  355. * the meeting. The notifications are batched so that quick consecutive
  356. * connection events are shown in one notification.
  357. *
  358. * @param {string} displayName - The name of the participant that connected.
  359. * @returns {Function}
  360. */
  361. export function showParticipantJoinedNotification(displayName) {
  362. joinedParticipantsNames.push(
  363. displayName || interfaceConfig.DEFAULT_REMOTE_DISPLAY_NAME);
  364. return dispatch => {
  365. _throttledNotifyParticipantConnected(dispatch);
  366. };
  367. }