Skip to content

Commit bef0945

Browse files
committed
Looser coupling with converse-vcard
To make it easier to remove it without breaking stuff.
1 parent d3a80e8 commit bef0945

File tree

5 files changed

+54
-21
lines changed

5 files changed

+54
-21
lines changed

src/converse-chatboxviews.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,22 +23,24 @@ const AvatarMixin = {
2323
if (_.isNull(canvas_el)) {
2424
return;
2525
}
26-
const image_type = this.model.vcard.get('image_type'),
27-
image = this.model.vcard.get('image');
28-
29-
canvas_el.outerHTML = tpl_avatar({
26+
const data = {
3027
'classes': canvas_el.getAttribute('class'),
3128
'width': canvas_el.width,
3229
'height': canvas_el.height,
33-
'image': "data:" + image_type + ";base64," + image,
34-
});
30+
}
31+
if (this.model.vcard) {
32+
const image_type = this.model.vcard.get('image_type'),
33+
image = this.model.vcard.get('image');
34+
data['image'] = "data:" + image_type + ";base64," + image;
35+
}
36+
canvas_el.outerHTML = tpl_avatar(data);
3537
},
3638
};
3739

3840

3941
converse.plugins.add('converse-chatboxviews', {
4042

41-
dependencies: ["converse-chatboxes"],
43+
dependencies: ["converse-chatboxes", "converse-vcard"],
4244

4345
overrides: {
4446
// Overrides mentioned here will be picked up by converse.js's

src/headless/converse-chatboxes.js

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,10 @@ converse.plugins.add('converse-chatboxes', {
118118
},
119119

120120
setVCard () {
121+
if (!_converse.vcards) {
122+
// VCards aren't supported
123+
return;
124+
}
121125
if (this.get('type') === 'error') {
122126
return;
123127
} else if (this.get('type') === 'groupchat') {
@@ -135,11 +139,12 @@ converse.plugins.add('converse-chatboxes', {
135139
getDisplayName () {
136140
if (this.get('type') === 'groupchat') {
137141
return this.get('nick');
142+
} else if (this.contact) {
143+
return this.contact.getDisplayName();
144+
} else if (this.vcard) {
145+
return this.vcard.getDisplayName();
138146
} else {
139-
if (this.contact) {
140-
return this.contact.getDisplayName();
141-
}
142-
return this.vcard.get('nickname') || this.vcard.get('fullname') || this.get('from');
147+
return this.get('from');
143148
}
144149
},
145150

@@ -271,12 +276,15 @@ converse.plugins.add('converse-chatboxes', {
271276
// but we're in embedded mode.
272277
return;
273278
}
274-
275-
this.vcard = _converse.vcards.findWhere({'jid': jid}) || _converse.vcards.create({'jid': jid});
276279
// XXX: this creates a dependency on converse-roster, which we
277280
// probably shouldn't have here, so we should probably move
278281
// ChatBox out of converse-chatboxes
279282
this.presence = _converse.presences.findWhere({'jid': jid}) || _converse.presences.create({'jid': jid});
283+
284+
if (_converse.vcards) {
285+
this.vcard = _converse.vcards.findWhere({'jid': jid}) || _converse.vcards.create({'jid': jid});
286+
}
287+
280288
if (this.get('type') === _converse.PRIVATE_CHAT_TYPE) {
281289
this.setRosterContact(jid);
282290
}
@@ -317,8 +325,11 @@ converse.plugins.add('converse-chatboxes', {
317325
getDisplayName () {
318326
if (this.contact) {
319327
return this.contact.getDisplayName();
328+
} else if (this.vcard) {
329+
return this.vcard.getDisplayName();
330+
} else {
331+
return this.get('jid');
320332
}
321-
return this.vcard.get('nickname') || this.vcard.get('fullname') || this.get('jid');
322333
},
323334

324335
getUpdatedMessageAttributes (message, stanza) {

src/headless/converse-core.js

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1059,11 +1059,6 @@ _converse.initialize = async function (settings, callback) {
10591059
},
10601060

10611061
initialize () {
1062-
this.vcard = _converse.vcards.findWhere({'jid': this.get('jid')});
1063-
if (_.isNil(this.vcard)) {
1064-
this.vcard = _converse.vcards.create({'jid': this.get('jid')});
1065-
}
1066-
10671062
this.on('change:status', (item) => {
10681063
const status = this.get('status');
10691064
this.sendPresence(status);

src/headless/converse-roster.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,10 @@ converse.plugins.add('converse-roster', {
211211
},
212212

213213
setVCard () {
214+
if (!_converse.vcards) {
215+
// VCards aren't supported
216+
return;
217+
}
214218
const jid = this.get('jid');
215219
this.vcard = _converse.vcards.findWhere({'jid': jid}) || _converse.vcards.create({'jid': jid});
216220
},
@@ -258,11 +262,21 @@ converse.plugins.add('converse-roster', {
258262
},
259263

260264
getDisplayName () {
261-
return this.get('nickname') || this.vcard.get('nickname') || this.vcard.get('fullname') || this.get('jid');
265+
if (this.get('nickname')) {
266+
return this.get('nickname');
267+
} else if (this.vcard) {
268+
return this.vcard.getDisplayName();
269+
} else {
270+
return this.get('jid');
271+
}
262272
},
263273

264274
getFullname () {
265-
return this.vcard.get('fullname');
275+
if (this.vcard) {
276+
return this.vcard.get('fullname');
277+
} else {
278+
return this.get('jid');
279+
}
266280
},
267281

268282
/**

src/headless/converse-vcard.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ converse.plugins.add('converse-vcard', {
4343
} else {
4444
return Backbone.Model.prototype.set.apply(this, arguments);
4545
}
46+
},
47+
48+
getDisplayName () {
49+
return this.get('nickname') || this.get('fullname') || this.get('jid');
4650
}
4751
});
4852

@@ -124,6 +128,13 @@ converse.plugins.add('converse-vcard', {
124128
_converse.api.listen.on('sessionInitialized', _converse.initVCardCollection);
125129

126130

131+
_converse.api.listen.on('statusInitialized', () => {
132+
const vcards = _converse.vcards;
133+
const jid = _converse.xmppstatus.get('jid');
134+
_converse.xmppstatus.vcard = vcards.findWhere({'jid': jid}) || vcards.create({'jid': jid});
135+
});
136+
137+
127138
_converse.api.listen.on('addClientFeatures', () => {
128139
_converse.api.disco.own.features.add(Strophe.NS.VCARD);
129140
});

0 commit comments

Comments
 (0)