Change logic for daily reward claiming to weekly

Closes 531
This commit is contained in:
infinite-persistence 2021-12-23 11:57:53 +08:00 committed by Thomas Zarebczan
parent d382671616
commit abff1f3259
3 changed files with 29 additions and 6 deletions

View file

@ -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 }));
}
}
};
}

View file

@ -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;
});

View file

@ -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) => {