Selaa lähdekoodia

Fire JitsiConferenceEvents.ENDPOINT_MESSAGE_RECEIVED events

via JSON messages received via the Chatroom
dev1
nikvaessen 8 vuotta sitten
vanhempi
commit
cca3251fdd
3 muutettua tiedostoa jossa 64 lisäystä ja 1 poistoa
  1. 17
    0
      JitsiConferenceEventManager.js
  2. 40
    0
      modules/xmpp/ChatRoom.js
  3. 7
    1
      service/xmpp/XMPPEvents.js

+ 17
- 0
JitsiConferenceEventManager.js Näytä tiedosto

300
                 JitsiConferenceEvents.USER_STATUS_CHANGED, id, status);
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
     chatRoom.addPresenceListener('startmuted', (data, from) => {
320
     chatRoom.addPresenceListener('startmuted', (data, from) => {
304
         let isModerator = false;
321
         let isModerator = false;
305
 
322
 

+ 40
- 0
modules/xmpp/ChatRoom.js Näytä tiedosto

79
     return res;
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
 // XXX As ChatRoom constructs XMPP stanzas and Strophe is build around the idea
109
 // XXX As ChatRoom constructs XMPP stanzas and Strophe is build around the idea
83
 // of chaining function calls, allow long function call chains.
110
 // of chaining function calls, allow long function call chains.
84
 /* eslint-disable newline-per-chained-call */
111
 /* eslint-disable newline-per-chained-call */
742
             this.discoRoomInfo();
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
         if (txt) {
785
         if (txt) {
746
             logger.log('chat', nick, txt);
786
             logger.log('chat', nick, txt);
747
             this.eventEmitter.emit(XMPPEvents.MESSAGE_RECEIVED,
787
             this.eventEmitter.emit(XMPPEvents.MESSAGE_RECEIVED,

+ 7
- 1
service/xmpp/XMPPEvents.js Näytä tiedosto

210
 
210
 
211
     // Designates an event indicating that the local ICE connection state has
211
     // Designates an event indicating that the local ICE connection state has
212
     // changed.
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
 module.exports = XMPPEvents;
222
 module.exports = XMPPEvents;

Loading…
Peruuta
Tallenna