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

153 lines
5.1 KiB
JavaScript
Raw Normal View History

2017-12-08 21:14:35 +01:00
// @flow
import * as ACTIONS from 'constants/action_types';
import { parseURI, normalizeURI, isURIEqual } from 'util/lbryURI';
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
lastActiveSubscriptions: undefined, // undefined = un-fetched, null = no results;
2020-11-02 17:51:08 +01:00
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.filter(
(subscription) => !isURIEqual(subscription.uri, subscriptionToRemove.uri)
);
const newFollowing = state.following.filter(
(subscription) => !isURIEqual(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,
}),
[ACTIONS.FETCH_LAST_ACTIVE_SUBS_DONE]: (state: SubscriptionState, action): SubscriptionState => {
const activeChannelClaims = action.data;
if (activeChannelClaims && activeChannelClaims.length > 0) {
const subs = [];
activeChannelClaims.forEach((claim) => {
const index = state.subscriptions.findIndex((sub) => isURIEqual(sub.uri, claim.permanent_url));
if (index !== -1) {
subs.push(state.subscriptions[index]);
}
});
return { ...state, lastActiveSubscriptions: subs };
}
return { ...state, lastActiveSubscriptions: null };
},
[ACTIONS.FETCH_LAST_ACTIVE_SUBS_FAIL]: (state: SubscriptionState, action): SubscriptionState => ({
...state,
lastActiveSubscriptions: null,
}),
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,
}),
[ACTIONS.USER_STATE_POPULATE]: (
2019-09-26 18:07:11 +02:00
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 ? `@${channelName}` : '',
2019-09-26 18:07:11 +02:00
};
});
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
);