2018-11-21 22:20:55 +01:00
|
|
|
import { SUGGESTED_FEATURED, SUGGESTED_TOP_SUBSCRIBED } from 'constants/subscriptions';
|
2017-12-21 18:32:51 +01:00
|
|
|
import { createSelector } from 'reselect';
|
2018-05-07 06:50:55 +02:00
|
|
|
import {
|
|
|
|
selectAllClaimsByChannel,
|
|
|
|
selectClaimsById,
|
|
|
|
selectAllFetchingChannelClaims,
|
2018-10-19 22:38:07 +02:00
|
|
|
makeSelectChannelForClaimUri,
|
|
|
|
selectClaimsByUri,
|
|
|
|
parseURI,
|
2019-09-04 18:00:56 +02:00
|
|
|
makeSelectClaimForUri,
|
2018-05-07 06:50:55 +02:00
|
|
|
} from 'lbry-redux';
|
2018-11-21 22:20:55 +01:00
|
|
|
import { swapKeyAndValue } from 'util/swap-json';
|
2017-12-08 21:14:35 +01:00
|
|
|
|
2018-10-19 22:38:07 +02:00
|
|
|
// Returns the entire subscriptions state
|
2017-12-21 18:32:51 +01:00
|
|
|
const selectState = state => state.subscriptions || {};
|
2017-12-08 21:14:35 +01:00
|
|
|
|
2018-10-19 22:38:07 +02:00
|
|
|
// Returns the list of channel uris a user is subscribed to
|
2019-03-05 05:46:57 +01:00
|
|
|
export const selectSubscriptions = createSelector(
|
|
|
|
selectState,
|
2019-06-19 07:05:43 +02:00
|
|
|
state => state.subscriptions && state.subscriptions.sort((a, b) => a.channelName.localeCompare(b.channelName))
|
2019-03-05 05:46:57 +01:00
|
|
|
);
|
2018-10-19 22:38:07 +02:00
|
|
|
|
|
|
|
// Fetching list of users subscriptions
|
2019-03-05 05:46:57 +01:00
|
|
|
export const selectIsFetchingSubscriptions = createSelector(
|
|
|
|
selectState,
|
|
|
|
state => state.loading
|
|
|
|
);
|
2018-05-07 06:50:55 +02:00
|
|
|
|
2018-10-19 22:38:07 +02:00
|
|
|
// The current view mode on the subscriptions page
|
2019-03-05 05:46:57 +01:00
|
|
|
export const selectViewMode = createSelector(
|
|
|
|
selectState,
|
|
|
|
state => state.viewMode
|
|
|
|
);
|
2018-03-26 09:31:52 +02:00
|
|
|
|
2018-11-21 22:20:55 +01:00
|
|
|
// Suggested subscriptions from internal apis
|
2019-03-05 05:46:57 +01:00
|
|
|
export const selectSuggested = createSelector(
|
|
|
|
selectState,
|
|
|
|
state => state.suggested
|
|
|
|
);
|
2018-12-04 21:12:03 +01:00
|
|
|
export const selectIsFetchingSuggested = createSelector(
|
|
|
|
selectState,
|
|
|
|
state => state.loadingSuggested
|
|
|
|
);
|
2018-11-21 22:20:55 +01:00
|
|
|
export const selectSuggestedChannels = createSelector(
|
|
|
|
selectSubscriptions,
|
|
|
|
selectSuggested,
|
|
|
|
(userSubscriptions, suggested) => {
|
|
|
|
if (!suggested) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Swap the key/value because we will use the uri for everything, this just makes it easier
|
|
|
|
// suggested is returned from the api with the form:
|
|
|
|
// {
|
|
|
|
// featured: { "Channel label": uri, ... },
|
|
|
|
// top_subscribed: { "@channel": uri, ... }
|
|
|
|
// top_bid: { "@channel": uri, ... }
|
|
|
|
// }
|
|
|
|
// To properly compare the suggested subscriptions from our current subscribed channels
|
|
|
|
// We only care about the uri, not the label
|
|
|
|
|
|
|
|
// We also only care about top_subscribed and featured
|
|
|
|
// top_bid could just be porn or a channel with no content
|
|
|
|
const topSubscribedSuggestions = swapKeyAndValue(suggested[SUGGESTED_TOP_SUBSCRIBED]);
|
|
|
|
const featuredSuggestions = swapKeyAndValue(suggested[SUGGESTED_FEATURED]);
|
|
|
|
|
|
|
|
// Make sure there are no duplicates
|
|
|
|
// If a uri isn't already in the suggested object, add it
|
|
|
|
const suggestedChannels = { ...topSubscribedSuggestions };
|
|
|
|
|
|
|
|
Object.keys(featuredSuggestions).forEach(uri => {
|
|
|
|
if (!suggestedChannels[uri]) {
|
|
|
|
const channelLabel = featuredSuggestions[uri];
|
|
|
|
suggestedChannels[uri] = channelLabel;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
userSubscriptions.forEach(({ uri }) => {
|
|
|
|
// Note to passer bys:
|
|
|
|
// Maybe we should just remove the `lbry://` prefix from subscription uris
|
|
|
|
// Most places don't store them like that
|
|
|
|
const subscribedUri = uri.slice('lbry://'.length);
|
|
|
|
|
|
|
|
if (suggestedChannels[subscribedUri]) {
|
|
|
|
delete suggestedChannels[subscribedUri];
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-06-11 20:10:58 +02:00
|
|
|
return Object.keys(suggestedChannels).map(uri => ({
|
|
|
|
uri,
|
|
|
|
label: suggestedChannels[uri],
|
|
|
|
}));
|
2018-11-21 22:20:55 +01:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
export const selectFirstRunCompleted = createSelector(
|
|
|
|
selectState,
|
|
|
|
state => state.firstRunCompleted
|
|
|
|
);
|
|
|
|
export const selectshowSuggestedSubs = createSelector(
|
|
|
|
selectState,
|
|
|
|
state => state.showSuggestedSubs
|
|
|
|
);
|
|
|
|
|
2018-10-19 22:38:07 +02:00
|
|
|
// Fetching any claims that are a part of a users subscriptions
|
|
|
|
export const selectSubscriptionsBeingFetched = createSelector(
|
|
|
|
selectSubscriptions,
|
|
|
|
selectAllFetchingChannelClaims,
|
|
|
|
(subscriptions, fetchingChannelClaims) => {
|
|
|
|
const fetchingSubscriptionMap = {};
|
|
|
|
subscriptions.forEach(sub => {
|
|
|
|
const isFetching = fetchingChannelClaims && fetchingChannelClaims[sub.uri];
|
|
|
|
if (isFetching) {
|
|
|
|
fetchingSubscriptionMap[sub.uri] = true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return fetchingSubscriptionMap;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2019-03-05 05:46:57 +01:00
|
|
|
export const selectUnreadByChannel = createSelector(
|
|
|
|
selectState,
|
|
|
|
state => state.unread
|
|
|
|
);
|
2018-10-19 22:38:07 +02:00
|
|
|
|
|
|
|
// Returns the current total of unread subscriptions
|
2019-03-05 05:46:57 +01:00
|
|
|
export const selectUnreadAmount = createSelector(
|
|
|
|
selectUnreadByChannel,
|
|
|
|
unreadByChannel => {
|
|
|
|
const unreadChannels = Object.keys(unreadByChannel);
|
|
|
|
let badges = 0;
|
2018-10-19 22:38:07 +02:00
|
|
|
|
2019-03-05 05:46:57 +01:00
|
|
|
if (!unreadChannels.length) {
|
|
|
|
return badges;
|
|
|
|
}
|
2018-10-19 22:38:07 +02:00
|
|
|
|
2019-03-05 05:46:57 +01:00
|
|
|
unreadChannels.forEach(channel => {
|
|
|
|
badges += unreadByChannel[channel].uris.length;
|
|
|
|
});
|
2018-10-19 22:38:07 +02:00
|
|
|
|
2019-03-05 05:46:57 +01:00
|
|
|
return badges;
|
|
|
|
}
|
|
|
|
);
|
2018-10-19 22:38:07 +02:00
|
|
|
|
|
|
|
// Returns the uris with channels as an array with the channel with the newest content first
|
|
|
|
// If you just want the `unread` state, use selectUnread
|
|
|
|
export const selectUnreadSubscriptions = createSelector(
|
|
|
|
selectUnreadAmount,
|
|
|
|
selectUnreadByChannel,
|
|
|
|
selectClaimsByUri,
|
|
|
|
(unreadAmount, unreadByChannel, claimsByUri) => {
|
|
|
|
// determine which channel has the newest content
|
|
|
|
const unreadList = [];
|
|
|
|
if (!unreadAmount) {
|
|
|
|
return unreadList;
|
|
|
|
}
|
|
|
|
|
|
|
|
const channelUriList = Object.keys(unreadByChannel);
|
|
|
|
|
|
|
|
// There is only one channel with unread notifications
|
|
|
|
if (unreadAmount === 1) {
|
|
|
|
channelUriList.forEach(channel => {
|
|
|
|
const unreadChannel = {
|
|
|
|
channel,
|
|
|
|
uris: unreadByChannel[channel].uris,
|
|
|
|
};
|
|
|
|
unreadList.push(unreadChannel);
|
|
|
|
});
|
|
|
|
|
|
|
|
return unreadList;
|
|
|
|
}
|
2017-12-08 21:14:35 +01:00
|
|
|
|
2018-10-19 22:38:07 +02:00
|
|
|
channelUriList
|
|
|
|
.sort((channel1, channel2) => {
|
|
|
|
const latestUriFromChannel1 = unreadByChannel[channel1].uris[0];
|
|
|
|
const latestClaimFromChannel1 = claimsByUri[latestUriFromChannel1] || {};
|
|
|
|
const latestUriFromChannel2 = unreadByChannel[channel2].uris[0];
|
|
|
|
const latestClaimFromChannel2 = claimsByUri[latestUriFromChannel2] || {};
|
|
|
|
|
|
|
|
const latestHeightFromChannel1 = latestClaimFromChannel1.height || 0;
|
|
|
|
const latestHeightFromChannel2 = latestClaimFromChannel2.height || 0;
|
|
|
|
|
|
|
|
if (latestHeightFromChannel1 !== latestHeightFromChannel2) {
|
|
|
|
return latestHeightFromChannel2 - latestHeightFromChannel1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
})
|
|
|
|
.forEach(channel => {
|
|
|
|
const unreadSubscription = unreadByChannel[channel];
|
|
|
|
const unreadChannel = {
|
|
|
|
channel,
|
|
|
|
uris: unreadSubscription.uris,
|
|
|
|
};
|
|
|
|
|
|
|
|
unreadList.push(unreadChannel);
|
|
|
|
});
|
|
|
|
|
|
|
|
return unreadList;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
// Returns all unread subscriptions for a uri passed in
|
|
|
|
export const makeSelectUnreadByChannel = uri =>
|
2019-03-05 05:46:57 +01:00
|
|
|
createSelector(
|
|
|
|
selectUnreadByChannel,
|
|
|
|
unread => unread[uri]
|
|
|
|
);
|
2018-10-19 22:38:07 +02:00
|
|
|
|
|
|
|
// Returns the first page of claims for every channel a user is subscribed to
|
2018-05-07 06:50:55 +02:00
|
|
|
export const selectSubscriptionClaims = createSelector(
|
2017-12-08 21:14:35 +01:00
|
|
|
selectAllClaimsByChannel,
|
|
|
|
selectClaimsById,
|
|
|
|
selectSubscriptions,
|
2018-10-19 22:38:07 +02:00
|
|
|
selectUnreadByChannel,
|
|
|
|
(channelIds, allClaims, savedSubscriptions, unreadByChannel) => {
|
2017-12-08 21:14:35 +01:00
|
|
|
// no claims loaded yet
|
|
|
|
if (!Object.keys(channelIds).length) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2018-08-10 16:51:51 +02:00
|
|
|
let fetchedSubscriptions = [];
|
2017-12-08 21:14:35 +01:00
|
|
|
|
|
|
|
savedSubscriptions.forEach(subscription => {
|
|
|
|
let channelClaims = [];
|
|
|
|
|
|
|
|
// if subscribed channel has content
|
2018-03-26 09:31:52 +02:00
|
|
|
if (channelIds[subscription.uri] && channelIds[subscription.uri]['1']) {
|
2017-12-08 21:14:35 +01:00
|
|
|
// This will need to be more robust, we will want to be able to load more than the first page
|
2018-10-19 22:38:07 +02:00
|
|
|
|
|
|
|
// Strip out any ids that will be shown as notifications
|
2017-12-21 18:32:51 +01:00
|
|
|
const pageOneChannelIds = channelIds[subscription.uri]['1'];
|
2017-12-08 21:14:35 +01:00
|
|
|
|
|
|
|
// we have the channel ids and the corresponding claims
|
|
|
|
// loop over the list of ids and grab the claim
|
|
|
|
pageOneChannelIds.forEach(id => {
|
|
|
|
const grabbedClaim = allClaims[id];
|
2018-10-19 22:38:07 +02:00
|
|
|
|
|
|
|
if (
|
|
|
|
unreadByChannel[subscription.uri] &&
|
|
|
|
unreadByChannel[subscription.uri].uris.some(uri => uri.includes(id))
|
|
|
|
) {
|
|
|
|
grabbedClaim.isNew = true;
|
|
|
|
}
|
|
|
|
|
2018-08-10 16:51:51 +02:00
|
|
|
channelClaims = channelClaims.concat([grabbedClaim]);
|
2017-12-08 21:14:35 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-10-19 22:38:07 +02:00
|
|
|
fetchedSubscriptions = fetchedSubscriptions.concat(channelClaims);
|
2017-12-08 21:14:35 +01:00
|
|
|
});
|
|
|
|
|
2018-10-19 22:38:07 +02:00
|
|
|
return fetchedSubscriptions;
|
2017-12-08 21:14:35 +01:00
|
|
|
}
|
|
|
|
);
|
2018-05-07 06:50:55 +02:00
|
|
|
|
2018-10-19 22:38:07 +02:00
|
|
|
// Returns true if a user is subscribed to the channel associated with the uri passed in
|
|
|
|
// Accepts content or channel uris
|
2020-02-05 07:14:37 +01:00
|
|
|
export const makeSelectChannelInSubscriptions = uri =>
|
|
|
|
createSelector(
|
|
|
|
selectSubscriptions,
|
|
|
|
subscriptions => subscriptions.some(sub => sub.uri === uri)
|
|
|
|
);
|
|
|
|
|
2018-10-19 22:38:07 +02:00
|
|
|
export const makeSelectIsSubscribed = uri =>
|
|
|
|
createSelector(
|
|
|
|
selectSubscriptions,
|
|
|
|
makeSelectChannelForClaimUri(uri, true),
|
2019-09-04 18:00:56 +02:00
|
|
|
makeSelectClaimForUri(uri),
|
|
|
|
(subscriptions, channelUri, claim) => {
|
2018-10-19 22:38:07 +02:00
|
|
|
if (channelUri) {
|
|
|
|
return subscriptions.some(sub => sub.uri === channelUri);
|
2018-05-07 06:50:55 +02:00
|
|
|
}
|
|
|
|
|
2018-10-19 22:38:07 +02:00
|
|
|
// If we couldn't get a channel uri from the claim uri, the uri passed in might be a channel already
|
2019-08-14 20:09:45 +02:00
|
|
|
let isChannel;
|
|
|
|
try {
|
|
|
|
({ isChannel } = parseURI(uri));
|
|
|
|
} catch (e) {}
|
|
|
|
|
2019-09-04 18:00:56 +02:00
|
|
|
if (isChannel && claim) {
|
|
|
|
const uri = claim.permanent_url;
|
|
|
|
return subscriptions.some(sub => sub.uri === uri);
|
2018-10-19 22:38:07 +02:00
|
|
|
}
|
2018-10-10 07:22:06 +02:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2018-10-19 22:38:07 +02:00
|
|
|
);
|
2018-11-27 17:49:52 +01:00
|
|
|
|
|
|
|
export const makeSelectIsNew = uri =>
|
|
|
|
createSelector(
|
|
|
|
makeSelectIsSubscribed(uri),
|
|
|
|
makeSelectChannelForClaimUri(uri),
|
|
|
|
selectUnreadByChannel,
|
|
|
|
(isSubscribed, channel, unreadByChannel) => {
|
|
|
|
if (!isSubscribed) {
|
|
|
|
return false;
|
|
|
|
}
|
2018-11-29 00:21:33 +01:00
|
|
|
|
2018-11-27 17:49:52 +01:00
|
|
|
const unreadForChannel = unreadByChannel[`lbry://${channel}`];
|
|
|
|
if (unreadForChannel) {
|
|
|
|
return unreadForChannel.uris.includes(uri);
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
// If they are subscribed, check to see if this uri is in the list of unreads
|
|
|
|
}
|
|
|
|
);
|