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

190 lines
5.4 KiB
JavaScript
Raw Normal View History

2017-12-08 21:14:35 +01:00
// @flow
import * as ACTIONS from 'constants/action_types';
2018-09-24 05:44:42 +02:00
import { Lbryio, rewards, doClaimRewardType } from 'lbryinc';
2019-10-15 06:20:12 +02:00
import { selectUnreadByChannel } from 'redux/selectors/subscriptions';
2019-11-19 21:34:25 +01:00
import { parseURI } from 'lbry-redux';
2017-12-08 21:14:35 +01:00
2019-03-18 06:09:50 +01:00
export const doSetViewMode = (viewMode: ViewMode) => (dispatch: Dispatch) =>
2018-10-19 22:38:07 +02:00
dispatch({
type: ACTIONS.SET_VIEW_MODE,
data: viewMode,
});
2019-05-07 23:38:29 +02:00
export const setSubscriptionLatest = (subscription: Subscription, uri: string) => (dispatch: Dispatch) =>
2017-12-13 22:36:30 +01:00
dispatch({
type: ACTIONS.SET_SUBSCRIPTION_LATEST,
data: {
subscription,
uri,
},
2017-12-08 21:14:35 +01:00
});
2018-10-19 22:38:07 +02:00
// Populate a channels unread subscriptions or update the type
export const doUpdateUnreadSubscriptions = (
channelUri: string,
uris: ?Array<string>,
type: ?SubscriptionNotificationType
2019-03-18 06:09:50 +01:00
) => (dispatch: Dispatch, getState: GetState) => {
2018-10-19 22:38:07 +02:00
const state = getState();
const unreadByChannel = selectUnreadByChannel(state);
const currentUnreadForChannel: UnreadSubscription = unreadByChannel[channelUri];
let newUris;
let newType;
if (!currentUnreadForChannel) {
newUris = uris;
newType = type;
} else {
if (uris) {
// If a channel currently has no unread uris, just add them all
if (!currentUnreadForChannel.uris || !currentUnreadForChannel.uris.length) {
newUris = uris;
} else {
// They already have unreads and now there are new ones
// Add the new ones to the beginning of the list
// Make sure there are no duplicates
const currentUnreadUris = currentUnreadForChannel.uris;
newUris = uris.filter(uri => !currentUnreadUris.includes(uri)).concat(currentUnreadUris);
}
} else {
newUris = currentUnreadForChannel.uris;
}
newType = type || currentUnreadForChannel.type;
}
dispatch({
2018-10-19 22:38:07 +02:00
type: ACTIONS.UPDATE_SUBSCRIPTION_UNREADS,
data: {
2018-10-19 22:38:07 +02:00
channel: channelUri,
uris: newUris,
type: newType,
},
});
2018-10-19 22:38:07 +02:00
};
// Remove multiple files (or all) from a channels unread subscriptions
export const doRemoveUnreadSubscriptions = (channelUri: ?string, readUris: ?Array<string>) => (
2019-03-18 06:09:50 +01:00
dispatch: Dispatch,
2018-10-19 22:38:07 +02:00
getState: GetState
) => {
const state = getState();
const unreadByChannel = selectUnreadByChannel(state);
// If no channel is passed in, remove all unread subscriptions from all channels
if (!channelUri) {
return dispatch({
type: ACTIONS.REMOVE_SUBSCRIPTION_UNREADS,
data: { channel: null },
});
}
2018-10-19 22:38:07 +02:00
const currentChannelUnread = unreadByChannel[channelUri];
if (!currentChannelUnread || !currentChannelUnread.uris) {
// Channel passed in doesn't have any unreads
2018-10-19 22:38:07 +02:00
return;
}
// For each uri passed in, remove it from the list of unread uris
// If no uris are passed in, remove them all
let newUris;
if (readUris) {
const urisToRemoveMap = readUris.reduce(
(acc, val) => ({
...acc,
[val]: true,
}),
{}
);
const filteredUris = currentChannelUnread.uris.filter(uri => !urisToRemoveMap[uri]);
newUris = filteredUris.length ? filteredUris : null;
} else {
newUris = null;
}
2018-10-19 22:38:07 +02:00
dispatch({
type: ACTIONS.REMOVE_SUBSCRIPTION_UNREADS,
data: {
channel: channelUri,
uris: newUris,
},
});
};
// Remove a single file from a channels unread subscriptions
2019-05-07 23:38:29 +02:00
export const doRemoveUnreadSubscription = (channelUri: string, readUri: string) => (dispatch: Dispatch) => {
2018-10-19 22:38:07 +02:00
dispatch(doRemoveUnreadSubscriptions(channelUri, [readUri]));
};
2019-05-07 23:38:29 +02:00
export const doChannelSubscribe = (subscription: Subscription) => (dispatch: Dispatch, getState: GetState) => {
const {
settings: { daemonSettings },
} = getState();
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,
});
2018-09-24 05:44:42 +02:00
dispatch(doClaimRewardType(rewards.TYPE_SUBSCRIPTION, { failSilently: true }));
}
};
2019-05-07 23:38:29 +02:00
export const doChannelUnsubscribe = (subscription: Subscription) => (dispatch: Dispatch, getState: GetState) => {
const {
settings: { daemonSettings },
} = getState();
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,
});
}
};
2019-03-18 06:09:50 +01:00
export const doFetchRecommendedSubscriptions = () => (dispatch: Dispatch) => {
2018-11-21 22:20:55 +01:00
dispatch({
type: ACTIONS.GET_SUGGESTED_SUBSCRIPTIONS_START,
});
return Lbryio.call('subscription', 'suggest')
.then(suggested =>
dispatch({
type: ACTIONS.GET_SUGGESTED_SUBSCRIPTIONS_SUCCESS,
data: suggested,
})
)
.catch(error =>
dispatch({
type: ACTIONS.GET_SUGGESTED_SUBSCRIPTIONS_FAIL,
error,
})
);
};