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.ts 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688
  1. import { IStore } from '../../app/types';
  2. import { showNotification } from '../../notifications/actions';
  3. import { NOTIFICATION_TIMEOUT_TYPE } from '../../notifications/constants';
  4. import { IJitsiConference } from '../conference/reducer';
  5. import { set } from '../redux/functions';
  6. import {
  7. DOMINANT_SPEAKER_CHANGED,
  8. GRANT_MODERATOR,
  9. KICK_PARTICIPANT,
  10. LOCAL_PARTICIPANT_AUDIO_LEVEL_CHANGED,
  11. LOCAL_PARTICIPANT_RAISE_HAND,
  12. MUTE_REMOTE_PARTICIPANT,
  13. OVERWRITE_PARTICIPANTS_NAMES,
  14. OVERWRITE_PARTICIPANT_NAME,
  15. PARTICIPANT_ID_CHANGED,
  16. PARTICIPANT_JOINED,
  17. PARTICIPANT_KICKED,
  18. PARTICIPANT_LEFT,
  19. PARTICIPANT_SOURCES_UPDATED,
  20. PARTICIPANT_UPDATED,
  21. PIN_PARTICIPANT,
  22. RAISE_HAND_CLEAR,
  23. RAISE_HAND_UPDATED,
  24. SCREENSHARE_PARTICIPANT_NAME_CHANGED,
  25. SET_LOADABLE_AVATAR_URL,
  26. SET_LOCAL_PARTICIPANT_RECORDING_STATUS
  27. } from './actionTypes';
  28. import {
  29. DISCO_REMOTE_CONTROL_FEATURE
  30. } from './constants';
  31. import {
  32. getLocalParticipant,
  33. getNormalizedDisplayName,
  34. getParticipantById,
  35. getParticipantDisplayName,
  36. getVirtualScreenshareParticipantOwnerId
  37. } from './functions';
  38. import logger from './logger';
  39. import { FakeParticipant, IJitsiParticipant, IParticipant } from './types';
  40. /**
  41. * Create an action for when dominant speaker changes.
  42. *
  43. * @param {string} dominantSpeaker - Participant ID of the dominant speaker.
  44. * @param {Array<string>} previousSpeakers - Participant IDs of the previous speakers.
  45. * @param {boolean} silence - Whether the dominant speaker is silent or not.
  46. * @param {JitsiConference} conference - The {@code JitsiConference} associated
  47. * with the participant identified by the specified {@code id}. Only the local
  48. * participant is allowed to not specify an associated {@code JitsiConference}
  49. * instance.
  50. * @returns {{
  51. * type: DOMINANT_SPEAKER_CHANGED,
  52. * participant: {
  53. * conference: JitsiConference,
  54. * id: string,
  55. * previousSpeakers: Array<string>,
  56. * silence: boolean
  57. * }
  58. * }}
  59. */
  60. export function dominantSpeakerChanged(
  61. dominantSpeaker: string, previousSpeakers: string[], silence: boolean, conference: IJitsiConference) {
  62. return {
  63. type: DOMINANT_SPEAKER_CHANGED,
  64. participant: {
  65. conference,
  66. id: dominantSpeaker,
  67. previousSpeakers,
  68. silence
  69. }
  70. };
  71. }
  72. /**
  73. * Create an action for granting moderator to a participant.
  74. *
  75. * @param {string} id - Participant's ID.
  76. * @returns {{
  77. * type: GRANT_MODERATOR,
  78. * id: string
  79. * }}
  80. */
  81. export function grantModerator(id: string) {
  82. return {
  83. type: GRANT_MODERATOR,
  84. id
  85. };
  86. }
  87. /**
  88. * Create an action for removing a participant from the conference.
  89. *
  90. * @param {string} id - Participant's ID.
  91. * @returns {{
  92. * type: KICK_PARTICIPANT,
  93. * id: string
  94. * }}
  95. */
  96. export function kickParticipant(id: string) {
  97. return {
  98. type: KICK_PARTICIPANT,
  99. id
  100. };
  101. }
  102. /**
  103. * Action to signal that the ID of local participant has changed. It happens
  104. * when the local participant joins a new conference or leaves an existing
  105. * conference.
  106. *
  107. * @param {string} id - New ID for local participant.
  108. * @returns {Function}
  109. */
  110. export function localParticipantIdChanged(id: string) {
  111. return (dispatch: IStore['dispatch'], getState: IStore['getState']) => {
  112. const participant = getLocalParticipant(getState);
  113. if (participant) {
  114. return dispatch({
  115. type: PARTICIPANT_ID_CHANGED,
  116. // XXX A participant is identified by an id-conference pair.
  117. // Only the local participant is with an undefined conference.
  118. conference: undefined,
  119. newValue: id,
  120. oldValue: participant.id
  121. });
  122. }
  123. };
  124. }
  125. /**
  126. * Action to signal that a local participant has joined.
  127. *
  128. * @param {IParticipant} participant={} - Information about participant.
  129. * @returns {{
  130. * type: PARTICIPANT_JOINED,
  131. * participant: IParticipant
  132. * }}
  133. */
  134. export function localParticipantJoined(participant: IParticipant = { id: '' }) {
  135. return participantJoined(set(participant, 'local', true));
  136. }
  137. /**
  138. * Action to remove a local participant.
  139. *
  140. * @returns {Function}
  141. */
  142. export function localParticipantLeft() {
  143. return (dispatch: IStore['dispatch'], getState: IStore['getState']) => {
  144. const participant = getLocalParticipant(getState);
  145. if (participant) {
  146. return (
  147. dispatch(
  148. participantLeft(
  149. participant.id,
  150. // XXX Only the local participant is allowed to leave
  151. // without stating the JitsiConference instance because
  152. // the local participant is uniquely identified by the
  153. // very fact that there is only one local participant
  154. // (and the fact that the local participant "joins" at
  155. // the beginning of the app and "leaves" at the end of
  156. // the app).
  157. undefined)));
  158. }
  159. };
  160. }
  161. /**
  162. * Action to signal the role of the local participant has changed. It can happen
  163. * when the participant has joined a conference, even before a non-default local
  164. * id has been set, or after a moderator leaves.
  165. *
  166. * @param {string} role - The new role of the local participant.
  167. * @returns {Function}
  168. */
  169. export function localParticipantRoleChanged(role: string) {
  170. return (dispatch: IStore['dispatch'], getState: IStore['getState']) => {
  171. const participant = getLocalParticipant(getState);
  172. if (participant) {
  173. return dispatch(participantRoleChanged(participant.id, role));
  174. }
  175. };
  176. }
  177. /**
  178. * Create an action for muting another participant in the conference.
  179. *
  180. * @param {string} id - Participant's ID.
  181. * @param {MEDIA_TYPE} mediaType - The media to mute.
  182. * @returns {{
  183. * type: MUTE_REMOTE_PARTICIPANT,
  184. * id: string,
  185. * mediaType: MEDIA_TYPE
  186. * }}
  187. */
  188. export function muteRemoteParticipant(id: string, mediaType: string) {
  189. return {
  190. type: MUTE_REMOTE_PARTICIPANT,
  191. id,
  192. mediaType
  193. };
  194. }
  195. /**
  196. * Action to signal that a participant has joined.
  197. *
  198. * @param {IParticipant} participant - Information about participant.
  199. * @returns {{
  200. * type: PARTICIPANT_JOINED,
  201. * participant: IParticipant
  202. * }}
  203. */
  204. export function participantJoined(participant: IParticipant) {
  205. // Only the local participant is not identified with an id-conference pair.
  206. if (participant.local) {
  207. return {
  208. type: PARTICIPANT_JOINED,
  209. participant
  210. };
  211. }
  212. // In other words, a remote participant is identified with an id-conference
  213. // pair.
  214. const { conference } = participant;
  215. if (!conference) {
  216. throw Error(
  217. 'A remote participant must be associated with a JitsiConference!');
  218. }
  219. return (dispatch: IStore['dispatch'], getState: IStore['getState']) => {
  220. // A remote participant is only expected to join in a joined or joining
  221. // conference. The following check is really necessary because a
  222. // JitsiConference may have moved into leaving but may still manage to
  223. // sneak a PARTICIPANT_JOINED in if its leave is delayed for any purpose
  224. // (which is not outragous given that leaving involves network
  225. // requests.)
  226. const stateFeaturesBaseConference
  227. = getState()['features/base/conference'];
  228. if (conference === stateFeaturesBaseConference.conference
  229. || conference === stateFeaturesBaseConference.joining) {
  230. return dispatch({
  231. type: PARTICIPANT_JOINED,
  232. participant
  233. });
  234. }
  235. };
  236. }
  237. /**
  238. * Updates the sources of a remote participant.
  239. *
  240. * @param {IJitsiParticipant} jitsiParticipant - The IJitsiParticipant instance.
  241. * @returns {{
  242. * type: PARTICIPANT_SOURCES_UPDATED,
  243. * participant: IParticipant
  244. * }}
  245. */
  246. export function participantSourcesUpdated(jitsiParticipant: IJitsiParticipant) {
  247. return (dispatch: IStore['dispatch'], getState: IStore['getState']) => {
  248. const id = jitsiParticipant.getId();
  249. const participant = getParticipantById(getState(), id);
  250. if (participant?.local) {
  251. return;
  252. }
  253. const sources = jitsiParticipant.getSources();
  254. if (!sources?.size) {
  255. return;
  256. }
  257. return dispatch({
  258. type: PARTICIPANT_SOURCES_UPDATED,
  259. participant: {
  260. id,
  261. sources
  262. }
  263. });
  264. };
  265. }
  266. /**
  267. * Updates the features of a remote participant.
  268. *
  269. * @param {JitsiParticipant} jitsiParticipant - The ID of the participant.
  270. * @returns {{
  271. * type: PARTICIPANT_UPDATED,
  272. * participant: IParticipant
  273. * }}
  274. */
  275. export function updateRemoteParticipantFeatures(jitsiParticipant: any) {
  276. return (dispatch: IStore['dispatch'], getState: IStore['getState']) => {
  277. if (!jitsiParticipant) {
  278. return;
  279. }
  280. const id = jitsiParticipant.getId();
  281. jitsiParticipant.getFeatures()
  282. .then((features: Map<string, string>) => {
  283. const supportsRemoteControl = features.has(DISCO_REMOTE_CONTROL_FEATURE);
  284. const participant = getParticipantById(getState(), id);
  285. if (!participant || participant.local) {
  286. return;
  287. }
  288. if (participant?.supportsRemoteControl !== supportsRemoteControl) {
  289. return dispatch({
  290. type: PARTICIPANT_UPDATED,
  291. participant: {
  292. id,
  293. supportsRemoteControl
  294. }
  295. });
  296. }
  297. })
  298. .catch((error: any) => {
  299. logger.error(`Failed to get participant features for ${id}!`, error);
  300. });
  301. };
  302. }
  303. /**
  304. * Action to signal that a participant has left.
  305. *
  306. * @param {string} id - Participant's ID.
  307. * @param {JitsiConference} conference - The {@code JitsiConference} associated
  308. * with the participant identified by the specified {@code id}. Only the local
  309. * participant is allowed to not specify an associated {@code JitsiConference}
  310. * instance.
  311. * @param {Object} participantLeftProps - Other participant properties.
  312. * @typedef {Object} participantLeftProps
  313. * @param {FakeParticipant|undefined} participantLeftProps.fakeParticipant - The type of fake participant.
  314. * @param {boolean} participantLeftProps.isReplaced - Whether the participant is to be replaced in the meeting.
  315. *
  316. * @returns {{
  317. * type: PARTICIPANT_LEFT,
  318. * participant: {
  319. * conference: JitsiConference,
  320. * id: string
  321. * }
  322. * }}
  323. */
  324. export function participantLeft(id: string, conference?: IJitsiConference, participantLeftProps: {
  325. fakeParticipant?: string; isReplaced?: boolean;
  326. } = {}) {
  327. return {
  328. type: PARTICIPANT_LEFT,
  329. participant: {
  330. conference,
  331. fakeParticipant: participantLeftProps.fakeParticipant,
  332. id,
  333. isReplaced: participantLeftProps.isReplaced
  334. }
  335. };
  336. }
  337. /**
  338. * Action to signal that a participant's presence status has changed.
  339. *
  340. * @param {string} id - Participant's ID.
  341. * @param {string} presence - Participant's new presence status.
  342. * @returns {{
  343. * type: PARTICIPANT_UPDATED,
  344. * participant: {
  345. * id: string,
  346. * presence: string
  347. * }
  348. * }}
  349. */
  350. export function participantPresenceChanged(id: string, presence: string) {
  351. return participantUpdated({
  352. id,
  353. presence
  354. });
  355. }
  356. /**
  357. * Action to signal that a participant's role has changed.
  358. *
  359. * @param {string} id - Participant's ID.
  360. * @param {PARTICIPANT_ROLE} role - Participant's new role.
  361. * @returns {{
  362. * type: PARTICIPANT_UPDATED,
  363. * participant: {
  364. * id: string,
  365. * role: PARTICIPANT_ROLE
  366. * }
  367. * }}
  368. */
  369. export function participantRoleChanged(id: string, role: string) {
  370. return participantUpdated({
  371. id,
  372. role
  373. });
  374. }
  375. /**
  376. * Action to signal that a participant's display name has changed.
  377. *
  378. * @param {string} id - Screenshare participant's ID.
  379. * @param {name} name - The new display name of the screenshare participant's owner.
  380. * @returns {{
  381. * type: SCREENSHARE_PARTICIPANT_NAME_CHANGED,
  382. * id: string,
  383. * name: string
  384. * }}
  385. */
  386. export function screenshareParticipantDisplayNameChanged(id: string, name: string) {
  387. return {
  388. type: SCREENSHARE_PARTICIPANT_NAME_CHANGED,
  389. id,
  390. name
  391. };
  392. }
  393. /**
  394. * Action to signal that some of participant properties has been changed.
  395. *
  396. * @param {IParticipant} participant={} - Information about participant. To
  397. * identify the participant the object should contain either property id with
  398. * value the id of the participant or property local with value true (if the
  399. * local participant hasn't joined the conference yet).
  400. * @returns {{
  401. * type: PARTICIPANT_UPDATED,
  402. * participant: IParticipant
  403. * }}
  404. */
  405. export function participantUpdated(participant: IParticipant = { id: '' }) {
  406. const participantToUpdate = {
  407. ...participant
  408. };
  409. if (participant.name) {
  410. participantToUpdate.name = getNormalizedDisplayName(participant.name);
  411. }
  412. return {
  413. type: PARTICIPANT_UPDATED,
  414. participant: participantToUpdate
  415. };
  416. }
  417. /**
  418. * Action to signal that a participant has muted us.
  419. *
  420. * @param {JitsiParticipant} participant - Information about participant.
  421. * @param {JitsiLocalTrack} track - Information about the track that has been muted.
  422. * @returns {Promise}
  423. */
  424. export function participantMutedUs(participant: any, track: any) {
  425. return (dispatch: IStore['dispatch'], getState: IStore['getState']) => {
  426. if (!participant) {
  427. return;
  428. }
  429. const isAudio = track.isAudioTrack();
  430. dispatch(showNotification({
  431. titleKey: isAudio ? 'notify.mutedRemotelyTitle' : 'notify.videoMutedRemotelyTitle',
  432. titleArguments: {
  433. participantDisplayName: getParticipantDisplayName(getState, participant.getId())
  434. }
  435. }, NOTIFICATION_TIMEOUT_TYPE.MEDIUM));
  436. };
  437. }
  438. /**
  439. * Action to create a virtual screenshare participant.
  440. *
  441. * @param {(string)} sourceName - The source name of the JitsiTrack instance.
  442. * @param {(boolean)} local - Whether it's a local or remote participant.
  443. * @param {JitsiConference} conference - The conference instance for which the participant is to be created.
  444. * @returns {Function}
  445. */
  446. export function createVirtualScreenshareParticipant(sourceName: string, local: boolean, conference?: IJitsiConference) {
  447. return (dispatch: IStore['dispatch'], getState: IStore['getState']) => {
  448. const state = getState();
  449. const ownerId = getVirtualScreenshareParticipantOwnerId(sourceName);
  450. const ownerName = getParticipantDisplayName(state, ownerId);
  451. dispatch(participantJoined({
  452. conference,
  453. fakeParticipant: local ? FakeParticipant.LocalScreenShare : FakeParticipant.RemoteScreenShare,
  454. id: sourceName,
  455. name: ownerName
  456. }));
  457. };
  458. }
  459. /**
  460. * Action to signal that a participant had been kicked.
  461. *
  462. * @param {JitsiParticipant} kicker - Information about participant performing the kick.
  463. * @param {JitsiParticipant} kicked - Information about participant that was kicked.
  464. * @returns {Promise}
  465. */
  466. export function participantKicked(kicker: any, kicked: any) {
  467. return (dispatch: IStore['dispatch'], getState: IStore['getState']) => {
  468. const state = getState();
  469. const localParticipant = getLocalParticipant(state);
  470. const kickedId = kicked.getId();
  471. const kickerId = kicker?.getId();
  472. dispatch({
  473. type: PARTICIPANT_KICKED,
  474. kicked: kickedId,
  475. kicker: kickerId
  476. });
  477. if (kicked.isReplaced?.() || !kickerId || kickerId === localParticipant?.id) {
  478. return;
  479. }
  480. dispatch(showNotification({
  481. titleArguments: {
  482. kicked:
  483. getParticipantDisplayName(state, kickedId),
  484. kicker:
  485. getParticipantDisplayName(state, kickerId)
  486. },
  487. titleKey: 'notify.kickParticipant'
  488. }, NOTIFICATION_TIMEOUT_TYPE.MEDIUM));
  489. };
  490. }
  491. /**
  492. * Create an action which pins a conference participant.
  493. *
  494. * @param {string|null} id - The ID of the conference participant to pin or null
  495. * if none of the conference's participants are to be pinned.
  496. * @returns {{
  497. * type: PIN_PARTICIPANT,
  498. * participant: {
  499. * id: string
  500. * }
  501. * }}
  502. */
  503. export function pinParticipant(id?: string | null) {
  504. return {
  505. type: PIN_PARTICIPANT,
  506. participant: {
  507. id
  508. }
  509. };
  510. }
  511. /**
  512. * Creates an action which notifies the app that the loadable URL of the avatar of a participant got updated.
  513. *
  514. * @param {string} participantId - The ID of the participant.
  515. * @param {string} url - The new URL.
  516. * @param {boolean} useCORS - Indicates whether we need to use CORS for this URL.
  517. * @returns {{
  518. * type: SET_LOADABLE_AVATAR_URL,
  519. * participant: {
  520. * id: string,
  521. * loadableAvatarUrl: string,
  522. * loadableAvatarUrlUseCORS: boolean
  523. * }
  524. * }}
  525. */
  526. export function setLoadableAvatarUrl(participantId: string, url: string, useCORS: boolean) {
  527. return {
  528. type: SET_LOADABLE_AVATAR_URL,
  529. participant: {
  530. id: participantId,
  531. loadableAvatarUrl: url,
  532. loadableAvatarUrlUseCORS: useCORS
  533. }
  534. };
  535. }
  536. /**
  537. * Raise hand for the local participant.
  538. *
  539. * @param {boolean} enabled - Raise or lower hand.
  540. * @returns {{
  541. * type: LOCAL_PARTICIPANT_RAISE_HAND,
  542. * raisedHandTimestamp: number
  543. * }}
  544. */
  545. export function raiseHand(enabled: boolean) {
  546. return {
  547. type: LOCAL_PARTICIPANT_RAISE_HAND,
  548. raisedHandTimestamp: enabled ? Date.now() : 0
  549. };
  550. }
  551. /**
  552. * Clear the raise hand queue.
  553. *
  554. * @returns {{
  555. * type: RAISE_HAND_CLEAR
  556. * }}
  557. */
  558. export function raiseHandClear() {
  559. return {
  560. type: RAISE_HAND_CLEAR
  561. };
  562. }
  563. /**
  564. * Update raise hand queue of participants.
  565. *
  566. * @param {Object} participant - Participant that updated raised hand.
  567. * @returns {{
  568. * type: RAISE_HAND_UPDATED,
  569. * participant: Object
  570. * }}
  571. */
  572. export function raiseHandUpdateQueue(participant: IParticipant) {
  573. return {
  574. type: RAISE_HAND_UPDATED,
  575. participant
  576. };
  577. }
  578. /**
  579. * Notifies if the local participant audio level has changed.
  580. *
  581. * @param {number} level - The audio level.
  582. * @returns {{
  583. * type: LOCAL_PARTICIPANT_AUDIO_LEVEL_CHANGED,
  584. * level: number
  585. * }}
  586. */
  587. export function localParticipantAudioLevelChanged(level: number) {
  588. return {
  589. type: LOCAL_PARTICIPANT_AUDIO_LEVEL_CHANGED,
  590. level
  591. };
  592. }
  593. /**
  594. * Overwrites the name of the participant with the given id.
  595. *
  596. * @param {string} id - Participant id;.
  597. * @param {string} name - New participant name.
  598. * @returns {Object}
  599. */
  600. export function overwriteParticipantName(id: string, name: string) {
  601. return {
  602. type: OVERWRITE_PARTICIPANT_NAME,
  603. id,
  604. name
  605. };
  606. }
  607. /**
  608. * Overwrites the names of the given participants.
  609. *
  610. * @param {Array<Object>} participantList - The list of participants to overwrite.
  611. * @returns {Object}
  612. */
  613. export function overwriteParticipantsNames(participantList: IParticipant[]) {
  614. return {
  615. type: OVERWRITE_PARTICIPANTS_NAMES,
  616. participantList
  617. };
  618. }
  619. /**
  620. * Local video recording status for the local participant.
  621. *
  622. * @param {boolean} recording - If local recording is ongoing.
  623. * @param {boolean} onlySelf - If recording only local streams.
  624. * @returns {{
  625. * type: SET_LOCAL_PARTICIPANT_RECORDING_STATUS,
  626. * recording: boolean
  627. * }}
  628. */
  629. export function updateLocalRecordingStatus(recording: boolean, onlySelf?: boolean) {
  630. return {
  631. type: SET_LOCAL_PARTICIPANT_RECORDING_STATUS,
  632. recording,
  633. onlySelf
  634. };
  635. }