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 5.9KB

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