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

230 lines
5.6 KiB
JavaScript
Raw Normal View History

import * as ACTIONS from 'constants/action_types';
import * as MODALS from 'constants/modal_types';
import Lbryio from 'lbryio';
import { doOpenModal, doShowSnackBar } from 'redux/actions/app';
import { doRewardList, doClaimRewardType } from 'redux/actions/rewards';
import { selectEmailToVerify } from 'redux/selectors/user';
import rewards from 'rewards';
export function doFetchInviteStatus() {
return function(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
export function doAuthenticate() {
return function(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 => {
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());
2017-06-01 18:20:12 +02:00
})
2017-06-08 02:56:52 +02:00
.catch(error => {
dispatch(doOpenModal(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 function(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 => {
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 doUserEmailNew(email) {
return function(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
};
}
2017-12-26 14:25:26 +01:00
export function doUserEmailVerifyFailure(error) {
return {
type: ACTIONS.USER_EMAIL_VERIFY_FAILURE,
data: { error },
};
}
2017-12-23 03:09:06 +01:00
export function doUserEmailVerify(verificationToken, recaptcha) {
2017-06-03 01:09:52 +02:00
return function(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(doClaimRewardType(rewards.TYPE_CONFIRM_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
})
2017-12-23 03:09:06 +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 function(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 function(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 function(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(
doShowSnackBar({
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 },
});
});
};
}