2021-06-11 22:49:18 +02:00
|
|
|
// @flow
|
2017-12-21 18:32:51 +01:00
|
|
|
import * as ACTIONS from 'constants/action_types';
|
2022-03-04 08:15:23 +01:00
|
|
|
import { SIDEBAR_SUBS_DISPLAYED } from 'constants/subscriptions';
|
2020-06-15 22:33:03 +02:00
|
|
|
import REWARDS from 'rewards';
|
|
|
|
import { Lbryio } from 'lbryinc';
|
2022-03-22 06:48:16 +01:00
|
|
|
import { doClaimSearch } from 'redux/actions/claims';
|
2020-06-15 22:33:03 +02:00
|
|
|
import { doClaimRewardType } from 'redux/actions/rewards';
|
2022-03-04 08:15:23 +01:00
|
|
|
import { getChannelFromClaim } from 'util/claim';
|
2021-10-17 10:36:14 +02:00
|
|
|
import { parseURI } from 'util/lbryURI';
|
2020-10-05 20:31:51 +02:00
|
|
|
import { doAlertWaitingForSync } from 'redux/actions/app';
|
2021-06-11 22:49:18 +02:00
|
|
|
import { doToast } from 'redux/actions/notifications';
|
2022-03-04 08:15:23 +01:00
|
|
|
import { selectSubscriptions } from 'redux/selectors/subscriptions';
|
2017-12-08 21:14:35 +01:00
|
|
|
|
2021-06-11 22:49:18 +02:00
|
|
|
type SubscriptionArgs = {
|
|
|
|
channelName: string,
|
|
|
|
uri: string,
|
|
|
|
notificationsDisabled?: boolean,
|
|
|
|
};
|
2020-10-05 20:31:51 +02:00
|
|
|
|
2022-03-04 08:15:23 +01:00
|
|
|
const FETCH_LAST_ACTIVE_SUBS_MIN_INTERVAL_MS = 5 * 60 * 1000;
|
|
|
|
let activeSubsLastFetchedTime = 0;
|
|
|
|
|
2021-10-17 10:36:14 +02:00
|
|
|
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();
|
2020-10-05 20:31:51 +02:00
|
|
|
|
2021-06-11 22:49:18 +02:00
|
|
|
if (!ready) {
|
|
|
|
return dispatch(doAlertWaitingForSync());
|
|
|
|
}
|
2018-05-07 06:50:55 +02:00
|
|
|
|
2021-06-11 22:49:18 +02:00
|
|
|
const { share_usage_data: shareSetting } = daemonSettings;
|
|
|
|
const isSharingData = shareSetting || IS_WEB;
|
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}`);
|
|
|
|
}
|
|
|
|
}
|
2018-03-26 09:31:52 +02:00
|
|
|
|
2021-06-11 22:49:18 +02:00
|
|
|
dispatch({
|
|
|
|
type: !isSubscribed ? ACTIONS.CHANNEL_SUBSCRIBE : ACTIONS.CHANNEL_UNSUBSCRIBE,
|
|
|
|
data: subscription,
|
2018-05-07 06:50:55 +02:00
|
|
|
});
|
2018-08-09 13:45:07 +02:00
|
|
|
|
2021-06-11 22:49:18 +02:00
|
|
|
// if the user isn't sharing data, keep the subscriptions entirely in the app
|
|
|
|
if (isSharingData || IS_WEB) {
|
|
|
|
const { channelClaimId } = parseURI(subscription.uri);
|
2018-05-07 06:50:55 +02:00
|
|
|
|
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,
|
|
|
|
});
|
2020-10-05 20:31:51 +02:00
|
|
|
|
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) {
|
2021-10-17 10:36:14 +02:00
|
|
|
dispatch(
|
|
|
|
doToast({
|
|
|
|
message: __(!isSubscribed ? 'You followed %CHANNEL_NAME%!' : 'Unfollowed %CHANNEL_NAME%.', {
|
|
|
|
CHANNEL_NAME: subscription.channelName,
|
|
|
|
}),
|
|
|
|
})
|
|
|
|
);
|
2021-07-05 18:49:58 +02:00
|
|
|
}
|
2022-03-04 08:15:23 +01:00
|
|
|
|
|
|
|
// Reset last-fetch counter
|
|
|
|
activeSubsLastFetchedTime = 0;
|
2021-06-11 22:49:18 +02:00
|
|
|
};
|
|
|
|
}
|
2018-05-07 06:50:55 +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
|
|
|
};
|
|
|
|
}
|
2018-03-26 09:31:52 +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
|
|
|
};
|
|
|
|
}
|
2022-03-04 08:15:23 +01:00
|
|
|
|
|
|
|
export function doFetchLastActiveSubs(forceFetch: boolean = false, count: number = SIDEBAR_SUBS_DISPLAYED) {
|
|
|
|
function parseIdFromUri(uri) {
|
|
|
|
try {
|
|
|
|
const { channelClaimId } = parseURI(uri);
|
|
|
|
return channelClaimId;
|
|
|
|
} catch {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return (dispatch: Dispatch, getState: GetState) => {
|
|
|
|
const now = Date.now();
|
|
|
|
if (!forceFetch && now - activeSubsLastFetchedTime < FETCH_LAST_ACTIVE_SUBS_MIN_INTERVAL_MS) {
|
|
|
|
dispatch({ type: ACTIONS.FETCH_LAST_ACTIVE_SUBS_SKIP });
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const state = getState();
|
|
|
|
const subscriptions = selectSubscriptions(state);
|
|
|
|
const channelIds = subscriptions.map((sub) => parseIdFromUri(sub.uri));
|
|
|
|
activeSubsLastFetchedTime = now;
|
|
|
|
|
2022-03-22 08:35:45 +01:00
|
|
|
if (channelIds.length === 0) {
|
|
|
|
dispatch({
|
|
|
|
type: ACTIONS.FETCH_LAST_ACTIVE_SUBS_DONE,
|
|
|
|
data: [],
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-03-04 08:15:23 +01:00
|
|
|
const searchOptions = {
|
|
|
|
limit_claims_per_channel: 1,
|
|
|
|
channel_ids: channelIds,
|
|
|
|
claim_type: ['stream', 'repost'],
|
|
|
|
page: 1,
|
|
|
|
page_size: count,
|
|
|
|
no_totals: true,
|
|
|
|
order_by: ['release_time'],
|
|
|
|
};
|
|
|
|
|
2022-03-22 06:48:16 +01:00
|
|
|
dispatch(doClaimSearch(searchOptions))
|
|
|
|
.then((results) => {
|
|
|
|
const values = Object.values(results || {});
|
2022-03-04 08:15:23 +01:00
|
|
|
dispatch({
|
|
|
|
type: ACTIONS.FETCH_LAST_ACTIVE_SUBS_DONE,
|
2022-03-22 06:48:16 +01:00
|
|
|
// $FlowFixMe https://github.com/facebook/flow/issues/2221
|
|
|
|
data: values.map((v) => getChannelFromClaim(v.stream)),
|
2022-03-04 08:15:23 +01:00
|
|
|
});
|
|
|
|
})
|
|
|
|
.catch(() => {
|
|
|
|
dispatch({ type: ACTIONS.FETCH_LAST_ACTIVE_SUBS_FAIL });
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|