diff --git a/functions/src/emails.ts b/functions/src/emails.ts index 4327796a..63de8fba 100644 --- a/functions/src/emails.ts +++ b/functions/src/emails.ts @@ -392,65 +392,56 @@ export const sendNewAnswerEmail = async ( ) } -export const sendSixContractsEmail = async ( +export const sendInterestingMarketsEmail = async ( + user: User, privateUser: PrivateUser, - contractsToSend: Contract[] + contractsToSend: Contract[], + deliveryTime?: string ) => { - const emailType = 'weekly-trending' - const unsubscribeUrl = `${UNSUBSCRIBE_ENDPOINT}?id=${privateUser.id}&type=${emailType}` if ( !privateUser || !privateUser.email || privateUser?.unsubscribedFromWeeklyTrendingEmails ) return + + const emailType = 'weekly-trending' + const unsubscribeUrl = `${UNSUBSCRIBE_ENDPOINT}?id=${privateUser.id}&type=${emailType}` + + const { name } = user + const firstName = name.split(' ')[0] + await sendTemplateEmail( privateUser.email, - contractsToSend[0].question + ' and 5 more questions for you.', - // used to be 3 and I can't change the template name! - '3-trending-markets', + 'Interesting markets on Manifold', + 'interesting-markets', { + name: firstName, + unsubscribeLink: unsubscribeUrl, + question1Title: contractsToSend[0].question, - question1Description: getTextDescription(contractsToSend[0]), question1Link: contractUrl(contractsToSend[0]), question1ImgSrc: imageSourceUrl(contractsToSend[0]), question2Title: contractsToSend[1].question, - question2Description: getTextDescription(contractsToSend[1]), question2Link: contractUrl(contractsToSend[1]), question2ImgSrc: imageSourceUrl(contractsToSend[1]), question3Title: contractsToSend[2].question, - question3Description: getTextDescription(contractsToSend[2]), question3Link: contractUrl(contractsToSend[2]), question3ImgSrc: imageSourceUrl(contractsToSend[2]), question4Title: contractsToSend[3].question, - question4Description: getTextDescription(contractsToSend[3]), question4Link: contractUrl(contractsToSend[3]), question4ImgSrc: imageSourceUrl(contractsToSend[3]), question5Title: contractsToSend[4].question, - question5Description: getTextDescription(contractsToSend[4]), question5Link: contractUrl(contractsToSend[4]), question5ImgSrc: imageSourceUrl(contractsToSend[4]), question6Title: contractsToSend[5].question, - question6Description: getTextDescription(contractsToSend[5]), question6Link: contractUrl(contractsToSend[5]), question6ImgSrc: imageSourceUrl(contractsToSend[5]), - unsubscribeLink: unsubscribeUrl, - } + }, + deliveryTime ? { 'o:deliverytime': deliveryTime } : undefined ) } -function getTextDescription(contract: Contract) { - const { description } = contract - let text = '' - if (typeof description === 'string') text = description - else text = richTextToString(description) - - if (text.length > 300) { - return text.substring(0, 300) + '...' - } - return text -} - function contractUrl(contract: Contract) { return `https://manifold.markets/${contract.creatorUsername}/${contract.slug}` } diff --git a/functions/src/weekly-markets-emails.ts b/functions/src/weekly-markets-emails.ts index ec985630..c5805d0b 100644 --- a/functions/src/weekly-markets-emails.ts +++ b/functions/src/weekly-markets-emails.ts @@ -2,9 +2,9 @@ import * as functions from 'firebase-functions' import * as admin from 'firebase-admin' import { Contract } from '../../common/contract' -import { getPrivateUser, getValues, isProd, log } from './utils' +import { getPrivateUser, getUser, getValues, isProd, log } from './utils' import { filterDefined } from '../../common/util/array' -import { sendSixContractsEmail } from './emails' +import { sendInterestingMarketsEmail } from './emails' import { createRNG, shuffle } from '../../common/util/random' import { DAY_MS } from '../../common/util/time' @@ -17,7 +17,7 @@ export const weeklyMarketsEmails = functions const firestore = admin.firestore() -async function getTrendingContracts() { +export async function getTrendingContracts() { return await getValues( firestore .collection('contracts') @@ -26,7 +26,7 @@ async function getTrendingContracts() { .where('visibility', '==', 'public') .orderBy('closeTime', 'asc') .orderBy('popularityScore', 'desc') - .limit(50) + .limit(15) ) } @@ -67,7 +67,10 @@ async function sendTrendingMarketsEmailsToAllUsers() { numEmailsToSend ) - await sendSixContractsEmail(privateUser, contractsToSend) + const user = await getUser(privateUser.id) + if (!user) continue + + await sendInterestingMarketsEmail(user, privateUser, contractsToSend) } }