2021-10-17 10:36:14 +02:00
|
|
|
import Lbry from 'lbry';
|
|
|
|
import { makeSelectClaimForUri } from 'redux/selectors/claims';
|
|
|
|
import { doFetchChannelListMine } from 'redux/actions/claims';
|
|
|
|
import { isURIValid, normalizeURI } from 'util/lbryURI';
|
|
|
|
import { batchActions } from 'util/batch-actions';
|
2022-03-09 19:05:37 +01:00
|
|
|
import { getStripeEnvironment } from 'util/stripe';
|
2021-10-17 10:36:14 +02:00
|
|
|
|
2020-06-15 22:33:03 +02:00
|
|
|
import * as ACTIONS from 'constants/action_types';
|
2022-03-16 16:09:43 +01:00
|
|
|
import { doFetchGeoBlockedList } from 'redux/actions/blocked';
|
2020-06-15 22:33:03 +02:00
|
|
|
import { doClaimRewardType, doRewardList } from 'redux/actions/rewards';
|
|
|
|
import { selectEmailToVerify, selectPhoneToVerify, selectUserCountryCode, selectUser } from 'redux/selectors/user';
|
|
|
|
import { doToast } from 'redux/actions/notifications';
|
|
|
|
import rewards from 'rewards';
|
|
|
|
import { Lbryio } from 'lbryinc';
|
2020-11-11 03:47:51 +01:00
|
|
|
import { DOMAIN } from 'config';
|
2021-01-06 19:13:56 +01:00
|
|
|
import { getDefaultLanguage } from 'util/default-languages';
|
2020-11-19 22:54:07 +01:00
|
|
|
const AUTH_IN_PROGRESS = 'authInProgress';
|
|
|
|
export let sessionStorageAvailable = false;
|
2021-01-19 18:01:01 +01:00
|
|
|
const CHECK_INTERVAL = 200;
|
|
|
|
const AUTH_WAIT_TIMEOUT = 10000;
|
2022-03-09 19:05:37 +01:00
|
|
|
const stripeEnvironment = getStripeEnvironment();
|
|
|
|
|
|
|
|
const ODYSEE_CHANNEL_ID = '80d2590ad04e36fb1d077a9b9e3a8bba76defdf8';
|
2020-06-15 22:33:03 +02:00
|
|
|
|
|
|
|
export function doFetchInviteStatus(shouldCallRewardList = true) {
|
2021-02-23 04:45:05 +01:00
|
|
|
return (dispatch) => {
|
2020-06-15 22:33:03 +02:00
|
|
|
dispatch({
|
|
|
|
type: ACTIONS.USER_INVITE_STATUS_FETCH_STARTED,
|
|
|
|
});
|
|
|
|
|
|
|
|
Promise.all([Lbryio.call('user', 'invite_status'), Lbryio.call('user_referral_code', 'list')])
|
|
|
|
.then(([status, code]) => {
|
|
|
|
if (shouldCallRewardList) {
|
|
|
|
dispatch(doRewardList());
|
|
|
|
}
|
|
|
|
|
|
|
|
dispatch({
|
|
|
|
type: ACTIONS.USER_INVITE_STATUS_FETCH_SUCCESS,
|
|
|
|
data: {
|
|
|
|
invitesRemaining: status.invites_remaining ? status.invites_remaining : 0,
|
|
|
|
invitees: status.invitees,
|
|
|
|
referralLink: `${Lbryio.CONNECTION_STRING}user/refer?r=${code}`,
|
|
|
|
referralCode: code,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
})
|
2021-02-23 04:45:05 +01:00
|
|
|
.catch((error) => {
|
2020-06-15 22:33:03 +02:00
|
|
|
dispatch({
|
|
|
|
type: ACTIONS.USER_INVITE_STATUS_FETCH_FAILURE,
|
|
|
|
data: { error },
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-12-20 06:35:32 +01:00
|
|
|
export function doInstallNew(appVersion, callbackForUsersWhoAreSharingData, domain) {
|
2020-06-15 22:33:03 +02:00
|
|
|
const payload = { app_version: appVersion, domain };
|
|
|
|
|
2021-02-23 04:45:05 +01:00
|
|
|
Lbry.status().then((status) => {
|
2020-06-15 22:33:03 +02:00
|
|
|
payload.app_id =
|
|
|
|
domain && domain !== 'lbry.tv'
|
|
|
|
? (domain.replace(/[.]/gi, '') + status.installation_id).slice(0, 66)
|
|
|
|
: status.installation_id;
|
|
|
|
payload.node_id = status.lbry_id;
|
2021-02-23 04:45:05 +01:00
|
|
|
Lbry.version().then((version) => {
|
2020-06-15 22:33:03 +02:00
|
|
|
payload.daemon_version = version.lbrynet_version;
|
2021-12-20 06:35:32 +01:00
|
|
|
payload.operating_system = version.os_system;
|
2020-06-15 22:33:03 +02:00
|
|
|
payload.platform = version.platform;
|
|
|
|
Lbryio.call('install', 'new', payload);
|
|
|
|
|
|
|
|
if (callbackForUsersWhoAreSharingData) {
|
|
|
|
callbackForUsersWhoAreSharingData(status);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-11-19 22:54:07 +01:00
|
|
|
function checkAuthBusy() {
|
2021-01-14 23:08:26 +01:00
|
|
|
let time = Date.now();
|
2021-10-17 10:36:14 +02:00
|
|
|
return new Promise(function (resolve, reject) {
|
2020-11-19 22:54:07 +01:00
|
|
|
(function waitForAuth() {
|
2021-01-14 23:08:26 +01:00
|
|
|
try {
|
|
|
|
sessionStorage.setItem('test', 'available');
|
|
|
|
sessionStorage.removeItem('test');
|
|
|
|
sessionStorageAvailable = true;
|
|
|
|
} catch (e) {
|
|
|
|
if (e) {
|
|
|
|
// no session storage
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!IS_WEB || !sessionStorageAvailable) {
|
|
|
|
return resolve();
|
|
|
|
}
|
|
|
|
const inProgress = window.sessionStorage.getItem(AUTH_IN_PROGRESS);
|
|
|
|
if (!inProgress) {
|
|
|
|
window.sessionStorage.setItem(AUTH_IN_PROGRESS, 'true');
|
|
|
|
return resolve();
|
|
|
|
} else {
|
2021-01-19 18:01:01 +01:00
|
|
|
if (Date.now() - time < AUTH_WAIT_TIMEOUT) {
|
|
|
|
setTimeout(waitForAuth, CHECK_INTERVAL);
|
2021-01-14 23:08:26 +01:00
|
|
|
} else {
|
|
|
|
return resolve();
|
|
|
|
}
|
|
|
|
}
|
2020-11-19 22:54:07 +01:00
|
|
|
})();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-03-09 19:05:37 +01:00
|
|
|
/***
|
|
|
|
* Given a user, return their highest ranking Odysee membership (Premium or Premium Plus)
|
|
|
|
* @param dispatch
|
|
|
|
* @param user
|
|
|
|
* @returns {Promise<void>}
|
|
|
|
*/
|
|
|
|
export function doCheckUserOdyseeMemberships(user) {
|
|
|
|
return async (dispatch) => {
|
|
|
|
// get memberships for a given user
|
|
|
|
// TODO: in the future, can we specify this just to @odysee?
|
|
|
|
|
|
|
|
const response = await Lbryio.call(
|
|
|
|
'membership',
|
|
|
|
'mine',
|
|
|
|
{
|
|
|
|
environment: stripeEnvironment,
|
|
|
|
},
|
|
|
|
'post'
|
|
|
|
);
|
|
|
|
|
|
|
|
let savedMemberships = [];
|
|
|
|
let highestMembershipRanking;
|
|
|
|
|
|
|
|
// TODO: this will work for now, but it should be adjusted
|
|
|
|
// TODO: to check if it's active, or if it's cancelled if it's still valid past current date
|
|
|
|
// loop through all memberships and save the @odysee ones
|
|
|
|
// maybe in the future we can only hit @odysee in the API call
|
|
|
|
for (const membership of response) {
|
|
|
|
if (membership.MembershipDetails && membership.MembershipDetails.channel_name === '@odysee') {
|
|
|
|
savedMemberships.push(membership.MembershipDetails.name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// determine highest ranking membership based on returned data
|
|
|
|
// note: this is from an odd state in the API where a user can be both premium/Premium + at the same time
|
|
|
|
// I expect this can change once upgrade/downgrade is implemented
|
|
|
|
if (savedMemberships.length > 0) {
|
|
|
|
// if premium plus is a membership, return that, otherwise it's only premium
|
|
|
|
const premiumPlusExists = savedMemberships.includes('Premium+');
|
|
|
|
if (premiumPlusExists) {
|
|
|
|
highestMembershipRanking = 'Premium+';
|
|
|
|
} else {
|
|
|
|
highestMembershipRanking = 'Premium';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
dispatch({
|
|
|
|
type: ACTIONS.ADD_ODYSEE_MEMBERSHIP_DATA,
|
|
|
|
data: { user, odyseeMembershipName: highestMembershipRanking },
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-06-15 22:33:03 +02:00
|
|
|
// TODO: Call doInstallNew separately so we don't have to pass appVersion and os_system params?
|
|
|
|
export function doAuthenticate(
|
|
|
|
appVersion,
|
|
|
|
shareUsageData = true,
|
|
|
|
callbackForUsersWhoAreSharingData,
|
2021-12-20 06:35:32 +01:00
|
|
|
callInstall = true
|
2020-06-15 22:33:03 +02:00
|
|
|
) {
|
2021-02-23 04:45:05 +01:00
|
|
|
return (dispatch) => {
|
2020-06-15 22:33:03 +02:00
|
|
|
dispatch({
|
|
|
|
type: ACTIONS.AUTHENTICATION_STARTED,
|
|
|
|
});
|
2020-11-19 22:54:07 +01:00
|
|
|
checkAuthBusy()
|
|
|
|
.then(() => {
|
2021-01-06 19:13:56 +01:00
|
|
|
return Lbryio.authenticate(DOMAIN, getDefaultLanguage());
|
2020-11-19 22:54:07 +01:00
|
|
|
})
|
2021-02-23 04:45:05 +01:00
|
|
|
.then((user) => {
|
2020-11-19 22:54:07 +01:00
|
|
|
if (sessionStorageAvailable) window.sessionStorage.removeItem(AUTH_IN_PROGRESS);
|
2021-02-23 04:45:05 +01:00
|
|
|
Lbryio.getAuthToken().then((token) => {
|
2020-06-15 22:33:03 +02:00
|
|
|
dispatch({
|
|
|
|
type: ACTIONS.AUTHENTICATION_SUCCESS,
|
|
|
|
data: { user, accessToken: token },
|
|
|
|
});
|
|
|
|
|
2022-03-09 19:05:37 +01:00
|
|
|
// if user is an Odysee member, get the membership details
|
|
|
|
if (user.odysee_member) {
|
|
|
|
dispatch(doCheckUserOdyseeMemberships(user));
|
|
|
|
}
|
|
|
|
|
2020-06-15 22:33:03 +02:00
|
|
|
if (shareUsageData) {
|
|
|
|
dispatch(doRewardList());
|
2022-01-21 11:11:42 +01:00
|
|
|
|
2022-01-03 10:12:18 +01:00
|
|
|
if (callInstall && !user?.device_types?.includes('web')) {
|
2021-12-20 06:35:32 +01:00
|
|
|
doInstallNew(appVersion, callbackForUsersWhoAreSharingData, DOMAIN);
|
2020-06-15 22:33:03 +02:00
|
|
|
}
|
|
|
|
}
|
2022-03-16 16:09:43 +01:00
|
|
|
|
|
|
|
dispatch(doFetchGeoBlockedList());
|
2020-06-15 22:33:03 +02:00
|
|
|
});
|
|
|
|
})
|
2021-02-23 04:45:05 +01:00
|
|
|
.catch((error) => {
|
2021-01-19 17:17:23 +01:00
|
|
|
if (sessionStorageAvailable) window.sessionStorage.removeItem(AUTH_IN_PROGRESS);
|
|
|
|
|
2020-06-15 22:33:03 +02:00
|
|
|
dispatch({
|
|
|
|
type: ACTIONS.AUTHENTICATION_FAILURE,
|
|
|
|
data: { error },
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function doUserFetch() {
|
2021-02-23 04:45:05 +01:00
|
|
|
return (dispatch) =>
|
2020-06-15 22:33:03 +02:00
|
|
|
new Promise((resolve, reject) => {
|
|
|
|
dispatch({
|
|
|
|
type: ACTIONS.USER_FETCH_STARTED,
|
|
|
|
});
|
|
|
|
|
|
|
|
Lbryio.getCurrentUser()
|
2021-02-23 04:45:05 +01:00
|
|
|
.then((user) => {
|
2022-03-09 19:05:37 +01:00
|
|
|
// get user membership status
|
|
|
|
if (user.odysee_member) {
|
|
|
|
dispatch(doCheckUserOdyseeMemberships(user));
|
|
|
|
}
|
|
|
|
|
2020-06-15 22:33:03 +02:00
|
|
|
dispatch({
|
|
|
|
type: ACTIONS.USER_FETCH_SUCCESS,
|
|
|
|
data: { user },
|
|
|
|
});
|
|
|
|
resolve(user);
|
|
|
|
})
|
2021-02-23 04:45:05 +01:00
|
|
|
.catch((error) => {
|
2020-06-15 22:33:03 +02:00
|
|
|
reject(error);
|
|
|
|
dispatch({
|
|
|
|
type: ACTIONS.USER_FETCH_FAILURE,
|
|
|
|
data: { error },
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export function doUserCheckEmailVerified() {
|
|
|
|
// This will happen in the background so we don't need loading booleans
|
2021-02-23 04:45:05 +01:00
|
|
|
return (dispatch) => {
|
|
|
|
Lbryio.getCurrentUser().then((user) => {
|
2020-06-15 22:33:03 +02:00
|
|
|
if (user.has_verified_email) {
|
2022-03-09 19:05:37 +01:00
|
|
|
// check premium membership
|
|
|
|
if (user.odysee_member) {
|
|
|
|
dispatch(doCheckUserOdyseeMemberships(user));
|
|
|
|
}
|
|
|
|
|
2020-06-15 22:33:03 +02:00
|
|
|
dispatch(doRewardList());
|
|
|
|
|
|
|
|
dispatch({
|
|
|
|
type: ACTIONS.USER_FETCH_SUCCESS,
|
|
|
|
data: { user },
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function doUserPhoneReset() {
|
|
|
|
return {
|
|
|
|
type: ACTIONS.USER_PHONE_RESET,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function doUserPhoneNew(phone, countryCode) {
|
2021-02-23 04:45:05 +01:00
|
|
|
return (dispatch) => {
|
2020-06-15 22:33:03 +02:00
|
|
|
dispatch({
|
|
|
|
type: ACTIONS.USER_PHONE_NEW_STARTED,
|
|
|
|
data: { phone, country_code: countryCode },
|
|
|
|
});
|
|
|
|
|
|
|
|
const success = () => {
|
|
|
|
dispatch({
|
|
|
|
type: ACTIONS.USER_PHONE_NEW_SUCCESS,
|
|
|
|
data: { phone },
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2021-02-23 04:45:05 +01:00
|
|
|
const failure = (error) => {
|
2020-06-15 22:33:03 +02:00
|
|
|
dispatch({
|
|
|
|
type: ACTIONS.USER_PHONE_NEW_FAILURE,
|
|
|
|
data: { error },
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
Lbryio.call('user', 'phone_number_new', { phone_number: phone, country_code: countryCode }, 'post').then(
|
|
|
|
success,
|
|
|
|
failure
|
|
|
|
);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function doUserPhoneVerifyFailure(error) {
|
|
|
|
return {
|
|
|
|
type: ACTIONS.USER_PHONE_VERIFY_FAILURE,
|
|
|
|
data: { error },
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function doUserPhoneVerify(verificationCode) {
|
|
|
|
return (dispatch, getState) => {
|
|
|
|
const phoneNumber = selectPhoneToVerify(getState());
|
|
|
|
const countryCode = selectUserCountryCode(getState());
|
|
|
|
|
|
|
|
dispatch({
|
|
|
|
type: ACTIONS.USER_PHONE_VERIFY_STARTED,
|
|
|
|
code: verificationCode,
|
|
|
|
});
|
|
|
|
|
|
|
|
Lbryio.call(
|
|
|
|
'user',
|
|
|
|
'phone_number_confirm',
|
|
|
|
{
|
|
|
|
verification_code: verificationCode,
|
|
|
|
phone_number: phoneNumber,
|
|
|
|
country_code: countryCode,
|
|
|
|
},
|
|
|
|
'post'
|
|
|
|
)
|
2021-02-23 04:45:05 +01:00
|
|
|
.then((user) => {
|
2020-06-15 22:33:03 +02:00
|
|
|
if (user.is_identity_verified) {
|
|
|
|
dispatch({
|
|
|
|
type: ACTIONS.USER_PHONE_VERIFY_SUCCESS,
|
|
|
|
data: { user },
|
|
|
|
});
|
|
|
|
dispatch(doClaimRewardType(rewards.TYPE_NEW_USER));
|
|
|
|
}
|
|
|
|
})
|
2021-02-23 04:45:05 +01:00
|
|
|
.catch((error) => dispatch(doUserPhoneVerifyFailure(error)));
|
2020-06-15 22:33:03 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function doUserEmailToVerify(email) {
|
2021-02-23 04:45:05 +01:00
|
|
|
return (dispatch) => {
|
2020-06-15 22:33:03 +02:00
|
|
|
dispatch({
|
|
|
|
type: ACTIONS.USER_EMAIL_VERIFY_SET,
|
|
|
|
data: { email },
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function doUserEmailNew(email) {
|
2021-02-23 04:45:05 +01:00
|
|
|
return (dispatch) => {
|
2020-06-15 22:33:03 +02:00
|
|
|
dispatch({
|
|
|
|
type: ACTIONS.USER_EMAIL_NEW_STARTED,
|
|
|
|
email,
|
|
|
|
});
|
|
|
|
|
|
|
|
const success = () => {
|
|
|
|
dispatch({
|
|
|
|
type: ACTIONS.USER_EMAIL_NEW_SUCCESS,
|
|
|
|
data: { email },
|
|
|
|
});
|
|
|
|
dispatch(doUserFetch());
|
|
|
|
};
|
|
|
|
|
2021-02-23 04:45:05 +01:00
|
|
|
const failure = (error) => {
|
2020-06-15 22:33:03 +02:00
|
|
|
dispatch({
|
|
|
|
type: ACTIONS.USER_EMAIL_NEW_FAILURE,
|
|
|
|
data: { error },
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
Lbryio.call('user_email', 'new', { email, send_verification_email: true }, 'post')
|
2021-02-23 04:45:05 +01:00
|
|
|
.catch((error) => {
|
2020-06-15 22:33:03 +02:00
|
|
|
if (error.response && error.response.status === 409) {
|
|
|
|
dispatch({
|
|
|
|
type: ACTIONS.USER_EMAIL_NEW_EXISTS,
|
|
|
|
});
|
|
|
|
|
|
|
|
return Lbryio.call('user_email', 'resend_token', { email, only_if_expired: true }, 'post').then(
|
|
|
|
success,
|
|
|
|
failure
|
|
|
|
);
|
|
|
|
}
|
|
|
|
throw error;
|
|
|
|
})
|
|
|
|
.then(success, failure);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function doUserCheckIfEmailExists(email) {
|
2021-02-23 04:45:05 +01:00
|
|
|
return (dispatch) => {
|
2020-06-15 22:33:03 +02:00
|
|
|
dispatch({
|
|
|
|
type: ACTIONS.USER_EMAIL_NEW_STARTED,
|
|
|
|
email,
|
|
|
|
});
|
|
|
|
|
2021-02-23 04:45:05 +01:00
|
|
|
const triggerEmailFlow = (hasPassword) => {
|
2020-06-15 22:33:03 +02:00
|
|
|
dispatch({
|
|
|
|
type: ACTIONS.USER_EMAIL_NEW_SUCCESS,
|
|
|
|
data: { email },
|
|
|
|
});
|
|
|
|
|
|
|
|
dispatch({
|
|
|
|
type: ACTIONS.USER_EMAIL_NEW_EXISTS,
|
|
|
|
});
|
|
|
|
|
|
|
|
if (hasPassword) {
|
|
|
|
dispatch({
|
|
|
|
type: ACTIONS.USER_PASSWORD_EXISTS,
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
// If they don't have a password, they will need to use the email verification api
|
|
|
|
Lbryio.call('user_email', 'resend_token', { email, only_if_expired: true }, 'post');
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-02-23 04:45:05 +01:00
|
|
|
const success = (response) => {
|
2020-06-15 22:33:03 +02:00
|
|
|
triggerEmailFlow(response.has_password);
|
|
|
|
};
|
|
|
|
|
2021-02-23 04:45:05 +01:00
|
|
|
const failure = (error) =>
|
2020-06-15 22:33:03 +02:00
|
|
|
dispatch({
|
|
|
|
type: ACTIONS.USER_EMAIL_NEW_FAILURE,
|
|
|
|
data: { error },
|
|
|
|
});
|
|
|
|
|
|
|
|
Lbryio.call('user', 'exists', { email }, 'post')
|
2021-02-23 04:45:05 +01:00
|
|
|
.catch((error) => {
|
2022-03-09 19:05:37 +01:00
|
|
|
// no email
|
2020-06-15 22:33:03 +02:00
|
|
|
if (error.response && error.response.status === 404) {
|
|
|
|
dispatch({
|
|
|
|
type: ACTIONS.USER_EMAIL_NEW_DOES_NOT_EXIST,
|
|
|
|
});
|
2022-03-09 19:05:37 +01:00
|
|
|
// sign in by email
|
2020-06-15 22:33:03 +02:00
|
|
|
} else if (error.response && error.response.status === 412) {
|
|
|
|
triggerEmailFlow(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
throw error;
|
|
|
|
})
|
2022-03-09 19:05:37 +01:00
|
|
|
// sign the user in
|
2020-06-15 22:33:03 +02:00
|
|
|
.then(success, failure);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function doUserSignIn(email, password) {
|
2021-02-23 04:45:05 +01:00
|
|
|
return (dispatch) => {
|
2020-06-15 22:33:03 +02:00
|
|
|
dispatch({
|
|
|
|
type: ACTIONS.USER_EMAIL_NEW_STARTED,
|
|
|
|
email,
|
|
|
|
});
|
|
|
|
|
|
|
|
const success = () => {
|
|
|
|
dispatch({
|
|
|
|
type: ACTIONS.USER_EMAIL_NEW_SUCCESS,
|
|
|
|
data: { email },
|
|
|
|
});
|
|
|
|
dispatch(doUserFetch());
|
|
|
|
};
|
|
|
|
|
2021-02-23 04:45:05 +01:00
|
|
|
const failure = (error) =>
|
2020-06-15 22:33:03 +02:00
|
|
|
dispatch({
|
|
|
|
type: ACTIONS.USER_EMAIL_NEW_FAILURE,
|
|
|
|
data: { error },
|
|
|
|
});
|
|
|
|
|
|
|
|
Lbryio.call('user', 'signin', { email, ...(password ? { password } : {}) }, 'post')
|
2021-02-23 04:45:05 +01:00
|
|
|
.catch((error) => {
|
2020-06-15 22:33:03 +02:00
|
|
|
if (error.response && error.response.status === 409) {
|
|
|
|
dispatch({
|
|
|
|
type: ACTIONS.USER_EMAIL_NEW_EXISTS,
|
|
|
|
});
|
|
|
|
|
|
|
|
return Lbryio.call('user_email', 'resend_token', { email, only_if_expired: true }, 'post').then(
|
|
|
|
success,
|
|
|
|
failure
|
|
|
|
);
|
|
|
|
}
|
|
|
|
throw error;
|
|
|
|
})
|
|
|
|
.then(success, failure);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function doUserSignUp(email, password) {
|
2021-02-23 04:45:05 +01:00
|
|
|
return (dispatch) =>
|
2020-06-15 22:33:03 +02:00
|
|
|
new Promise((resolve, reject) => {
|
|
|
|
dispatch({
|
|
|
|
type: ACTIONS.USER_EMAIL_NEW_STARTED,
|
|
|
|
email,
|
|
|
|
});
|
|
|
|
|
|
|
|
const success = () => {
|
|
|
|
dispatch({
|
|
|
|
type: ACTIONS.USER_EMAIL_NEW_SUCCESS,
|
|
|
|
data: { email },
|
|
|
|
});
|
|
|
|
dispatch(doUserFetch());
|
|
|
|
resolve();
|
|
|
|
};
|
|
|
|
|
2021-02-23 04:45:05 +01:00
|
|
|
const failure = (error) => {
|
2020-06-15 22:33:03 +02:00
|
|
|
if (error.response && error.response.status === 409) {
|
|
|
|
dispatch({
|
|
|
|
type: ACTIONS.USER_EMAIL_NEW_EXISTS,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
dispatch({
|
|
|
|
type: ACTIONS.USER_EMAIL_NEW_FAILURE,
|
|
|
|
data: { error },
|
|
|
|
});
|
|
|
|
|
|
|
|
reject(error);
|
|
|
|
};
|
|
|
|
|
|
|
|
Lbryio.call('user', 'signup', { email, ...(password ? { password } : {}) }, 'post').then(success, failure);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export function doUserPasswordReset(email) {
|
2021-02-23 04:45:05 +01:00
|
|
|
return (dispatch) => {
|
2020-06-15 22:33:03 +02:00
|
|
|
dispatch({
|
|
|
|
type: ACTIONS.USER_PASSWORD_RESET_STARTED,
|
|
|
|
email,
|
|
|
|
});
|
|
|
|
|
|
|
|
const success = () => {
|
|
|
|
dispatch({
|
|
|
|
type: ACTIONS.USER_PASSWORD_RESET_SUCCESS,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2021-02-23 04:45:05 +01:00
|
|
|
const failure = (error) =>
|
2020-06-15 22:33:03 +02:00
|
|
|
dispatch({
|
|
|
|
type: ACTIONS.USER_PASSWORD_RESET_FAILURE,
|
|
|
|
data: { error },
|
|
|
|
});
|
|
|
|
|
|
|
|
Lbryio.call('user_password', 'reset', { email }, 'post').then(success, failure);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function doUserPasswordSet(newPassword, oldPassword, authToken) {
|
2021-02-23 04:45:05 +01:00
|
|
|
return (dispatch) => {
|
2020-06-15 22:33:03 +02:00
|
|
|
dispatch({
|
|
|
|
type: ACTIONS.USER_PASSWORD_SET_STARTED,
|
|
|
|
});
|
|
|
|
|
|
|
|
const success = () => {
|
|
|
|
dispatch({
|
|
|
|
type: ACTIONS.USER_PASSWORD_SET_SUCCESS,
|
|
|
|
});
|
|
|
|
dispatch(doUserFetch());
|
|
|
|
};
|
|
|
|
|
2021-02-23 04:45:05 +01:00
|
|
|
const failure = (error) =>
|
2020-06-15 22:33:03 +02:00
|
|
|
dispatch({
|
|
|
|
type: ACTIONS.USER_PASSWORD_SET_FAILURE,
|
|
|
|
data: { error },
|
|
|
|
});
|
|
|
|
|
|
|
|
Lbryio.call(
|
|
|
|
'user_password',
|
|
|
|
'set',
|
|
|
|
{
|
|
|
|
new_password: newPassword,
|
|
|
|
...(oldPassword ? { old_password: oldPassword } : {}),
|
|
|
|
...(authToken ? { auth_token: authToken } : {}),
|
|
|
|
},
|
|
|
|
'post'
|
|
|
|
).then(success, failure);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function doUserResendVerificationEmail(email) {
|
2021-02-23 04:45:05 +01:00
|
|
|
return (dispatch) => {
|
2020-06-15 22:33:03 +02:00
|
|
|
dispatch({
|
|
|
|
type: ACTIONS.USER_EMAIL_VERIFY_RETRY_STARTED,
|
|
|
|
});
|
|
|
|
|
|
|
|
const success = () => {
|
|
|
|
dispatch({
|
|
|
|
type: ACTIONS.USER_EMAIL_VERIFY_RETRY_SUCCESS,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2021-02-23 04:45:05 +01:00
|
|
|
const failure = (error) => {
|
2020-06-15 22:33:03 +02:00
|
|
|
dispatch({
|
|
|
|
type: ACTIONS.USER_EMAIL_VERIFY_RETRY_FAILURE,
|
|
|
|
data: { error },
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2020-08-25 16:18:11 +02:00
|
|
|
Lbryio.call('user_email', 'resend_token', { email, only_if_expired: true }, 'post')
|
2021-02-23 04:45:05 +01:00
|
|
|
.catch((error) => {
|
2020-06-15 22:33:03 +02:00
|
|
|
if (error.response && error.response.status === 409) {
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.then(success, failure);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function doClearEmailEntry() {
|
|
|
|
return {
|
|
|
|
type: ACTIONS.USER_EMAIL_NEW_CLEAR_ENTRY,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function doClearPasswordEntry() {
|
|
|
|
return {
|
|
|
|
type: ACTIONS.USER_PASSWORD_SET_CLEAR,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function doUserEmailVerifyFailure(error) {
|
|
|
|
return {
|
|
|
|
type: ACTIONS.USER_EMAIL_VERIFY_FAILURE,
|
|
|
|
data: { error },
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function doUserEmailVerify(verificationToken, recaptcha) {
|
|
|
|
return (dispatch, getState) => {
|
|
|
|
const email = selectEmailToVerify(getState());
|
|
|
|
|
|
|
|
dispatch({
|
|
|
|
type: ACTIONS.USER_EMAIL_VERIFY_STARTED,
|
|
|
|
code: verificationToken,
|
|
|
|
recaptcha,
|
|
|
|
});
|
|
|
|
|
|
|
|
Lbryio.call(
|
|
|
|
'user_email',
|
|
|
|
'confirm',
|
|
|
|
{
|
|
|
|
verification_token: verificationToken,
|
|
|
|
email,
|
|
|
|
recaptcha,
|
|
|
|
},
|
|
|
|
'post'
|
|
|
|
)
|
2021-02-23 04:45:05 +01:00
|
|
|
.then((userEmail) => {
|
2020-06-15 22:33:03 +02:00
|
|
|
if (userEmail.is_verified) {
|
|
|
|
dispatch({
|
|
|
|
type: ACTIONS.USER_EMAIL_VERIFY_SUCCESS,
|
|
|
|
data: { email },
|
|
|
|
});
|
|
|
|
dispatch(doUserFetch());
|
|
|
|
} else {
|
|
|
|
throw new Error('Your email is still not verified.'); // shouldn't happen
|
|
|
|
}
|
|
|
|
})
|
2021-02-23 04:45:05 +01:00
|
|
|
.catch((error) => dispatch(doUserEmailVerifyFailure(error)));
|
2020-06-15 22:33:03 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function doUserIdentityVerify(stripeToken) {
|
2021-02-23 04:45:05 +01:00
|
|
|
return (dispatch) => {
|
2020-06-15 22:33:03 +02:00
|
|
|
dispatch({
|
|
|
|
type: ACTIONS.USER_IDENTITY_VERIFY_STARTED,
|
|
|
|
token: stripeToken,
|
|
|
|
});
|
|
|
|
|
|
|
|
Lbryio.call('user', 'verify_identity', { stripe_token: stripeToken }, 'post')
|
2021-02-23 04:45:05 +01:00
|
|
|
.then((user) => {
|
2020-06-15 22:33:03 +02:00
|
|
|
if (user.is_identity_verified) {
|
|
|
|
dispatch({
|
|
|
|
type: ACTIONS.USER_IDENTITY_VERIFY_SUCCESS,
|
|
|
|
data: { user },
|
|
|
|
});
|
|
|
|
dispatch(doClaimRewardType(rewards.TYPE_NEW_USER));
|
|
|
|
} else {
|
|
|
|
throw new Error('Your identity is still not verified. This should not happen.'); // shouldn't happen
|
|
|
|
}
|
|
|
|
})
|
2021-02-23 04:45:05 +01:00
|
|
|
.catch((error) => {
|
2020-06-15 22:33:03 +02:00
|
|
|
dispatch({
|
|
|
|
type: ACTIONS.USER_IDENTITY_VERIFY_FAILURE,
|
|
|
|
data: { error: error.toString() },
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function doUserInviteNew(email) {
|
2021-02-23 04:45:05 +01:00
|
|
|
return (dispatch) => {
|
2020-06-15 22:33:03 +02:00
|
|
|
dispatch({
|
|
|
|
type: ACTIONS.USER_INVITE_NEW_STARTED,
|
|
|
|
});
|
|
|
|
|
|
|
|
return Lbryio.call('user', 'invite', { email }, 'post')
|
2021-02-23 04:45:05 +01:00
|
|
|
.then((success) => {
|
2020-06-15 22:33:03 +02:00
|
|
|
dispatch({
|
|
|
|
type: ACTIONS.USER_INVITE_NEW_SUCCESS,
|
|
|
|
data: { email },
|
|
|
|
});
|
|
|
|
|
|
|
|
dispatch(
|
|
|
|
doToast({
|
2020-10-03 06:34:51 +02:00
|
|
|
message: __('Invite sent to %email_address%', { email_address: email }),
|
2020-06-15 22:33:03 +02:00
|
|
|
})
|
|
|
|
);
|
|
|
|
|
|
|
|
dispatch(doFetchInviteStatus());
|
|
|
|
return success;
|
|
|
|
})
|
2021-02-23 04:45:05 +01:00
|
|
|
.catch((error) => {
|
2020-06-15 22:33:03 +02:00
|
|
|
dispatch({
|
|
|
|
type: ACTIONS.USER_INVITE_NEW_FAILURE,
|
|
|
|
data: { error },
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function doUserSetReferrerReset() {
|
2021-02-23 04:45:05 +01:00
|
|
|
return (dispatch) => {
|
2020-06-15 22:33:03 +02:00
|
|
|
dispatch({
|
|
|
|
type: ACTIONS.USER_SET_REFERRER_RESET,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
export function doUserSetReferrer(referrer, shouldClaim) {
|
|
|
|
return async (dispatch, getState) => {
|
|
|
|
dispatch({
|
|
|
|
type: ACTIONS.USER_SET_REFERRER_STARTED,
|
|
|
|
});
|
|
|
|
let claim;
|
|
|
|
let referrerCode;
|
2020-10-07 02:59:38 +02:00
|
|
|
const isValid = isURIValid(referrer);
|
|
|
|
if (isValid) {
|
2021-01-22 16:47:21 +01:00
|
|
|
const uri = normalizeURI(referrer);
|
2020-06-15 22:33:03 +02:00
|
|
|
claim = makeSelectClaimForUri(uri)(getState());
|
|
|
|
if (!claim) {
|
|
|
|
try {
|
|
|
|
const response = await Lbry.resolve({ urls: [uri] });
|
2020-10-08 05:20:35 +02:00
|
|
|
if (response && response[uri] && !response[uri].error) claim = response && response[uri];
|
|
|
|
if (claim) {
|
|
|
|
if (claim.signing_channel) {
|
|
|
|
referrerCode = claim.signing_channel.permanent_url.replace('lbry://', '');
|
|
|
|
} else {
|
|
|
|
referrerCode = claim.permanent_url.replace('lbry://', '');
|
|
|
|
}
|
|
|
|
}
|
2020-06-15 22:33:03 +02:00
|
|
|
} catch (error) {
|
|
|
|
dispatch({
|
|
|
|
type: ACTIONS.USER_SET_REFERRER_FAILURE,
|
|
|
|
data: { error },
|
|
|
|
});
|
|
|
|
}
|
2021-02-23 04:45:05 +01:00
|
|
|
} else {
|
|
|
|
referrerCode = claim.permanent_url.replace('lbry://', '');
|
2020-06-15 22:33:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!referrerCode) {
|
|
|
|
referrerCode = referrer;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
await Lbryio.call('user', 'referral', { referrer: referrerCode }, 'post');
|
|
|
|
dispatch({
|
|
|
|
type: ACTIONS.USER_SET_REFERRER_SUCCESS,
|
|
|
|
});
|
|
|
|
if (shouldClaim) {
|
|
|
|
dispatch(doClaimRewardType(rewards.TYPE_REFEREE));
|
|
|
|
dispatch(doUserFetch());
|
|
|
|
} else {
|
|
|
|
dispatch(doUserFetch());
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
dispatch({
|
|
|
|
type: ACTIONS.USER_SET_REFERRER_FAILURE,
|
|
|
|
data: { error },
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function doUserSetCountry(country) {
|
|
|
|
return (dispatch, getState) => {
|
|
|
|
const state = getState();
|
|
|
|
const user = selectUser(state);
|
|
|
|
|
|
|
|
Lbryio.call('user_country', 'set', { country }).then(() => {
|
|
|
|
const newUser = { ...user, country };
|
|
|
|
dispatch({
|
|
|
|
type: ACTIONS.USER_FETCH_SUCCESS,
|
|
|
|
data: { user: newUser },
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function doClaimYoutubeChannels() {
|
2021-02-23 04:45:05 +01:00
|
|
|
return (dispatch) => {
|
2020-06-15 22:33:03 +02:00
|
|
|
dispatch({
|
|
|
|
type: ACTIONS.USER_YOUTUBE_IMPORT_STARTED,
|
|
|
|
});
|
|
|
|
|
|
|
|
let transferResponse;
|
|
|
|
return Lbry.address_list({ page: 1, page_size: 99999 })
|
2021-02-23 04:45:05 +01:00
|
|
|
.then((addressList) => addressList.items[0])
|
|
|
|
.then((address) =>
|
2020-06-15 22:33:03 +02:00
|
|
|
Lbryio.call('yt', 'transfer', {
|
|
|
|
address: address.address,
|
|
|
|
public_key: address.pubkey,
|
2021-02-23 04:45:05 +01:00
|
|
|
}).then((response) => {
|
2020-06-15 22:33:03 +02:00
|
|
|
if (response && response.length) {
|
|
|
|
transferResponse = response;
|
|
|
|
return Promise.all(
|
2021-02-23 04:45:05 +01:00
|
|
|
response.map((channelMeta) => {
|
2020-06-15 22:33:03 +02:00
|
|
|
if (channelMeta && channelMeta.channel && channelMeta.channel.channel_certificate) {
|
|
|
|
return Lbry.channel_import({
|
|
|
|
channel_data: channelMeta.channel.channel_certificate,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
})
|
|
|
|
).then(() => {
|
|
|
|
const actions = [
|
|
|
|
{
|
|
|
|
type: ACTIONS.USER_YOUTUBE_IMPORT_SUCCESS,
|
|
|
|
data: transferResponse,
|
|
|
|
},
|
|
|
|
];
|
|
|
|
actions.push(doUserFetch());
|
|
|
|
actions.push(doFetchChannelListMine());
|
|
|
|
dispatch(batchActions(...actions));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
})
|
|
|
|
)
|
2021-02-23 04:45:05 +01:00
|
|
|
.catch((error) => {
|
2020-06-15 22:33:03 +02:00
|
|
|
dispatch({
|
|
|
|
type: ACTIONS.USER_YOUTUBE_IMPORT_FAILURE,
|
|
|
|
data: String(error),
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function doCheckYoutubeTransfer() {
|
2021-02-23 04:45:05 +01:00
|
|
|
return (dispatch) => {
|
2020-06-15 22:33:03 +02:00
|
|
|
dispatch({
|
|
|
|
type: ACTIONS.USER_YOUTUBE_IMPORT_STARTED,
|
|
|
|
});
|
|
|
|
|
|
|
|
return Lbryio.call('yt', 'transfer')
|
2021-02-23 04:45:05 +01:00
|
|
|
.then((response) => {
|
2020-06-15 22:33:03 +02:00
|
|
|
if (response && response.length) {
|
|
|
|
dispatch({
|
|
|
|
type: ACTIONS.USER_YOUTUBE_IMPORT_SUCCESS,
|
|
|
|
data: response,
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
throw new Error();
|
|
|
|
}
|
|
|
|
})
|
2021-02-23 04:45:05 +01:00
|
|
|
.catch((error) => {
|
2020-06-15 22:33:03 +02:00
|
|
|
dispatch({
|
|
|
|
type: ACTIONS.USER_YOUTUBE_IMPORT_FAILURE,
|
|
|
|
data: String(error),
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
2022-03-09 19:05:37 +01:00
|
|
|
|
|
|
|
/***
|
|
|
|
* Receives a csv of channel claim ids, hits the backend and returns nicely formatted object with relevant info
|
|
|
|
* @param claimIdCsv
|
|
|
|
* @returns {(function(*): Promise<void>)|*}
|
|
|
|
*/
|
|
|
|
export function doFetchUserMemberships(claimIdCsv) {
|
|
|
|
return async (dispatch) => {
|
|
|
|
// check if users have odysee memberships (premium/premium+)
|
|
|
|
const response = await Lbryio.call('membership', 'check', {
|
|
|
|
channel_id: ODYSEE_CHANNEL_ID,
|
|
|
|
claim_ids: claimIdCsv,
|
|
|
|
});
|
|
|
|
|
|
|
|
let updatedResponse = {};
|
|
|
|
|
|
|
|
// loop through returned users
|
|
|
|
for (const user in response) {
|
|
|
|
// if array was returned for a user (indicating a membership exists), otherwise is null
|
|
|
|
if (response[user] && response[user].length) {
|
|
|
|
// get membership for user
|
|
|
|
// note: a for loop is kind of odd, indicates there may be multiple memberships?
|
|
|
|
// probably not needed depending on what we do with the frontend, should revisit
|
|
|
|
for (const membership of response[user]) {
|
|
|
|
if (membership.channel_name) {
|
|
|
|
updatedResponse[user] = membership.name;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// note the user has been fetched but is null
|
|
|
|
updatedResponse[user] = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
dispatch({ type: ACTIONS.ADD_CLAIMIDS_MEMBERSHIP_DATA, data: { response: updatedResponse } });
|
|
|
|
};
|
|
|
|
}
|