Browse Source

Uses json-message extension in Chatroom.js (#770)

* Uses json message extension

* Sends json-message without the body element

* Info comments and parameter order changes
dev1
Praveen Gupta 7 years ago
parent
commit
9207f1f6b9
2 changed files with 64 additions and 40 deletions
  1. 18
    14
      JitsiConference.js
  2. 46
    26
      modules/xmpp/ChatRoom.js

+ 18
- 14
JitsiConference.js View File

43
 import VideoSIPGW from './modules/videosipgw/VideoSIPGW';
43
 import VideoSIPGW from './modules/videosipgw/VideoSIPGW';
44
 import * as VideoSIPGWConstants from './modules/videosipgw/VideoSIPGWConstants';
44
 import * as VideoSIPGWConstants from './modules/videosipgw/VideoSIPGWConstants';
45
 import * as XMPPEvents from './service/xmpp/XMPPEvents';
45
 import * as XMPPEvents from './service/xmpp/XMPPEvents';
46
-import { JITSI_MEET_MUC_TOPIC } from './modules/xmpp/ChatRoom';
46
+import { JITSI_MEET_MUC_TYPE } from './modules/xmpp/ChatRoom';
47
 
47
 
48
 import SpeakerStatsCollector from './modules/statistics/SpeakerStatsCollector';
48
 import SpeakerStatsCollector from './modules/statistics/SpeakerStatsCollector';
49
 
49
 
588
 /**
588
 /**
589
  * Sends text message to the other participants in the conference
589
  * Sends text message to the other participants in the conference
590
  * @param message the text message.
590
  * @param message the text message.
591
+ * @param elementName the element name to encapsulate the message.
591
  * @deprecated Use 'sendMessage' instead. TODO: this should be private.
592
  * @deprecated Use 'sendMessage' instead. TODO: this should be private.
592
  */
593
  */
593
-JitsiConference.prototype.sendTextMessage = function(message) {
594
+JitsiConference.prototype.sendTextMessage = function(
595
+        message, elementName = 'body') {
594
     if (this.room) {
596
     if (this.room) {
595
-        this.room.sendMessage(message);
597
+        this.room.sendMessage(message, elementName);
596
     }
598
     }
597
 };
599
 };
598
 
600
 
599
 /**
601
 /**
600
  * Send private text message to another participant of the conference
602
  * Send private text message to another participant of the conference
603
+ * @param id the id of the participant to send a private message.
601
  * @param message the text message.
604
  * @param message the text message.
605
+ * @param elementName the element name to encapsulate the message.
602
  * @deprecated Use 'sendMessage' instead. TODO: this should be private.
606
  * @deprecated Use 'sendMessage' instead. TODO: this should be private.
603
  */
607
  */
604
-JitsiConference.prototype.sendPrivateTextMessage = function(id, message) {
608
+JitsiConference.prototype.sendPrivateTextMessage = function(
609
+        id, message, elementName = 'body') {
605
     if (this.room) {
610
     if (this.room) {
606
-        this.room.sendPrivateMessage(id, message);
611
+        this.room.sendPrivateMessage(id, message, elementName);
607
     }
612
     }
608
 };
613
 };
609
 
614
 
2144
     } else {
2149
     } else {
2145
         let messageToSend = message;
2150
         let messageToSend = message;
2146
 
2151
 
2147
-        if (messageType === 'object') {
2148
-            // Encapsulate the object in the jitsi-meet format, and convert it
2149
-            // to JSON.
2150
-            messageToSend = {
2151
-                payload: message,
2152
-                [JITSI_MEET_MUC_TOPIC]: ''
2153
-            };
2152
+        // Name of packet extension of message stanza to send the required
2153
+        // message in.
2154
+        let elementName = 'body';
2154
 
2155
 
2156
+        if (messageType === 'object') {
2155
             try {
2157
             try {
2158
+                messageToSend[JITSI_MEET_MUC_TYPE] = '';
2156
                 messageToSend = JSON.stringify(messageToSend);
2159
                 messageToSend = JSON.stringify(messageToSend);
2160
+                elementName = 'json-message';
2157
             } catch (e) {
2161
             } catch (e) {
2158
                 logger.error('Can not send a message, stringify failed: ', e);
2162
                 logger.error('Can not send a message, stringify failed: ', e);
2159
 
2163
 
2162
         }
2166
         }
2163
 
2167
 
2164
         if (to) {
2168
         if (to) {
2165
-            this.sendPrivateTextMessage(to, messageToSend);
2169
+            this.sendPrivateTextMessage(to, messageToSend, elementName);
2166
         } else {
2170
         } else {
2167
             // Broadcast
2171
             // Broadcast
2168
-            this.sendTextMessage(messageToSend);
2172
+            this.sendTextMessage(messageToSend, elementName);
2169
         }
2173
         }
2170
     }
2174
     }
2171
 
2175
 

+ 46
- 26
modules/xmpp/ChatRoom.js View File

79
 /**
79
 /**
80
  * The name of the field used to recognize a chat message as carrying a JSON
80
  * The name of the field used to recognize a chat message as carrying a JSON
81
  * payload from another endpoint.
81
  * payload from another endpoint.
82
- * If the body of a chat message contains a valid JSON object, and the JSON has
83
- * this key, then the transported payload is contained in the 'payload'
84
- * property of the JSON object.
82
+ * If the json-message of a chat message contains a valid JSON object, and the
83
+ * JSON has this key, then it is a valid json-message to be sent.
85
  */
84
  */
86
-export const JITSI_MEET_MUC_TOPIC = 'jitsi-meet-muc-msg-topic';
85
+export const JITSI_MEET_MUC_TYPE = 'type';
87
 
86
 
88
 /**
87
 /**
89
  * Check if the given argument is a valid JSON ENDPOINT_MESSAGE string by
88
  * Check if the given argument is a valid JSON ENDPOINT_MESSAGE string by
90
- * parsing it and checking if it has a field called 'jitsi-meet-muc-msg-topic'
91
- * and a field called 'payload'
89
+ * parsing it and checking if it has a field called 'type'.
92
  *
90
  *
93
  * @param {string} jsonString check if this string is a valid json string
91
  * @param {string} jsonString check if this string is a valid json string
94
- * and contains the special structure
92
+ * and contains the special structure.
95
  * @returns {boolean, object} if given object is a valid JSON string, return
93
  * @returns {boolean, object} if given object is a valid JSON string, return
96
- * the json object. Otherwise, return false;
94
+ * the json object. Otherwise, returns false.
97
  */
95
  */
98
 function tryParseJSONAndVerify(jsonString) {
96
 function tryParseJSONAndVerify(jsonString) {
99
     try {
97
     try {
107
         // so we must check for that, too.
105
         // so we must check for that, too.
108
         // Thankfully, null is falsey, so this suffices:
106
         // Thankfully, null is falsey, so this suffices:
109
         if (json && typeof json === 'object') {
107
         if (json && typeof json === 'object') {
110
-            const topic = json[JITSI_MEET_MUC_TOPIC];
111
-            const payload = json.payload;
108
+            const type = json[JITSI_MEET_MUC_TYPE];
112
 
109
 
113
-            if ((typeof topic !== 'undefined') && payload) {
114
-                return payload;
110
+            if (typeof type !== 'undefined') {
111
+                return json;
115
             }
112
             }
116
 
113
 
117
             logger.debug('parsing valid json but does not have correct '
114
             logger.debug('parsing valid json but does not have correct '
118
-                + 'structure', 'topic: ', topic, 'payload: ', payload);
115
+                + 'structure', 'topic: ', type);
119
         }
116
         }
120
     } catch (e) {
117
     } catch (e) {
121
 
118
 
708
 
705
 
709
     /**
706
     /**
710
      * Send text message to the other participants in the conference
707
      * Send text message to the other participants in the conference
711
-     * @param body
708
+     * @param message
709
+     * @param elementName
712
      * @param nickname
710
      * @param nickname
713
      */
711
      */
714
-    sendMessage(body, nickname) {
712
+    sendMessage(message, elementName, nickname) {
715
         const msg = $msg({ to: this.roomjid,
713
         const msg = $msg({ to: this.roomjid,
716
             type: 'groupchat' });
714
             type: 'groupchat' });
717
 
715
 
718
-        msg.c('body', body).up();
716
+        // We are adding the message in a packet extension. If this element
717
+        // is different from 'body', we add a custom namespace.
718
+        // e.g. for 'json-message' extension of message stanza.
719
+        if (elementName === 'body') {
720
+            msg.c(elementName, message).up();
721
+        } else {
722
+            msg.c(elementName, { xmlns: 'http://jitsi.org/jitmeet' }, message)
723
+                .up();
724
+        }
719
         if (nickname) {
725
         if (nickname) {
720
             msg.c('nick', { xmlns: 'http://jabber.org/protocol/nick' })
726
             msg.c('nick', { xmlns: 'http://jabber.org/protocol/nick' })
721
                 .t(nickname)
727
                 .t(nickname)
723
                 .up();
729
                 .up();
724
         }
730
         }
725
         this.connection.send(msg);
731
         this.connection.send(msg);
726
-        this.eventEmitter.emit(XMPPEvents.SENDING_CHAT_MESSAGE, body);
732
+        this.eventEmitter.emit(XMPPEvents.SENDING_CHAT_MESSAGE, message);
727
     }
733
     }
728
 
734
 
735
+    /* eslint-disable max-params */
729
     /**
736
     /**
730
      * Send private text message to another participant of the conference
737
      * Send private text message to another participant of the conference
731
      * @param id id/muc resource of the receiver
738
      * @param id id/muc resource of the receiver
732
-     * @param body
739
+     * @param message
740
+     * @param elementName
733
      * @param nickname
741
      * @param nickname
734
      */
742
      */
735
-    sendPrivateMessage(id, body, nickname) {
743
+    sendPrivateMessage(id, message, elementName, nickname) {
736
         const msg = $msg({ to: `${this.roomjid}/${id}`,
744
         const msg = $msg({ to: `${this.roomjid}/${id}`,
737
             type: 'chat' });
745
             type: 'chat' });
738
 
746
 
739
-        msg.c('body', body).up();
747
+        // We are adding the message in packet. If this element is different
748
+        // from 'body', we add our custom namespace for the same.
749
+        // e.g. for 'json-message' message extension.
750
+        if (elementName === 'body') {
751
+            msg.c(elementName, message).up();
752
+        } else {
753
+            msg.c(elementName, { xmlns: 'http://jitsi.org/jitmeet' }, message)
754
+                .up();
755
+        }
740
         if (nickname) {
756
         if (nickname) {
741
             msg.c('nick', { xmlns: 'http://jabber.org/protocol/nick' })
757
             msg.c('nick', { xmlns: 'http://jabber.org/protocol/nick' })
742
                 .t(nickname)
758
                 .t(nickname)
745
         }
761
         }
746
 
762
 
747
         this.connection.send(msg);
763
         this.connection.send(msg);
748
-        this.eventEmitter.emit(XMPPEvents.SENDING_PRIVATE_CHAT_MESSAGE, body);
764
+        this.eventEmitter.emit(
765
+            XMPPEvents.SENDING_PRIVATE_CHAT_MESSAGE, message);
749
     }
766
     }
750
-
767
+    /* eslint-enable max-params */
751
 
768
 
752
     /**
769
     /**
753
      *
770
      *
907
                     .length) {
924
                     .length) {
908
             this.discoRoomInfo();
925
             this.discoRoomInfo();
909
         }
926
         }
927
+        const jsonMessage = $(msg).find('>json-message').text();
928
+        const parsedJson = tryParseJSONAndVerify(jsonMessage);
910
 
929
 
911
-        const jsonPayload = tryParseJSONAndVerify(txt);
912
-
913
-        if (jsonPayload) {
930
+        // We emit this event if the message is a valid json, and is not
931
+        // delivered after a delay, i.e. stamp is undefined.
932
+        // e.g. - subtitles should not be displayed if delayed.
933
+        if (parsedJson && stamp === undefined) {
914
             this.eventEmitter.emit(XMPPEvents.JSON_MESSAGE_RECEIVED,
934
             this.eventEmitter.emit(XMPPEvents.JSON_MESSAGE_RECEIVED,
915
-                from, jsonPayload);
935
+                from, parsedJson);
916
 
936
 
917
             return;
937
             return;
918
         }
938
         }

Loading…
Cancel
Save