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

API.js 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /* global APP, getConfigParamsFromUrl */
  2. /**
  3. * Implements API class that communicates with external api class
  4. * and provides interface to access Jitsi Meet features by external
  5. * applications that embed Jitsi Meet
  6. */
  7. import postisInit from 'postis';
  8. /**
  9. * List of the available commands.
  10. * @type {{
  11. * displayName: inputDisplayNameHandler,
  12. * toggleAudio: toggleAudio,
  13. * toggleVideo: toggleVideo,
  14. * toggleFilmStrip: toggleFilmStrip,
  15. * toggleChat: toggleChat,
  16. * toggleContactList: toggleContactList
  17. * }}
  18. */
  19. let commands = {};
  20. let hashParams = getConfigParamsFromUrl();
  21. /**
  22. * JitsiMeetExternalAPI id - unique for a webpage.
  23. */
  24. let jitsi_meet_external_api_id = hashParams.jitsi_meet_external_api_id;
  25. /**
  26. * Object that will execute sendMessage
  27. */
  28. let target = window.opener ? window.opener : window.parent;
  29. /**
  30. * Postis instance. Used to communicate with the external application.
  31. */
  32. let postis;
  33. /**
  34. * Current status (enabled/disabled) of API.
  35. */
  36. let enabled = false;
  37. function initCommands() {
  38. commands = {
  39. "display-name": APP.UI.inputDisplayNameHandler,
  40. "toggle-audio": APP.conference.toggleAudioMuted,
  41. "toggle-video": APP.conference.toggleVideoMuted,
  42. "toggle-film-strip": APP.UI.toggleFilmStrip,
  43. "toggle-chat": APP.UI.toggleChat,
  44. "toggle-contact-list": APP.UI.toggleContactList,
  45. "toggle-share-screen": APP.conference.toggleScreenSharing
  46. };
  47. Object.keys(commands).forEach(function (key) {
  48. postis.listen(key, commands[key]);
  49. });
  50. }
  51. /**
  52. * Maps the supported events and their status
  53. * (true it the event is enabled and false if it is disabled)
  54. * @type {{
  55. * incoming-message: boolean,
  56. * outgoing-message: boolean,
  57. * display-name-change: boolean,
  58. * participant-left: boolean,
  59. * participant-joined: boolean,
  60. * video-conference-left: boolean,
  61. * video-conference-joined: boolean
  62. * }}
  63. */
  64. const events = {
  65. "incoming-message": false,
  66. "outgoing-message":false,
  67. "display-name-change": false,
  68. "participant-joined": false,
  69. "participant-left": false,
  70. "video-conference-joined": false,
  71. "video-conference-left": false
  72. };
  73. /**
  74. * Sends message to the external application.
  75. * @param message {object}
  76. * @param method {string}
  77. * @param params {object} the object that will be sent as JSON string
  78. */
  79. function sendMessage(message) {
  80. if(enabled)
  81. postis.send(message);
  82. }
  83. /**
  84. * Check whether the API should be enabled or not.
  85. * @returns {boolean}
  86. */
  87. function isEnabled () {
  88. return (typeof jitsi_meet_external_api_id === "number");
  89. }
  90. /**
  91. * Checks whether the event is enabled ot not.
  92. * @param name the name of the event.
  93. * @returns {*}
  94. */
  95. function isEventEnabled (name) {
  96. return events[name];
  97. }
  98. /**
  99. * Sends event object to the external application that has been subscribed
  100. * for that event.
  101. * @param name the name event
  102. * @param object data associated with the event
  103. */
  104. function triggerEvent (name, object) {
  105. if(isEventEnabled(name))
  106. sendMessage({method: name, params: object});
  107. }
  108. /**
  109. * Handles system messages. (for example: enable/disable events)
  110. * @param message {object} the message
  111. */
  112. function onSystemMessage(message) {
  113. switch (message.type) {
  114. case "eventStatus":
  115. if(!message.name || !message.value) {
  116. console.warn("Unknown system message format", message);
  117. break;
  118. }
  119. events[message.name] = message.value;
  120. break;
  121. default:
  122. console.warn("Unknown system message type", message);
  123. }
  124. }
  125. export default {
  126. /**
  127. * Initializes the APIConnector. Setups message event listeners that will
  128. * receive information from external applications that embed Jitsi Meet.
  129. * It also sends a message to the external application that APIConnector
  130. * is initialized.
  131. * @param options {object}
  132. * @param forceEnable {boolean} if true the module will be enabled.
  133. * @param enabledEvents {array} array of events that should be enabled.
  134. */
  135. init (options = {}) {
  136. if(!isEnabled() && !options.forceEnable)
  137. return;
  138. enabled = true;
  139. if(options.enabledEvents)
  140. options.enabledEvents.forEach(function (eventName) {
  141. events[eventName] = true;
  142. });
  143. let postisOptions = {
  144. window: target
  145. };
  146. if(typeof jitsi_meet_external_api_id === "number")
  147. postisOptions.scope
  148. = "jitsi_meet_external_api_" + jitsi_meet_external_api_id;
  149. postis = postisInit(postisOptions);
  150. postis.listen("jitsiSystemMessage", onSystemMessage);
  151. initCommands();
  152. },
  153. /**
  154. * Notify external application (if API is enabled) that message was sent.
  155. * @param {string} body message body
  156. */
  157. notifySendingChatMessage (body) {
  158. triggerEvent("outgoing-message", {"message": body});
  159. },
  160. /**
  161. * Notify external application (if API is enabled) that
  162. * message was received.
  163. * @param {string} id user id
  164. * @param {string} nick user nickname
  165. * @param {string} body message body
  166. * @param {number} ts message creation timestamp
  167. */
  168. notifyReceivedChatMessage (id, nick, body, ts) {
  169. if (APP.conference.isLocalId(id)) {
  170. return;
  171. }
  172. triggerEvent(
  173. "incoming-message",
  174. {"from": id, "nick": nick, "message": body, "stamp": ts}
  175. );
  176. },
  177. /**
  178. * Notify external application (if API is enabled) that
  179. * user joined the conference.
  180. * @param {string} id user id
  181. */
  182. notifyUserJoined (id) {
  183. triggerEvent("participant-joined", {id});
  184. },
  185. /**
  186. * Notify external application (if API is enabled) that
  187. * user left the conference.
  188. * @param {string} id user id
  189. */
  190. notifyUserLeft (id) {
  191. triggerEvent("participant-left", {id});
  192. },
  193. /**
  194. * Notify external application (if API is enabled) that
  195. * user changed their nickname.
  196. * @param {string} id user id
  197. * @param {string} displayName user nickname
  198. */
  199. notifyDisplayNameChanged (id, displayName) {
  200. triggerEvent("display-name-change", {id, displayname: displayName});
  201. },
  202. /**
  203. * Notify external application (if API is enabled) that
  204. * user changed their nickname.
  205. * @param {string} id user id
  206. * @param {string} displayName user nickname
  207. */
  208. notifyConferenceJoined (room) {
  209. triggerEvent("video-conference-joined", {roomName: room});
  210. },
  211. /**
  212. * Notify external application (if API is enabled) that
  213. * user changed their nickname.
  214. * @param {string} id user id
  215. * @param {string} displayName user nickname
  216. */
  217. notifyConferenceLeft (room) {
  218. triggerEvent("video-conference-left", {roomName: room});
  219. },
  220. /**
  221. * Removes the listeners.
  222. */
  223. dispose: function () {
  224. if(enabled)
  225. postis.destroy();
  226. }
  227. };