Skip to content

Commit 54e9c51

Browse files
committed
Don't rely on getUniqueId of Strophe.Connection
since we can't assume it's always available.
1 parent 445ff4f commit 54e9c51

File tree

8 files changed

+28
-24
lines changed

8 files changed

+28
-24
lines changed

src/converse-muc-views.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1238,7 +1238,7 @@ converse.plugins.add('converse-muc-views', {
12381238
_converse.api.send($pres({
12391239
from: _converse.connection.jid,
12401240
to: `${jid}/${args}`,
1241-
id: _converse.connection.getUniqueId()
1241+
id: u.getUniqueId()
12421242
}).tree());
12431243
}
12441244
break;

src/converse-register.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import utils from "@converse/headless/utils/form";
2323

2424
// Strophe methods for building stanzas
2525
const { Strophe, Backbone, sizzle, $iq, _ } = converse.env;
26+
const u = converse.env.utils;
2627

2728
// Add Strophe Namespaces
2829
Strophe.addNamespace('REGISTER', 'jabber:iq:register');
@@ -589,7 +590,7 @@ converse.plugins.add('converse-register', {
589590
if (has_empty_inputs) { return; }
590591

591592
const inputs = sizzle(':input:not([type=button]):not([type=submit])', form),
592-
iq = $iq({'type': 'set', 'id': _converse.connection.getUniqueId()})
593+
iq = $iq({'type': 'set', 'id': u.getUniqueId()})
593594
.c("query", {xmlns:Strophe.NS.REGISTER});
594595

595596
if (this.form_type === 'xform') {

src/headless/converse-chatboxes.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ converse.plugins.add('converse-chatboxes', {
9191

9292
defaults () {
9393
return {
94-
'msgid': _converse.connection.getUniqueId(),
94+
'msgid': u.getUniqueId(),
9595
'time': (new Date()).toISOString(),
9696
'ephemeral': false
9797
};
@@ -620,7 +620,7 @@ converse.plugins.add('converse-chatboxes', {
620620
sendMarker(to_jid, id, type) {
621621
const stanza = $msg({
622622
'from': _converse.connection.jid,
623-
'id': _converse.connection.getUniqueId(),
623+
'id': u.getUniqueId(),
624624
'to': to_jid,
625625
'type': 'chat',
626626
}).c(type, {'xmlns': Strophe.NS.MARKERS, 'id': id});
@@ -665,7 +665,7 @@ converse.plugins.add('converse-chatboxes', {
665665
sendReceiptStanza (to_jid, id) {
666666
const receipt_stanza = $msg({
667667
'from': _converse.connection.jid,
668-
'id': _converse.connection.getUniqueId(),
668+
'id': u.getUniqueId(),
669669
'to': to_jid,
670670
'type': 'chat',
671671
}).c('received', {'xmlns': Strophe.NS.RECEIPTS, 'id': id}).up()
@@ -704,7 +704,7 @@ converse.plugins.add('converse-chatboxes', {
704704
'from': _converse.connection.jid,
705705
'to': this.get('jid'),
706706
'type': this.get('message_type'),
707-
'id': message.get('edited') && _converse.connection.getUniqueId() || message.get('msgid'),
707+
'id': message.get('edited') && u.getUniqueId() || message.get('msgid'),
708708
}).c('body').t(message.get('message')).up()
709709
.c(_converse.ACTIVE, {'xmlns': Strophe.NS.CHATSTATES}).root();
710710

@@ -748,7 +748,7 @@ converse.plugins.add('converse-chatboxes', {
748748

749749
getOutgoingMessageAttributes (text, spoiler_hint) {
750750
const is_spoiler = this.get('composing_spoiler');
751-
const origin_id = _converse.connection.getUniqueId();
751+
const origin_id = u.getUniqueId();
752752
return {
753753
'id': origin_id,
754754
'jid': this.get('jid'),
@@ -821,7 +821,7 @@ converse.plugins.add('converse-chatboxes', {
821821
'older_versions': older_versions,
822822
'references': attrs.references,
823823
'is_single_emoji': attrs.message ? u.isSingleEmoji(attrs.message) : false,
824-
'origin_id': _converse.connection.getUniqueId(),
824+
'origin_id': u.getUniqueId(),
825825
'received': undefined
826826
});
827827
} else {
@@ -846,7 +846,7 @@ converse.plugins.add('converse-chatboxes', {
846846
}
847847
_converse.api.send(
848848
$msg({
849-
'id': _converse.connection.getUniqueId(),
849+
'id': u.getUniqueId(),
850850
'to': this.get('jid'),
851851
'type': 'chat'
852852
}).c(this.get('chat_state'), {'xmlns': Strophe.NS.CHATSTATES}).up()
@@ -1039,7 +1039,7 @@ converse.plugins.add('converse-chatboxes', {
10391039
// We prefer to use one of the XEP-0359 unique and stable stanza IDs as the Model id, to avoid duplicates.
10401040
attrs['id'] = attrs['origin_id'] ||
10411041
attrs[`stanza_id ${attrs.from}`] ||
1042-
_converse.connection.getUniqueId();
1042+
u.getUniqueId();
10431043
return attrs;
10441044
},
10451045

src/headless/converse-mam.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -477,7 +477,7 @@ converse.plugins.add('converse-mam', {
477477
return {'messages': []};
478478
}
479479

480-
const queryid = _converse.connection.getUniqueId();
480+
const queryid = u.getUniqueId();
481481
const stanza = $iq(attrs).c('query', {'xmlns':Strophe.NS.MAM, 'queryid':queryid});
482482
if (options) {
483483
stanza.c('x', {'xmlns':Strophe.NS.XFORM, 'type': 'submit'})

src/headless/converse-muc.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -728,7 +728,7 @@ converse.plugins.add('converse-muc', {
728728
const is_spoiler = this.get('composing_spoiler');
729729
var references;
730730
[text, references] = this.parseTextForReferences(text);
731-
const origin_id = _converse.connection.getUniqueId();
731+
const origin_id = u.getUniqueId();
732732

733733
return {
734734
'id': origin_id,
@@ -814,7 +814,7 @@ converse.plugins.add('converse-muc', {
814814
const invitation = $msg({
815815
'from': _converse.connection.jid,
816816
'to': recipient,
817-
'id': _converse.connection.getUniqueId()
817+
'id': u.getUniqueId()
818818
}).c('x', attrs);
819819
_converse.api.send(invitation);
820820
/**
@@ -1911,10 +1911,7 @@ converse.plugins.add('converse-muc', {
19111911
},
19121912

19131913
initialize (attributes) {
1914-
this.set(Object.assign(
1915-
{'id': _converse.connection.getUniqueId()},
1916-
attributes)
1917-
);
1914+
this.set(Object.assign({'id': u.getUniqueId()}, attributes));
19181915
this.on('change:image_hash', this.onAvatarChanged, this);
19191916
},
19201917

src/headless/converse-ping.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ converse.plugins.add('converse-ping', {
9999
const iq = $iq({
100100
'type': 'get',
101101
'to': jid,
102-
'id': _converse.connection.getUniqueId('ping')
102+
'id': u.getUniqueId('ping')
103103
}).c('ping', {'xmlns': Strophe.NS.PING});
104104

105105
const result = await _converse.api.sendIQ(iq, 10000, false);

src/headless/converse-roster.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -639,7 +639,7 @@ converse.plugins.add('converse-roster', {
639639
async fetchFromServer () {
640640
const stanza = $iq({
641641
'type': 'get',
642-
'id': _converse.connection.getUniqueId('roster')
642+
'id': u.getUniqueId('roster')
643643
}).c('query', {xmlns: Strophe.NS.ROSTER});
644644
if (this.rosterVersioningSupported()) {
645645
stanza.attrs({'ver': this.data.get('version')});

src/headless/utils/core.js

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -557,13 +557,19 @@ u.placeCaretAtEnd = function (textarea) {
557557
this.scrollTop = 999999;
558558
};
559559

560-
u.getUniqueId = function () {
561-
return 'xxxxxxxx-xxxx'.replace(/[x]/g, function(c) {
562-
var r = Math.random() * 16 | 0,
563-
v = c === 'x' ? r : r & 0x3 | 0x8;
560+
u.getUniqueId = function (suffix) {
561+
const uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
562+
const r = Math.random() * 16 | 0,
563+
v = c === 'x' ? r : r & 0x3 | 0x8;
564564
return v.toString(16);
565565
});
566-
};
566+
// We prefix the ID with letters so that it's also a valid ID for DOM elements.
567+
if (typeof(suffix) === "string" || typeof(suffix) === "number") {
568+
return "id" + uuid + ":" + suffix;
569+
} else {
570+
return "id" + uuid;
571+
}
572+
}
567573

568574

569575
/**

0 commit comments

Comments
 (0)