Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

actions.js 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. import JitsiMeetJS from '../lib-jitsi-meet';
  2. import {
  3. changeParticipantEmail,
  4. dominantSpeakerChanged,
  5. participantJoined,
  6. participantLeft,
  7. participantRoleChanged
  8. } from '../participants';
  9. import { trackAdded, trackRemoved } from '../tracks';
  10. import {
  11. CONFERENCE_FAILED,
  12. CONFERENCE_JOINED,
  13. CONFERENCE_LEFT,
  14. CONFERENCE_WILL_LEAVE,
  15. LOCK_STATE_CHANGED,
  16. SET_PASSWORD,
  17. SET_ROOM
  18. } from './actionTypes';
  19. import { EMAIL_COMMAND } from './constants';
  20. import { _addLocalTracksToConference } from './functions';
  21. import './middleware';
  22. import './reducer';
  23. /**
  24. * Adds conference (event) listeners.
  25. *
  26. * @param {JitsiConference} conference - The JitsiConference instance.
  27. * @param {Dispatch} dispatch - The Redux dispatch function.
  28. * @private
  29. * @returns {void}
  30. */
  31. function _addConferenceListeners(conference, dispatch) {
  32. const JitsiConferenceEvents = JitsiMeetJS.events.conference;
  33. conference.on(
  34. JitsiConferenceEvents.CONFERENCE_FAILED,
  35. (...args) => dispatch(_conferenceFailed(conference, ...args)));
  36. conference.on(
  37. JitsiConferenceEvents.CONFERENCE_JOINED,
  38. (...args) => dispatch(_conferenceJoined(conference, ...args)));
  39. conference.on(
  40. JitsiConferenceEvents.CONFERENCE_LEFT,
  41. (...args) => dispatch(_conferenceLeft(conference, ...args)));
  42. conference.on(
  43. JitsiConferenceEvents.DOMINANT_SPEAKER_CHANGED,
  44. (...args) => dispatch(dominantSpeakerChanged(...args)));
  45. conference.on(
  46. JitsiConferenceEvents.LOCK_STATE_CHANGED,
  47. (...args) => dispatch(_lockStateChanged(conference, ...args)));
  48. conference.on(
  49. JitsiConferenceEvents.TRACK_ADDED,
  50. t => t && !t.isLocal() && dispatch(trackAdded(t)));
  51. conference.on(
  52. JitsiConferenceEvents.TRACK_REMOVED,
  53. t => t && !t.isLocal() && dispatch(trackRemoved(t)));
  54. conference.on(
  55. JitsiConferenceEvents.USER_JOINED,
  56. (id, user) => dispatch(participantJoined({
  57. id,
  58. name: user.getDisplayName(),
  59. role: user.getRole()
  60. })));
  61. conference.on(
  62. JitsiConferenceEvents.USER_LEFT,
  63. (...args) => dispatch(participantLeft(...args)));
  64. conference.on(
  65. JitsiConferenceEvents.USER_ROLE_CHANGED,
  66. (...args) => dispatch(participantRoleChanged(...args)));
  67. conference.addCommandListener(
  68. EMAIL_COMMAND,
  69. (data, id) => dispatch(changeParticipantEmail(id, data.value)));
  70. }
  71. /**
  72. * Signals that a specific conference has failed.
  73. *
  74. * @param {JitsiConference} conference - The JitsiConference that has failed.
  75. * @param {string} error - The error describing/detailing the cause of the
  76. * failure.
  77. * @returns {{
  78. * type: CONFERENCE_FAILED,
  79. * conference: JitsiConference,
  80. * error: string
  81. * }}
  82. */
  83. function _conferenceFailed(conference, error) {
  84. return {
  85. type: CONFERENCE_FAILED,
  86. conference,
  87. error
  88. };
  89. }
  90. /**
  91. * Attach any pre-existing local media to the conference once the conference has
  92. * been joined.
  93. *
  94. * @param {JitsiConference} conference - The JitsiConference instance which was
  95. * joined by the local participant.
  96. * @returns {Function}
  97. */
  98. function _conferenceJoined(conference) {
  99. return (dispatch, getState) => {
  100. const localTracks
  101. = getState()['features/base/tracks']
  102. .filter(t => t.local)
  103. .map(t => t.jitsiTrack);
  104. if (localTracks.length) {
  105. _addLocalTracksToConference(conference, localTracks);
  106. }
  107. dispatch({
  108. type: CONFERENCE_JOINED,
  109. conference
  110. });
  111. };
  112. }
  113. /**
  114. * Signals that a specific conference has been left.
  115. *
  116. * @param {JitsiConference} conference - The JitsiConference instance which was
  117. * left by the local participant.
  118. * @returns {{
  119. * type: CONFERENCE_LEFT,
  120. * conference: JitsiConference
  121. * }}
  122. */
  123. function _conferenceLeft(conference) {
  124. return {
  125. type: CONFERENCE_LEFT,
  126. conference
  127. };
  128. }
  129. /**
  130. * Signals the intention of the application to have the local participant leave
  131. * a specific conference. Similar in fashion to CONFERENCE_LEFT. Contrary to it
  132. * though, it's not guaranteed because CONFERENCE_LEFT may be triggered by
  133. * lib-jitsi-meet and not the application.
  134. *
  135. * @param {JitsiConference} conference - The JitsiConference instance which will
  136. * be left by the local participant.
  137. * @returns {{
  138. * type: CONFERENCE_LEFT,
  139. * conference: JitsiConference
  140. * }}
  141. */
  142. export function conferenceWillLeave(conference) {
  143. return {
  144. type: CONFERENCE_WILL_LEAVE,
  145. conference
  146. };
  147. }
  148. /**
  149. * Initializes a new conference.
  150. *
  151. * @returns {Function}
  152. */
  153. export function createConference() {
  154. return (dispatch, getState) => {
  155. const state = getState();
  156. const connection = state['features/base/connection'].connection;
  157. if (!connection) {
  158. throw new Error('Cannot create conference without connection');
  159. }
  160. const { password, room } = state['features/base/conference'];
  161. if (typeof room === 'undefined' || room === '') {
  162. throw new Error('Cannot join conference without room name');
  163. }
  164. // TODO Take options from config.
  165. const conference
  166. = connection.initJitsiConference(room, { openSctp: true });
  167. _addConferenceListeners(conference, dispatch);
  168. conference.join(password);
  169. };
  170. }
  171. /**
  172. * Signals that the lock state of a specific JitsiConference changed.
  173. *
  174. * @param {JitsiConference} conference - The JitsiConference which had its lock
  175. * state changed.
  176. * @param {boolean} locked - If the specified conference became locked, true;
  177. * otherwise, false.
  178. * @returns {{
  179. * type: LOCK_STATE_CHANGED,
  180. * conference: JitsiConference,
  181. * locked: boolean
  182. * }}
  183. */
  184. function _lockStateChanged(conference, locked) {
  185. return {
  186. type: LOCK_STATE_CHANGED,
  187. conference,
  188. locked
  189. };
  190. }
  191. /**
  192. * Sets the password to join or lock a specific JitsiConference.
  193. *
  194. * @param {JitsiConference} conference - The JitsiConference which requires a
  195. * password to join or is to be locked with the specified password.
  196. * @param {Function} method - The JitsiConference method of password protection
  197. * such as join or lock.
  198. * @param {string} password - The password with which the specified conference
  199. * is to be joined or locked.
  200. * @returns {{
  201. * type: SET_PASSWORD,
  202. * conference: JitsiConference,
  203. * method: Function,
  204. * password: string
  205. * }}
  206. */
  207. export function setPassword(conference, method, password) {
  208. return {
  209. type: SET_PASSWORD,
  210. conference,
  211. method,
  212. password
  213. };
  214. }
  215. /**
  216. * Sets (the name of) the room of the conference to be joined.
  217. *
  218. * @param {(string|undefined)} room - The name of the room of the conference to
  219. * be joined.
  220. * @returns {{
  221. * type: SET_ROOM,
  222. * room: string
  223. * }}
  224. */
  225. export function setRoom(room) {
  226. return {
  227. type: SET_ROOM,
  228. room
  229. };
  230. }