Переглянути джерело

Merge pull request #456 from isymchych/rewrite-API-module

do not use xmpp module in API module
j8
hristoterezov 9 роки тому
джерело
коміт
c2cfd4d6e2
3 змінених файлів з 107 додано та 76 видалено
  1. 6
    10
      app.js
  2. 7
    1
      conference.js
  3. 94
    65
      modules/API/API.js

+ 6
- 10
app.js Переглянути файл

@@ -19,10 +19,10 @@ import RoomnameGenerator from './modules/util/RoomnameGenerator';
19 19
 import UI from "./modules/UI/UI";
20 20
 import statistics from "./modules/statistics/statistics";
21 21
 import settings from "./modules/settings/Settings";
22
-import UIEvents from './service/UI/UIEvents';
23
-
24 22
 import conference from './conference';
23
+import API from './modules/API/API';
25 24
 
25
+import UIEvents from './service/UI/UIEvents';
26 26
 
27 27
 
28 28
 function buildRoomName () {
@@ -61,9 +61,9 @@ const APP = {
61 61
     UI,
62 62
     statistics,
63 63
     settings,
64
+    conference,
65
+    API,
64 66
     init () {
65
-        this.conference = conference;
66
-        this.API = require("./modules/API/API");
67 67
         this.connectionquality =
68 68
             require("./modules/connectionquality/connectionquality");
69 69
         this.desktopsharing =
@@ -140,17 +140,13 @@ $(document).ready(function () {
140 140
 
141 141
     APP.translation.init(settings.getLanguage());
142 142
 
143
-    if (APP.API.isEnabled()) {
144
-        APP.API.init();
145
-    }
143
+    APP.API.init();
146 144
 
147 145
     obtainConfigAndInit();
148 146
 });
149 147
 
150 148
 $(window).bind('beforeunload', function () {
151
-    if (APP.API.isEnabled()) {
152
-        APP.API.dispose();
153
-    }
149
+    APP.API.dispose();
154 150
 });
155 151
 
156 152
 module.exports = APP;

+ 7
- 1
conference.js Переглянути файл

@@ -346,6 +346,7 @@ export default {
346 346
 
347 347
         room.on(ConferenceEvents.USER_JOINED, (id, user) => {
348 348
             console.log('USER %s connnected', id, user);
349
+            APP.API.notifyUserJoined(id);
349 350
             // FIXME email???
350 351
             APP.UI.addUser(id, user.getDisplayName());
351 352
 
@@ -354,6 +355,7 @@ export default {
354 355
         });
355 356
         room.on(ConferenceEvents.USER_LEFT, (id, user) => {
356 357
             console.log('USER %s LEFT', id, user);
358
+            APP.API.notifyUserLeft(id);
357 359
             APP.UI.removeUser(id, user.getDisplayName());
358 360
             APP.UI.stopPrezi(id);
359 361
         });
@@ -435,11 +437,14 @@ export default {
435 437
                 APP.UI.markVideoInterrupted(false);
436 438
             });
437 439
             room.on(ConferenceEvents.MESSAGE_RECEIVED, (id, text, ts) => {
438
-                APP.UI.addMessage(id, getDisplayName(id), text, ts);
440
+                let nick = getDisplayName(id);
441
+                APP.API.notifyReceivedChatMessage(id, nick, text, ts);
442
+                APP.UI.addMessage(id, nick, text, ts);
439 443
             });
440 444
         }
441 445
 
442 446
         room.on(ConferenceEvents.DISPLAY_NAME_CHANGED, (id, displayName) => {
447
+            APP.API.notifyDisplayNameChanged(id, displayName);
443 448
             APP.UI.changeDisplayName(id, displayName);
444 449
         });
445 450
 
@@ -482,6 +487,7 @@ export default {
482 487
 
483 488
         if (!interfaceConfig.filmStripOnly) {
484 489
             APP.UI.addListener(UIEvents.MESSAGE_CREATED, (message) => {
490
+                APP.API.notifySendingChatMessage(message);
485 491
                 room.sendTextMessage(message);
486 492
             });
487 493
         }

+ 94
- 65
modules/API/API.js Переглянути файл

@@ -5,8 +5,6 @@
5 5
  * applications that embed Jitsi Meet
6 6
  */
7 7
 
8
-var XMPPEvents = require("../../service/xmpp/XMPPEvents");
9
-
10 8
 /**
11 9
  * List of the available commands.
12 10
  * @type {{
@@ -43,7 +41,7 @@ function initCommands() {
43 41
  *              participantLeft: boolean
44 42
  *      }}
45 43
  */
46
-var events = {
44
+const events = {
47 45
     incomingMessage: false,
48 46
     outgoingMessage:false,
49 47
     displayNameChange: false,
@@ -51,8 +49,6 @@ var events = {
51 49
     participantLeft: false
52 50
 };
53 51
 
54
-var displayName = {};
55
-
56 52
 /**
57 53
  * Processes commands from external application.
58 54
  * @param message the object with the command
@@ -128,44 +124,42 @@ function processMessage(event) {
128 124
     }
129 125
 }
130 126
 
131
-function setupListeners() {
132
-    APP.xmpp.addListener(XMPPEvents.MUC_MEMBER_JOINED, function (from) {
133
-        API.triggerEvent("participantJoined", {jid: from});
134
-    });
135
-    APP.xmpp.addListener(XMPPEvents.MESSAGE_RECEIVED,
136
-                         function (from, nick, txt, myjid, stamp) {
137
-        if (from != myjid)
138
-            API.triggerEvent("incomingMessage",
139
-                {"from": from, "nick": nick, "message": txt, "stamp": stamp});
140
-    });
141
-    APP.xmpp.addListener(XMPPEvents.MUC_MEMBER_LEFT, function (jid) {
142
-        API.triggerEvent("participantLeft", {jid: jid});
143
-    });
144
-    APP.xmpp.addListener(XMPPEvents.DISPLAY_NAME_CHANGED,
145
-                         function (jid, newDisplayName) {
146
-        var name = displayName[jid];
147
-        if(!name || name != newDisplayName) {
148
-            API.triggerEvent("displayNameChange",
149
-                             {jid: jid, displayname: newDisplayName});
150
-            displayName[jid] = newDisplayName;
151
-        }
152
-    });
153
-    APP.xmpp.addListener(XMPPEvents.SENDING_CHAT_MESSAGE, function (body) {
154
-        APP.API.triggerEvent("outgoingMessage", {"message": body});
155
-    });
127
+/**
128
+ * Check whether the API should be enabled or not.
129
+ * @returns {boolean}
130
+ */
131
+function isEnabled () {
132
+    let hash = location.hash;
133
+    return hash && hash.indexOf("external") > -1 && window.postMessage;
156 134
 }
157 135
 
158
-var API = {
159
-    /**
160
-     * Check whether the API should be enabled or not.
161
-     * @returns {boolean}
162
-     */
163
-    isEnabled: function () {
164
-        var hash = location.hash;
165
-        if (hash && hash.indexOf("external") > -1 && window.postMessage)
166
-            return true;
167
-        return false;
168
-    },
136
+/**
137
+ * Checks whether the event is enabled ot not.
138
+ * @param name the name of the event.
139
+ * @returns {*}
140
+ */
141
+function isEventEnabled (name) {
142
+    return events[name];
143
+}
144
+
145
+/**
146
+ * Sends event object to the external application that has been subscribed
147
+ * for that event.
148
+ * @param name the name event
149
+ * @param object data associated with the event
150
+ */
151
+function triggerEvent (name, object) {
152
+    if (this.isEnabled() && isEventEnabled(name)) {
153
+        sendMessage({
154
+            type: "event",
155
+            action: "result",
156
+            event: name,
157
+            result: object
158
+        });
159
+    }
160
+}
161
+
162
+export default {
169 163
     /**
170 164
      * Initializes the APIConnector. Setups message event listeners that will
171 165
      * receive information from external applications that embed Jitsi Meet.
@@ -173,50 +167,85 @@ var API = {
173 167
      * is initialized.
174 168
      */
175 169
     init: function () {
170
+        if (!isEnabled()) {
171
+            return;
172
+        }
176 173
         initCommands();
177 174
         if (window.addEventListener) {
178
-            window.addEventListener('message',
179
-                processMessage, false);
180
-        }
181
-        else {
175
+            window.addEventListener('message', processMessage, false);
176
+        } else {
182 177
             window.attachEvent('onmessage', processMessage);
183 178
         }
184 179
         sendMessage({type: "system", loaded: true});
185
-        setupListeners();
186 180
     },
181
+
182
+    /**
183
+     * Notify external application (if API is enabled) that message was sent.
184
+     * @param {string} body message body
185
+     */
186
+    notifySendingChatMessage (body) {
187
+        triggerEvent("outgoingMessage", {"message": body});
188
+    },
189
+
187 190
     /**
188
-     * Checks whether the event is enabled ot not.
189
-     * @param name the name of the event.
190
-     * @returns {*}
191
+     * Notify external application (if API is enabled) that
192
+     * message was received.
193
+     * @param {string} id user id
194
+     * @param {string} nick user nickname
195
+     * @param {string} body message body
196
+     * @param {number} ts message creation timestamp
191 197
      */
192
-    isEventEnabled: function (name) {
193
-        return events[name];
198
+    notifyReceivedChatMessage (id, nick, body, ts) {
199
+        if (APP.conference.isLocalId(id)) {
200
+            return;
201
+        }
202
+
203
+        triggerEvent(
204
+            "incomingMessage",
205
+            {"from": id, "nick": nick, "message": body, "stamp": ts}
206
+        );
194 207
     },
195 208
 
196 209
     /**
197
-     * Sends event object to the external application that has been subscribed
198
-     * for that event.
199
-     * @param name the name event
200
-     * @param object data associated with the event
210
+     * Notify external application (if API is enabled) that
211
+     * user joined the conference.
212
+     * @param {string} id user id
201 213
      */
202
-    triggerEvent: function (name, object) {
203
-        if(this.isEnabled() && this.isEventEnabled(name))
204
-            sendMessage({
205
-                type: "event", action: "result", event: name, result: object});
214
+    notifyUserJoined (id) {
215
+        triggerEvent("participantJoined", {id});
216
+    },
217
+
218
+    /**
219
+     * Notify external application (if API is enabled) that
220
+     * user left the conference.
221
+     * @param {string} id user id
222
+     */
223
+    notifyUserLeft (id) {
224
+        triggerEvent("participantLeft", {id});
225
+    },
226
+
227
+    /**
228
+     * Notify external application (if API is enabled) that
229
+     * user changed their nickname.
230
+     * @param {string} id user id
231
+     * @param {string} displayName user nickname
232
+     */
233
+    notifyDisplayNameChanged (id, displayName) {
234
+        triggerEvent("displayNameChange", {id, displayname: displayName});
206 235
     },
207 236
 
208 237
     /**
209 238
      * Removes the listeners.
210 239
      */
211 240
     dispose: function () {
212
-        if(window.removeEventListener) {
213
-            window.removeEventListener("message",
214
-                processMessage, false);
241
+        if (!isEnabled()) {
242
+            return;
215 243
         }
216
-        else {
244
+
245
+        if (window.removeEventListener) {
246
+            window.removeEventListener("message", processMessage, false);
247
+        } else {
217 248
             window.detachEvent('onmessage', processMessage);
218 249
         }
219 250
     }
220 251
 };
221
-
222
-module.exports = API;

Завантаження…
Відмінити
Зберегти