|
@@ -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;
|