2017-12-08 21:14:35 +01:00
|
|
|
// @flow
|
2017-12-21 18:32:51 +01:00
|
|
|
import * as ACTIONS from 'constants/action_types';
|
2021-10-08 05:47:39 +02:00
|
|
|
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
|
|
|
|
following: [],
|
2018-05-07 06:50:55 +02:00
|
|
|
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 {
|
2021-04-29 05:44:29 +02:00
|
|
|
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()
|
2022-01-19 21:12:54 +01:00
|
|
|
.filter((subscription) => !isURIEqual(subscription.uri, subscriptionToRemove.uri));
|
2020-11-02 17:51:08 +01:00
|
|
|
const newFollowing = state.following
|
|
|
|
.slice()
|
2022-01-19 21:12:54 +01:00
|
|
|
.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
|
|
|
};
|
|
|
|
},
|
2018-05-07 06:50:55 +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 => ({
|
2018-05-07 06:50:55 +02:00
|
|
|
...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,
|
|
|
|
}),
|
2022-01-06 21:30:24 +01:00
|
|
|
[ACTIONS.SYNC_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;
|
2020-10-05 20:31:51 +02:00
|
|
|
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 {
|
2021-04-29 05:44:29 +02:00
|
|
|
const parsedSubscriptions = subscriptions.map((uri) => {
|
2019-09-26 18:07:11 +02:00
|
|
|
const { channelName } = parseURI(uri);
|
|
|
|
return {
|
|
|
|
uri,
|
2021-10-14 22:46:36 +02:00
|
|
|
channelName: channelName ? `@${channelName}` : '',
|
2019-09-26 18:07:11 +02:00
|
|
|
};
|
|
|
|
});
|
2020-02-04 02:53:49 +01: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
|
|
|
|
);
|