]> git.mxchange.org Git - friendica-addons.git/blob - jappixmini/jappix/js/avatar.js
Merge branch '3.6-release'
[friendica-addons.git] / jappixmini / jappix / js / avatar.js
1 /*
2
3 Jappix - An open social platform
4 These are the avatar JS scripts for Jappix
5
6 -------------------------------------------------
7
8 License: AGPL
9 Author: Vanaryon
10 Last revision: 01/03/11
11
12 */
13
14 // Requests the avatar of a given user
15 var AVATAR_PENDING = [];
16
17 function getAvatar(xid, mode, enabled, photo) {
18         /* REF: http://xmpp.org/extensions/xep-0153.html */
19         
20         // No need to get the avatar, another process is yet running
21         if(existArrayValue(AVATAR_PENDING, xid))
22                 return false;
23         
24         // Initialize: XML data is in one SQL entry, because some browser are sloooow with SQL requests
25         var xml = XMLFromString(getPersistent('avatar', xid));
26         var forced = false;
27         
28         // Retrieving forced?
29         if($(xml).find('forced').text() == 'true')
30                 forced = true;
31         
32         // No avatar in presence
33         if(!photo && !forced && (enabled == 'true')) {
34                 // Pending marker
35                 AVATAR_PENDING.push(xid);
36                 
37                 // Reset the avatar
38                 resetAvatar(xid, hex_md5(xid));
39                 
40                 logThis('No avatar for: ' + xid, 2);
41         }
42         
43         // Try to catch the avatar
44         else {
45                 // Define some stuffs
46                 var type = $(xml).find('type').text();
47                 var binval = $(xml).find('binval').text();
48                 var checksum = $(xml).find('checksum').text();
49                 var updated = false;
50                 
51                 // Process the checksum of the avatar
52                 if((checksum == photo) || (photo == 'forget') || forced)
53                         updated = true;
54                 
55                 // If the avatar is yet stored and a new retrieving is not needed
56                 if((mode == 'cache') && type && binval && checksum && updated) {
57                         // Pending marker
58                         AVATAR_PENDING.push(xid);
59                         
60                         // Display the cache avatar
61                         displayAvatar(xid, hex_md5(xid), type, binval);
62                         
63                         logThis('Read avatar from cache: ' + xid, 3);
64                 }
65                 
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')) {
68                         // Pending marker
69                         AVATAR_PENDING.push(xid);
70                         
71                         // Get the latest avatar
72                         var iq = new JSJaCIQ();
73                         iq.setType('get');
74                         iq.setTo(xid);
75                         
76                         iq.appendNode('vCard', {'xmlns': NS_VCARD});
77                         
78                         con.send(iq, handleAvatar);
79                         
80                         logThis('Get avatar from server: ' + xid, 3);
81                 }
82         }
83         
84         return true;
85 }
86
87 // Handles the avatar
88 function handleAvatar(iq) {
89         // Extract the XML values
90         var handleXML = iq.getNode();
91         var handleFrom = fullXID(getStanzaFrom(iq));
92         
93         // Is this me? Remove the resource!
94         if(bareXID(handleFrom) == getXID())
95                 handleFrom = bareXID(handleFrom);
96         
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;
102         
103         // Read our own checksum
104         if(handleFrom == getXID()) {
105                 oChecksum = getDB('checksum', 1);
106                 
107                 // Avoid the "null" value
108                 if(!oChecksum)
109                         oChecksum = '';
110         }
111         
112         // vCard not empty?
113         if(find.size()) {
114                 // We get our profile details
115                 if(handleFrom == getXID()) {
116                         // Get the names
117                         var names = generateBuddyName(iq);
118                         
119                         // Write the values to the database
120                         setDB('profile', 'name', names[0]);
121                         setDB('profile', 'nick', names[1]);
122                 }
123                 
124                 // We get the avatar
125                 var aType = find.find('TYPE:first').text();
126                 var aBinval = find.find('BINVAL:first').text();
127                 
128                 // No binval?
129                 if(!aBinval) {
130                         aType = 'none';
131                         aBinval = 'none';
132                 }
133                 
134                 // Enough data
135                 else {
136                         // No type?
137                         if(!aType)
138                                 aType = 'image/png';
139                         
140                         // Process the checksum
141                         else
142                                 aChecksum = hex_sha1(Base64.decode(aBinval));
143                 }
144                 
145                 // We display the user avatar
146                 displayAvatar(handleFrom, hash, aType, aBinval);
147                 
148                 // Store the avatar
149                 setPersistent('avatar', handleFrom, '<avatar><type>' + aType + '</type><binval>' + aBinval + '</binval><checksum>' + aChecksum + '</checksum><forced>false</forced></avatar>');
150                 
151                 logThis('Avatar retrieved from server: ' + handleFrom, 3);
152         }
153         
154         // vCard is empty
155         else
156                 resetAvatar(handleFrom);
157         
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;
162                 
163                 if(pChecksum == 'none')
164                         pChecksum = '';
165                 
166                 // Update our temp. checksum
167                 setDB('checksum', 1, pChecksum);
168                 
169                 // Send the stanza
170                 if(FIRST_PRESENCE_SENT)
171                         presenceSend(pChecksum);
172                 else
173                         getStorage(NS_OPTIONS);
174         }
175 }
176
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>');
181         
182         // Display the empty avatar
183         displayAvatar(xid, hash, 'none', 'none');
184 }
185
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="';
191         
192         // If the avatar exists
193         if((type != 'none') && (binval != 'none'))
194                 code += 'data:' + type + ';base64,' + binval;
195         else
196                 code += './img/others/default-avatar.png';
197         
198         code += '" alt="" />';
199         
200         // Replace with the new avatar (in the roster and in the chat)
201         $('.' + container).html(code);
202         
203         // We can remove the pending marker
204         removeArrayValue(AVATAR_PENDING, xid);
205 }