您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

actions.js 9.0KB

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