瀏覽代碼

Don't rely on `getUniqueId` of Strophe.Connection

since we can't assume it's always available.
JC Brand 5 年之前
父節點
當前提交
54e9c51a1a

+ 1 - 1
src/converse-muc-views.js

@@ -1238,7 +1238,7 @@ converse.plugins.add('converse-muc-views', {
                             _converse.api.send($pres({
                             _converse.api.send($pres({
                                 from: _converse.connection.jid,
                                 from: _converse.connection.jid,
                                 to: `${jid}/${args}`,
                                 to: `${jid}/${args}`,
-                                id: _converse.connection.getUniqueId()
+                                id: u.getUniqueId()
                             }).tree());
                             }).tree());
                         }
                         }
                         break;
                         break;

+ 2 - 1
src/converse-register.js

@@ -23,6 +23,7 @@ import utils from "@converse/headless/utils/form";
 
 
 // Strophe methods for building stanzas
 // Strophe methods for building stanzas
 const { Strophe, Backbone, sizzle, $iq, _ } = converse.env;
 const { Strophe, Backbone, sizzle, $iq, _ } = converse.env;
+const u = converse.env.utils;
 
 
 // Add Strophe Namespaces
 // Add Strophe Namespaces
 Strophe.addNamespace('REGISTER', 'jabber:iq:register');
 Strophe.addNamespace('REGISTER', 'jabber:iq:register');
