瀏覽代碼

Fire JitsiConferenceEvents.ENDPOINT_MESSAGE_RECEIVED events

via JSON messages received via the Chatroom
dev1
nikvaessen 8 年之前
父節點
當前提交
cca3251fdd
共有 3 個檔案被更改,包括 64 行新增1 行删除
  1. 17
    0
      JitsiConferenceEventManager.js
  2. 40
    0
      modules/xmpp/ChatRoom.js
  3. 7
    1
      service/xmpp/XMPPEvents.js

+ 17
- 0
JitsiConferenceEventManager.js 查看文件

@@ -300,6 +300,23 @@ JitsiConferenceEventManager.prototype.setupChatRoomListeners = function() {
300 300
                 JitsiConferenceEvents.USER_STATUS_CHANGED, id, status);
301 301
         });
302 302
 
303
+    chatRoom.addListener(XMPPEvents.JSON_MESSAGE_RECEIVED,
304
+        (from, payload) => {
305
+            const participant = conference.getParticipantById(
306
+                from.split('/')[1]);
307
+
308
+            if (participant) {
309
+                conference.eventEmitter.emit(
310
+                    JitsiConferenceEvents.ENDPOINT_MESSAGE_RECEIVED,
311
+                    participant, payload);
312
+            } else {
313
+                logger.warn(
314
+                    'Ignored ENDPOINT_MESSAGE_RECEIVED for not existing '
315
+                    + `participant: ${from}`,
316
+                    payload);
317
+            }
318
+        });
319
+
303 320
     chatRoom.addPresenceListener('startmuted', (data, from) => {
304 321
         let isModerator = false;
305 322
 

+ 40
- 0
modules/xmpp/ChatRoom.js 查看文件

@@ -79,6 +79,33 @@ function filterNodeFromPresenceJSON(pres, nodeName) {
79 79
     return res;
80 80
 }
81 81
 
82
+/**
83
+ * Check if the given argument is a valid JSON string by parsing it.
84
+ * If it successfully parses, the JSON object is returned.
85
+ *
86
+ * @param jsonString check if this string is a valid json string
87
+ * @returns {boolean, object} if given object is a valid JSON string, return
88
+ * the json object. Otherwise, return false;
89
+ */
90
+function tryParseJSON(jsonString) {
91
+    try {
92
+        const o = JSON.parse(jsonString);
93
+
94
+        // Handle non-exception-throwing cases:
95
+        // Neither JSON.parse(false) or JSON.parse(1234) throw errors,
96
+        // hence the type-checking,
97
+        // but... JSON.parse(null) returns null, and
98
+        // typeof null === "object",
99
+        // so we must check for that, too.
100
+        // Thankfully, null is falsey, so this suffices:
101
+        if (o && typeof o === 'object') {
102
+            return o;
103
+        }
104
+    } catch (e) {
105
+        return false;
106
+    }
107
+}
108
+
82 109
 // XXX As ChatRoom constructs XMPP stanzas and Strophe is build around the idea
83 110
 // of chaining function calls, allow long function call chains.
84 111
 /* eslint-disable newline-per-chained-call */
@@ -742,6 +769,19 @@ export default class ChatRoom extends Listenable {
742 769
             this.discoRoomInfo();
743 770
         }
744 771
 
772
+        // todo add a JSON layer such that not every json
773
+        // passed around in the chat is automatically assumed to be
774
+        // json used for messaging
775
+        const json = tryParseJSON(txt);
776
+
777
+        if (json) {
778
+            logger.log('chat json message', from, json);
779
+            this.eventEmitter.emit(XMPPEvents.JSON_MESSAGE_RECEIVED,
780
+                from, json);
781
+
782
+            return;
783
+        }
784
+
745 785
         if (txt) {
746 786
             logger.log('chat', nick, txt);
747 787
             this.eventEmitter.emit(XMPPEvents.MESSAGE_RECEIVED,

+ 7
- 1
service/xmpp/XMPPEvents.js 查看文件

@@ -210,7 +210,13 @@ const XMPPEvents = {
210 210
 
211 211
     // Designates an event indicating that the local ICE connection state has
212 212
     // changed.
213
-    ICE_CONNECTION_STATE_CHANGED: 'xmpp.ice_connection_state_changed'
213
+    ICE_CONNECTION_STATE_CHANGED: 'xmpp.ice_connection_state_changed',
214
+
215
+    /**
216
+     * Event which is emitted when the body in an XMPP message in the MUC
217
+     * contains JSON
218
+     */
219
+    JSON_MESSAGE_RECEIVED: 'xmmp.json_message_received'
214 220
 };
215 221
 
216 222
 module.exports = XMPPEvents;

Loading…
取消
儲存