Verify email reminders (#4510)

* Clarify intent

* Increase email reminder period to once per day

* Fallback

* Snooze immediately after account creation, prevent showing right after signup

* Fix e2e test exports

* Remove redundant check

* Better simple date generation

* Replace in DateField

* Use non-string comparison

* Revert change to unrelated code

* Also parse

* Remove side effect
zio/stable
Eric Bailey 2024-06-18 17:21:34 -05:00 committed by GitHub
parent 853c32b4d8
commit 32b4063185
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 53 additions and 27 deletions

View File

@ -54,8 +54,8 @@ import {useModalControls} from './state/modals'
import {useUnreadNotifications} from './state/queries/notifications/unread'
import {useSession} from './state/session'
import {
setEmailConfirmationRequested,
shouldRequestEmailConfirmation,
snoozeEmailConfirmationPrompt,
} from './state/shell/reminders'
import {AccessibilitySettingsScreen} from './view/screens/AccessibilitySettings'
import {CommunityGuidelinesScreen} from './view/screens/CommunityGuidelines'
@ -585,7 +585,7 @@ function RoutesContainer({children}: React.PropsWithChildren<{}>) {
if (currentAccount && shouldRequestEmailConfirmation(currentAccount)) {
openModal({name: 'verify-email', showReminder: true})
setEmailConfirmationRequested()
snoozeEmailConfirmationPrompt()
}
}

View File

@ -19,3 +19,14 @@ export function getAge(birthDate: Date): number {
}
return age
}
/**
* Compares two dates by year, month, and day only
*/
export function simpleAreDatesEqual(a: Date, b: Date): boolean {
return (
a.getFullYear() === b.getFullYear() &&
a.getMonth() === b.getMonth() &&
a.getDate() === b.getDate()
)
}

View File

@ -11,6 +11,7 @@ import {
import {tryFetchGates} from '#/lib/statsig/statsig'
import {getAge} from '#/lib/strings/time'
import {logger} from '#/logger'
import {snoozeEmailConfirmationPrompt} from '#/state/shell/reminders'
import {
configureModerationForAccount,
configureModerationForGuest,
@ -191,6 +192,13 @@ export async function createAgentAndCreateAccount(
agent.setPersonalDetails({birthDate: birthDate.toISOString()})
}
try {
// snooze first prompt after signup, defer to next prompt
snoozeEmailConfirmationPrompt()
} catch (e: any) {
logger.error(e, {context: `session: failed snoozeEmailConfirmationPrompt`})
}
return prepareAgent(agent, gates, moderation, onSessionChange)
}

View File

@ -1,7 +1,5 @@
export function init() {}
export function shouldRequestEmailConfirmation() {
return false
}
export function setEmailConfirmationRequested() {}
export function snoozeEmailConfirmationPrompt() {}

View File

@ -1,36 +1,45 @@
import {simpleAreDatesEqual} from '#/lib/strings/time'
import {logger} from '#/logger'
import * as persisted from '#/state/persisted'
import {toHashCode} from 'lib/strings/helpers'
import {isOnboardingActive} from './onboarding'
import {SessionAccount} from '../session'
import {isOnboardingActive} from './onboarding'
export function shouldRequestEmailConfirmation(account: SessionAccount) {
if (!account) {
return false
}
if (account.emailConfirmed) {
return false
}
if (isOnboardingActive()) {
return false
}
// only prompt once
if (persisted.get('reminders').lastEmailConfirm) {
return false
}
// ignore logged out
if (!account) return false
// ignore confirmed accounts, this is the success state of this reminder
if (account.emailConfirmed) return false
// wait for onboarding to complete
if (isOnboardingActive()) return false
const snoozedAt = persisted.get('reminders').lastEmailConfirm
const today = new Date()
// shard the users into 2 day of the week buckets
// (this is to avoid a sudden influx of email updates when
// this feature rolls out)
const code = toHashCode(account.did) % 7
if (code !== today.getDay() && code !== (today.getDay() + 1) % 7) {
return false
}
logger.debug('Checking email confirmation reminder', {
today,
snoozedAt,
})
// never been snoozed, new account
if (!snoozedAt) {
return true
}
export function setEmailConfirmationRequested() {
// already snoozed today
if (simpleAreDatesEqual(new Date(Date.parse(snoozedAt)), new Date())) {
return false
}
return true
}
export function snoozeEmailConfirmationPrompt() {
const lastEmailConfirm = new Date().toISOString()
logger.debug('Snoozing email confirmation reminder', {
snoozedAt: lastEmailConfirm,
})
persisted.write('reminders', {
...persisted.get('reminders'),
lastEmailConfirm: new Date().toISOString(),
lastEmailConfirm,
})
}