@@ -589,7 +590,7 @@ converse.plugins.add('converse-register', {
                 if (has_empty_inputs) { return; }
                 if (has_empty_inputs) { return; }
 
 
                 const inputs = sizzle(':input:not([type=button]):not([type=submit])', form),
                 const inputs = sizzle(':input:not([type=button]):not([type=submit])', form),
-                      iq = $iq({'type': 'set', 'id': _converse.connection.getUniqueId()})
+                      iq = $iq({'type': 'set', 'id': u.getUniqueId()})
                             .c("query", {xmlns:Strophe.NS.REGISTER});
                             .c("query", {xmlns:Strophe.NS.REGISTER});
 
 
                 if (this.form_type === 'xform') {
                 if (this.form_type === 'xform') {

+ 8 - 8
src/headless/converse-chatboxes.js

@@ -91,7 +91,7 @@ converse.plugins.add('converse-chatboxes', {
 
 
             defaults () {
             defaults () {
                 return {
                 return {
-                    'msgid': _converse.connection.getUniqueId(),
+                    'msgid': u.getUniqueId(),
                     'time': (new Date()).toISOString(),
                     'time': (new Date()).toISOString(),
                     'ephemeral': false
                     'ephemeral': false
                 };
                 };
@@ -620,7 +620,7 @@ converse.plugins.add('converse-chatboxes', {
             sendMarker(to_jid, id, type) {
             sendMarker(to_jid, id, type) {
                 const stanza = $msg({
                 const stanza = $msg({
                     'from': _converse.connection.jid,
                     'from': _converse.connection.jid,
-                    'id': _converse.connection.getUniqueId(),
+                    'id': u.getUniqueId(),
                     'to': to_jid,
                     'to': to_jid,
                     'type': 'chat',
                     'type': 'chat',
                 }).c(type, {'xmlns': Strophe.NS.MARKERS, 'id': id});
                 }).c(type, {'xmlns': Strophe.NS.MARKERS, 'id': id});
@@ -665,7 +665,7 @@ converse.plugins.add('converse-chatboxes', {
             sendReceiptStanza (to_jid, id) {
             sendReceiptStanza (to_jid, id) {
                 const receipt_stanza = $msg({
                 const receipt_stanza = $msg({
                     'from': _converse.connection.jid,
                     'from': _converse.connection.jid,
-                    'id': _converse.connection.getUniqueId(),
+                    'id': u.getUniqueId(),
                     'to': to_jid,
                     'to': to_jid,
                     'type': 'chat',
                     'type': 'chat',
                 }).c('received', {'xmlns': Strophe.NS.RECEIPTS, 'id': id}).up()
                 }).c('received', {'xmlns': Strophe.NS.RECEIPTS, 'id': id}).up()
@@ -704,7 +704,7 @@ converse.plugins.add('converse-chatboxes', {
                         'from': _converse.connection.jid,
                         'from': _converse.connection.jid,
                         'to': this.get('jid'),
                         'to': this.get('jid'),
                         'type': this.get('message_type'),
                         'type': this.get('message_type'),
-                        'id': message.get('edited') && _converse.connection.getUniqueId() || message.get('msgid'),
+                        'id': message.get('edited') && u.getUniqueId() || message.get('msgid'),
                     }).c('body').t(message.get('message')).up()
                     }).c('body').t(message.get('message')).up()
                       .c(_converse.ACTIVE, {'xmlns': Strophe.NS.CHATSTATES}).root();
                       .c(_converse.ACTIVE, {'xmlns': Strophe.NS.CHATSTATES}).root();
 
 
@@ -748,7 +748,7 @@ converse.plugins.add('converse-chatboxes', {
 
 
             getOutgoingMessageAttributes (text, spoiler_hint) {
             getOutgoingMessageAttributes (text, spoiler_hint) {
                 const is_spoiler = this.get('composing_spoiler');
                 const is_spoiler = this.get('composing_spoiler');
-                const origin_id = _converse.connection.getUniqueId();
+                const origin_id = u.getUniqueId();
                 return {
                 return {
                     'id': origin_id,
                     'id': origin_id,
                     'jid': this.get('jid'),
                     'jid': this.get('jid'),
@@ -821,7 +821,7 @@ converse.plugins.add('converse-chatboxes', {
                         'older_versions': older_versions,
                         'older_versions': older_versions,
                         'references': attrs.references,
                         'references': attrs.references,
                         'is_single_emoji':  attrs.message ? u.isSingleEmoji(attrs.message) : false,
                         'is_single_emoji':  attrs.message ? u.isSingleEmoji(attrs.message) : false,
-                        'origin_id': _converse.connection.getUniqueId(),
+                        'origin_id': u.getUniqueId(),
                         'received': undefined
                         'received': undefined
                     });
                     });
                 } else {
                 } else {
@@ -846,7 +846,7 @@ converse.plugins.add('converse-chatboxes', {
                     }
                     }
                     _converse.api.send(
                     _converse.api.send(
                         $msg({
                         $msg({
-                            'id': _converse.connection.getUniqueId(),
+                            'id': u.getUniqueId(),
                             'to': this.get('jid'),
                             'to': this.get('jid'),
                             'type': 'chat'
                             'type': 'chat'
                         }).c(this.get('chat_state'), {'xmlns': Strophe.NS.CHATSTATES}).up()
                         }).c(this.get('chat_state'), {'xmlns': Strophe.NS.CHATSTATES}).up()
@@ -1039,7 +1039,7 @@ converse.plugins.add('converse-chatboxes', {
                 // We prefer to use one of the XEP-0359 unique and stable stanza IDs as the Model id, to avoid duplicates.
                 // We prefer to use one of the XEP-0359 unique and stable stanza IDs as the Model id, to avoid duplicates.
                 attrs['id'] = attrs['origin_id'] ||
                 attrs['id'] = attrs['origin_id'] ||
                     attrs[`stanza_id ${attrs.from}`] ||
                     attrs[`stanza_id ${attrs.from}`] ||
-                    _converse.connection.getUniqueId();
+                    u.getUniqueId();
                 return attrs;
                 return attrs;
             },
             },
 
 

+ 1 - 1
src/headless/converse-mam.js

@@ -477,7 +477,7 @@ converse.plugins.add('converse-mam', {
                         return {'messages': []};
                         return {'messages': []};
                     }
                     }
 
 
-                    const queryid = _converse.connection.getUniqueId();
+                    const queryid = u.getUniqueId();
                     const stanza = $iq(attrs).c('query', {'xmlns':Strophe.NS.MAM, 'queryid':queryid});
                     const stanza = $iq(attrs).c('query', {'xmlns':Strophe.NS.MAM, 'queryid':queryid});
                     if (options) {
                     if (options) {
                         stanza.c('x', {'xmlns':Strophe.NS.XFORM, 'type': 'submit'})
                         stanza.c('x', {'xmlns':Strophe.NS.XFORM, 'type': 'submit'})

+ 3 - 6
src/headless/converse-muc.js

@@ -728,7 +728,7 @@ converse.plugins.add('converse-muc', {
                 const is_spoiler = this.get('composing_spoiler');
                 const is_spoiler = this.get('composing_spoiler');
                 var references;
                 var references;
                 [text, references] = this.parseTextForReferences(text);
                 [text, references] = this.parseTextForReferences(text);
-                const origin_id = _converse.connection.getUniqueId();
+                const origin_id = u.getUniqueId();
 
 
                 return {
                 return {
                     'id': origin_id,
                     'id': origin_id,
@@ -814,7 +814,7 @@ converse.plugins.add('converse-muc', {
                 const invitation = $msg({
                 const invitation = $msg({
                     'from': _converse.connection.jid,
                     'from': _converse.connection.jid,
                     'to': recipient,
                     'to': recipient,
-                    'id': _converse.connection.getUniqueId()
+                    'id': u.getUniqueId()
                 }).c('x', attrs);
                 }).c('x', attrs);
                 _converse.api.send(invitation);
                 _converse.api.send(invitation);
                 /**
                 /**
@@ -1911,10 +1911,7 @@ converse.plugins.add('converse-muc', {
             },
             },
 
 
             initialize (attributes) {
             initialize (attributes) {
-                this.set(Object.assign(
-                    {'id': _converse.connection.getUniqueId()},
-                    attributes)
-                );
+                this.set(Object.assign({'id': u.getUniqueId()}, attributes));
                 this.on('change:image_hash', this.onAvatarChanged, this);
                 this.on('change:image_hash', this.onAvatarChanged, this);
             },
             },
 
 

+ 1 - 1
src/headless/converse-ping.js

@@ -99,7 +99,7 @@ converse.plugins.add('converse-ping', {
                     const iq = $iq({
                     const iq = $iq({
                             'type': 'get',
                             'type': 'get',
                             'to': jid,
                             'to': jid,
-                            'id': _converse.connection.getUniqueId('ping')
+                            'id': u.getUniqueId('ping')
                         }).c('ping', {'xmlns': Strophe.NS.PING});
                         }).c('ping', {'xmlns': Strophe.NS.PING});
 
 
                     const result = await _converse.api.sendIQ(iq, 10000, false);
                     const result = await _converse.api.sendIQ(iq, 10000, false);

+ 1 - 1
src/headless/converse-roster.js

@@ -639,7 +639,7 @@ converse.plugins.add('converse-roster', {
             async fetchFromServer () {
             async fetchFromServer () {
                 const stanza = $iq({
                 const stanza = $iq({
                     'type': 'get',
                     'type': 'get',
-                    'id': _converse.connection.getUniqueId('roster')
+                    'id': u.getUniqueId('roster')
                 }).c('query', {xmlns: Strophe.NS.ROSTER});
                 }).c('query', {xmlns: Strophe.NS.ROSTER});
                 if (this.rosterVersioningSupported()) {
                 if (this.rosterVersioningSupported()) {
                     stanza.attrs({'ver': this.data.get('version')});
                     stanza.attrs({'ver': this.data.get('version')});

+ 11 - 5
src/headless/utils/core.js

@@ -557,13 +557,19 @@ u.placeCaretAtEnd = function (textarea) {
     this.scrollTop = 999999;
     this.scrollTop = 999999;
 };
 };
 
 
-u.getUniqueId = function () {
-    return 'xxxxxxxx-xxxx'.replace(/[x]/g, function(c) {
-        var r = Math.random() * 16 | 0,
-            v = c === 'x' ? r : r & 0x3 | 0x8;
+u.getUniqueId = function (suffix) {
+    const uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
+        const r = Math.random() * 16 | 0,
+                v = c === 'x' ? r : r & 0x3 | 0x8;
         return v.toString(16);
         return v.toString(16);
     });
     });
-};
+    // We prefix the ID with letters so that it's also a valid ID for DOM elements.
+    if (typeof(suffix) === "string" || typeof(suffix) === "number") {
+        return "id" + uuid + ":" + suffix;
+    } else {
+        return "id" + uuid;
+    }
+}
 
 
 
 
 /**
 /**