2017-04-10 14:32:40 +02:00
|
|
|
import {getLocal, getSession, setSession, setLocal} from './utils.js';
|
2017-04-09 17:06:23 +02:00
|
|
|
import lbry from './lbry.js';
|
|
|
|
|
2017-03-30 00:44:18 +02:00
|
|
|
const querystring = require('querystring');
|
|
|
|
|
2017-04-09 17:06:23 +02:00
|
|
|
const lbryio = {
|
|
|
|
_accessToken: getLocal('accessToken'),
|
|
|
|
_authenticationPromise: null,
|
2017-04-15 00:23:42 +02:00
|
|
|
_user : null,
|
2017-04-17 15:06:13 +02:00
|
|
|
enabled: false
|
2017-04-17 15:46:41 +02:00
|
|
|
};
|
2017-03-30 00:44:18 +02:00
|
|
|
|
2017-04-09 17:06:23 +02:00
|
|
|
const CONNECTION_STRING = 'http://localhost:8080/';
|
2017-03-30 00:44:18 +02:00
|
|
|
|
|
|
|
const mocks = {
|
2017-04-02 12:08:25 +02:00
|
|
|
'reward_type.get': ({name}) => {
|
2017-03-30 00:44:18 +02:00
|
|
|
return {
|
|
|
|
name: 'link_github',
|
|
|
|
title: 'Link your GitHub account',
|
|
|
|
description: 'Link LBRY to your GitHub account',
|
|
|
|
value: 50,
|
|
|
|
claimed: false,
|
|
|
|
};
|
2017-04-10 14:32:40 +02:00
|
|
|
}
|
2017-03-30 00:44:18 +02:00
|
|
|
};
|
|
|
|
|
2017-04-02 12:08:25 +02:00
|
|
|
lbryio.call = function(resource, action, params={}, method='get') {
|
2017-03-30 00:44:18 +02:00
|
|
|
return new Promise((resolve, reject) => {
|
2017-04-15 00:23:42 +02:00
|
|
|
if (!lbryio.enabled) {
|
|
|
|
reject(new Error("LBRY interal API is disabled"))
|
|
|
|
return
|
|
|
|
}
|
2017-03-30 00:44:18 +02:00
|
|
|
/* temp code for mocks */
|
|
|
|
if (`${resource}.${action}` in mocks) {
|
|
|
|
resolve(mocks[`${resource}.${action}`](params));
|
|
|
|
return;
|
|
|
|
}
|
2017-03-30 06:48:18 +02:00
|
|
|
|
2017-03-30 00:44:18 +02:00
|
|
|
/* end temp */
|
|
|
|
|
|
|
|
const xhr = new XMLHttpRequest;
|
2017-04-01 08:54:56 +02:00
|
|
|
|
2017-04-09 17:06:23 +02:00
|
|
|
xhr.addEventListener('error', function (event) {
|
|
|
|
reject(new Error("Something went wrong making an internal API call."));
|
2017-03-30 00:44:18 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
xhr.addEventListener('timeout', function() {
|
|
|
|
reject(new Error('XMLHttpRequest connection timed out'));
|
|
|
|
});
|
|
|
|
|
|
|
|
xhr.addEventListener('load', function() {
|
|
|
|
const response = JSON.parse(xhr.responseText);
|
|
|
|
|
2017-03-30 20:05:37 +02:00
|
|
|
if (!response.success) {
|
2017-03-30 00:44:18 +02:00
|
|
|
if (reject) {
|
|
|
|
reject(new Error(response.error));
|
|
|
|
} else {
|
|
|
|
document.dispatchEvent(new CustomEvent('unhandledError', {
|
|
|
|
detail: {
|
|
|
|
connectionString: connectionString,
|
2017-03-30 20:05:37 +02:00
|
|
|
method: action,
|
2017-03-30 00:44:18 +02:00
|
|
|
params: params,
|
|
|
|
message: response.error.message,
|
2017-03-30 20:05:37 +02:00
|
|
|
... response.error.data ? {data: response.error.data} : {},
|
2017-03-30 00:44:18 +02:00
|
|
|
}
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
} else {
|
2017-03-31 01:00:33 +02:00
|
|
|
resolve(response.data);
|
2017-03-30 00:44:18 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2017-04-02 11:55:24 +02:00
|
|
|
// For social media auth:
|
|
|
|
//const accessToken = localStorage.getItem('accessToken');
|
|
|
|
//const fullParams = {...params, ... accessToken ? {access_token: accessToken} : {}};
|
|
|
|
|
|
|
|
// Temp app ID based auth:
|
2017-04-09 17:06:23 +02:00
|
|
|
const fullParams = {app_id: lbryio._accessToken, ...params};
|
2017-04-02 11:55:24 +02:00
|
|
|
|
2017-03-31 01:00:33 +02:00
|
|
|
if (method == 'get') {
|
2017-04-02 11:55:24 +02:00
|
|
|
xhr.open('get', CONNECTION_STRING + resource + '/' + action + '?' + querystring.stringify(fullParams), true);
|
2017-03-31 01:00:33 +02:00
|
|
|
xhr.send();
|
|
|
|
} else if (method == 'post') {
|
|
|
|
xhr.open('post', CONNECTION_STRING + resource + '/' + action, true);
|
2017-03-30 20:05:37 +02:00
|
|
|
xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
|
2017-04-02 11:55:24 +02:00
|
|
|
xhr.send(querystring.stringify(fullParams));
|
2017-03-30 20:05:37 +02:00
|
|
|
}
|
2017-03-30 00:44:18 +02:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2017-04-09 17:06:23 +02:00
|
|
|
lbryio.setAccessToken = (token) => {
|
|
|
|
setLocal('accessToken', token)
|
|
|
|
lbryio._accessToken = token
|
|
|
|
}
|
|
|
|
|
2017-04-10 14:32:40 +02:00
|
|
|
lbryio.authenticate = function() {
|
2017-04-15 00:23:42 +02:00
|
|
|
if (!lbryio.enabled) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
resolve({
|
|
|
|
ID: 1,
|
|
|
|
HasVerifiedEmail: true
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
2017-04-09 17:06:23 +02:00
|
|
|
if (lbryio._authenticationPromise === null) {
|
|
|
|
lbryio._authenticationPromise = new Promise((resolve, reject) => {
|
|
|
|
lbry.status().then(({installation_id}) => {
|
|
|
|
|
|
|
|
//temp hack for installation_ids being wrong
|
|
|
|
installation_id += "Y".repeat(96 - installation_id.length)
|
|
|
|
|
|
|
|
function setCurrentUser() {
|
|
|
|
lbryio.call('user', 'me').then((data) => {
|
|
|
|
lbryio.user = data
|
|
|
|
resolve(data)
|
|
|
|
}).catch(function(err) {
|
|
|
|
lbryio.setAccessToken(null);
|
2017-04-10 14:32:40 +02:00
|
|
|
if (!getSession('reloadedOnFailedAuth')) {
|
|
|
|
setSession('reloadedOnFailedAuth', true)
|
|
|
|
window.location.reload();
|
|
|
|
} else {
|
|
|
|
reject(err);
|
|
|
|
}
|
2017-04-09 17:06:23 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!lbryio._accessToken) {
|
|
|
|
lbryio.call('user', 'new', {
|
|
|
|
language: 'en',
|
|
|
|
app_id: installation_id,
|
|
|
|
}, 'post').then(function(responseData) {
|
|
|
|
if (!responseData.ID) {
|
|
|
|
reject(new Error("Received invalid authentication response."));
|
|
|
|
}
|
|
|
|
lbryio.setAccessToken(installation_id)
|
|
|
|
setCurrentUser()
|
|
|
|
}).catch(function(error) {
|
|
|
|
|
|
|
|
/*
|
|
|
|
until we have better error code format, assume all errors are duplicate application id
|
|
|
|
if we're wrong, this will be caught by later attempts to make a valid call
|
|
|
|
*/
|
|
|
|
lbryio.setAccessToken(installation_id)
|
|
|
|
setCurrentUser()
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
setCurrentUser()
|
|
|
|
}
|
|
|
|
// if (!lbryio._
|
|
|
|
//(data) => {
|
|
|
|
// resolve(data)
|
|
|
|
// localStorage.setItem('accessToken', ID);
|
|
|
|
// localStorage.setItem('appId', installation_id);
|
|
|
|
// this.setState({
|
|
|
|
// registrationCheckComplete: true,
|
|
|
|
// justRegistered: true,
|
|
|
|
// });
|
|
|
|
//});
|
|
|
|
// lbryio.call('user_install', 'exists', {app_id: installation_id}).then((userExists) => {
|
|
|
|
// // TODO: deal with case where user exists already with the same app ID, but we have no access token.
|
|
|
|
// // Possibly merge in to the existing user with the same app ID.
|
|
|
|
// })
|
|
|
|
}).catch(reject);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return lbryio._authenticationPromise;
|
|
|
|
}
|
|
|
|
|
2017-03-30 00:44:18 +02:00
|
|
|
export default lbryio;
|