Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

actions.js 9.1KB

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