lbry-desktop/ui/redux/actions/subscriptions.js

88 lines
2.7 KiB
JavaScript
Raw Normal View History

2021-06-11 22:49:18 +02:00
// @flow
import * as ACTIONS from 'constants/action_types';
import REWARDS from 'rewards';
import { Lbryio } from 'lbryinc';
import { doClaimRewardType } from 'redux/actions/rewards';
import { parseURI } from 'util/lbryURI';
import { doAlertWaitingForSync } from 'redux/actions/app';
2021-06-11 22:49:18 +02:00
import { doToast } from 'redux/actions/notifications';
2017-12-08 21:14:35 +01:00
2021-06-11 22:49:18 +02:00
type SubscriptionArgs = {
channelName: string,
uri: string,
notificationsDisabled?: boolean,
};
export function doToggleSubscription(
subscription: SubscriptionArgs,
followToast: boolean,
isSubscribed: boolean = false
) {
2021-06-11 22:49:18 +02:00
return async (dispatch: Dispatch, getState: GetState) => {
const {
settings: { daemonSettings },
sync: { prefsReady: ready },
} = getState();
2021-06-11 22:49:18 +02:00
if (!ready) {
return dispatch(doAlertWaitingForSync());
}
2021-06-11 22:49:18 +02:00
const { share_usage_data: shareSetting } = daemonSettings;
2022-01-07 20:02:33 +01:00
const isSharingData = shareSetting;
2018-10-31 18:08:30 +01:00
2021-06-11 22:49:18 +02:00
if (!isSubscribed) {
const subscriptionUri = subscription.uri;
if (!subscriptionUri.startsWith('lbry://')) {
throw Error(`Subscription uris must include the "lbry://" prefix.\nTried to subscribe to ${subscriptionUri}`);
}
}
2021-06-11 22:49:18 +02:00
dispatch({
type: !isSubscribed ? ACTIONS.CHANNEL_SUBSCRIBE : ACTIONS.CHANNEL_UNSUBSCRIBE,
data: subscription,
});
2021-06-11 22:49:18 +02:00
// if the user isn't sharing data, keep the subscriptions entirely in the app
2022-01-07 20:02:33 +01:00
if (isSharingData) {
2021-06-11 22:49:18 +02:00
const { channelClaimId } = parseURI(subscription.uri);
2021-06-11 22:49:18 +02:00
if (!isSubscribed) {
// They are sharing data, we can store their subscriptions in our internal database
Lbryio.call('subscription', 'new', {
channel_name: subscription.channelName,
claim_id: channelClaimId,
notifications_disabled: subscription.notificationsDisabled,
});
2021-06-11 22:49:18 +02:00
dispatch(doClaimRewardType(REWARDS.TYPE_SUBSCRIPTION, { failSilently: true }));
} else {
Lbryio.call('subscription', 'delete', {
claim_id: channelClaimId,
});
}
}
2021-07-05 18:49:58 +02:00
if (followToast) {
dispatch(
doToast({
message: __(!isSubscribed ? 'You followed %CHANNEL_NAME%!' : 'Unfollowed %CHANNEL_NAME%.', {
CHANNEL_NAME: subscription.channelName,
}),
})
);
2021-07-05 18:49:58 +02:00
}
2021-06-11 22:49:18 +02:00
};
}
2021-07-05 18:49:58 +02:00
export function doChannelSubscribe(subscription: SubscriptionArgs, followToast: boolean = true) {
2021-06-11 22:49:18 +02:00
return (dispatch: Dispatch) => {
2021-07-05 18:49:58 +02:00
return dispatch(doToggleSubscription(subscription, followToast));
2021-06-11 22:49:18 +02:00
};
}
2021-07-05 18:49:58 +02:00
export function doChannelUnsubscribe(subscription: SubscriptionArgs, followToast: boolean = true) {
2021-06-11 22:49:18 +02:00
return (dispatch: Dispatch) => {
2021-07-05 18:49:58 +02:00
return dispatch(doToggleSubscription(subscription, followToast, true));
2021-06-11 22:49:18 +02:00
};
}