From abff1f3259aae47530258c1a6f8a5f19d11cef69 Mon Sep 17 00:00:00 2001 From: infinite-persistence Date: Thu, 23 Dec 2021 11:57:53 +0800 Subject: [PATCH] Change logic for daily reward claiming to weekly Closes 531 --- ui/redux/actions/rewards.js | 12 +++++++----- ui/redux/selectors/rewards.js | 21 +++++++++++++++++++++ ui/rewards.js | 2 +- 3 files changed, 29 insertions(+), 6 deletions(-) diff --git a/ui/redux/actions/rewards.js b/ui/redux/actions/rewards.js index 7c2792ff0..3e87a6688 100644 --- a/ui/redux/actions/rewards.js +++ b/ui/redux/actions/rewards.js @@ -2,7 +2,7 @@ import { Lbryio } from 'lbryinc'; import { doUpdateBalance } from 'redux/actions/wallet'; import { doToast } from 'redux/actions/notifications'; import * as ACTIONS from 'constants/action_types'; -import { selectUnclaimedRewards } from 'redux/selectors/rewards'; +import { selectUnclaimedRewards, selectWeeklyWatchClaimedThisWeek } from 'redux/selectors/rewards'; import { selectUserIsRewardApproved } from 'redux/selectors/user'; import { doFetchInviteStatus } from 'redux/actions/user'; import rewards from 'rewards'; @@ -43,7 +43,7 @@ export function doClaimRewardType(rewardType, options = {}) { if ( rewardType !== rewards.TYPE_REWARD_CODE && rewardType !== rewards.TYPE_CONFIRM_EMAIL && - rewardType !== rewards.TYPE_DAILY_VIEW && + rewardType !== rewards.TYPE_WEEKLY_WATCH && rewardType !== rewards.TYPE_NEW_ANDROID ) { if (!reward || reward.transaction_id) { @@ -143,9 +143,11 @@ export function doClaimEligiblePurchaseRewards() { if (unclaimedRewards.find((ur) => ur.reward_type === rewards.TYPE_FIRST_STREAM)) { dispatch(doClaimRewardType(rewards.TYPE_FIRST_STREAM)); } else { - [rewards.TYPE_MANY_DOWNLOADS, rewards.TYPE_DAILY_VIEW].forEach((type) => { - dispatch(doClaimRewardType(type, { failSilently: true })); - }); + dispatch(doClaimRewardType(rewards.TYPE_MANY_DOWNLOADS, { failSilently: true })); + + if (!selectWeeklyWatchClaimedThisWeek(state)) { + dispatch(doClaimRewardType(rewards.TYPE_WEEKLY_WATCH, { failSilently: true })); + } } }; } diff --git a/ui/redux/selectors/rewards.js b/ui/redux/selectors/rewards.js index 08dde0619..df492467e 100644 --- a/ui/redux/selectors/rewards.js +++ b/ui/redux/selectors/rewards.js @@ -70,3 +70,24 @@ export const selectHasClaimedInitialRewards = createSelector(selectClaimedReward const confirmEmailClaimed = !!claims.find((claim) => claim && claim.reward_type === REWARDS.TYPE_CONFIRM_EMAIL); return newUserClaimed && confirmEmailClaimed; }); + +export const selectWeeklyWatchClaimedThisWeek = createSelector(selectClaimedRewardsById, (claimedRewardsById) => { + const claimedRewards = Object.values(claimedRewardsById); + + // The list could be huge, so: + // - don't use find() which will look from the top. + // - only search until LOOKUP_LIMIT from the back. + const LOOKUP_LIMIT = 15; + let i = claimedRewards.length; + const stopIndex = i > LOOKUP_LIMIT ? i - LOOKUP_LIMIT : 0; + + while (--i >= stopIndex) { + if (claimedRewards[i].reward_type === REWARDS.TYPE_WEEKLY_WATCH) { + const last = new Date(claimedRewards[i].updated_at || claimedRewards[i].created_at); + const diff = new Date() - last; + const diffDays = Math.ceil(diff / (1000 * 60 * 60 * 24)); + return diffDays < 6; + } + } + return false; +}); diff --git a/ui/rewards.js b/ui/rewards.js index 0847a2202..da185577b 100644 --- a/ui/rewards.js +++ b/ui/rewards.js @@ -16,7 +16,7 @@ rewards.TYPE_REFEREE = 'referee'; rewards.TYPE_REWARD_CODE = 'reward_code'; rewards.TYPE_SUBSCRIPTION = 'subscription'; rewards.YOUTUBE_CREATOR = 'youtube_creator'; -rewards.TYPE_DAILY_VIEW = 'daily_view'; +rewards.TYPE_WEEKLY_WATCH = 'weekly_watch'; rewards.TYPE_NEW_ANDROID = 'new_android'; rewards.claimReward = (type, rewardParams) => {