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.js 9.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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_JOIN,
  15. CONFERENCE_WILL_LEAVE,
  16. LOCK_STATE_CHANGED,
  17. SET_PASSWORD,
  18. SET_ROOM
  19. } from './actionTypes';
  20. import { EMAIL_COMMAND } from './constants';
  21. import { _addLocalTracksToConference } from './functions';
  22. /**
  23. * Adds conference (event) listeners.
  24. *
  25. * @param {JitsiConference} conference - The JitsiConference instance.
  26. * @param {Dispatch} dispatch - The Redux dispatch function.
  27. * @private
  28. * @returns {void}
  29. */
  30. function _addConferenceListeners(conference, dispatch) {
  31. const JitsiConferenceEvents = JitsiMeetJS.events.conference;
  32. conference.on(
  33. JitsiConferenceEvents.CONFERENCE_FAILED,
  34. (...args) => dispatch(conferenceFailed(conference, ...args)));
  35. conference.on(
  36. JitsiConferenceEvents.CONFERENCE_JOINED,
  37. (...args) => dispatch(_conferenceJoined(conference, ...args)));
  38. conference.on(
  39. JitsiConferenceEvents.CONFERENCE_LEFT,
  40. (...args) => dispatch(_conferenceLeft(conference, ...args)));
  41. conference.on(
  42. JitsiConferenceEvents.DOMINANT_SPEAKER_CHANGED,
  43. (...args) => dispatch(dominantSpeakerChanged(...args)));
  44. conference.on(
  45. JitsiConferenceEvents.LOCK_STATE_CHANGED,
  46. (...args) => dispatch(_lockStateChanged(conference, ...args)));
  47. conference.on(
  48. JitsiConferenceEvents.TRACK_ADDED,
  49. t => t && !t.isLocal() && dispatch(trackAdded(t)));
  50. conference.on(
  51. JitsiConferenceEvents.TRACK_REMOVED,
  52. t => t && !t.isLocal() && dispatch(trackRemoved(t)));
  53. conference.on(
  54. JitsiConferenceEvents.USER_JOINED,
  55. (id, user) => dispatch(participantJoined({
  56. id,
  57. name: user.getDisplayName(),
  58. role: user.getRole()
  59. })));
  60. conference.on(
  61. JitsiConferenceEvents.USER_LEFT,
  62. (...args) => dispatch(participantLeft(...args)));
  63. conference.on(
  64. JitsiConferenceEvents.USER_ROLE_CHANGED,
  65. (...args) => dispatch(participantRoleChanged(...args)));
  66. conference.addCommandListener(
  67. EMAIL_COMMAND,
  68. (data, id) => dispatch(changeParticipantEmail(id, data.value)));
  69. }
  70. /**
  71. * Signals that a specific conference has failed.
  72. *
  73. * @param {JitsiConference} conference - The JitsiConference that has failed.
  74. * @param {string} error - The error describing/detailing the cause of the
  75. * failure.
  76. * @returns {{
  77. * type: CONFERENCE_FAILED,
  78. * conference: JitsiConference,
  79. * error: string
  80. * }}
  81. * @public
  82. */
  83. export 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 join a
  131. * conference with a specific room (name). Similar in fashion
  132. * to CONFERENCE_JOINED.
  133. *
  134. * @param {string} room - The room (name) which identifies the conference the
  135. * local participant will (try to) join.
  136. * @returns {{
  137. * type: CONFERENCE_WILL_JOIN,
  138. * room: string
  139. * }}
  140. */
  141. function _conferenceWillJoin(room) {
  142. return {
  143. type: CONFERENCE_WILL_JOIN,
  144. room
  145. };
  146. }
  147. /**
  148. * Signals the intention of the application to have the local participant leave
  149. * a specific conference. Similar in fashion to CONFERENCE_LEFT. Contrary to it
  150. * though, it's not guaranteed because CONFERENCE_LEFT may be triggered by
  151. * lib-jitsi-meet and not the application.
  152. *
  153. * @param {JitsiConference} conference - The JitsiConference instance which will
  154. * be left by the local participant.
  155. * @returns {{
  156. * type: CONFERENCE_LEFT,
  157. * conference: JitsiConference
  158. * }}
  159. */
  160. export function conferenceWillLeave(conference) {
  161. return {
  162. type: CONFERENCE_WILL_LEAVE,
  163. conference
  164. };
  165. }
  166. /**
  167. * Initializes a new conference.
  168. *
  169. * @returns {Function}
  170. */
  171. export function createConference() {
  172. return (dispatch, getState) => {
  173. const state = getState();
  174. const connection = state['features/base/connection'].connection;
  175. if (!connection) {
  176. throw new Error('Cannot create conference without connection');
  177. }
  178. const { password, room } = state['features/base/conference'];
  179. if (typeof room === 'undefined' || room === '') {
  180. throw new Error('Cannot join conference without room name');
  181. }
  182. dispatch(_conferenceWillJoin(room));
  183. // TODO Take options from config.
  184. const conference
  185. = connection.initJitsiConference(
  186. // XXX Lib-jitsi-meet does not accept uppercase letters.
  187. room.toLowerCase(),
  188. { openSctp: true });
  189. _addConferenceListeners(conference, dispatch);
  190. conference.join(password);
  191. };
  192. }
  193. /**
  194. * Signals that the lock state of a specific JitsiConference changed.
  195. *
  196. * @param {JitsiConference} conference - The JitsiConference which had its lock
  197. * state changed.
  198. * @param {boolean} locked - If the specified conference became locked, true;
  199. * otherwise, false.
  200. * @returns {{
  201. * type: LOCK_STATE_CHANGED,
  202. * conference: JitsiConference,
  203. * locked: boolean
  204. * }}
  205. */
  206. function _lockStateChanged(conference, locked) {
  207. return {
  208. type: LOCK_STATE_CHANGED,
  209. conference,
  210. locked
  211. };
  212. }
  213. /**
  214. * Sets the password to join or lock a specific JitsiConference.
  215. *
  216. * @param {JitsiConference} conference - The JitsiConference which requires a
  217. * password to join or is to be locked with the specified password.
  218. * @param {Function} method - The JitsiConference method of password protection
  219. * such as join or lock.
  220. * @param {string} password - The password with which the specified conference
  221. * is to be joined or locked.
  222. * @returns {Function}
  223. */
  224. export function setPassword(conference, method, password) {
  225. return (dispatch, getState) => {
  226. switch (method) {
  227. case conference.join: {
  228. let state = getState()['features/base/conference'];
  229. // Make sure that the action will set a password for a conference
  230. // that the application wants joined.
  231. if (state.passwordRequired === conference) {
  232. dispatch({
  233. type: SET_PASSWORD,
  234. conference,
  235. method,
  236. password
  237. });
  238. // Join the conference with the newly-set password.
  239. // Make sure that the action did set the password.
  240. state = getState()['features/base/conference'];
  241. if (state.password === password
  242. && !state.passwordRequired
  243. // Make sure that the application still wants the
  244. // conference joined.
  245. && !state.conference) {
  246. method.call(conference, password);
  247. }
  248. }
  249. break;
  250. }
  251. case conference.lock: {
  252. const state = getState()['features/base/conference'];
  253. if (state.conference === conference) {
  254. return (
  255. method.call(conference, password)
  256. .then(() => dispatch({
  257. type: SET_PASSWORD,
  258. conference,
  259. method,
  260. password
  261. })));
  262. }
  263. return Promise.reject();
  264. }
  265. }
  266. };
  267. }
  268. /**
  269. * Sets (the name of) the room of the conference to be joined.
  270. *
  271. * @param {(string|undefined)} room - The name of the room of the conference to
  272. * be joined.
  273. * @returns {{
  274. * type: SET_ROOM,
  275. * room: string
  276. * }}
  277. */
  278. export function setRoom(room) {
  279. return {
  280. type: SET_ROOM,
  281. room
  282. };
  283. }