Skip to content

Commit

Permalink
Add support for avatars stored as PHOTO/EXTVAL in vcards
Browse files Browse the repository at this point in the history
  • Loading branch information
prefiks committed Feb 15, 2013
1 parent 04669e1 commit 5c93d78
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ JSJaCPacket.prototype.getNode = function() {
return null;
};

JSJaCPacket.prototype.$Q = function() {
return $Q(this.getNode());
};

/**
* Sets the 'to' attribute of the root node of this packet
* @param {String} to
Expand Down
51 changes: 42 additions & 9 deletions chrome/oneteam/content/JavaScript/xmpptypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -333,18 +333,49 @@ _DECL_(vCardDataAccessor, null, XMPPDataAccessorBase).prototype =

_handleVCard: function(pkt, value)
{
var photo, photos = pkt.getNode().getElementsByTagName("PHOTO");
var photo, photos = pkt.$Q().all("PHOTO");

for (var i = 0; i < photos.length; i++) {
var binval = photos[i].getElementsByTagName("BINVAL")[0];
if (binval && binval.textContent) {
photo = binval.textContent.replace(/\s/g,"");
break;
this.avatarRetrieved = true;

for (var p in photos) {
var val;

if ((val = p.first("BINVAL")).length) {
photo = val.text().replace(/\s/g,"");
this._handleVCardPhoto(atob(photo));
return;
} else if ((val = p.first("EXTVAL")).length) {
var hash = account.cache.getValue("urlAvatarHash-"+val.text());

if (hash) {
this.avatar = account.cache.getValue("avatar-"+hash, true);
if (this.avatar) {
var jid = this.realJID ? this.realJID.shortJID :
this.jid.resource ? null : this.jid;
var date = new Date(Date.now()+30*24*60*60*1000);

account.cache.bumpExpirationDate("avatar-"+hash, date);
if (jid)
account.cache.setValue("jidAvatar-"+jid, hash, date);

this.avatarHash = hash;
this.modelUpdated("avatar");

return;
}
}
slurpFileAsync(new Callback(this._avatarImageReceived, this).
addArgs(val.text()), val.text());
}
}
this._handleVCardPhoto(null);
},

this.avatarRetrieved = true;
_avatarImageReceived: function(data, url) {
this._handleVCardPhoto(data, url)
},

_handleVCardPhoto: function(photo, url) {
var jid = this.realJID ? this.realJID.shortJID :
this.jid.resource ? null : this.jid;

Expand All @@ -359,11 +390,13 @@ _DECL_(vCardDataAccessor, null, XMPPDataAccessorBase).prototype =
return;
}

photo = atob(photo);

this.avatarHash = hex_sha1(photo);
this.avatar = account.cache.getValue("avatar-"+this.avatarHash, true);

if (url)
account.cache.setValue("urlAvatarHash-"+url, this.avatarHash,
new Date(Date.now()+30*24*60*60*1000))

if (jid)
account.cache.setValue("jidAvatar-"+jid, this.avatarHash,
new Date(Date.now()+30*24*60*60*1000));
Expand Down

0 comments on commit 5c93d78

Please sign in to comment.