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.

API.js 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /*
  2. * Copyright @ 2015 Atlassian Pty Ltd
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /**
  17. * Implements API class that communicates with external api class
  18. * and provides interface to access Jitsi Meet features by external
  19. * applications that embed Jitsi Meet
  20. */
  21. var XMPPEvents = require("../../service/xmpp/XMPPEvents");
  22. /**
  23. * List of the available commands.
  24. * @type {{
  25. * displayName: inputDisplayNameHandler,
  26. * muteAudio: toggleAudio,
  27. * muteVideo: toggleVideo,
  28. * filmStrip: toggleFilmStrip
  29. * }}
  30. */
  31. var commands =
  32. {
  33. displayName: APP.UI.inputDisplayNameHandler,
  34. muteAudio: APP.UI.toggleAudio,
  35. muteVideo: APP.UI.toggleVideo,
  36. toggleFilmStrip: APP.UI.toggleFilmStrip,
  37. toggleChat: APP.UI.toggleChat,
  38. toggleContactList: APP.UI.toggleContactList
  39. };
  40. /**
  41. * Maps the supported events and their status
  42. * (true it the event is enabled and false if it is disabled)
  43. * @type {{
  44. * incomingMessage: boolean,
  45. * outgoingMessage: boolean,
  46. * displayNameChange: boolean,
  47. * participantJoined: boolean,
  48. * participantLeft: boolean
  49. * }}
  50. */
  51. var events =
  52. {
  53. incomingMessage: false,
  54. outgoingMessage:false,
  55. displayNameChange: false,
  56. participantJoined: false,
  57. participantLeft: false
  58. };
  59. var displayName = {};
  60. /**
  61. * Processes commands from external applicaiton.
  62. * @param message the object with the command
  63. */
  64. function processCommand(message)
  65. {
  66. if(message.action != "execute")
  67. {
  68. console.error("Unknown action of the message");
  69. return;
  70. }
  71. for(var key in message)
  72. {
  73. if(commands[key])
  74. commands[key].apply(null, message[key]);
  75. }
  76. }
  77. /**
  78. * Processes events objects from external applications
  79. * @param event the event
  80. */
  81. function processEvent(event) {
  82. if(!event.action)
  83. {
  84. console.error("Event with no action is received.");
  85. return;
  86. }
  87. var i = 0;
  88. switch(event.action)
  89. {
  90. case "add":
  91. for(; i < event.events.length; i++)
  92. {
  93. events[event.events[i]] = true;
  94. }
  95. break;
  96. case "remove":
  97. for(; i < event.events.length; i++)
  98. {
  99. events[event.events[i]] = false;
  100. }
  101. break;
  102. default:
  103. console.error("Unknown action for event.");
  104. }
  105. }
  106. /**
  107. * Sends message to the external application.
  108. * @param object
  109. */
  110. function sendMessage(object) {
  111. window.parent.postMessage(JSON.stringify(object), "*");
  112. }
  113. /**
  114. * Processes a message event from the external application
  115. * @param event the message event
  116. */
  117. function processMessage(event)
  118. {
  119. var message;
  120. try {
  121. message = JSON.parse(event.data);
  122. } catch (e) {}
  123. if(!message.type)
  124. return;
  125. switch (message.type)
  126. {
  127. case "command":
  128. processCommand(message);
  129. break;
  130. case "event":
  131. processEvent(message);
  132. break;
  133. default:
  134. console.error("Unknown type of the message");
  135. return;
  136. }
  137. }
  138. function setupListeners() {
  139. APP.xmpp.addListener(XMPPEvents.MUC_MEMBER_JOINED, function (from) {
  140. API.triggerEvent("participantJoined", {jid: from});
  141. });
  142. APP.xmpp.addListener(XMPPEvents.MESSAGE_RECEIVED, function (from, nick, txt, myjid) {
  143. if (from != myjid)
  144. API.triggerEvent("incomingMessage",
  145. {"from": from, "nick": nick, "message": txt});
  146. });
  147. APP.xmpp.addListener(XMPPEvents.MUC_MEMBER_LEFT, function (jid) {
  148. API.triggerEvent("participantLeft", {jid: jid});
  149. });
  150. APP.xmpp.addListener(XMPPEvents.DISPLAY_NAME_CHANGED, function (jid, newDisplayName) {
  151. name = displayName[jid];
  152. if(!name || name != newDisplayName) {
  153. API.triggerEvent("displayNameChange", {jid: jid, displayname: newDisplayName});
  154. displayName[jid] = newDisplayName;
  155. }
  156. });
  157. APP.xmpp.addListener(XMPPEvents.SENDING_CHAT_MESSAGE, function (body) {
  158. APP.API.triggerEvent("outgoingMessage", {"message": body});
  159. });
  160. }
  161. var API = {
  162. /**
  163. * Check whether the API should be enabled or not.
  164. * @returns {boolean}
  165. */
  166. isEnabled: function () {
  167. var hash = location.hash;
  168. if(hash && hash.indexOf("external") > -1 && window.postMessage)
  169. return true;
  170. return false;
  171. },
  172. /**
  173. * Initializes the APIConnector. Setups message event listeners that will
  174. * receive information from external applications that embed Jitsi Meet.
  175. * It also sends a message to the external application that APIConnector
  176. * is initialized.
  177. */
  178. init: function () {
  179. if (window.addEventListener)
  180. {
  181. window.addEventListener('message',
  182. processMessage, false);
  183. }
  184. else
  185. {
  186. window.attachEvent('onmessage', processMessage);
  187. }
  188. sendMessage({type: "system", loaded: true});
  189. setupListeners();
  190. },
  191. /**
  192. * Checks whether the event is enabled ot not.
  193. * @param name the name of the event.
  194. * @returns {*}
  195. */
  196. isEventEnabled: function (name) {
  197. return events[name];
  198. },
  199. /**
  200. * Sends event object to the external application that has been subscribed
  201. * for that event.
  202. * @param name the name event
  203. * @param object data associated with the event
  204. */
  205. triggerEvent: function (name, object) {
  206. if(this.isEnabled() && this.isEventEnabled(name))
  207. sendMessage({
  208. type: "event", action: "result", event: name, result: object});
  209. },
  210. /**
  211. * Removes the listeners.
  212. */
  213. dispose: function () {
  214. if(window.removeEventListener)
  215. {
  216. window.removeEventListener("message",
  217. processMessage, false);
  218. }
  219. else
  220. {
  221. window.detachEvent('onmessage', processMessage);
  222. }
  223. }
  224. };
  225. module.exports = API;