2017-12-08 21:14:35 +01:00
|
|
|
// @flow
|
2017-12-21 18:32:51 +01:00
|
|
|
import * as ACTIONS from 'constants/action_types';
|
2018-03-06 08:44:36 +01:00
|
|
|
import type { Subscription, Dispatch, SubscriptionState } from 'redux/reducers/subscriptions';
|
|
|
|
import { selectSubscriptions } from 'redux/selectors/subscriptions';
|
|
|
|
import Lbry from 'lbry';
|
|
|
|
import { doPurchaseUri } from 'redux/actions/content';
|
|
|
|
import { doNavigate } from 'redux/actions/navigation';
|
2018-03-06 10:37:53 +01:00
|
|
|
import { buildURI } from 'lbryURI';
|
2018-03-06 08:44:36 +01:00
|
|
|
|
|
|
|
const CHECK_SUBSCRIPTIONS_INTERVAL = 10 * 60 * 1000;
|
2017-12-08 21:14:35 +01:00
|
|
|
|
2017-12-21 18:32:51 +01:00
|
|
|
export const doChannelSubscribe = (subscription: Subscription) => (dispatch: Dispatch) =>
|
2017-12-13 22:36:30 +01:00
|
|
|
dispatch({
|
2017-12-21 18:32:51 +01:00
|
|
|
type: ACTIONS.CHANNEL_SUBSCRIBE,
|
2017-12-08 21:14:35 +01:00
|
|
|
data: subscription,
|
|
|
|
});
|
|
|
|
|
2017-12-21 18:32:51 +01:00
|
|
|
export const doChannelUnsubscribe = (subscription: Subscription) => (dispatch: Dispatch) =>
|
2017-12-13 22:36:30 +01:00
|
|
|
dispatch({
|
2017-12-21 18:32:51 +01:00
|
|
|
type: ACTIONS.CHANNEL_UNSUBSCRIBE,
|
2017-12-08 21:14:35 +01:00
|
|
|
data: subscription,
|
|
|
|
});
|
|
|
|
|
2018-03-06 09:36:04 +01:00
|
|
|
export const doCheckSubscriptions = () => (
|
|
|
|
dispatch: Dispatch,
|
|
|
|
getState: () => SubscriptionState
|
|
|
|
) => {
|
2018-03-06 08:44:36 +01:00
|
|
|
const checkSubscriptionsTimer = setInterval(
|
2018-03-06 09:36:04 +01:00
|
|
|
() =>
|
|
|
|
selectSubscriptions(getState()).map((subscription: Subscription) =>
|
|
|
|
dispatch(doCheckSubscription(subscription))
|
|
|
|
),
|
2018-03-06 08:44:36 +01:00
|
|
|
CHECK_SUBSCRIPTIONS_INTERVAL
|
|
|
|
);
|
|
|
|
dispatch({
|
|
|
|
type: ACTIONS.CHECK_SUBSCRIPTIONS_SUBSCRIBE,
|
|
|
|
data: { checkSubscriptionsTimer },
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
export const doCheckSubscription = (subscription: Subscription) => (dispatch: Dispatch) => {
|
|
|
|
dispatch({
|
|
|
|
type: ACTIONS.CHECK_SUBSCRIPTION_STARTED,
|
|
|
|
data: subscription,
|
|
|
|
});
|
|
|
|
|
|
|
|
Lbry.claim_list_by_channel({ uri: subscription.uri, page: 1 }).then(result => {
|
|
|
|
const claimResult = result[subscription.uri] || {};
|
|
|
|
const { claims_in_channel: claimsInChannel } = claimResult;
|
|
|
|
|
2018-03-07 20:41:14 +01:00
|
|
|
const count = subscription.latest
|
2018-03-06 09:36:04 +01:00
|
|
|
? claimsInChannel.reduce(
|
|
|
|
(prev, cur, index) =>
|
2018-03-07 20:19:45 +01:00
|
|
|
buildURI({ contentName: cur.name, claimId: cur.claim_id }, false) ===
|
|
|
|
subscription.latest
|
|
|
|
? index
|
|
|
|
: prev,
|
2018-03-06 09:36:04 +01:00
|
|
|
-1
|
|
|
|
)
|
|
|
|
: 1;
|
2018-03-06 08:44:36 +01:00
|
|
|
|
2018-03-06 09:36:04 +01:00
|
|
|
if (count !== 0) {
|
|
|
|
if (!claimsInChannel[0].value.stream.metadata.fee) {
|
|
|
|
dispatch(
|
2018-03-07 20:19:45 +01:00
|
|
|
doPurchaseUri(
|
|
|
|
buildURI(
|
|
|
|
{ contentName: claimsInChannel[0].name, claimId: claimsInChannel[0].claim_id },
|
|
|
|
false
|
|
|
|
),
|
|
|
|
{ cost: 0 }
|
|
|
|
)
|
2018-03-06 09:36:04 +01:00
|
|
|
);
|
2018-03-06 08:44:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const notif = new window.Notification(subscription.channelName, {
|
2018-03-06 09:36:04 +01:00
|
|
|
body: `Posted ${claimsInChannel[0].value.stream.metadata.title}${
|
|
|
|
count > 1 ? ` and ${count - 1} other new items` : ''
|
|
|
|
}${count < 0 ? ' and 9+ other new items' : ''}`,
|
2018-03-06 08:44:36 +01:00
|
|
|
silent: false,
|
|
|
|
});
|
|
|
|
notif.onclick = () => {
|
2018-03-06 09:36:04 +01:00
|
|
|
dispatch(
|
|
|
|
doNavigate('/show', {
|
2018-03-07 20:19:45 +01:00
|
|
|
uri: buildURI(
|
|
|
|
{ contentName: claimsInChannel[0].name, claimId: claimsInChannel[0].claim_id },
|
|
|
|
true
|
|
|
|
),
|
2018-03-06 09:36:04 +01:00
|
|
|
})
|
|
|
|
);
|
2018-03-06 08:44:36 +01:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-03-06 10:37:53 +01:00
|
|
|
//$FlowIssue
|
2018-03-06 08:44:36 +01:00
|
|
|
dispatch({
|
|
|
|
type: ACTIONS.CHECK_SUBSCRIPTION_COMPLETED,
|
2018-03-06 09:36:04 +01:00
|
|
|
data: subscription,
|
2018-03-06 08:44:36 +01:00
|
|
|
});
|
|
|
|
});
|
2018-03-06 09:36:04 +01:00
|
|
|
};
|
2018-03-06 08:44:36 +01:00
|
|
|
|
2018-03-06 09:36:04 +01:00
|
|
|
export const checkSubscriptionLatest = (channel: Subscription, uri: string) => (
|
|
|
|
dispatch: Dispatch
|
|
|
|
) => {
|
2018-03-06 09:32:58 +01:00
|
|
|
Lbry.claim_list_by_channel({ uri: channel.uri, page: 1 }).then(result => {
|
|
|
|
const claimResult = result[channel.uri] || {};
|
|
|
|
const { claims_in_channel: claimsInChannel } = claimResult;
|
|
|
|
|
2018-03-06 09:36:04 +01:00
|
|
|
if (
|
|
|
|
claimsInChannel &&
|
|
|
|
claimsInChannel.length &&
|
2018-03-07 20:19:45 +01:00
|
|
|
buildURI(
|
|
|
|
{ contentName: claimsInChannel[0].name, claimId: claimsInChannel[0].claim_id },
|
|
|
|
false
|
|
|
|
) === uri
|
2018-03-06 09:36:04 +01:00
|
|
|
) {
|
2018-03-06 09:32:58 +01:00
|
|
|
dispatch(setSubscriptionLatest(channel, uri));
|
|
|
|
}
|
|
|
|
});
|
2018-03-06 09:36:04 +01:00
|
|
|
};
|
2018-03-06 09:32:58 +01:00
|
|
|
|
2018-03-06 09:36:04 +01:00
|
|
|
export const setSubscriptionLatest = (subscription: Subscription, uri: string) => (
|
|
|
|
dispatch: Dispatch
|
|
|
|
) =>
|
|
|
|
dispatch({
|
2018-03-06 01:28:11 +01:00
|
|
|
type: ACTIONS.SET_SUBSCRIPTION_LATEST,
|
|
|
|
data: {
|
|
|
|
subscription,
|
2018-03-06 09:36:04 +01:00
|
|
|
uri,
|
|
|
|
},
|
2018-03-06 09:32:58 +01:00
|
|
|
});
|
2018-03-06 01:28:11 +01:00
|
|
|
|
2017-12-13 22:36:30 +01:00
|
|
|
export const setHasFetchedSubscriptions = () => (dispatch: Dispatch) =>
|
2017-12-21 18:32:51 +01:00
|
|
|
dispatch({ type: ACTIONS.HAS_FETCHED_SUBSCRIPTIONS });
|