Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

API.js 6.0KB

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