Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

actions.js 9.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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 the intention of the application to have the local participant leave
  116. * a specific conference. Similar in fashion to CONFERENCE_LEFT. Contrary to it
  117. * though, it's not guaranteed because CONFERENCE_LEFT may be triggered by
  118. * lib-jitsi-meet and not the application.
  119. *
  120. * @param {string} room - The JitsiConference instance which will
  121. * be left by the local participant.
  122. * @returns {{
  123. * type: CONFERENCE_WILL_JOIN,
  124. * room: string
  125. * }}
  126. */
  127. function _conferenceWillJoin(room) {
  128. return {
  129. type: CONFERENCE_WILL_JOIN,
  130. room
  131. };
  132. }
  133. /**
  134. * Signals that a specific conference has been left.
  135. *
  136. * @param {JitsiConference} conference - The JitsiConference instance which was
  137. * left by the local participant.
  138. * @returns {{
  139. * type: CONFERENCE_LEFT,
  140. * conference: JitsiConference
  141. * }}
  142. */
  143. function _conferenceLeft(conference) {
  144. return {
  145. type: CONFERENCE_LEFT,
  146. conference
  147. };
  148. }
  149. /**
  150. * Signals the intention of the application to have the local participant leave
  151. * a specific conference. Similar in fashion to CONFERENCE_LEFT. Contrary to it
  152. * though, it's not guaranteed because CONFERENCE_LEFT may be triggered by
  153. * lib-jitsi-meet and not the application.
  154. *
  155. * @param {JitsiConference} conference - The JitsiConference instance which will
  156. * be left by the local participant.
  157. * @returns {{
  158. * type: CONFERENCE_LEFT,
  159. * conference: JitsiConference
  160. * }}
  161. */
  162. export function conferenceWillLeave(conference) {
  163. return {
  164. type: CONFERENCE_WILL_LEAVE,
  165. conference
  166. };
  167. }
  168. /**
  169. * Initializes a new conference.
  170. *
  171. * @returns {Function}
  172. */
  173. export function createConference() {
  174. return (dispatch, getState) => {
  175. const state = getState();
  176. const connection = state['features/base/connection'].connection;
  177. if (!connection) {
  178. throw new Error('Cannot create conference without connection');
  179. }
  180. const { password, room } = state['features/base/conference'];
  181. if (typeof room === 'undefined' || room === '') {
  182. throw new Error('Cannot join conference without room name');
  183. }
  184. // XXX Lib-jitsi-meet does not accept uppercase letters.
  185. const _room = room.toLowerCase();
  186. dispatch(_conferenceWillJoin(_room));
  187. // TODO Take options from config.
  188. const conference
  189. = connection.initJitsiConference(
  190. _room,
  191. { openSctp: true });
  192. _addConferenceListeners(conference, dispatch);
  193. conference.join(password);
  194. };
  195. }
  196. /**
  197. * Signals that the lock state of a specific JitsiConference changed.
  198. *
  199. * @param {JitsiConference} conference - The JitsiConference which had its lock
  200. * state changed.
  201. * @param {boolean} locked - If the specified conference became locked, true;
  202. * otherwise, false.
  203. * @returns {{
  204. * type: LOCK_STATE_CHANGED,
  205. * conference: JitsiConference,
  206. * locked: boolean
  207. * }}
  208. */
  209. function _lockStateChanged(conference, locked) {
  210. return {
  211. type: LOCK_STATE_CHANGED,
  212. conference,
  213. locked
  214. };
  215. }
  216. /**
  217. * Sets the password to join or lock a specific JitsiConference.
  218. *
  219. * @param {JitsiConference} conference - The JitsiConference which requires a
  220. * password to join or is to be locked with the specified password.
  221. * @param {Function} method - The JitsiConference method of password protection
  222. * such as join or lock.
  223. * @param {string} password - The password with which the specified conference
  224. * is to be joined or locked.
  225. * @returns {Function}
  226. */
  227. export function setPassword(conference, method, password) {
  228. return (dispatch, getState) => {
  229. switch (method) {
  230. case conference.join: {
  231. let state = getState()['features/base/conference'];
  232. // Make sure that the action will set a password for a conference
  233. // that the application wants joined.
  234. if (state.passwordRequired === conference) {
  235. dispatch({
  236. type: SET_PASSWORD,
  237. conference,
  238. method,
  239. password
  240. });
  241. // Join the conference with the newly-set password.
  242. // Make sure that the action did set the password.
  243. state = getState()['features/base/conference'];
  244. if (state.password === password
  245. && !state.passwordRequired
  246. // Make sure that the application still wants the
  247. // conference joined.
  248. && !state.conference) {
  249. method.call(conference, password);
  250. }
  251. }
  252. break;
  253. }
  254. case conference.lock: {
  255. const state = getState()['features/base/conference'];
  256. if (state.conference === conference) {
  257. return (
  258. method.call(conference, password)
  259. .then(() => dispatch({
  260. type: SET_PASSWORD,
  261. conference,
  262. method,
  263. password
  264. })));
  265. }
  266. return Promise.reject();
  267. }
  268. }
  269. };
  270. }
  271. /**
  272. * Sets (the name of) the room of the conference to be joined.
  273. *
  274. * @param {(string|undefined)} room - The name of the room of the conference to
  275. * be joined.
  276. * @returns {{
  277. * type: SET_ROOM,
  278. * room: string
  279. * }}
  280. */
  281. export function setRoom(room) {
  282. return {
  283. type: SET_ROOM,
  284. room
  285. };
  286. }