1
0
Fork 0
mirror of https://github.com/YunoHost-Apps/jappix_ynh.git synced 2024-09-03 19:26:19 +02:00
jappix_ynh/source/app/javascripts/avatar.js

290 lines
8.4 KiB
JavaScript
Raw Normal View History

2014-03-12 14:52:47 +01:00
/*
Jappix - An open social platform
These are the avatar JS scripts for Jappix
-------------------------------------------------
License: AGPL
Author: Valérian Saliou, Maranda
*/
// Bundle
var Avatar = (function () {
/**
* Alias of this
* @private
*/
var self = {};
2014-04-08 20:14:28 +02:00
/* Variables */
self.pending = [];
2014-03-12 14:52:47 +01:00
2014-04-08 20:14:28 +02:00
/**
2014-03-12 14:52:47 +01:00
* Requests the avatar of a given user
* @public
* @param {string} xid
* @param {string} mode
* @param {boolean} enabled
* @param {boolean} photo
* @return {boolean}
*/
self.get = function(xid, mode, enabled, photo) {
/* REF: http://xmpp.org/extensions/xep-0153.html */
try {
// No need to get the avatar, another process is yet running
2014-11-25 20:12:58 +01:00
if(Utils.existArrayValue(self.pending, xid)) {
2014-03-12 14:52:47 +01:00
return false;
2014-11-25 20:12:58 +01:00
}
2014-11-25 23:42:38 +01:00
2014-03-12 14:52:47 +01:00
// Initialize: XML data is in one SQL entry, because some browser are sloooow with SQL requests
var xml = Common.XMLFromString(
DataStore.getPersistent('global', 'avatar', xid)
);
var forced = false;
2014-11-25 23:42:38 +01:00
2014-03-12 14:52:47 +01:00
// Retrieving forced?
2014-11-25 20:12:58 +01:00
if($(xml).find('forced').text() == 'true') {
2014-03-12 14:52:47 +01:00
forced = true;
2014-11-25 20:12:58 +01:00
}
2014-11-25 23:42:38 +01:00
2014-03-12 14:52:47 +01:00
// No avatar in presence
if(!photo && !forced && enabled == 'true') {
// Pending marker
self.pending.push(xid);
2014-11-25 23:42:38 +01:00
2014-03-12 14:52:47 +01:00
// Reset the avatar
self.reset(xid, hex_md5(xid));
2014-11-25 23:42:38 +01:00
2014-03-12 14:52:47 +01:00
Console.warn('No avatar for: ' + xid);
}
2014-11-25 23:42:38 +01:00
2014-03-12 14:52:47 +01:00
// Try to catch the avatar
else {
// Define some stuffs
var type = $(xml).find('type').text();
var binval = $(xml).find('binval').text();
var checksum = $(xml).find('checksum').text();
var updated = false;
// Process the checksum of the avatar
2014-11-25 20:12:58 +01:00
if(checksum == photo || photo == 'forget' || forced) {
2014-03-12 14:52:47 +01:00
updated = true;
2014-11-25 20:12:58 +01:00
}
2014-03-12 14:52:47 +01:00
// If the avatar is yet stored and a new retrieving is not needed
if(mode == 'cache' && type && binval && checksum && updated) {
// Pending marker
self.pending.push(xid);
2014-11-25 23:42:38 +01:00
2014-03-12 14:52:47 +01:00
// Display the cache avatar
self.display(xid, hex_md5(xid), type, binval);
2014-11-25 23:42:38 +01:00
2014-03-12 14:52:47 +01:00
Console.info('Read avatar from cache: ' + xid);
}
2014-11-25 23:42:38 +01:00
2014-03-12 14:52:47 +01:00
// Else if the request has not yet been fired, we get it
else if((!updated || mode == 'force' || photo == 'forget') && enabled != 'false') {
// Pending marker
self.pending.push(xid);
2014-11-25 23:42:38 +01:00
2014-03-12 14:52:47 +01:00
// Get the latest avatar
var iq = new JSJaCIQ();
iq.setType('get');
iq.setTo(xid);
2014-11-25 23:42:38 +01:00
2014-03-12 14:52:47 +01:00
iq.appendNode('vCard', {'xmlns': NS_VCARD});
2014-11-25 23:42:38 +01:00
2014-03-12 14:52:47 +01:00
con.send(iq, self.handle);
2014-11-25 23:42:38 +01:00
2014-03-12 14:52:47 +01:00
Console.info('Get avatar from server: ' + xid);
}
}
2014-11-25 23:42:38 +01:00
2014-03-12 14:52:47 +01:00
return true;
} catch(e) {
Console.error('Avatar.get', e);
}
};
/**
* Handles the avatar
* @public
* @param {object} iq
* @return {undefined}
*/
self.handle = function(iq) {
try {
// Extract the XML values
var handleXML = iq.getNode();
var handleFrom = Common.fullXID(Common.getStanzaFrom(iq));
2014-11-25 23:42:38 +01:00
2014-03-12 14:52:47 +01:00
// Is this me? Remove the resource!
if(Common.bareXID(handleFrom) == Common.getXID()) {
handleFrom = Common.bareXID(handleFrom);
}
2014-11-25 23:42:38 +01:00
2014-03-12 14:52:47 +01:00
// Get some other values
var hash = hex_md5(handleFrom);
var find = $(handleXML).find('vCard');
var aChecksum = 'none';
var oChecksum = null;
2014-11-25 23:42:38 +01:00
2014-03-12 14:52:47 +01:00
// Read our own checksum
if(handleFrom == Common.getXID()) {
oChecksum = DataStore.getDB(Connection.desktop_hash, 'checksum', 1);
2014-11-25 23:42:38 +01:00
2014-03-12 14:52:47 +01:00
// Avoid the "null" value
2014-11-25 20:12:58 +01:00
if(!oChecksum) {
2014-03-12 14:52:47 +01:00
oChecksum = '';
2014-11-25 20:12:58 +01:00
}
2014-03-12 14:52:47 +01:00
}
2014-11-25 23:42:38 +01:00
2014-03-12 14:52:47 +01:00
// vCard not empty?
if(find.size()) {
// We get our profile details
if(handleFrom == Common.getXID()) {
// Get the names
var names = Name.generateBuddy(iq);
2014-11-25 20:12:58 +01:00
var phone_number = find.find('TEL:has(NUMBER):first NUMBER:first').text();
2014-11-25 23:42:38 +01:00
2014-03-12 14:52:47 +01:00
// Write the values to the database
DataStore.setDB(Connection.desktop_hash, 'profile', 'name', names[0]);
DataStore.setDB(Connection.desktop_hash, 'profile', 'nick', names[1]);
2014-11-25 20:12:58 +01:00
DataStore.setDB(Connection.desktop_hash, 'profile', 'phone', phone_number);
2014-03-12 14:52:47 +01:00
}
2014-11-25 23:42:38 +01:00
2014-03-12 14:52:47 +01:00
// We get the avatar
var aType = find.find('TYPE:first').text();
var aBinval = find.find('BINVAL:first').text();
2014-11-25 23:42:38 +01:00
2014-03-12 14:52:47 +01:00
// No binval?
if(!aBinval) {
aType = 'none';
aBinval = 'none';
}
2014-11-25 23:42:38 +01:00
2014-03-12 14:52:47 +01:00
// Enough data
else {
// No type?
2014-11-25 20:12:58 +01:00
if(!aType) {
2014-03-12 14:52:47 +01:00
aType = 'image/png';
2014-11-25 20:12:58 +01:00
} else {
2014-03-12 14:52:47 +01:00
aChecksum = hex_sha1(Base64.decode(aBinval));
2014-11-25 20:12:58 +01:00
}
2014-03-12 14:52:47 +01:00
}
2014-11-25 23:42:38 +01:00
2014-03-12 14:52:47 +01:00
// We display the user avatar
self.display(handleFrom, hash, aType, aBinval);
2014-11-25 23:42:38 +01:00
2014-03-12 14:52:47 +01:00
// Store the avatar
DataStore.setPersistent('global', 'avatar', handleFrom, '<avatar><type>' + aType + '</type><binval>' + aBinval + '</binval><checksum>' + aChecksum + '</checksum><forced>false</forced></avatar>');
2014-11-25 23:42:38 +01:00
2014-03-12 14:52:47 +01:00
Console.info('Avatar retrieved from server: ' + handleFrom);
}
2014-11-25 23:42:38 +01:00
2014-03-12 14:52:47 +01:00
// vCard is empty
else {
self.reset(handleFrom);
}
2014-11-25 23:42:38 +01:00
2014-03-12 14:52:47 +01:00
// We got a new checksum for us?
if(((oChecksum !== null) && (oChecksum != aChecksum)) || !Presence.first_sent) {
// Define a proper checksum
var pChecksum = aChecksum;
2014-11-25 23:42:38 +01:00
2014-11-25 20:12:58 +01:00
if(pChecksum == 'none') {
2014-03-12 14:52:47 +01:00
pChecksum = '';
2014-11-25 20:12:58 +01:00
}
2014-11-25 23:42:38 +01:00
2014-03-12 14:52:47 +01:00
// Update our temp. checksum
DataStore.setDB(Connection.desktop_hash, 'checksum', 1, pChecksum);
2014-11-25 23:42:38 +01:00
2014-03-12 14:52:47 +01:00
// Send the stanza
2014-11-25 20:12:58 +01:00
if(!Presence.first_sent) {
2014-03-12 14:52:47 +01:00
Storage.get(NS_OPTIONS);
2014-11-25 20:12:58 +01:00
} else if(DataStore.hasPersistent()) {
2014-03-12 14:52:47 +01:00
Presence.sendActions(pChecksum);
2014-11-25 20:12:58 +01:00
}
2014-03-12 14:52:47 +01:00
}
} catch(e) {
Console.error('Avatar.handle', e);
}
};
/**
* Reset the avatar of an user
* @public
* @param {string} xid
* @param {string} hash
* @return {undefined}
*/
self.reset = function(xid, hash) {
try {
// Store the empty avatar
DataStore.setPersistent('global', 'avatar', xid, '<avatar><type>none</type><binval>none</binval><checksum>none</checksum><forced>false</forced></avatar>');
2014-11-25 23:42:38 +01:00
2014-03-12 14:52:47 +01:00
// Display the empty avatar
self.display(xid, hash, 'none', 'none');
} catch(e) {
Console.error('Avatar.reset', e);
}
};
/**
* Displays the avatar of an user
* @public
* @param {string} xid
* @param {string} hash
* @param {string} type
* @param {string} binval
* @return {undefined}
*/
self.display = function(xid, hash, type, binval) {
try {
// Initialize the vars
var container = hash + ' .avatar-container';
var code = '<img class="avatar" src="';
2014-11-25 23:42:38 +01:00
2014-03-12 14:52:47 +01:00
// If the avatar exists
2014-11-25 20:12:58 +01:00
if((type != 'none') && (binval != 'none')) {
2014-03-12 14:52:47 +01:00
code += 'data:' + type + ';base64,' + binval;
2014-11-25 20:12:58 +01:00
} else {
2014-03-12 14:52:47 +01:00
code += './images/others/default-avatar.png';
2014-11-25 20:12:58 +01:00
}
2014-11-25 23:42:38 +01:00
2014-03-12 14:52:47 +01:00
code += '" alt="" />';
2014-11-25 23:42:38 +01:00
2014-03-12 14:52:47 +01:00
// Replace with the new avatar (in the roster and in the chat)
$('.' + container).html(code);
2014-11-25 23:42:38 +01:00
2014-03-12 14:52:47 +01:00
// We can remove the pending marker
Utils.removeArrayValue(self.pending, xid);
} catch(e) {
Console.error('Avatar.display', e);
}
};
/**
* Return class scope
*/
return self;
})();