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

134 lines
4.3 KiB
JavaScript
Raw Permalink Normal View History

2017-12-08 21:14:35 +01:00
// @flow
import * as ACTIONS from 'constants/action_types';
2021-07-15 22:22:44 +02:00
import { parseURI, normalizeURI, isURIEqual, ACTIONS as LBRY_REDUX_ACTIONS } from 'lbry-redux';
2017-12-22 02:21:22 +01:00
import { handleActions } from 'util/redux-utils';
2018-10-19 22:38:07 +02:00
const defaultState: SubscriptionState = {
2020-11-02 17:51:08 +01:00
subscriptions: [], // Deprecated
following: [],
loading: false,
2018-11-21 22:20:55 +01:00
firstRunCompleted: false,
2017-12-08 21:14:35 +01:00
};
export default handleActions(
{
2020-11-02 17:51:08 +01:00
[ACTIONS.CHANNEL_SUBSCRIBE]: (state: SubscriptionState, action): SubscriptionState => {
const newSubscription: { uri: string, channelName: string, notificationsDisabled: boolean } = action.data;
2017-12-13 22:36:30 +01:00
const newSubscriptions: Array<Subscription> = state.subscriptions.slice();
2020-11-02 17:51:08 +01:00
let newFollowing: Array<Following> = state.following.slice();
2020-01-14 21:44:07 +01:00
// prevent duplicates in the sidebar
2021-07-15 22:22:44 +02:00
if (!newSubscriptions.some((sub) => isURIEqual(sub.uri, newSubscription.uri))) {
2020-11-02 17:51:08 +01:00
// $FlowFixMe
2020-01-14 21:44:07 +01:00
newSubscriptions.unshift(newSubscription);
}
2017-12-08 21:14:35 +01:00
2021-07-15 22:22:44 +02:00
if (!newFollowing.some((sub) => isURIEqual(sub.uri, newSubscription.uri))) {
2020-11-02 17:51:08 +01:00
newFollowing.unshift({
uri: newSubscription.uri,
notificationsDisabled: newSubscription.notificationsDisabled,
});
} else {
newFollowing = newFollowing.map((following) => {
2021-07-15 22:22:44 +02:00
if (isURIEqual(following.uri, newSubscription.uri)) {
2020-11-02 17:51:08 +01:00
return {
2021-07-15 22:22:44 +02:00
uri: normalizeURI(newSubscription.uri),
2020-11-02 17:51:08 +01:00
notificationsDisabled: newSubscription.notificationsDisabled,
};
} else {
return following;
}
});
}
2017-12-08 21:14:35 +01:00
return {
...state,
subscriptions: newSubscriptions,
2020-11-02 17:51:08 +01:00
following: newFollowing,
2017-12-08 21:14:35 +01:00
};
},
2020-11-02 17:51:08 +01:00
[ACTIONS.CHANNEL_UNSUBSCRIBE]: (state: SubscriptionState, action): SubscriptionState => {
2017-12-08 21:14:35 +01:00
const subscriptionToRemove: Subscription = action.data;
const newSubscriptions = state.subscriptions
.slice()
.filter(
(subscription) => subscription.channelName.toLowerCase() !== subscriptionToRemove.channelName.toLowerCase()
);
2020-11-02 17:51:08 +01:00
const newFollowing = state.following
.slice()
.filter((subscription) => subscription.uri !== subscriptionToRemove.uri);
2017-12-08 21:14:35 +01:00
return {
...state,
subscriptions: newSubscriptions,
2020-11-02 17:51:08 +01:00
following: newFollowing,
2018-10-19 22:38:07 +02:00
};
},
[ACTIONS.FETCH_SUBSCRIPTIONS_START]: (state: SubscriptionState): SubscriptionState => ({
...state,
loading: true,
}),
[ACTIONS.FETCH_SUBSCRIPTIONS_FAIL]: (state: SubscriptionState): SubscriptionState => ({
...state,
loading: false,
}),
2020-11-02 17:51:08 +01:00
[ACTIONS.FETCH_SUBSCRIPTIONS_SUCCESS]: (state: SubscriptionState, action): SubscriptionState => ({
...state,
loading: false,
subscriptions: action.data,
}),
2020-11-02 17:51:08 +01:00
[ACTIONS.SET_VIEW_MODE]: (state: SubscriptionState, action): SubscriptionState => ({
2018-10-19 22:38:07 +02:00
...state,
viewMode: action.data,
}),
2019-09-26 18:07:11 +02:00
[LBRY_REDUX_ACTIONS.USER_STATE_POPULATE]: (
state: SubscriptionState,
2020-11-02 17:51:08 +01:00
action: { data: { subscriptions: ?Array<string>, following: ?Array<Subscription> } }
2019-09-26 18:07:11 +02:00
) => {
2020-11-02 17:51:08 +01:00
const { subscriptions, following } = action.data;
const incomingSubscriptions = Array.isArray(subscriptions) && subscriptions.length;
if (!incomingSubscriptions) {
return {
...state,
};
}
2019-09-26 18:07:11 +02:00
let newSubscriptions;
2020-11-02 17:51:08 +01:00
let newFollowing;
2019-09-26 18:07:11 +02:00
if (!subscriptions) {
newSubscriptions = state.subscriptions;
} else {
const parsedSubscriptions = subscriptions.map((uri) => {
2019-09-26 18:07:11 +02:00
const { channelName } = parseURI(uri);
return {
uri,
channelName: `@${channelName}`,
};
});
newSubscriptions = parsedSubscriptions;
2019-09-26 18:07:11 +02:00
}
2020-11-02 17:51:08 +01:00
if (!following) {
newFollowing = newSubscriptions.slice().map(({ uri }) => {
return {
uri,
// Default first time movers to notifications on
// This value is for email notifications too so we can't default off
// New subscriptions after population will default off
notificationsDisabled: false,
};
});
} else {
newFollowing = following;
}
2019-09-26 18:07:11 +02:00
return {
...state,
subscriptions: newSubscriptions,
2020-11-02 17:51:08 +01:00
following: newFollowing,
2019-09-26 18:07:11 +02:00
};
},
2017-12-08 21:14:35 +01:00
},
defaultState
);