mirror of
https://github.com/twisterarmy/twister-html.git
synced 2025-03-13 05:51:03 +00:00
rewrite of twister_io.js: indentation, vars renaming, styling, console messages, misc
This commit is contained in:
parent
f0d80e25c0
commit
c5d178779d
508
js/twister_io.js
508
js/twister_io.js
@ -10,10 +10,10 @@
|
||||
function twisterRpc(method, params, resultFunc, resultArg, errorFunc, errorArg) {
|
||||
// removing hardcoded username from javascript: please use url http://user:pwd@localhost:28332 instead
|
||||
//var foo = new $.JsonRpcClient({ ajaxUrl: '/', username: 'user', password: 'pwd'});
|
||||
var foo = new $.JsonRpcClient({ ajaxUrl: window.location.pathname.replace(/[^\/]*$/, '') });
|
||||
var foo = new $.JsonRpcClient({ajaxUrl: window.location.pathname.replace(/[^\/]*$/, '')});
|
||||
foo.call(method, params,
|
||||
function(ret) { resultFunc(resultArg, ret); },
|
||||
function(ret) { if(ret != null) errorFunc(errorArg, ret); }
|
||||
function(ret) {resultFunc(resultArg, ret);},
|
||||
function(ret) {if (ret != null) errorFunc(errorArg, ret);} // FIXME why only if "(ret != null)"?
|
||||
);
|
||||
}
|
||||
|
||||
@ -36,76 +36,72 @@ var _maxDhtgets = 5;
|
||||
var _queuedDhtgets = [];
|
||||
|
||||
// private function to define a key in _dhtgetPendingMap
|
||||
function _dhtgetLocator(username, resource, multi) {
|
||||
return username+";"+resource+";"+multi;
|
||||
function _dhtgetLocator(peerAlias, resource, multi) {
|
||||
return peerAlias + ';' + resource + ';' + multi;
|
||||
}
|
||||
|
||||
function _dhtgetAddPending(locator, cbFunc, cbArg)
|
||||
{
|
||||
if( !(locator in _dhtgetPendingMap) ) {
|
||||
function _dhtgetAddPending(locator, cbFunc, cbReq) {
|
||||
if (!_dhtgetPendingMap[locator]) {
|
||||
_dhtgetPendingMap[locator] = [];
|
||||
}
|
||||
_dhtgetPendingMap[locator].push( {cbFunc:cbFunc, cbArg:cbArg} );
|
||||
_dhtgetPendingMap[locator].push({cbFunc: cbFunc, cbReq: cbReq});
|
||||
}
|
||||
|
||||
function _dhtgetProcessPending(locator, multi, ret)
|
||||
{
|
||||
if( locator in _dhtgetPendingMap ) {
|
||||
for( var i = 0; i < _dhtgetPendingMap[locator].length; i++) {
|
||||
function _dhtgetProcessPending(locator, multi, ret) {
|
||||
if (_dhtgetPendingMap[locator]) {
|
||||
for (var i = 0; i < _dhtgetPendingMap[locator].length; i++) {
|
||||
var cbFunc = _dhtgetPendingMap[locator][i].cbFunc;
|
||||
var cbArg = _dhtgetPendingMap[locator][i].cbArg;
|
||||
var cbReq = _dhtgetPendingMap[locator][i].cbReq;
|
||||
|
||||
if( multi == 'url' ) {
|
||||
// here is decodeshorturl case
|
||||
cbFunc(cbArg, ret);
|
||||
} else if( multi == 's' ) {
|
||||
if( ret[0] != undefined ) {
|
||||
cbFunc(cbArg, ret[0]["p"]["v"], ret);
|
||||
if (multi === 'url') {
|
||||
cbFunc(cbReq, ret); // here is decodeshorturl case
|
||||
} else if (multi === 's') {
|
||||
if (typeof ret[0] !== 'undefined') {
|
||||
cbFunc(cbReq, ret[0].p.v, ret);
|
||||
} else {
|
||||
cbFunc(cbArg, null);
|
||||
cbFunc(cbReq);
|
||||
}
|
||||
} else {
|
||||
var multiret = [];
|
||||
for (var j = 0; j < ret.length; j++) {
|
||||
multiret.push(ret[j]["p"]["v"]);
|
||||
multiret.push(ret[j].p.v);
|
||||
}
|
||||
cbFunc(cbArg, multiret, ret);
|
||||
cbFunc(cbReq, multiret, ret);
|
||||
}
|
||||
}
|
||||
delete _dhtgetPendingMap[locator];
|
||||
} else {
|
||||
console.log("warning: _dhtgetProcessPending with unknown locator "+locator);
|
||||
console.warn('_dhtgetProcessPending(): unknown locator ' + locator);
|
||||
}
|
||||
}
|
||||
|
||||
function _dhtgetAbortPending(locator)
|
||||
{
|
||||
if( locator in _dhtgetPendingMap ) {
|
||||
for( var i = 0; i < _dhtgetPendingMap[locator].length; i++) {
|
||||
function _dhtgetAbortPending(locator) {
|
||||
if (_dhtgetPendingMap[locator]) {
|
||||
for (var i = 0; i < _dhtgetPendingMap[locator].length; i++) {
|
||||
var cbFunc = _dhtgetPendingMap[locator][i].cbFunc;
|
||||
var cbArg = _dhtgetPendingMap[locator][i].cbArg;
|
||||
cbFunc(cbArg, null);
|
||||
var cbReq = _dhtgetPendingMap[locator][i].cbReq;
|
||||
cbFunc(cbReq);
|
||||
}
|
||||
delete _dhtgetPendingMap[locator];
|
||||
} else {
|
||||
console.log("warning: _dhtgetAbortPending with unknown locator "+locator);
|
||||
console.warn('_dhtgetAbortPending(): unknown locator ' + locator);
|
||||
}
|
||||
}
|
||||
|
||||
// get data from dht resource
|
||||
// the value ["v"] is extracted from response and returned to callback
|
||||
// null is passed to callback in case of an error
|
||||
function dhtget( username, resource, multi, cbFunc, cbArg, timeoutArgs ) {
|
||||
//console.log('dhtget '+username+' '+resource+' '+multi);
|
||||
var locator = _dhtgetLocator(username, resource, multi);
|
||||
if( locator in _dhtgetPendingMap) {
|
||||
_dhtgetAddPending(locator, cbFunc, cbArg);
|
||||
function dhtget(peerAlias, resource, multi, cbFunc, cbReq, timeoutArgs) {
|
||||
//console.log('dhtget ' + peerAlias + ' ' + resource + ' ' + multi);
|
||||
var locator = _dhtgetLocator(peerAlias, resource, multi);
|
||||
if (_dhtgetPendingMap[locator]) {
|
||||
_dhtgetAddPending(locator, cbFunc, cbReq);
|
||||
} else {
|
||||
_dhtgetAddPending(locator, cbFunc, cbArg);
|
||||
_dhtgetAddPending(locator, cbFunc, cbReq);
|
||||
// limit the number of simultaneous dhtgets.
|
||||
// this should leave some sockets for other non-blocking daemon requests.
|
||||
if( _dhtgetsInProgress < _maxDhtgets ) {
|
||||
_dhtgetInternal( username, resource, multi, timeoutArgs );
|
||||
if (_dhtgetsInProgress < _maxDhtgets) {
|
||||
_dhtgetInternal(peerAlias, resource, multi, timeoutArgs);
|
||||
} else {
|
||||
// just queue the locator. it will be unqueue when some dhtget completes.
|
||||
_queuedDhtgets.push(locator);
|
||||
@ -116,15 +112,15 @@ function dhtget( username, resource, multi, cbFunc, cbArg, timeoutArgs ) {
|
||||
// decode shortened url
|
||||
// the expanded url is returned to callback
|
||||
// null is passed to callback in case of an error
|
||||
function decodeshorturl( locator, cbFunc, cbArg, timeoutArgs ) {
|
||||
if( locator in _dhtgetPendingMap) {
|
||||
_dhtgetAddPending(locator, cbFunc, cbArg);
|
||||
function decodeShortURI(locator, cbFunc, cbReq, timeoutArgs) {
|
||||
if (_dhtgetPendingMap[locator]) {
|
||||
_dhtgetAddPending(locator, cbFunc, cbReq);
|
||||
} else {
|
||||
_dhtgetAddPending(locator, cbFunc, cbArg);
|
||||
_dhtgetAddPending(locator, cbFunc, cbReq);
|
||||
// limit the number of simultaneous decodeshorturl's and dhtgets.
|
||||
// this should leave some sockets for other non-blocking daemon requests.
|
||||
if( _dhtgetsInProgress < _maxDhtgets ) {
|
||||
_decodeshorturlInternal( locator, timeoutArgs );
|
||||
if (_dhtgetsInProgress < _maxDhtgets) {
|
||||
_decodeshorturlInternal(locator, timeoutArgs);
|
||||
} else {
|
||||
// just queue the locator. it will be unqueue when some dhtget completes.
|
||||
_queuedDhtgets.push(locator);
|
||||
@ -132,52 +128,54 @@ function decodeshorturl( locator, cbFunc, cbArg, timeoutArgs ) {
|
||||
}
|
||||
}
|
||||
|
||||
function _dhtgetInternal( username, resource, multi, timeoutArgs ) {
|
||||
var locator = _dhtgetLocator(username, resource, multi);
|
||||
function _dhtgetInternal(peerAlias, resource, multi, timeoutArgs) {
|
||||
var locator = _dhtgetLocator(peerAlias, resource, multi);
|
||||
_dhtgetsInProgress++;
|
||||
argsList = [username,resource,multi];
|
||||
if( typeof timeoutArgs !== 'undefined' ) {
|
||||
argsList = [peerAlias, resource, multi];
|
||||
if (typeof timeoutArgs !== 'undefined') {
|
||||
argsList = argsList.concat(timeoutArgs);
|
||||
}
|
||||
twisterRpc("dhtget", argsList,
|
||||
function(args, ret) {
|
||||
_dhtgetsInProgress--;
|
||||
_dhtgetProcessPending(args.locator, args.multi, ret);
|
||||
_dhtgetDequeue();
|
||||
}, {locator:locator,multi:multi},
|
||||
function(cbArg, ret) {
|
||||
console.log("ajax error:" + ret);
|
||||
_dhtgetsInProgress--;
|
||||
_dhtgetAbortPending(locator);
|
||||
_dhtgetDequeue();
|
||||
}, locator);
|
||||
twisterRpc('dhtget', argsList,
|
||||
function(req, ret) {
|
||||
_dhtgetsInProgress--;
|
||||
_dhtgetProcessPending(req.locator, req.multi, ret);
|
||||
_dhtgetDequeue();
|
||||
}, {locator: locator, multi: multi},
|
||||
function(req, ret) {
|
||||
console.warn('RPC "dhtget" error: ' + (ret && ret.message) ? ret.message : ret);
|
||||
_dhtgetsInProgress--;
|
||||
_dhtgetAbortPending(req);
|
||||
_dhtgetDequeue();
|
||||
}, locator
|
||||
);
|
||||
}
|
||||
|
||||
function _decodeshorturlInternal( locator, timeoutArgs ) {
|
||||
function _decodeshorturlInternal(locator, timeoutArgs) {
|
||||
_dhtgetsInProgress++;
|
||||
argsList = [locator];
|
||||
if( typeof timeoutArgs !== 'undefined' ) {
|
||||
if (typeof timeoutArgs !== 'undefined') {
|
||||
argsList = argsList.concat(timeoutArgs);
|
||||
}
|
||||
twisterRpc("decodeshorturl", argsList,
|
||||
function(args, ret) {
|
||||
_dhtgetsInProgress--;
|
||||
_dhtgetProcessPending(args.locator, "url", ret);
|
||||
_dhtgetDequeue();
|
||||
}, {locator:locator},
|
||||
function(cbArg, ret) {
|
||||
console.log("ajax error:" + ret);
|
||||
_dhtgetsInProgress--;
|
||||
_dhtgetAbortPending(locator);
|
||||
_dhtgetDequeue();
|
||||
}, locator);
|
||||
twisterRpc('decodeshorturl', argsList,
|
||||
function(req, ret) {
|
||||
_dhtgetsInProgress--;
|
||||
_dhtgetProcessPending(req, 'url', ret);
|
||||
_dhtgetDequeue();
|
||||
}, locator,
|
||||
function(req, ret) {
|
||||
console.warn('can\'t fetch URI "' + req + '" — RPC "decodeshorturl" error: ' + (ret && ret.message) ? ret.message : ret);
|
||||
_dhtgetsInProgress--;
|
||||
_dhtgetAbortPending(req);
|
||||
_dhtgetDequeue();
|
||||
}, locator
|
||||
);
|
||||
}
|
||||
|
||||
function _dhtgetDequeue() {
|
||||
if( _queuedDhtgets.length ) {
|
||||
if (_queuedDhtgets.length) {
|
||||
var locator = _queuedDhtgets.pop();
|
||||
var locatorSplit = locator.split(";");
|
||||
if( locatorSplit.length == 3) {
|
||||
var locatorSplit = locator.split(';');
|
||||
if (locatorSplit.length === 3) {
|
||||
_dhtgetInternal(locatorSplit[0], locatorSplit[1], locatorSplit[2]);
|
||||
} else {
|
||||
_decodeshorturlInternal( locator )
|
||||
@ -187,67 +185,69 @@ function _dhtgetDequeue() {
|
||||
|
||||
// removes queued dhtgets (requests that have not been made to the daemon)
|
||||
// this is used by user search dropdown to discard old users we are not interested anymore
|
||||
function removeUserFromDhtgetQueue(username) {
|
||||
var resources = ["profile","avatar"]
|
||||
function removeUserFromDhtgetQueue(peerAlias) {
|
||||
var resources = ['profile', 'avatar']
|
||||
for (var i = 0; i < resources.length; i++) {
|
||||
var locator = _dhtgetLocator(username,resources[i],"s");
|
||||
var locator = _dhtgetLocator(peerAlias, resources[i], 's');
|
||||
var locatorIndex = _queuedDhtgets.indexOf(locator);
|
||||
if( locatorIndex > -1 ) {
|
||||
_queuedDhtgets.splice(locatorIndex,1);
|
||||
if (locatorIndex > -1) {
|
||||
_queuedDhtgets.splice(locatorIndex, 1);
|
||||
delete _dhtgetPendingMap[locator];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function removeUsersFromDhtgetQueue(users) {
|
||||
for (var i = 0; i < users.length; i++ ) {
|
||||
removeUserFromDhtgetQueue( users[i] );
|
||||
for (var i = 0; i < users.length; i++) {
|
||||
removeUserFromDhtgetQueue(users[i]);
|
||||
}
|
||||
}
|
||||
|
||||
// store value at the dht resource
|
||||
function dhtput( username, resource, multi, value, sig_user, seq, cbFunc, cbArg ) {
|
||||
twisterRpc("dhtput", [username,resource,multi, value, sig_user, seq],
|
||||
function(args, ret) {
|
||||
if( args.cbFunc )
|
||||
args.cbFunc(args.cbArg, true);
|
||||
}, {cbFunc:cbFunc, cbArg:cbArg},
|
||||
function(args, ret) {
|
||||
console.log("ajax error:" + ret);
|
||||
if( args.cbFunc )
|
||||
args.cbFunc(args.cbArg, false);
|
||||
}, cbArg);
|
||||
function dhtput(peerAlias, resource, multi, value, sig_user, seq, cbFunc, cbReq) {
|
||||
twisterRpc('dhtput', [peerAlias, resource, multi, value, sig_user, seq],
|
||||
function(req, ret) {
|
||||
if (req.cbFunc)
|
||||
req.cbFunc(req.cbReq, true);
|
||||
}, {cbFunc: cbFunc, cbReq: cbReq},
|
||||
function(req, ret) {
|
||||
console.warn('RPC "dhtput" error: ' + (ret && ret.message) ? ret.message : ret);
|
||||
if (req.cbFunc)
|
||||
req.cbFunc(req.cbReq, false);
|
||||
}, {cbFunc: cbFunc, cbReq: cbReq}
|
||||
);
|
||||
}
|
||||
|
||||
// get something from profile and store it in item.text or do callback
|
||||
function getProfileResource( username, resource, item, cbFunc, cbArg ){
|
||||
var profile = undefined;
|
||||
if( username in _profileMap ) {
|
||||
profile = _profileMap[username];
|
||||
// get something from profile and store it in elem.text or do callback
|
||||
function getProfileResource(peerAlias, resource, elem, cbFunc, cbReq) {
|
||||
var profile;
|
||||
if (_profileMap[peerAlias]) {
|
||||
profile = _profileMap[peerAlias];
|
||||
} else {
|
||||
profile = _getResourceFromStorage("profile:" + username);
|
||||
profile = _getResourceFromStorage('profile:' + peerAlias);
|
||||
}
|
||||
if( profile ) {
|
||||
_profileMap[username] = profile;
|
||||
if( item )
|
||||
item.text(profile[resource]);
|
||||
if( cbFunc )
|
||||
cbFunc(cbArg, profile[resource]);
|
||||
if (profile) {
|
||||
_profileMap[peerAlias] = profile;
|
||||
if (elem)
|
||||
elem.text(profile[resource]);
|
||||
if (cbFunc)
|
||||
cbFunc(cbReq, profile[resource]);
|
||||
} else {
|
||||
dhtget( username, "profile", "s",
|
||||
function(args, profile) {
|
||||
if( profile ) {
|
||||
_profileMap[args.username] = profile;
|
||||
_putResourceIntoStorage("profile:" + username, profile);
|
||||
if( args.item )
|
||||
args.item.text(profile[resource]);
|
||||
if( args.cbFunc )
|
||||
args.cbFunc(args.cbArg, profile[resource]);
|
||||
} else {
|
||||
if( args.cbFunc )
|
||||
args.cbFunc(args.cbArg, null);
|
||||
}
|
||||
}, {username:username,item:item,cbFunc:cbFunc,cbArg:cbArg});
|
||||
dhtget(peerAlias, 'profile', 's',
|
||||
function(req, profile) {
|
||||
if (profile) {
|
||||
_profileMap[req.peerAlias] = profile;
|
||||
_putResourceIntoStorage('profile:' + peerAlias, profile);
|
||||
if (req.elem)
|
||||
req.elem.text(profile[resource]);
|
||||
if (req.cbFunc)
|
||||
req.cbFunc(req.cbReq, profile[resource]);
|
||||
} else {
|
||||
if (req.cbFunc)
|
||||
req.cbFunc(req.cbReq);
|
||||
}
|
||||
}, {peerAlias: peerAlias, elem: elem, cbFunc: cbFunc, cbReq: cbReq}
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@ -307,56 +307,61 @@ function getBioToElem(peerAlias, elem) {
|
||||
getProfileResource(peerAlias, 'bio', undefined, fillElemWithTxt, elem);
|
||||
}
|
||||
|
||||
// get tox address and store it in item.text
|
||||
function getTox( username, item ){
|
||||
getProfileResource( username, "tox", false, function(item, text){
|
||||
//item.empty();
|
||||
if(text) {
|
||||
item.attr('href', 'tox:'+text);
|
||||
item.next().attr('data', text).attr('title', 'Copy to clipboard');
|
||||
item.parent().css('display','inline-block').parent().show();
|
||||
}
|
||||
}, item);
|
||||
// get tox address and store it in elem.text
|
||||
function getTox(peerAlias, elem) {
|
||||
getProfileResource(peerAlias, 'tox', false,
|
||||
function(elem, val) {
|
||||
if (val) {
|
||||
elem.attr('href', 'tox:' + val);
|
||||
elem.next().attr('data', val).attr('title', 'Copy to clipboard');
|
||||
elem.parent().css('display', 'inline-block').parent().show();
|
||||
}
|
||||
}, elem
|
||||
);
|
||||
}
|
||||
|
||||
// get bitmessage address and store it in item.text
|
||||
function getBitmessage( username, item ){
|
||||
getProfileResource( username, "bitmessage", false, function(item, text){
|
||||
//item.empty();
|
||||
if(text) {
|
||||
item.attr('href', 'bitmsg:'+text+'?action=add&label='+username);
|
||||
item.next().attr('data', text).attr('title', 'Copy to clipboard');
|
||||
item.parent().css('display','inline-block').parent().show();
|
||||
}
|
||||
}, item);
|
||||
// get bitmessage address and store it in elem.text
|
||||
function getBitmessage(peerAlias, elem) {
|
||||
getProfileResource(peerAlias, 'bitmessage', false,
|
||||
function(elem, val) {
|
||||
if (val) {
|
||||
elem.attr('href', 'bitmsg:' + val + '?action=add&label=' + peerAlias);
|
||||
elem.next().attr('data', val).attr('title', 'Copy to clipboard');
|
||||
elem.parent().css('display', 'inline-block').parent().show();
|
||||
}
|
||||
}, elem
|
||||
);
|
||||
}
|
||||
|
||||
// get location and store it in item.text
|
||||
function getLocation( username, item ){
|
||||
getProfileResource( username, "location", item);
|
||||
// get location and store it in elem.text
|
||||
function getLocation(peerAlias, elem) {
|
||||
getProfileResource(peerAlias, 'location', elem);
|
||||
}
|
||||
|
||||
// get location and store it in item.text
|
||||
function getWebpage( username, item ){
|
||||
getProfileResource( username, "url", item,
|
||||
function(args, val) {
|
||||
if(typeof(val) !== 'undefined') {
|
||||
if (val.indexOf("://") < 0) {
|
||||
val = "http://" + val;
|
||||
}
|
||||
args.item.attr("href", val);
|
||||
}
|
||||
}, {item:item} );
|
||||
// get location and store it in elem.text
|
||||
function getWebpage(peerAlias, elem) {
|
||||
getProfileResource(peerAlias, 'url', elem,
|
||||
function(elem, val) {
|
||||
if (typeof(val) !== 'undefined') {
|
||||
if (val.indexOf('://') < 0) {
|
||||
val = 'http://' + val;
|
||||
}
|
||||
elem.attr('href', val);
|
||||
}
|
||||
}, elem
|
||||
);
|
||||
}
|
||||
|
||||
function getGroupChatName( groupalias, item ){
|
||||
twisterRpc("getgroupinfo", [groupalias],
|
||||
function(args, ret) {
|
||||
args.item.text(ret["description"]);
|
||||
}, {item:item},
|
||||
function(args, ret) {
|
||||
args.item.text("getgroupinfo error");
|
||||
}, {item:item});
|
||||
function getGroupChatName(groupAlias, elem) {
|
||||
twisterRpc('getgroupinfo', [groupAlias],
|
||||
function(elem, ret) {
|
||||
elem.text(ret.description);
|
||||
}, elem,
|
||||
function(req, ret) {
|
||||
console.warn('RPC "getgroupinfo" error: ' + (ret && ret.message) ? ret.message : ret);
|
||||
req.elem.text(req.groupAlias);
|
||||
}, {elem: elem, groupAlias: groupAlias}
|
||||
);
|
||||
}
|
||||
|
||||
// we must cache avatar results to disk to lower bandwidth on
|
||||
@ -414,21 +419,21 @@ function cleanupStorage() {
|
||||
|
||||
// get avatar and set it in img.attr("src")
|
||||
// TODO rename to getAvatarImgToELem(), move nin theme related stuff to nin's theme_option.js
|
||||
function getAvatar(username, img) {
|
||||
function getAvatar(peerAlias, img) {
|
||||
if (!img.length)
|
||||
return;
|
||||
|
||||
if (username === 'nobody') {
|
||||
if (peerAlias === 'nobody') {
|
||||
img.attr('src', ($.Options.theme.val === 'nin') ?
|
||||
'theme_nin/img/tornado_avatar.png' : 'img/tornado_avatar.png');
|
||||
return;
|
||||
}
|
||||
|
||||
if (_avatarMap[username]) {
|
||||
//img.attr('src', 'data:image/jpg;base64,'+avatarMap[username]);
|
||||
img.attr('src', _avatarMap[username]);
|
||||
if (_avatarMap[peerAlias]) {
|
||||
//img.attr('src', 'data:image/jpg;base64,'+avatarMap[peerAlias]);
|
||||
img.attr('src', _avatarMap[peerAlias]);
|
||||
} else {
|
||||
var data = _getResourceFromStorage('avatar:' + username);
|
||||
var data = _getResourceFromStorage('avatar:' + peerAlias);
|
||||
|
||||
if (data) {
|
||||
switch (data.substr(0, 4)) {
|
||||
@ -442,125 +447,134 @@ function getAvatar(username, img) {
|
||||
data = 'data:image/gif;base64,' + window.btoa(data.slice(4));
|
||||
break;
|
||||
}
|
||||
_avatarMap[username] = data;
|
||||
_avatarMap[peerAlias] = data;
|
||||
img.attr('src', data);
|
||||
} else {
|
||||
dhtget(username, 'avatar', 's',
|
||||
dhtget(peerAlias, 'avatar', 's',
|
||||
function(req, imagedata) {
|
||||
if (imagedata && imagedata.length) {
|
||||
_avatarMap[req.username] = imagedata;
|
||||
_avatarMap[req.peerAlias] = imagedata;
|
||||
if (imagedata !== 'img/genericPerson.png') {
|
||||
if (imagedata.substr(0, 27) === 'data:image/jpeg;base64,/9j/')
|
||||
_putResourceIntoStorage('avatar:' + username, 'jpg/' + window.atob(imagedata.slice(27)));
|
||||
_putResourceIntoStorage('avatar:' + peerAlias, 'jpg/' + window.atob(imagedata.slice(27)));
|
||||
else {
|
||||
var s = imagedata.substr(0, 22);
|
||||
if (s === 'data:image/png;base64,' || s === 'data:image/gif;base64,')
|
||||
_putResourceIntoStorage('avatar:' + username, imagedata.substr(11, 3) + '/' + window.atob(imagedata.slice(22)));
|
||||
_putResourceIntoStorage('avatar:' + peerAlias, imagedata.substr(11, 3) + '/' + window.atob(imagedata.slice(22)));
|
||||
else
|
||||
_putResourceIntoStorage('avatar:' + username, imagedata);
|
||||
_putResourceIntoStorage('avatar:' + peerAlias, imagedata);
|
||||
}
|
||||
}
|
||||
req.img.attr('src', imagedata);
|
||||
}
|
||||
}, {username: username, img: img}
|
||||
}, {peerAlias: peerAlias, img: img}
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function clearAvatarAndProfileCache(username) {
|
||||
function clearAvatarAndProfileCache(peerAlias) {
|
||||
var storage = $.localStorage;
|
||||
storage.remove("avatar:" + username);
|
||||
storage.remove("profile:" + username);
|
||||
if( username in _avatarMap ) {
|
||||
delete _avatarMap[username];
|
||||
storage.remove('avatar:' + peerAlias);
|
||||
storage.remove('profile:' + peerAlias);
|
||||
if (_avatarMap[peerAlias]) {
|
||||
delete _avatarMap[peerAlias];
|
||||
}
|
||||
if( username in _profileMap ) {
|
||||
delete _profileMap[username];
|
||||
if (_profileMap[peerAlias]) {
|
||||
delete _profileMap[peerAlias];
|
||||
}
|
||||
}
|
||||
|
||||
// get estimative for number of followers (use known peers of torrent tracker)
|
||||
function getFollowers( username, item ) {
|
||||
dhtget( username, "tracker", "m",
|
||||
function(args, ret) {
|
||||
if( ret && ret.length && ret[0]["followers"] ) {
|
||||
args.item.text(ret[0]["followers"])
|
||||
}
|
||||
}, {username:username,item:item} );
|
||||
function getFollowers(peerAlias, elem) {
|
||||
dhtget(peerAlias, 'tracker', 'm',
|
||||
function(elem, ret) {
|
||||
if (ret && ret.length && ret[0].followers) {
|
||||
elem.text(ret[0].followers)
|
||||
}
|
||||
}, elem
|
||||
);
|
||||
}
|
||||
|
||||
function getPostsCount( username, item ) {
|
||||
dhtget( username, "status", "s",
|
||||
function(args, v) {
|
||||
var count = 0;
|
||||
if( v && v["userpost"] ) {
|
||||
count = v["userpost"]["k"]+1;
|
||||
}
|
||||
var oldCount = parseInt(args.item.text());
|
||||
if( !oldCount || count > oldCount ) {
|
||||
args.item.text(count);
|
||||
}
|
||||
if( username == defaultScreenName && count ) {
|
||||
incLastPostId( v["userpost"]["k"] );
|
||||
}
|
||||
}, {username:username,item:item} );
|
||||
function getPostsCount(peerAlias, elem) {
|
||||
dhtget(peerAlias, 'status', 's',
|
||||
function(req, v) {
|
||||
var count = 0;
|
||||
if (v && v.userpost) {
|
||||
count = v.userpost.k + 1;
|
||||
}
|
||||
var oldCount = parseInt(req.elem.text());
|
||||
if (!oldCount || count > oldCount) {
|
||||
req.elem.text(count);
|
||||
}
|
||||
if (peerAlias === defaultScreenName && count) {
|
||||
incLastPostId(v.userpost.k);
|
||||
}
|
||||
}, {peerAlias: peerAlias, elem: elem}
|
||||
);
|
||||
}
|
||||
|
||||
function getPostMaxAvailability(username, k, cbFunc, cbArg) {
|
||||
twisterRpc("getpiecemaxseen", [username,k],
|
||||
function(args, ret) {
|
||||
args.cbFunc(args.cbArg, ret);
|
||||
}, {cbFunc:cbFunc, cbArg:cbArg},
|
||||
function(args, ret) {
|
||||
console.log("getPostAvailability error");
|
||||
}, {cbFunc:cbFunc, cbArg:cbArg});
|
||||
function getPostMaxAvailability(peerAlias, k, cbFunc, cbReq) {
|
||||
twisterRpc('getpiecemaxseen', [peerAlias, k],
|
||||
function(req, ret) {
|
||||
req.cbFunc(req.cbReq, ret);
|
||||
}, {cbFunc: cbFunc, cbReq: cbReq},
|
||||
function(req, ret) {
|
||||
console.warn('RPC "getpiecemaxseen" error: ' + (ret && ret.message) ? ret.message : ret);
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
function checkPubkeyExists(username, cbFunc, cbArg) {
|
||||
function checkPubkeyExists(peerAlias, cbFunc, cbReq) {
|
||||
// pubkey is checked in block chain db.
|
||||
// so only accepted registrations are reported (local wallet users are not)
|
||||
twisterRpc("dumppubkey", [username],
|
||||
function(args, ret) {
|
||||
args.cbFunc(args.cbArg, ret.length > 0);
|
||||
}, {cbFunc:cbFunc, cbArg:cbArg},
|
||||
function(args, ret) {
|
||||
alert(polyglot.t("error_connecting_to_daemon"));
|
||||
}, {cbFunc:cbFunc, cbArg:cbArg});
|
||||
twisterRpc('dumppubkey', [peerAlias],
|
||||
function(req, ret) {
|
||||
req.cbFunc(req.cbReq, ret.length > 0);
|
||||
}, {cbFunc: cbFunc, cbReq: cbReq},
|
||||
function(req, ret) {
|
||||
console.warn('RPC "dumppubkey" error: ' + (ret && ret.message) ? ret.message : ret);
|
||||
alert(polyglot.t('error_connecting_to_daemon'));
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
// pubkey is obtained from block chain db.
|
||||
// so only accepted registrations are reported (local wallet users are not)
|
||||
// cbFunc is called as cbFunc(cbArg, pubkey)
|
||||
// cbFunc is called as cbFunc(cbReq, pubkey)
|
||||
// if user doesn't exist then pubkey.length == 0
|
||||
function dumpPubkey(username, cbFunc, cbArg) {
|
||||
if( username in _pubkeyMap ) {
|
||||
if( cbFunc )
|
||||
cbFunc(cbArg, _pubkeyMap[username]);
|
||||
function dumpPubkey(peerAlias, cbFunc, cbReq) {
|
||||
if (_pubkeyMap[peerAlias]) {
|
||||
if (cbFunc)
|
||||
cbFunc(cbReq, _pubkeyMap[peerAlias]);
|
||||
} else {
|
||||
twisterRpc("dumppubkey", [username],
|
||||
function(args, ret) {
|
||||
if( ret.length > 0 ) {
|
||||
_pubkeyMap[username] = ret;
|
||||
}
|
||||
args.cbFunc(args.cbArg, ret);
|
||||
}, {cbFunc:cbFunc, cbArg:cbArg},
|
||||
function(args, ret) {
|
||||
alert(polyglot.t("error_connecting_to_daemon"));
|
||||
}, {cbFunc:cbFunc, cbArg:cbArg});
|
||||
twisterRpc('dumppubkey', [peerAlias],
|
||||
function (req, ret) {
|
||||
if (ret.length > 0) {
|
||||
_pubkeyMap[peerAlias] = ret;
|
||||
}
|
||||
if (req.cbFunc) {
|
||||
req.cbFunc(req.cbReq, ret);
|
||||
}
|
||||
}, {cbFunc: cbFunc, cbReq: cbReq},
|
||||
function (req, ret) {
|
||||
console.warn('RPC "dumppubkey" error: ' + (ret && ret.message) ? ret.message : ret);
|
||||
alert(polyglot.t('error_connecting_to_daemon'));
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// privkey is obtained from wallet db
|
||||
// so privkey is returned even for unsent transactions
|
||||
function dumpPrivkey(username, cbFunc, cbArg) {
|
||||
twisterRpc("dumpprivkey", [username],
|
||||
function(args, ret) {
|
||||
args.cbFunc(args.cbArg, ret);
|
||||
}, {cbFunc:cbFunc, cbArg:cbArg},
|
||||
function(args, ret) {
|
||||
args.cbFunc(args.cbArg, "");
|
||||
console.log("dumpprivkey: user unknown");
|
||||
}, {cbFunc:cbFunc, cbArg:cbArg});
|
||||
function dumpPrivkey(peerAlias, cbFunc, cbReq) {
|
||||
twisterRpc('dumpprivkey', [peerAlias],
|
||||
function(req, ret) {
|
||||
req.cbFunc(req.cbReq, ret);
|
||||
}, {cbFunc: cbFunc, cbReq: cbReq},
|
||||
function(req, ret) {
|
||||
req.cbFunc(req.cbReq, '');
|
||||
console.warn('user unknown — RPC "dumppubkey" error: ' + ret.message);
|
||||
}, {cbFunc: cbFunc, cbReq: cbReq}
|
||||
);
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user