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