2017-06-08 02:56:52 +02:00
|
|
|
import lbry from "./lbry.js";
|
2017-04-09 17:06:23 +02:00
|
|
|
|
2017-06-08 02:56:52 +02:00
|
|
|
const querystring = require("querystring");
|
2017-06-26 16:47:49 +02:00
|
|
|
const { ipcRenderer } = require("electron");
|
2017-03-30 00:44:18 +02:00
|
|
|
|
2017-04-09 17:06:23 +02:00
|
|
|
const lbryio = {
|
2017-06-08 02:56:52 +02:00
|
|
|
enabled: true,
|
2017-06-05 18:29:17 +02:00
|
|
|
_authenticationPromise: null,
|
|
|
|
_exchangePromise: null,
|
|
|
|
_exchangeLastFetched: null,
|
2017-04-17 16:01:33 +02:00
|
|
|
};
|
2017-03-30 00:44:18 +02:00
|
|
|
|
2017-06-06 06:21:55 +02:00
|
|
|
const CONNECTION_STRING = process.env.LBRY_APP_API_URL
|
2017-06-08 02:56:52 +02:00
|
|
|
? process.env.LBRY_APP_API_URL.replace(/\/*$/, "/") // exactly one slash at the end
|
|
|
|
: "https://api.lbry.io/";
|
2017-06-05 18:29:17 +02:00
|
|
|
|
2017-04-17 18:18:33 +02:00
|
|
|
const EXCHANGE_RATE_TIMEOUT = 20 * 60 * 1000;
|
2017-03-30 00:44:18 +02:00
|
|
|
|
2017-04-17 18:18:33 +02:00
|
|
|
lbryio.getExchangeRates = function() {
|
2017-06-21 14:42:22 +02:00
|
|
|
if (
|
|
|
|
!lbryio._exchangeLastFetched ||
|
|
|
|
Date.now() - lbryio._exchangeLastFetched > EXCHANGE_RATE_TIMEOUT
|
|
|
|
) {
|
2017-06-08 02:56:52 +02:00
|
|
|
lbryio._exchangePromise = new Promise((resolve, reject) => {
|
|
|
|
lbryio
|
|
|
|
.call("lbc", "exchange_rate", {}, "get", true)
|
|
|
|
.then(({ lbc_usd, lbc_btc, btc_usd }) => {
|
|
|
|
const rates = { lbc_usd, lbc_btc, btc_usd };
|
|
|
|
resolve(rates);
|
|
|
|
})
|
|
|
|
.catch(reject);
|
|
|
|
});
|
|
|
|
lbryio._exchangeLastFetched = Date.now();
|
|
|
|
}
|
|
|
|
return lbryio._exchangePromise;
|
2017-06-06 06:21:55 +02:00
|
|
|
};
|
|
|
|
|
2017-06-08 23:15:34 +02:00
|
|
|
lbryio.call = function(resource, action, params = {}, method = "get") {
|
2017-06-08 02:56:52 +02:00
|
|
|
return new Promise((resolve, reject) => {
|
2017-06-08 23:15:34 +02:00
|
|
|
if (!lbryio.enabled && (resource != "discover" || action != "list")) {
|
2017-06-08 02:56:52 +02:00
|
|
|
console.log(__("Internal API disabled"));
|
|
|
|
reject(new Error(__("LBRY internal API is disabled")));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const xhr = new XMLHttpRequest();
|
|
|
|
|
|
|
|
xhr.addEventListener("error", function(event) {
|
2017-06-21 14:42:22 +02:00
|
|
|
reject(
|
|
|
|
new Error(__("Something went wrong making an internal API call."))
|
|
|
|
);
|
2017-06-08 02:56:52 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
xhr.addEventListener("timeout", function() {
|
|
|
|
reject(new Error(__("XMLHttpRequest connection timed out")));
|
|
|
|
});
|
|
|
|
|
|
|
|
xhr.addEventListener("load", function() {
|
|
|
|
const response = JSON.parse(xhr.responseText);
|
|
|
|
|
|
|
|
if (!response.success) {
|
|
|
|
if (reject) {
|
2017-06-05 18:29:17 +02:00
|
|
|
const error = new Error(response.error);
|
2017-06-08 02:56:52 +02:00
|
|
|
error.xhr = xhr;
|
|
|
|
reject(error);
|
|
|
|
} else {
|
|
|
|
document.dispatchEvent(
|
|
|
|
new CustomEvent("unhandledError", {
|
|
|
|
detail: {
|
|
|
|
connectionString: connectionString,
|
|
|
|
method: action,
|
|
|
|
params: params,
|
|
|
|
message: response.error.message,
|
|
|
|
...(response.error.data ? { data: response.error.data } : {}),
|
|
|
|
},
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
resolve(response.data);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2017-06-05 18:29:17 +02:00
|
|
|
lbryio
|
|
|
|
.getAuthToken()
|
|
|
|
.then(token => {
|
|
|
|
const fullParams = { auth_token: token, ...params };
|
|
|
|
|
|
|
|
if (method == "get") {
|
2017-06-21 14:42:22 +02:00
|
|
|
xhr.open(
|
|
|
|
"get",
|
|
|
|
CONNECTION_STRING +
|
|
|
|
resource +
|
|
|
|
"/" +
|
|
|
|
action +
|
|
|
|
"?" +
|
|
|
|
querystring.stringify(fullParams),
|
|
|
|
true
|
|
|
|
);
|
2017-06-05 18:29:17 +02:00
|
|
|
xhr.send();
|
|
|
|
} else if (method == "post") {
|
|
|
|
xhr.open("post", CONNECTION_STRING + resource + "/" + action, true);
|
2017-06-21 14:42:22 +02:00
|
|
|
xhr.setRequestHeader(
|
|
|
|
"Content-type",
|
|
|
|
"application/x-www-form-urlencoded"
|
|
|
|
);
|
2017-06-05 18:29:17 +02:00
|
|
|
xhr.send(querystring.stringify(fullParams));
|
|
|
|
} else {
|
|
|
|
reject(new Error(__("Invalid method")));
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(reject);
|
2017-06-08 02:56:52 +02:00
|
|
|
});
|
2017-03-30 00:44:18 +02:00
|
|
|
};
|
|
|
|
|
2017-08-03 18:15:52 +02:00
|
|
|
lbryio._authToken = null;
|
|
|
|
|
2017-06-05 18:29:17 +02:00
|
|
|
lbryio.getAuthToken = () => {
|
2017-06-26 16:47:49 +02:00
|
|
|
return new Promise((resolve, reject) => {
|
2017-08-03 18:15:52 +02:00
|
|
|
if (lbryio._authToken) {
|
|
|
|
resolve(lbryio._authToken);
|
|
|
|
} else {
|
|
|
|
ipcRenderer.once("auth-token-response", (event, token) => {
|
|
|
|
lbryio._authToken = token;
|
|
|
|
return resolve(token);
|
|
|
|
});
|
|
|
|
ipcRenderer.send("get-auth-token");
|
|
|
|
}
|
2017-06-05 18:29:17 +02:00
|
|
|
});
|
2017-06-06 06:21:55 +02:00
|
|
|
};
|
2017-04-18 21:45:15 +02:00
|
|
|
|
2017-06-05 18:29:17 +02:00
|
|
|
lbryio.setAuthToken = token => {
|
2017-08-03 18:15:52 +02:00
|
|
|
lbryio._authToken = token ? token.toString().trim() : null;
|
2017-06-26 16:47:49 +02:00
|
|
|
ipcRenderer.send("set-auth-token", token);
|
2017-06-08 02:56:52 +02:00
|
|
|
};
|
|
|
|
|
2017-06-05 18:29:17 +02:00
|
|
|
lbryio.getCurrentUser = () => {
|
|
|
|
return lbryio.call("user", "me");
|
2017-06-06 06:21:55 +02:00
|
|
|
};
|
2017-04-09 17:06:23 +02:00
|
|
|
|
2017-04-10 14:32:40 +02:00
|
|
|
lbryio.authenticate = function() {
|
2017-06-01 01:29:10 +02:00
|
|
|
if (!lbryio.enabled) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
resolve({
|
|
|
|
id: 1,
|
2017-06-08 02:56:52 +02:00
|
|
|
language: "en",
|
2017-07-20 21:03:01 +02:00
|
|
|
primary_email: "disabled@lbry.io",
|
2017-06-08 02:56:52 +02:00
|
|
|
has_verified_email: true,
|
2017-07-20 03:07:17 +02:00
|
|
|
is_identity_verified: true,
|
2017-06-08 02:56:52 +02:00
|
|
|
is_reward_approved: false,
|
|
|
|
});
|
|
|
|
});
|
2017-06-01 01:29:10 +02:00
|
|
|
}
|
2017-06-05 18:29:17 +02:00
|
|
|
|
2017-06-01 01:29:10 +02:00
|
|
|
if (lbryio._authenticationPromise === null) {
|
|
|
|
lbryio._authenticationPromise = new Promise((resolve, reject) => {
|
2017-06-05 18:29:17 +02:00
|
|
|
lbryio
|
|
|
|
.getAuthToken()
|
|
|
|
.then(token => {
|
2017-06-21 14:42:22 +02:00
|
|
|
if (!token || token.length > 60) {
|
2017-06-05 18:29:17 +02:00
|
|
|
return false;
|
2017-06-08 02:56:52 +02:00
|
|
|
}
|
2017-06-05 18:29:17 +02:00
|
|
|
|
|
|
|
// check that token works
|
|
|
|
return lbryio
|
|
|
|
.getCurrentUser()
|
2017-06-21 14:42:22 +02:00
|
|
|
.then(() => {
|
|
|
|
return true;
|
|
|
|
})
|
|
|
|
.catch(() => {
|
|
|
|
return false;
|
|
|
|
});
|
2017-06-08 02:56:52 +02:00
|
|
|
})
|
2017-06-21 14:42:22 +02:00
|
|
|
.then(isTokenValid => {
|
2017-06-05 18:29:17 +02:00
|
|
|
if (isTokenValid) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
return lbry
|
|
|
|
.status()
|
|
|
|
.then(status => {
|
2017-06-21 14:42:22 +02:00
|
|
|
return lbryio.call(
|
|
|
|
"user",
|
2017-08-24 21:07:16 +02:00
|
|
|
"new",
|
|
|
|
{
|
|
|
|
auth_token: "",
|
|
|
|
language: "en",
|
|
|
|
app_id: status.installation_id,
|
|
|
|
},
|
2017-06-21 14:42:22 +02:00
|
|
|
"post"
|
|
|
|
);
|
2017-06-05 18:29:17 +02:00
|
|
|
})
|
|
|
|
.then(response => {
|
|
|
|
if (!response.auth_token) {
|
|
|
|
throw new Error(__("auth_token is missing from response"));
|
|
|
|
}
|
|
|
|
return lbryio.setAuthToken(response.auth_token);
|
|
|
|
});
|
|
|
|
})
|
2017-06-22 00:49:23 +02:00
|
|
|
.then(lbryio.getCurrentUser)
|
2017-06-05 18:29:17 +02:00
|
|
|
.then(resolve, reject);
|
2017-06-08 02:56:52 +02:00
|
|
|
});
|
|
|
|
}
|
2017-06-05 18:29:17 +02:00
|
|
|
|
2017-06-08 02:56:52 +02:00
|
|
|
return lbryio._authenticationPromise;
|
2017-06-06 06:21:55 +02:00
|
|
|
};
|
2017-04-09 17:06:23 +02:00
|
|
|
|
2017-08-25 22:49:12 +02:00
|
|
|
lbryio.getStripeToken = () => {
|
|
|
|
return CONNECTION_STRING.startsWith("http://localhost:")
|
|
|
|
? "pk_test_NoL1JWL7i1ipfhVId5KfDZgo"
|
|
|
|
: "pk_live_e8M4dRNnCCbmpZzduEUZBgJO";
|
|
|
|
};
|
|
|
|
|
2017-03-30 00:44:18 +02:00
|
|
|
export default lbryio;
|