lbry-desktop/src/renderer/redux/actions/user.js

361 lines
8.5 KiB
JavaScript
Raw Normal View History

import * as ACTIONS from 'constants/action_types';
import Lbryio from 'lbryio';
import { Lbry, doNotify, MODALS, doHideNotification } from 'lbry-redux';
import { doClaimRewardType, doRewardList } from 'redux/actions/rewards';
2018-01-18 06:09:08 +01:00
import {
selectEmailToVerify,
selectPhoneToVerify,
selectUserCountryCode,
} from 'redux/selectors/user';
import rewards from 'rewards';
2018-02-16 09:47:52 +01:00
import analytics from 'analytics';
2018-06-12 21:22:20 +02:00
import pjson from 'package.json';
export function doFetchInviteStatus() {
return dispatch => {
dispatch({
type: ACTIONS.USER_INVITE_STATUS_FETCH_STARTED,
});
Lbryio.call('user', 'invite_status')
.then(status => {
dispatch({
type: ACTIONS.USER_INVITE_STATUS_FETCH_SUCCESS,
data: {
invitesRemaining: status.invites_remaining ? status.invites_remaining : 0,
invitees: status.invitees,
},
});
})
.catch(error => {
dispatch({
type: ACTIONS.USER_INVITE_STATUS_FETCH_FAILURE,
data: { error },
});
});
};
}
2017-06-01 18:20:12 +02:00
2018-06-12 21:22:20 +02:00
export function doInstallNew() {
const payload = { app_version: pjson.version };
Lbry.status().then(status => {
payload.app_id = status.installation_id;
2018-06-15 05:34:42 +02:00
payload.node_id = status.lbry_id;
Lbry.version().then(version => {
payload.daemon_version = version.lbrynet_version;
payload.operating_system = version.os_system;
payload.platform = version.platform;
Lbryio.call('install', 'new', payload);
2018-06-12 21:22:20 +02:00
});
});
2018-06-12 21:22:20 +02:00
}
2017-06-01 18:20:12 +02:00
export function doAuthenticate() {
return dispatch => {
2017-06-01 18:20:12 +02:00
dispatch({
type: ACTIONS.AUTHENTICATION_STARTED,
2017-06-08 02:56:52 +02:00
});
Lbryio.authenticate()
2017-06-08 02:56:52 +02:00
.then(user => {
2018-02-16 09:47:52 +01:00
analytics.setUser(user);
2017-06-08 02:56:52 +02:00
dispatch({
type: ACTIONS.AUTHENTICATION_SUCCESS,
2017-06-08 02:56:52 +02:00
data: { user },
});
dispatch(doRewardList());
2017-08-20 23:42:00 +02:00
dispatch(doFetchInviteStatus());
doInstallNew();
2017-06-01 18:20:12 +02:00
})
2017-06-08 02:56:52 +02:00
.catch(error => {
2018-04-19 18:51:18 +02:00
dispatch(doNotify({ id: MODALS.AUTHENTICATION_FAILURE }));
2017-06-08 02:56:52 +02:00
dispatch({
type: ACTIONS.AUTHENTICATION_FAILURE,
2017-06-08 02:56:52 +02:00
data: { error },
});
});
};
}
2017-06-08 23:15:34 +02:00
export function doUserFetch() {
return dispatch => {
2017-06-08 23:15:34 +02:00
dispatch({
type: ACTIONS.USER_FETCH_STARTED,
2017-06-08 23:15:34 +02:00
});
Lbryio.getCurrentUser()
.then(user => {
2018-02-16 09:47:52 +01:00
analytics.setUser(user);
2017-06-22 02:09:30 +02:00
dispatch(doRewardList());
2017-06-08 23:15:34 +02:00
dispatch({
type: ACTIONS.USER_FETCH_SUCCESS,
2017-06-08 23:15:34 +02:00
data: { user },
});
})
.catch(error => {
2017-06-08 23:15:34 +02:00
dispatch({
type: ACTIONS.USER_FETCH_FAILURE,
2017-06-08 23:15:34 +02:00
data: { error },
});
});
2017-06-08 23:15:34 +02:00
};
}
export function doUserPhoneReset() {
return {
type: ACTIONS.USER_PHONE_RESET,
};
}
2018-04-18 06:22:42 +02:00
export function doUserPhoneNew(phone, countryCode) {
2018-01-15 14:32:01 +01:00
return dispatch => {
dispatch({
type: ACTIONS.USER_PHONE_NEW_STARTED,
2018-04-18 06:22:42 +02:00
data: { phone, country_code: countryCode },
2018-01-15 14:32:01 +01:00
});
const success = () => {
dispatch({
type: ACTIONS.USER_PHONE_NEW_SUCCESS,
data: { phone },
});
};
const failure = error => {
2018-01-15 14:32:01 +01:00
dispatch({
type: ACTIONS.USER_PHONE_NEW_FAILURE,
data: { error },
2018-01-15 14:32:01 +01:00
});
};
2018-04-18 06:22:42 +02:00
Lbryio.call(
'user',
'phone_number_new',
{ phone_number: phone, country_code: countryCode },
'post'
).then(success, failure);
2018-01-15 14:32:01 +01:00
};
}
export function doUserPhoneVerifyFailure(error) {
return {
type: ACTIONS.USER_PHONE_VERIFY_FAILURE,
data: { error },
};
}
export function doUserPhoneVerify(verificationCode) {
return (dispatch, getState) => {
const phoneNumber = selectPhoneToVerify(getState());
2018-01-18 06:09:08 +01:00
const countryCode = selectUserCountryCode(getState());
2018-01-15 14:32:01 +01:00
dispatch({
type: ACTIONS.USER_PHONE_VERIFY_STARTED,
code: verificationCode,
});
Lbryio.call(
'user',
'phone_number_confirm',
2018-01-15 14:32:01 +01:00
{
verification_code: verificationCode,
phone_number: phoneNumber,
2018-01-18 06:09:08 +01:00
country_code: countryCode,
2018-01-15 14:32:01 +01:00
},
'post'
)
.then(user => {
if (user.is_identity_verified) {
dispatch({
type: ACTIONS.USER_PHONE_VERIFY_SUCCESS,
data: { user },
});
dispatch(doHideNotification());
dispatch(doClaimRewardType(rewards.TYPE_NEW_USER));
}
2018-01-15 14:32:01 +01:00
})
.catch(error => dispatch(doUserPhoneVerifyFailure(error)));
};
}
export function doUserEmailNew(email) {
return dispatch => {
dispatch({
type: ACTIONS.USER_EMAIL_NEW_STARTED,
2017-12-13 22:36:30 +01:00
email,
2017-06-08 02:56:52 +02:00
});
const success = () => {
dispatch({
type: ACTIONS.USER_EMAIL_NEW_SUCCESS,
data: { email },
});
dispatch(doUserFetch());
2017-12-13 22:36:30 +01:00
};
const failure = error => {
dispatch({
type: ACTIONS.USER_EMAIL_NEW_FAILURE,
data: { error },
});
2017-12-13 22:36:30 +01:00
};
Lbryio.call('user_email', 'new', { email, send_verification_email: true }, 'post')
.catch(error => {
if (error.response && error.response.status === 409) {
return Lbryio.call(
'user_email',
'resend_token',
{ email, only_if_expired: true },
'post'
).then(success, failure);
}
2017-06-21 14:42:22 +02:00
throw error;
})
.then(success, failure);
2017-06-08 02:56:52 +02:00
};
}
export function doUserResendVerificationEmail(email) {
return dispatch => {
dispatch({
type: ACTIONS.USER_EMAIL_VERIFY_RETRY,
email,
});
const success = () => {
dispatch({
type: ACTIONS.USER_EMAIL_NEW_SUCCESS,
data: { email },
});
dispatch(doUserFetch());
};
const failure = error => {
dispatch({
type: ACTIONS.USER_EMAIL_NEW_FAILURE,
data: { error },
});
};
Lbryio.call('user_email', 'resend_token', { email }, 'post')
.catch(error => {
if (error.response && error.response.status === 409) {
throw error;
}
})
.then(success, failure);
};
}
2018-01-15 14:32:01 +01:00
export function doUserEmailVerifyFailure(error) {
2017-12-26 14:25:26 +01:00
return {
type: ACTIONS.USER_EMAIL_VERIFY_FAILURE,
data: { error },
};
}
2018-01-15 14:32:01 +01:00
export function doUserEmailVerify(verificationToken, recaptcha) {
return (dispatch, getState) => {
2017-06-08 02:56:52 +02:00
const email = selectEmailToVerify(getState());
2017-06-03 01:09:52 +02:00
dispatch({
type: ACTIONS.USER_EMAIL_VERIFY_STARTED,
2017-06-08 02:56:52 +02:00
code: verificationToken,
2017-12-26 14:25:26 +01:00
recaptcha,
2017-06-08 02:56:52 +02:00
});
2017-06-03 01:09:52 +02:00
Lbryio.call(
'user_email',
'confirm',
2017-12-26 14:25:26 +01:00
{
verification_token: verificationToken,
email,
recaptcha,
},
'post'
)
2017-06-08 02:56:52 +02:00
.then(userEmail => {
if (userEmail.is_verified) {
dispatch({
type: ACTIONS.USER_EMAIL_VERIFY_SUCCESS,
2017-06-08 02:56:52 +02:00
data: { email },
});
dispatch(doUserFetch());
2017-06-08 02:56:52 +02:00
} else {
throw new Error('Your email is still not verified.'); // shouldn't happen
2017-06-08 02:56:52 +02:00
}
2017-06-23 19:47:07 +02:00
})
2018-01-15 14:32:01 +01:00
.catch(error => dispatch(doUserEmailVerifyFailure(error)));
2017-06-08 02:56:52 +02:00
};
}
2017-07-19 01:00:13 +02:00
export function doUserIdentityVerify(stripeToken) {
return dispatch => {
2017-07-19 01:00:13 +02:00
dispatch({
type: ACTIONS.USER_IDENTITY_VERIFY_STARTED,
2017-07-19 01:00:13 +02:00
token: stripeToken,
});
Lbryio.call('user', 'verify_identity', { stripe_token: stripeToken }, 'post')
2017-07-19 01:00:13 +02:00
.then(user => {
if (user.is_identity_verified) {
dispatch({
type: ACTIONS.USER_IDENTITY_VERIFY_SUCCESS,
2017-07-19 01:00:13 +02:00
data: { user },
});
2017-07-25 00:59:26 +02:00
dispatch(doClaimRewardType(rewards.TYPE_NEW_USER));
2017-07-19 01:00:13 +02:00
} else {
throw new Error('Your identity is still not verified. This should not happen.'); // shouldn't happen
2017-07-19 01:00:13 +02:00
}
})
.catch(error => {
dispatch({
type: ACTIONS.USER_IDENTITY_VERIFY_FAILURE,
data: { error: error.toString() },
});
2017-07-19 01:00:13 +02:00
});
};
}
2017-07-21 23:45:37 +02:00
2017-07-21 08:23:39 +02:00
export function doFetchAccessToken() {
return dispatch => {
2017-07-21 08:23:39 +02:00
const success = token =>
dispatch({
type: ACTIONS.FETCH_ACCESS_TOKEN_SUCCESS,
2017-07-21 08:23:39 +02:00
data: { token },
});
Lbryio.getAuthToken().then(success);
2017-08-18 05:31:44 +02:00
};
}
export function doUserInviteNew(email) {
return dispatch => {
2017-08-18 05:31:44 +02:00
dispatch({
type: ACTIONS.USER_INVITE_NEW_STARTED,
2017-08-18 05:31:44 +02:00
});
Lbryio.call('user', 'invite', { email }, 'post')
.then(() => {
2017-08-18 05:31:44 +02:00
dispatch({
type: ACTIONS.USER_INVITE_NEW_SUCCESS,
2017-08-18 05:31:44 +02:00
data: { email },
});
dispatch(
2018-04-19 20:49:47 +02:00
doNotify({
displayType: ['snackbar'],
message: __('Invite sent to %s', email),
2017-08-18 05:31:44 +02:00
})
);
dispatch(doFetchInviteStatus());
})
.catch(error => {
dispatch({
type: ACTIONS.USER_INVITE_NEW_FAILURE,
2017-08-18 05:31:44 +02:00
data: { error },
});
});
};
}