mirror of
https://github.com/RetroShare/RSNewWebUI.git
synced 2026-09-14 11:05:47 +05:00
rswebui: add eventqueue, switch login to eventqueue
This commit is contained in:
parent
cee5576a22
commit
bc2eab69dc
@ -14,26 +14,10 @@ const verifyLogin = async function(uname, passwd, url, displayAuthError = true)
|
||||
return;
|
||||
}
|
||||
rs.setKeys('', '', url, false);
|
||||
rs.rsJsonApiRequest(
|
||||
'/rsPeers/GetRetroshareInvite',
|
||||
{},
|
||||
(data, successful) => {
|
||||
if (successful) {
|
||||
rs.setKeys(uname, passwd, url);
|
||||
m.route.set('/home');
|
||||
} else if (data.status == 401 && !displayAuthError) {
|
||||
// skip - Autologon failed
|
||||
} else if (data.status == 401) {
|
||||
displayErrorMessage('Incorrect login/password.');
|
||||
} else if (data.status == 0) {
|
||||
displayErrorMessage(['Retroshare-jsonapi not available.',m('br'),'Please fix host and/or port.']);
|
||||
} else {
|
||||
displayErrorMessage('Login failed: HTTP ' + data.status + ' ' + data.statusText);
|
||||
}
|
||||
},
|
||||
true,
|
||||
loginHeader
|
||||
);
|
||||
rs.startEventQueue('login', loginHeader, displayAuthError ? displayErrorMessage : () => {}, displayErrorMessage, () => {
|
||||
rs.setKeys(uname, passwd, url);
|
||||
m.route.set('/home');
|
||||
});
|
||||
};
|
||||
|
||||
function loginComponent() {
|
||||
|
||||
@ -23,7 +23,8 @@ function rsJsonApiRequest(
|
||||
async = true,
|
||||
headers = {},
|
||||
handleDeserialize = JSON.parse,
|
||||
handleSerialize = JSON.stringify
|
||||
handleSerialize = JSON.stringify,
|
||||
config = null
|
||||
) {
|
||||
headers['Accept'] = 'application/json';
|
||||
if (loginKey.isVerified) {
|
||||
@ -49,7 +50,8 @@ function rsJsonApiRequest(
|
||||
},
|
||||
serialize: handleSerialize,
|
||||
headers: headers,
|
||||
body: data
|
||||
body: data,
|
||||
config: config
|
||||
})
|
||||
.then(result => {
|
||||
if (result.status === 200) {
|
||||
@ -87,9 +89,66 @@ function setBackgroundTask(task, interval, taskInScope) {
|
||||
}, interval);
|
||||
return taskId;
|
||||
}
|
||||
/*
|
||||
path,
|
||||
data = {},
|
||||
callback = () => {},
|
||||
async = true,
|
||||
headers = {},
|
||||
handleDeserialize = JSON.parse,
|
||||
handleSerialize = JSON.stringify
|
||||
config
|
||||
*/
|
||||
function startEventQueue(info, loginHeader = {}, displayAuthError = () => {}, displayErrorMessage = () => {}, successful = () => {}){
|
||||
return rsJsonApiRequest('/rsEvents/registerEventsHandler', {},
|
||||
(data, success) => {
|
||||
if (success) {
|
||||
// unused
|
||||
} else if (data.status == 401) {
|
||||
displayAuthError('Incorrect login/password.');
|
||||
} else if (data.status == 0) {
|
||||
displayErrorMessage(['Retroshare-jsonapi not available.',m('br'),'Please fix host and/or port.']);
|
||||
} else {
|
||||
displayErrorMessage('Login failed: HTTP ' + data.status + ' ' + data.statusText);
|
||||
}
|
||||
},
|
||||
true, loginHeader, JSON.parse, JSON.stringify,
|
||||
(xhr, args, url) => {
|
||||
let lastIndex = 0;
|
||||
xhr.onprogress = ev => {
|
||||
let currIndex = xhr.responseText.length;
|
||||
let registered = false;
|
||||
if (currIndex > lastIndex) {
|
||||
let parts = xhr.responseText.substring(lastIndex,currIndex);
|
||||
lastIndex=currIndex;
|
||||
for(data of parts.trim().split('\n\n').filter(e=> e.startsWith('data: {')).map(e=> e.substr(6)).map(JSON.parse)){
|
||||
if (data.hasOwnProperty('retval')) {
|
||||
console.info(info + ' [' + data.retval.errorCategory + '] ' + data.retval.errorMessage);
|
||||
if (data.retval.errorNumber === 0) {
|
||||
successful();
|
||||
} else {
|
||||
displayErrorMessage(info + ' failed: [' + data.retval.errorCategory + '] ' + data.retval.errorMessage);
|
||||
}
|
||||
registered = true;
|
||||
} else if(data.hasOwnProperty('event')) {
|
||||
data.event.queueSize = currIndex;
|
||||
console.info(data.event);
|
||||
}
|
||||
}
|
||||
if (currIndex > 1e6) { // max 1 MB eventQueue
|
||||
startEventQueue('restart queue');
|
||||
xhr.abort();
|
||||
}
|
||||
}
|
||||
}
|
||||
return xhr;
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
rsJsonApiRequest,
|
||||
setKeys,
|
||||
setBackgroundTask
|
||||
setBackgroundTask,
|
||||
startEventQueue
|
||||
};
|
||||
|
||||
Loading…
Reference in New Issue
Block a user