Compare commits

..

1 commit

Author SHA1 Message Date
Anthony
76e79886b2
add comments and begin stripe integration 2021-06-12 21:16:34 +02:00
7 changed files with 91 additions and 82 deletions

View file

@ -17,9 +17,6 @@ yarn link lbryinc
### Build ### Build
Run `$ yarn build`. If the symlink does not work, just build the file and move the `bundle.js` file in to the `node_modules/` folder. Run `$ yarn build`. If the symlink does not work, just build the file and move the `bundle.js` file in to the `node_modules/` folder.
### Automatic rebuild
To have the code automatically rebuild upon changes you can run `$ yarn dev` which will use `rollup` to watch the files and build upon detection of updated source code.
## License ## License
[MIT © LBRY](LICENSE) [MIT © LBRY](LICENSE)

27
dist/bundle.es.js vendored
View file

@ -759,19 +759,19 @@ function doFetchTrendingUris() {
} }
// //
const doFetchViewCount = claimIdCsv => dispatch => { const doFetchViewCount = claimId => dispatch => {
dispatch({ dispatch({
type: FETCH_VIEW_COUNT_STARTED type: FETCH_VIEW_COUNT_STARTED
}); });
return Lbryio.call('file', 'view_count', { return Lbryio.call('file', 'view_count', {
claim_id: claimIdCsv claim_id: claimId
}).then(result => { }).then(result => {
const viewCounts = result; const viewCount = result[0];
dispatch({ dispatch({
type: FETCH_VIEW_COUNT_COMPLETED, type: FETCH_VIEW_COUNT_COMPLETED,
data: { data: {
claimIdCsv, claimId,
viewCounts viewCount
} }
}); });
}).catch(error => { }).catch(error => {
@ -1318,18 +1318,12 @@ const statsReducer = handleActions({
}), }),
[FETCH_VIEW_COUNT_COMPLETED]: (state, action) => { [FETCH_VIEW_COUNT_COMPLETED]: (state, action) => {
const { const {
claimIdCsv, claimId,
viewCounts viewCount
} = action.data; } = action.data;
const viewCountById = Object.assign({}, state.viewCountById); const viewCountById = { ...state.viewCountById,
const claimIds = claimIdCsv.split(','); [claimId]: viewCount
};
if (claimIds.length === viewCounts.length) {
claimIds.forEach((claimId, index) => {
viewCountById[claimId] = viewCounts[index];
});
}
return { ...state, return { ...state,
fetchingViewCount: false, fetchingViewCount: false,
viewCountById viewCountById
@ -1605,7 +1599,6 @@ exports.selectSyncData = selectSyncData;
exports.selectSyncHash = selectSyncHash; exports.selectSyncHash = selectSyncHash;
exports.selectTrendingUris = selectTrendingUris; exports.selectTrendingUris = selectTrendingUris;
exports.selectUploadCount = selectUploadCount; exports.selectUploadCount = selectUploadCount;
exports.selectViewCount = selectViewCount;
exports.statsReducer = statsReducer; exports.statsReducer = statsReducer;
exports.syncReducer = syncReducer; exports.syncReducer = syncReducer;
exports.webReducer = webReducer; exports.webReducer = webReducer;

24
dist/bundle.js vendored
View file

@ -210,8 +210,6 @@ __webpack_require__.r(__webpack_exports__);
/* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, "selectFetchingTrendingUris", function() { return redux_selectors_homepage__WEBPACK_IMPORTED_MODULE_25__["selectFetchingTrendingUris"]; }); /* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, "selectFetchingTrendingUris", function() { return redux_selectors_homepage__WEBPACK_IMPORTED_MODULE_25__["selectFetchingTrendingUris"]; });
/* harmony import */ var redux_selectors_stats__WEBPACK_IMPORTED_MODULE_26__ = __webpack_require__(38); /* harmony import */ var redux_selectors_stats__WEBPACK_IMPORTED_MODULE_26__ = __webpack_require__(38);
/* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, "selectViewCount", function() { return redux_selectors_stats__WEBPACK_IMPORTED_MODULE_26__["selectViewCount"]; });
/* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, "makeSelectViewCountForUri", function() { return redux_selectors_stats__WEBPACK_IMPORTED_MODULE_26__["makeSelectViewCountForUri"]; }); /* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, "makeSelectViewCountForUri", function() { return redux_selectors_stats__WEBPACK_IMPORTED_MODULE_26__["makeSelectViewCountForUri"]; });
/* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, "makeSelectSubCountForUri", function() { return redux_selectors_stats__WEBPACK_IMPORTED_MODULE_26__["makeSelectSubCountForUri"]; }); /* harmony reexport (safe) */ __webpack_require__.d(__webpack_exports__, "makeSelectSubCountForUri", function() { return redux_selectors_stats__WEBPACK_IMPORTED_MODULE_26__["makeSelectSubCountForUri"]; });
@ -3478,20 +3476,20 @@ __webpack_require__.r(__webpack_exports__);
/* harmony import */ var constants_action_types__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(1); /* harmony import */ var constants_action_types__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(1);
var doFetchViewCount = function doFetchViewCount(claimIdCsv) { var doFetchViewCount = function doFetchViewCount(claimId) {
return function (dispatch) { return function (dispatch) {
dispatch({ dispatch({
type: constants_action_types__WEBPACK_IMPORTED_MODULE_1__["FETCH_VIEW_COUNT_STARTED"] type: constants_action_types__WEBPACK_IMPORTED_MODULE_1__["FETCH_VIEW_COUNT_STARTED"]
}); });
return lbryio__WEBPACK_IMPORTED_MODULE_0__["default"].call('file', 'view_count', { return lbryio__WEBPACK_IMPORTED_MODULE_0__["default"].call('file', 'view_count', {
claim_id: claimIdCsv claim_id: claimId
}).then(function (result) { }).then(function (result) {
var viewCounts = result; var viewCount = result[0];
dispatch({ dispatch({
type: constants_action_types__WEBPACK_IMPORTED_MODULE_1__["FETCH_VIEW_COUNT_COMPLETED"], type: constants_action_types__WEBPACK_IMPORTED_MODULE_1__["FETCH_VIEW_COUNT_COMPLETED"],
data: { data: {
claimIdCsv: claimIdCsv, claimId: claimId,
viewCounts: viewCounts viewCount: viewCount
} }
}); });
})["catch"](function (error) { })["catch"](function (error) {
@ -4170,16 +4168,10 @@ var statsReducer = Object(util_redux_utils__WEBPACK_IMPORTED_MODULE_0__["handleA
}); });
}), _defineProperty(_handleActions, constants_action_types__WEBPACK_IMPORTED_MODULE_1__["FETCH_VIEW_COUNT_COMPLETED"], function (state, action) { }), _defineProperty(_handleActions, constants_action_types__WEBPACK_IMPORTED_MODULE_1__["FETCH_VIEW_COUNT_COMPLETED"], function (state, action) {
var _action$data = action.data, var _action$data = action.data,
claimIdCsv = _action$data.claimIdCsv, claimId = _action$data.claimId,
viewCounts = _action$data.viewCounts; viewCount = _action$data.viewCount;
var viewCountById = Object.assign({}, state.viewCountById);
var claimIds = claimIdCsv.split(',');
if (claimIds.length === viewCounts.length) { var viewCountById = _objectSpread({}, state.viewCountById, _defineProperty({}, claimId, viewCount));
claimIds.forEach(function (claimId, index) {
viewCountById[claimId] = viewCounts[index];
});
}
return _objectSpread({}, state, { return _objectSpread({}, state, {
fetchingViewCount: false, fetchingViewCount: false,

View file

@ -58,11 +58,7 @@ export {
selectTrendingUris, selectTrendingUris,
selectFetchingTrendingUris, selectFetchingTrendingUris,
} from 'redux/selectors/homepage'; } from 'redux/selectors/homepage';
export { export { makeSelectViewCountForUri, makeSelectSubCountForUri } from 'redux/selectors/stats';
selectViewCount,
makeSelectViewCountForUri,
makeSelectSubCountForUri,
} from 'redux/selectors/stats';
export { export {
selectHasSyncedWallet, selectHasSyncedWallet,
selectSyncData, selectSyncData,

View file

@ -10,6 +10,7 @@ const Lbryio = {
CONNECTION_STRING: 'https://api.lbry.com/', CONNECTION_STRING: 'https://api.lbry.com/',
}; };
// 20 minute exchange rate timeout
const EXCHANGE_RATE_TIMEOUT = 20 * 60 * 1000; const EXCHANGE_RATE_TIMEOUT = 20 * 60 * 1000;
const INTERNAL_APIS_DOWN = 'internal_apis_down'; const INTERNAL_APIS_DOWN = 'internal_apis_down';
@ -18,43 +19,57 @@ Lbryio.setLocalApi = endpoint => {
Lbryio.CONNECTION_STRING = endpoint.replace(/\/*$/, '/'); // exactly one slash at the end; Lbryio.CONNECTION_STRING = endpoint.replace(/\/*$/, '/'); // exactly one slash at the end;
}; };
function checkAndParse(response) {
if (response.status >= 200 && response.status < 300) {
return response.json();
}
if (response.status === 500) {
return Promise.reject(INTERNAL_APIS_DOWN);
}
if (response)
return response.json().then(json => {
let error;
if (json.error) {
error = new Error(json.error);
} else {
error = new Error('Unknown API error signature');
}
error.response = response; // This is primarily a hack used in actions/user.js
return Promise.reject(error);
});
}
function makeRequest(url, options) {
return fetch(url, options).then(checkAndParse);
}
/**
* Call a Lbry API method
*
* @param {string} resource - resource value (me/lbc/customer/account/etc.)
* @param {string} action - the subresource (aka for customer: status/link)
* @param {object || string} params - used to build querystring for fetch (default {})
* @param {string} method - 'get' or 'post' (default - get)
* @returns {string} returns response.data returned by the API
*/
Lbryio.call = (resource, action, params = {}, method = 'get') => { Lbryio.call = (resource, action, params = {}, method = 'get') => {
// reject promise if API disabled
if (!Lbryio.enabled) { if (!Lbryio.enabled) {
return Promise.reject(new Error(__('LBRY internal API is disabled'))); return Promise.reject(new Error(__('LBRY internal API is disabled')));
} }
// only allow get or post requests
if (!(method === 'get' || method === 'post')) { if (!(method === 'get' || method === 'post')) {
return Promise.reject(new Error(__('Invalid method'))); return Promise.reject(new Error(__('Invalid method')));
} }
function checkAndParse(response) { // get auth token and build request to be called via fetch
if (response.status >= 200 && response.status < 300) {
return response.json();
}
if (response.status === 500) {
return Promise.reject(INTERNAL_APIS_DOWN);
}
if (response)
return response.json().then(json => {
let error;
if (json.error) {
error = new Error(json.error);
} else {
error = new Error('Unknown API error signature');
}
error.response = response; // This is primarily a hack used in actions/user.js
return Promise.reject(error);
});
}
function makeRequest(url, options) {
return fetch(url, options).then(checkAndParse);
}
return Lbryio.getAuthToken().then(token => { return Lbryio.getAuthToken().then(token => {
// add auth token to params
const fullParams = { auth_token: token, ...params }; const fullParams = { auth_token: token, ...params };
// stringify param values
Object.keys(fullParams).forEach(key => { Object.keys(fullParams).forEach(key => {
const value = fullParams[key]; const value = fullParams[key];
if (typeof value === 'object') { if (typeof value === 'object') {
@ -62,16 +77,21 @@ Lbryio.call = (resource, action, params = {}, method = 'get') => {
} }
}); });
// build querystring for fetch
const qs = querystring.stringify(fullParams); const qs = querystring.stringify(fullParams);
// build url for fetch
let url = `${Lbryio.CONNECTION_STRING}${resource}/${action}?${qs}`; let url = `${Lbryio.CONNECTION_STRING}${resource}/${action}?${qs}`;
// set method to GET (changed to POST if applicable later)
let options = { let options = {
method: 'GET', method: 'GET',
}; };
// build post request for fetch
if (method === 'post') { if (method === 'post') {
options = { options = {
method: 'POST', method: 'POST',
// send as a form encoded url with query string
headers: { headers: {
'Content-Type': 'application/x-www-form-urlencoded', 'Content-Type': 'application/x-www-form-urlencoded',
}, },
@ -80,20 +100,29 @@ Lbryio.call = (resource, action, params = {}, method = 'get') => {
url = `${Lbryio.CONNECTION_STRING}${resource}/${action}`; url = `${Lbryio.CONNECTION_STRING}${resource}/${action}`;
} }
// make request per url and options and return response.data
return makeRequest(url, options).then(response => response.data); return makeRequest(url, options).then(response => response.data);
}); });
}; };
Lbryio.authToken = null; Lbryio.authToken = null;
/**
* Get user auth token, either stored on the window, or via a request to the API
*
* @returns {string} returns user's auth token
*/
Lbryio.getAuthToken = () => Lbryio.getAuthToken = () =>
new Promise(resolve => { new Promise(resolve => {
// if authToken already saved on Lbryio object, return it
if (Lbryio.authToken) { if (Lbryio.authToken) {
resolve(Lbryio.authToken); resolve(Lbryio.authToken);
// if there is an override, use that to get token
} else if (Lbryio.overrides.getAuthToken) { } else if (Lbryio.overrides.getAuthToken) {
Lbryio.overrides.getAuthToken().then(token => { Lbryio.overrides.getAuthToken().then(token => {
resolve(token); resolve(token);
}); });
// if the window is already defined, get token from there
} else if (typeof window !== 'undefined') { } else if (typeof window !== 'undefined') {
const { store } = window; const { store } = window;
if (store) { if (store) {
@ -102,9 +131,10 @@ Lbryio.getAuthToken = () =>
Lbryio.authToken = token; Lbryio.authToken = token;
resolve(token); resolve(token);
} }
// if everything whifs, return null
resolve(null); resolve(null);
} else { } else {
// return null if nothing above worked
resolve(null); resolve(null);
} }
}); });
@ -235,4 +265,13 @@ Lbryio.setOverride = (methodName, newMethod) => {
Lbryio.overrides[methodName] = newMethod; Lbryio.overrides[methodName] = newMethod;
}; };
function getCustomerStatus() {
const response = Lbryio.call('account', 'status', {}, 'post');
console.log(response);
}
setTimeout(() => {
getCustomerStatus();
}, 1000 * 5);
export default Lbryio; export default Lbryio;

View file

@ -2,13 +2,13 @@
import Lbryio from 'lbryio'; import Lbryio from 'lbryio';
import * as ACTIONS from 'constants/action_types'; import * as ACTIONS from 'constants/action_types';
export const doFetchViewCount = (claimIdCsv: string) => dispatch => { export const doFetchViewCount = (claimId: string) => dispatch => {
dispatch({ type: ACTIONS.FETCH_VIEW_COUNT_STARTED }); dispatch({ type: ACTIONS.FETCH_VIEW_COUNT_STARTED });
return Lbryio.call('file', 'view_count', { claim_id: claimIdCsv }) return Lbryio.call('file', 'view_count', { claim_id: claimId })
.then((result: Array<number>) => { .then((result: Array<number>) => {
const viewCounts = result; const viewCount = result[0];
dispatch({ type: ACTIONS.FETCH_VIEW_COUNT_COMPLETED, data: { claimIdCsv, viewCounts } }); dispatch({ type: ACTIONS.FETCH_VIEW_COUNT_COMPLETED, data: { claimId, viewCount } });
}) })
.catch(error => { .catch(error => {
dispatch({ type: ACTIONS.FETCH_VIEW_COUNT_FAILED, data: error }); dispatch({ type: ACTIONS.FETCH_VIEW_COUNT_FAILED, data: error });

View file

@ -18,17 +18,9 @@ export const statsReducer = handleActions(
viewCountError: action.data, viewCountError: action.data,
}), }),
[ACTIONS.FETCH_VIEW_COUNT_COMPLETED]: (state, action) => { [ACTIONS.FETCH_VIEW_COUNT_COMPLETED]: (state, action) => {
const { claimIdCsv, viewCounts } = action.data; const { claimId, viewCount } = action.data;
const viewCountById = Object.assign({}, state.viewCountById);
const claimIds = claimIdCsv.split(',');
if (claimIds.length === viewCounts.length) {
claimIds.forEach((claimId, index) => {
viewCountById[claimId] = viewCounts[index];
});
}
const viewCountById = { ...state.viewCountById, [claimId]: viewCount };
return { return {
...state, ...state,
fetchingViewCount: false, fetchingViewCount: false,