From e1775681aa47b67601abda567b19024529c190d8 Mon Sep 17 00:00:00 2001 From: Ian Philips Date: Mon, 22 Aug 2022 16:36:39 -0600 Subject: [PATCH] Add weekly email sent flag, filter out manifold grouped markets --- common/user.ts | 1 + functions/src/index.ts | 1 + functions/src/reset-betting-streaks.ts | 2 +- functions/src/reset-weekly-emails-flag.ts | 24 +++++++++++++++++++++++ functions/src/weekly-markets-emails.ts | 20 ++++++++++++++----- 5 files changed, 42 insertions(+), 6 deletions(-) create mode 100644 functions/src/reset-weekly-emails-flag.ts diff --git a/common/user.ts b/common/user.ts index dee1413f..9927a3d3 100644 --- a/common/user.ts +++ b/common/user.ts @@ -54,6 +54,7 @@ export type PrivateUser = { unsubscribedFromAnswerEmails?: boolean unsubscribedFromGenericEmails?: boolean unsubscribedFromWeeklyTrendingEmails?: boolean + weeklyTrendingEmailSent?: boolean manaBonusEmailSent?: boolean initialDeviceToken?: string initialIpAddress?: string diff --git a/functions/src/index.ts b/functions/src/index.ts index b0ad50fa..26a1ddf6 100644 --- a/functions/src/index.ts +++ b/functions/src/index.ts @@ -29,6 +29,7 @@ export * from './on-delete-group' export * from './score-contracts' export * from './weekly-markets-emails' export * from './reset-betting-streaks' +export * from './reset-weekly-emails-flag' // v2 export * from './health' diff --git a/functions/src/reset-betting-streaks.ts b/functions/src/reset-betting-streaks.ts index c781aba2..924f5c22 100644 --- a/functions/src/reset-betting-streaks.ts +++ b/functions/src/reset-betting-streaks.ts @@ -9,7 +9,7 @@ const firestore = admin.firestore() export const resetBettingStreaksForUsers = functions.pubsub .schedule(`0 ${BETTING_STREAK_RESET_HOUR} * * *`) - .timeZone('utc') + .timeZone('Etc/UTC') .onRun(async () => { await resetBettingStreaksInternal() }) diff --git a/functions/src/reset-weekly-emails-flag.ts b/functions/src/reset-weekly-emails-flag.ts new file mode 100644 index 00000000..fc6b396a --- /dev/null +++ b/functions/src/reset-weekly-emails-flag.ts @@ -0,0 +1,24 @@ +import * as functions from 'firebase-functions' +import * as admin from 'firebase-admin' +import { getAllPrivateUsers } from './utils' + +export const resetWeeklyEmailsFlag = functions + .runWith({ secrets: ['MAILGUN_KEY'] }) + // every Monday at 1 am PT (UTC -07:00) ( 12 hours before the emails will be sent) + .pubsub.schedule('0 7 * * 1') + .timeZone('Etc/UTC') + .onRun(async () => { + const privateUsers = await getAllPrivateUsers() + // get all users that haven't unsubscribed from weekly emails + const privateUsersToSendEmailsTo = privateUsers.filter((user) => { + return !user.unsubscribedFromWeeklyTrendingEmails + }) + const firestore = admin.firestore() + await Promise.all( + privateUsersToSendEmailsTo.map(async (user) => { + return firestore.collection('private-users').doc(user.id).update({ + weeklyTrendingEmailSent: false, + }) + }) + ) + }) diff --git a/functions/src/weekly-markets-emails.ts b/functions/src/weekly-markets-emails.ts index 881ba7ba..c7331dae 100644 --- a/functions/src/weekly-markets-emails.ts +++ b/functions/src/weekly-markets-emails.ts @@ -9,9 +9,9 @@ import { DAY_MS } from '../../common/util/time' export const weeklyMarketsEmails = functions .runWith({ secrets: ['MAILGUN_KEY'] }) - // every Monday at 12pm PT (UTC -07:00) - .pubsub.schedule('0 19 * * 1') - .timeZone('utc') + // every minute on Monday for an hour at 12pm PT (UTC -07:00) + .pubsub.schedule('* 18 * * 1') + .timeZone('Etc/UTC') .onRun(async () => { await sendTrendingMarketsEmailsToAllUsers() }) @@ -37,7 +37,10 @@ async function sendTrendingMarketsEmailsToAllUsers() { const privateUsers = await getAllPrivateUsers() // get all users that haven't unsubscribed from weekly emails const privateUsersToSendEmailsTo = privateUsers.filter((user) => { - return !user.unsubscribedFromWeeklyTrendingEmails + return ( + !user.unsubscribedFromWeeklyTrendingEmails && + !user.weeklyTrendingEmailSent + ) }) log( 'Sending weekly trending emails to', @@ -50,13 +53,17 @@ async function sendTrendingMarketsEmailsToAllUsers() { !( contract.question.toLowerCase().includes('trump') && contract.question.toLowerCase().includes('president') - ) && (contract?.closeTime ?? 0) > Date.now() + DAY_MS + ) && + (contract?.closeTime ?? 0) > Date.now() + DAY_MS && + !contract.groupSlugs?.includes('manifold-features') && + !contract.groupSlugs?.includes('manifold-6748e065087e') ) .slice(0, 20) log( `Found ${trendingContracts.length} trending contracts:\n`, trendingContracts.map((c) => c.question).join('\n ') ) + for (const privateUser of privateUsersToSendEmailsTo) { if (!privateUser.email) { log(`No email for ${privateUser.username}`) @@ -79,6 +86,9 @@ async function sendTrendingMarketsEmailsToAllUsers() { if (!user) continue await sendInterestingMarketsEmail(user, privateUser, contractsToSend) + await firestore.collection('private-users').doc(user.id).update({ + weeklyTrendingEmailSent: true, + }) } }