Pārlūkot izejas kodu

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 gadus atpakaļ
vecāks
revīzija
9207f1f6b9
2 mainītis faili ar 64 papildinājumiem un 40 dzēšanām
  1. 18
    14
      JitsiConference.js
  2. 46
    26
      modules/xmpp/ChatRoom.js

+ 18
- 14
JitsiConference.js Parādīt failu

@@ -43,7 +43,7 @@ import RecordingManager from './modules/recording/RecordingManager';
43 43
 import VideoSIPGW from './modules/videosipgw/VideoSIPGW';
44 44
 import * as VideoSIPGWConstants from './modules/videosipgw/VideoSIPGWConstants';
45 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 48
 import SpeakerStatsCollector from './modules/statistics/SpeakerStatsCollector';
49 49
 
@@ -588,22 +588,27 @@ JitsiConference.prototype.removeCommandListener = function(command) {
588 588
 /**
589 589
  * Sends text message to the other participants in the conference
590 590
  * @param message the text message.
591
+ * @param elementName the element name to encapsulate the message.
591 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 596
     if (this.room) {
595
-        this.room.sendMessage(message);
597
+        this.room.sendMessage(message, elementName);
596 598
     }
597 599
 };
598 600
 
599 601
 /**
600 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 604
  * @param message the text message.
605
+ * @param elementName the element name to encapsulate the message.
602 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 610
     if (this.room) {
606
-        this.room.sendPrivateMessage(id, message);
611
+        this.room.sendPrivateMessage(id, message, elementName);
607 612
     }
608 613
 };
609 614
 
@@ -2144,16 +2149,15 @@ JitsiConference.prototype.sendMessage = function(
2144 2149
     } else {
2145 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 2157
             try {
2158
+                messageToSend[JITSI_MEET_MUC_TYPE] = '';
2156 2159
                 messageToSend = JSON.stringify(messageToSend);
2160
+                elementName = 'json-message';
2157 2161
             } catch (e) {
2158 2162
                 logger.error('Can not send a message, stringify failed: ', e);
2159 2163
 
@@ -2162,10 +2166,10 @@ JitsiConference.prototype.sendMessage = function(
2162 2166
         }
2163 2167
 
2164 2168
         if (to) {
2165
-            this.sendPrivateTextMessage(to, messageToSend);
2169
+            this.sendPrivateTextMessage(to, messageToSend, elementName);
2166 2170
         } else {
2167 2171
             // Broadcast
2168
-            this.sendTextMessage(messageToSend);
2172
+            this.sendTextMessage(messageToSend, elementName);
2169 2173
         }
2170 2174
     }
2171 2175
 

+ 46
- 26
modules/xmpp/ChatRoom.js Parādīt failu

@@ -79,21 +79,19 @@ function filterNodeFromPresenceJSON(pres, nodeName) {
79 79
 /**
80 80
  * The name of the field used to recognize a chat message as carrying a JSON
81 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 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 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 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 96
 function tryParseJSONAndVerify(jsonString) {
99 97
     try {
@@ -107,15 +105,14 @@ function tryParseJSONAndVerify(jsonString) {
107 105
         // so we must check for that, too.
108 106
         // Thankfully, null is falsey, so this suffices:
109 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 114
             logger.debug('parsing valid json but does not have correct '
118
-                + 'structure', 'topic: ', topic, 'payload: ', payload);
115
+                + 'structure', 'topic: ', type);
119 116
         }
120 117
     } catch (e) {
121 118
 
@@ -708,14 +705,23 @@ export default class ChatRoom extends Listenable {
708 705
 
709 706
     /**
710 707
      * Send text message to the other participants in the conference
711
-     * @param body
708
+     * @param message
709
+     * @param elementName
712 710
      * @param nickname
713 711
      */
714
-    sendMessage(body, nickname) {
712
+    sendMessage(message, elementName, nickname) {
715 713
         const msg = $msg({ to: this.roomjid,
716 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 725
         if (nickname) {
720 726
             msg.c('nick', { xmlns: 'http://jabber.org/protocol/nick' })
721 727
                 .t(nickname)
@@ -723,20 +729,30 @@ export default class ChatRoom extends Listenable {
723 729
                 .up();
724 730
         }
725 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 737
      * Send private text message to another participant of the conference
731 738
      * @param id id/muc resource of the receiver
732
-     * @param body
739
+     * @param message
740
+     * @param elementName
733 741
      * @param nickname
734 742
      */
735
-    sendPrivateMessage(id, body, nickname) {
743
+    sendPrivateMessage(id, message, elementName, nickname) {
736 744
         const msg = $msg({ to: `${this.roomjid}/${id}`,
737 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 756
         if (nickname) {
741 757
             msg.c('nick', { xmlns: 'http://jabber.org/protocol/nick' })
742 758
                 .t(nickname)
@@ -745,9 +761,10 @@ export default class ChatRoom extends Listenable {
745 761
         }
746 762
 
747 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,12 +924,15 @@ export default class ChatRoom extends Listenable {
907 924
                     .length) {
908 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 934
             this.eventEmitter.emit(XMPPEvents.JSON_MESSAGE_RECEIVED,
915
-                from, jsonPayload);
935
+                from, parsedJson);
916 936
 
917 937
             return;
918 938
         }

Notiek ielāde…
Atcelt
Saglabāt