diff --git a/functions/src/emails.ts b/functions/src/emails.ts index 3dd0873f..a7ca2845 100644 --- a/functions/src/emails.ts +++ b/functions/src/emails.ts @@ -1,9 +1,7 @@ -import * as admin from 'firebase-admin' - import { Contract } from '../../common/contract' import { User } from '../../common/user' import { sendTemplateEmail } from './send-email' -import { getUser } from './utils' +import { getPrivateUser, getUser } from './utils' type market_resolved_template = { name: string @@ -21,13 +19,17 @@ export const sendMarketResolutionEmail = async ( contract: Contract, resolution: 'YES' | 'NO' | 'CANCEL' | 'MKT' ) => { + const privateUser = await getPrivateUser(userId) + if ( + !privateUser || + privateUser.unsubscribedFromResolutionEmails || + !privateUser.email + ) + return + const user = await getUser(userId) if (!user) return - const fbUser = await admin.auth().getUser(userId) - const email = fbUser.email - if (!email) return - const outcome = toDisplayResolution[resolution] const subject = `Resolved ${outcome}: ${contract.question}` @@ -45,7 +47,12 @@ export const sendMarketResolutionEmail = async ( // https://app.mailgun.com/app/sending/domains/mg.manifold.markets/templates/edit/market-resolved/initial // Mailgun username: james@mantic.markets - await sendTemplateEmail(email, subject, 'market-resolved', templateData) + await sendTemplateEmail( + privateUser.email, + subject, + 'market-resolved', + templateData + ) } const toDisplayResolution = { YES: 'YES', NO: 'NO', CANCEL: 'N/A', MKT: 'MKT' } diff --git a/functions/src/index.ts b/functions/src/index.ts index 534e72cf..0b4ef72c 100644 --- a/functions/src/index.ts +++ b/functions/src/index.ts @@ -10,5 +10,6 @@ export * from './stripe' export * from './sell-bet' export * from './create-contract' export * from './create-user' +export * from './unsubscribe' export * from './update-contract-metrics' export * from './update-user-metrics' diff --git a/functions/src/unsubscribe.ts b/functions/src/unsubscribe.ts new file mode 100644 index 00000000..d556f4ba --- /dev/null +++ b/functions/src/unsubscribe.ts @@ -0,0 +1,33 @@ +import * as functions from 'firebase-functions' +import * as admin from 'firebase-admin' +import * as _ from 'lodash' +import { getPrivateUser } from './utils' +import { PrivateUser } from '../../common/user' + +export const unsubscribe = functions + .runWith({ minInstances: 1 }) + .https.onRequest(async (req, res) => { + let id = req.query.id as string + if (!id) return + + let privateUser = await getPrivateUser(id) + + if (privateUser) { + let { username } = privateUser + + const update: Partial = { + unsubscribedFromResolutionEmails: true, + } + + await firestore.collection('private-users').doc(id).update(update) + + res.send( + username + + ', you have been unsubscribed from market resolution emails on Manifold Markets.' + ) + } else { + res.send('This user is not currently subscribed or does not exist.') + } + }) + +const firestore = admin.firestore() diff --git a/functions/src/utils.ts b/functions/src/utils.ts index e6ecd7cf..1a52d116 100644 --- a/functions/src/utils.ts +++ b/functions/src/utils.ts @@ -1,7 +1,7 @@ import * as admin from 'firebase-admin' import { Contract } from '../../common/contract' -import { User } from '../../common/user' +import { PrivateUser, User } from '../../common/user' export const getValue = async (collection: string, doc: string) => { const snap = await admin.firestore().collection(collection).doc(doc).get() @@ -22,6 +22,10 @@ export const getUser = (userId: string) => { return getValue('users', userId) } +export const getPrivateUser = (userId: string) => { + return getValue('private-users', userId) +} + export const getUserByUsername = async (username: string) => { const snap = await firestore .collection('users')