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

70 lines
2 KiB
JavaScript
Raw Permalink Normal View History

import * as ACTIONS from 'constants/action_types';
import REWARDS from 'rewards';
import { Lbryio } from 'lbryinc';
import { doClaimRewardType } from 'redux/actions/rewards';
2019-11-19 21:34:25 +01:00
import { parseURI } from 'lbry-redux';
import { doAlertWaitingForSync } from 'redux/actions/app';
2017-12-08 21:14:35 +01:00
2020-11-02 17:51:08 +01:00
export const doChannelSubscribe = subscription => (dispatch, getState) => {
const {
settings: { daemonSettings },
sync: { prefsReady: ready },
} = getState();
if (!ready) {
return dispatch(doAlertWaitingForSync());
}
const { share_usage_data: shareSetting } = daemonSettings;
const isSharingData = shareSetting || IS_WEB;
2018-10-31 18:08:30 +01:00
const subscriptionUri = subscription.uri;
if (!subscriptionUri.startsWith('lbry://')) {
2019-10-13 19:41:51 +02:00
throw Error(`Subscription uris must include the "lbry://" prefix.\nTried to subscribe to ${subscriptionUri}`);
2018-10-31 18:08:30 +01:00
}
dispatch({
type: ACTIONS.CHANNEL_SUBSCRIBE,
data: subscription,
});
// if the user isn't sharing data, keep the subscriptions entirely in the app
if (isSharingData || IS_WEB) {
const { channelClaimId } = parseURI(subscription.uri);
// They are sharing data, we can store their subscriptions in our internal database
Lbryio.call('subscription', 'new', {
channel_name: subscription.channelName,
claim_id: channelClaimId,
2020-11-02 17:51:08 +01:00
notifications_disabled: subscription.notificationsDisabled,
});
dispatch(doClaimRewardType(REWARDS.TYPE_SUBSCRIPTION, { failSilently: true }));
}
};
2020-11-02 17:51:08 +01:00
export const doChannelUnsubscribe = subscription => (dispatch, getState) => {
const {
settings: { daemonSettings },
sync: { prefsReady: ready },
} = getState();
if (!ready) {
return dispatch(doAlertWaitingForSync());
}
const { share_usage_data: shareSetting } = daemonSettings;
const isSharingData = shareSetting || IS_WEB;
dispatch({
type: ACTIONS.CHANNEL_UNSUBSCRIBE,
data: subscription,
});
if (isSharingData) {
const { channelClaimId } = parseURI(subscription.uri);
Lbryio.call('subscription', 'delete', {
claim_id: channelClaimId,
});
}
};