3 Jappix - An open social platform
4 These are the avatar JS scripts for Jappix
6 -------------------------------------------------
10 Last revision: 01/03/11
14 // Requests the avatar of a given user
15 var AVATAR_PENDING = [];
17 function getAvatar(xid, mode, enabled, photo) {
18 /* REF: http://xmpp.org/extensions/xep-0153.html */
20 // No need to get the avatar, another process is yet running
21 if(existArrayValue(AVATAR_PENDING, xid))
24 // Initialize: XML data is in one SQL entry, because some browser are sloooow with SQL requests
25 var xml = XMLFromString(getPersistent('avatar', xid));
29 if($(xml).find('forced').text() == 'true')
32 // No avatar in presence
33 if(!photo && !forced && (enabled == 'true')) {
35 AVATAR_PENDING.push(xid);
38 resetAvatar(xid, hex_md5(xid));
40 logThis('No avatar for: ' + xid, 2);
43 // Try to catch the avatar
46 var type = $(xml).find('type').text();
47 var binval = $(xml).find('binval').text();
48 var checksum = $(xml).find('checksum').text();
51 // Process the checksum of the avatar
52 if((checksum == photo) || (photo == 'forget') || forced)
55 // If the avatar is yet stored and a new retrieving is not needed
56 if((mode == 'cache') && type && binval && checksum && updated) {
58 AVATAR_PENDING.push(xid);
60 // Display the cache avatar
61 displayAvatar(xid, hex_md5(xid), type, binval);
63 logThis('Read avatar from cache: ' + xid, 3);
66 // Else if the request has not yet been fired, we get it
67 else if((!updated || (mode == 'cache' && !updated) || (mode == 'force') || (photo = 'forget')) && (enabled != 'false')) {
69 AVATAR_PENDING.push(xid);
71 // Get the latest avatar
72 var iq = new JSJaCIQ();
76 iq.appendNode('vCard', {'xmlns': NS_VCARD});
78 con.send(iq, handleAvatar);
80 logThis('Get avatar from server: ' + xid, 3);
88 function handleAvatar(iq) {
89 // Extract the XML values
90 var handleXML = iq.getNode();
91 var handleFrom = fullXID(getStanzaFrom(iq));
93 // Is this me? Remove the resource!
94 if(bareXID(handleFrom) == getXID())
95 handleFrom = bareXID(handleFrom);
97 // Get some other values
98 var hash = hex_md5(handleFrom);
99 var find = $(handleXML).find('vCard');
100 var aChecksum = 'none';
101 var oChecksum = null;
103 // Read our own checksum
104 if(handleFrom == getXID()) {
105 oChecksum = getDB('checksum', 1);
107 // Avoid the "null" value
114 // We get our profile details
115 if(handleFrom == getXID()) {
117 var names = generateBuddyName(iq);
119 // Write the values to the database
120 setDB('profile', 'name', names[0]);
121 setDB('profile', 'nick', names[1]);
125 var aType = find.find('TYPE:first').text();
126 var aBinval = find.find('BINVAL:first').text();
140 // Process the checksum
142 aChecksum = hex_sha1(Base64.decode(aBinval));
145 // We display the user avatar
146 displayAvatar(handleFrom, hash, aType, aBinval);
149 setPersistent('avatar', handleFrom, '<avatar><type>' + aType + '</type><binval>' + aBinval + '</binval><checksum>' + aChecksum + '</checksum><forced>false</forced></avatar>');
151 logThis('Avatar retrieved from server: ' + handleFrom, 3);
156 resetAvatar(handleFrom);
158 // We got a new checksum for us?
159 if(((oChecksum != null) && (oChecksum != aChecksum)) || !FIRST_PRESENCE_SENT) {
160 // Define a proper checksum
161 var pChecksum = aChecksum;
163 if(pChecksum == 'none')
166 // Update our temp. checksum
167 setDB('checksum', 1, pChecksum);
170 if(FIRST_PRESENCE_SENT)
171 presenceSend(pChecksum);
173 getStorage(NS_OPTIONS);
177 // Reset the avatar of an user
178 function resetAvatar(xid, hash) {
179 // Store the empty avatar
180 setPersistent('avatar', xid, '<avatar><type>none</type><binval>none</binval><checksum>none</checksum><forced>false</forced></avatar>');
182 // Display the empty avatar
183 displayAvatar(xid, hash, 'none', 'none');
186 // Displays the avatar of an user
187 function displayAvatar(xid, hash, type, binval) {
188 // Initialize the vars
189 var container = hash + ' .avatar-container';
190 var code = '<img class="avatar" src="';
192 // If the avatar exists
193 if((type != 'none') && (binval != 'none'))
194 code += 'data:' + type + ';base64,' + binval;
196 code += './img/others/default-avatar.png';
198 code += '" alt="" />';
200 // Replace with the new avatar (in the roster and in the chat)
201 $('.' + container).html(code);
203 // We can remove the pending marker
204 removeArrayValue(AVATAR_PENDING, xid);