Migrate unsubscribe function to v2 (#637)
* Migrate unsubscribe function to v2 * Move Stripe import because I forgot to do it before
This commit is contained in:
parent
83c5f9b323
commit
eb9b14d6d5
|
@ -613,7 +613,7 @@
|
||||||
>our Discord</a
|
>our Discord</a
|
||||||
>! Or,
|
>! Or,
|
||||||
<a
|
<a
|
||||||
href="https://us-central1-mantic-markets.cloudfunctions.net/unsubscribe?id={{userId}}&type=market-resolve"
|
href="https://unsubscribe-nggbo3neva-uc.a.run.app?id={{userId}}&type=market-resolve"
|
||||||
style="
|
style="
|
||||||
font-family: 'Helvetica Neue', Helvetica, Arial,
|
font-family: 'Helvetica Neue', Helvetica, Arial,
|
||||||
sans-serif;
|
sans-serif;
|
||||||
|
|
|
@ -635,7 +635,7 @@
|
||||||
>our Discord</a
|
>our Discord</a
|
||||||
>! Or,
|
>! Or,
|
||||||
<a
|
<a
|
||||||
href="https://us-central1-mantic-markets.cloudfunctions.net/unsubscribe?id={{userId}}&type=market-resolved"
|
href="https://unsubscribe-nggbo3neva-uc.a.run.app?id={{userId}}&type=market-resolved"
|
||||||
style="
|
style="
|
||||||
font-family: 'Helvetica Neue', Helvetica, Arial,
|
font-family: 'Helvetica Neue', Helvetica, Arial,
|
||||||
sans-serif;
|
sans-serif;
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
import { DOMAIN, PROJECT_ID } from '../../common/envs/constants'
|
import { DOMAIN, ENV_CONFIG } from '../../common/envs/constants'
|
||||||
import { Answer } from '../../common/answer'
|
import { Answer } from '../../common/answer'
|
||||||
import { Bet } from '../../common/bet'
|
import { Bet } from '../../common/bet'
|
||||||
import { getProbability } from '../../common/calculate'
|
import { getProbability } from '../../common/calculate'
|
||||||
|
@ -141,7 +141,8 @@ export const sendWelcomeEmail = async (
|
||||||
const firstName = name.split(' ')[0]
|
const firstName = name.split(' ')[0]
|
||||||
|
|
||||||
const emailType = 'generic'
|
const emailType = 'generic'
|
||||||
const unsubscribeLink = `https://us-central1-${PROJECT_ID}.cloudfunctions.net/unsubscribe?id=${userId}&type=${emailType}`
|
const { cloudRunId, cloudRunRegion } = ENV_CONFIG
|
||||||
|
const unsubscribeLink = `https://unsubscribe-${cloudRunId}-${cloudRunRegion}.a.run.app?id=${userId}&type=${emailType}`
|
||||||
|
|
||||||
await sendTemplateEmail(
|
await sendTemplateEmail(
|
||||||
privateUser.email,
|
privateUser.email,
|
||||||
|
@ -173,7 +174,8 @@ export const sendOneWeekBonusEmail = async (
|
||||||
const firstName = name.split(' ')[0]
|
const firstName = name.split(' ')[0]
|
||||||
|
|
||||||
const emailType = 'generic'
|
const emailType = 'generic'
|
||||||
const unsubscribeLink = `https://us-central1-${PROJECT_ID}.cloudfunctions.net/unsubscribe?id=${userId}&type=${emailType}`
|
const { cloudRunId, cloudRunRegion } = ENV_CONFIG
|
||||||
|
const unsubscribeLink = `https://unsubscribe-${cloudRunId}-${cloudRunRegion}.a.run.app?id=${userId}&type=${emailType}`
|
||||||
|
|
||||||
await sendTemplateEmail(
|
await sendTemplateEmail(
|
||||||
privateUser.email,
|
privateUser.email,
|
||||||
|
@ -205,7 +207,8 @@ export const sendThankYouEmail = async (
|
||||||
const firstName = name.split(' ')[0]
|
const firstName = name.split(' ')[0]
|
||||||
|
|
||||||
const emailType = 'generic'
|
const emailType = 'generic'
|
||||||
const unsubscribeLink = `https://us-central1-${PROJECT_ID}.cloudfunctions.net/unsubscribe?id=${userId}&type=${emailType}`
|
const { cloudRunId, cloudRunRegion } = ENV_CONFIG
|
||||||
|
const unsubscribeLink = `https://unsubscribe-${cloudRunId}-${cloudRunRegion}.a.run.app?id=${userId}&type=${emailType}`
|
||||||
|
|
||||||
await sendTemplateEmail(
|
await sendTemplateEmail(
|
||||||
privateUser.email,
|
privateUser.email,
|
||||||
|
@ -277,8 +280,9 @@ export const sendNewCommentEmail = async (
|
||||||
|
|
||||||
const { question, creatorUsername, slug } = contract
|
const { question, creatorUsername, slug } = contract
|
||||||
const marketUrl = `https://${DOMAIN}/${creatorUsername}/${slug}#${comment.id}`
|
const marketUrl = `https://${DOMAIN}/${creatorUsername}/${slug}#${comment.id}`
|
||||||
|
const emailType = 'market-comment'
|
||||||
const unsubscribeUrl = `https://us-central1-${PROJECT_ID}.cloudfunctions.net/unsubscribe?id=${userId}&type=market-comment`
|
const { cloudRunId, cloudRunRegion } = ENV_CONFIG
|
||||||
|
const unsubscribeUrl = `https://unsubscribe-${cloudRunId}-${cloudRunRegion}.a.run.app?id=${userId}&type=${emailType}`
|
||||||
|
|
||||||
const { name: commentorName, avatarUrl: commentorAvatarUrl } = commentCreator
|
const { name: commentorName, avatarUrl: commentorAvatarUrl } = commentCreator
|
||||||
const { text } = comment
|
const { text } = comment
|
||||||
|
@ -359,7 +363,9 @@ export const sendNewAnswerEmail = async (
|
||||||
const { name, avatarUrl, text } = answer
|
const { name, avatarUrl, text } = answer
|
||||||
|
|
||||||
const marketUrl = `https://${DOMAIN}/${creatorUsername}/${slug}`
|
const marketUrl = `https://${DOMAIN}/${creatorUsername}/${slug}`
|
||||||
const unsubscribeUrl = `https://us-central1-${PROJECT_ID}.cloudfunctions.net/unsubscribe?id=${userId}&type=market-answer`
|
const emailType = 'market-answer'
|
||||||
|
const { cloudRunId, cloudRunRegion } = ENV_CONFIG
|
||||||
|
const unsubscribeUrl = `https://unsubscribe-${cloudRunId}-${cloudRunRegion}.a.run.app?id=${userId}&type=${emailType}`
|
||||||
|
|
||||||
const subject = `New answer on ${question}`
|
const subject = `New answer on ${question}`
|
||||||
const from = `${name} <info@manifold.markets>`
|
const from = `${name} <info@manifold.markets>`
|
||||||
|
|
|
@ -3,12 +3,10 @@ import * as admin from 'firebase-admin'
|
||||||
admin.initializeApp()
|
admin.initializeApp()
|
||||||
|
|
||||||
// v1
|
// v1
|
||||||
export * from './stripe'
|
|
||||||
export * from './create-user'
|
export * from './create-user'
|
||||||
export * from './on-create-bet'
|
export * from './on-create-bet'
|
||||||
export * from './on-create-comment-on-contract'
|
export * from './on-create-comment-on-contract'
|
||||||
export * from './on-view'
|
export * from './on-view'
|
||||||
export * from './unsubscribe'
|
|
||||||
export * from './update-metrics'
|
export * from './update-metrics'
|
||||||
export * from './update-stats'
|
export * from './update-stats'
|
||||||
export * from './backup-db'
|
export * from './backup-db'
|
||||||
|
@ -41,3 +39,5 @@ export * from './withdraw-liquidity'
|
||||||
export * from './create-group'
|
export * from './create-group'
|
||||||
export * from './resolve-market'
|
export * from './resolve-market'
|
||||||
export * from './get-daily-bonuses'
|
export * from './get-daily-bonuses'
|
||||||
|
export * from './unsubscribe'
|
||||||
|
export * from './stripe'
|
||||||
|
|
|
@ -1,71 +1,66 @@
|
||||||
import * as functions from 'firebase-functions'
|
import { onRequest } from 'firebase-functions/v2/https'
|
||||||
import * as admin from 'firebase-admin'
|
import * as admin from 'firebase-admin'
|
||||||
import { getUser } from './utils'
|
import { getUser } from './utils'
|
||||||
import { PrivateUser } from '../../common/user'
|
import { PrivateUser } from '../../common/user'
|
||||||
|
|
||||||
export const unsubscribe = functions
|
export const unsubscribe = onRequest({ minInstances: 1 }, async (req, res) => {
|
||||||
.runWith({ minInstances: 1 })
|
const id = req.query.id as string
|
||||||
.https.onRequest(async (req, res) => {
|
let type = req.query.type as string
|
||||||
const id = req.query.id as string
|
if (!id || !type) {
|
||||||
let type = req.query.type as string
|
res.status(400).send('Empty id or type parameter.')
|
||||||
if (!id || !type) {
|
return
|
||||||
res.status(400).send('Empty id or type parameter.')
|
}
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if (type === 'market-resolved') type = 'market-resolve'
|
if (type === 'market-resolved') type = 'market-resolve'
|
||||||
|
|
||||||
if (
|
if (
|
||||||
![
|
!['market-resolve', 'market-comment', 'market-answer', 'generic'].includes(
|
||||||
'market-resolve',
|
type
|
||||||
'market-comment',
|
)
|
||||||
'market-answer',
|
) {
|
||||||
'generic',
|
res.status(400).send('Invalid type parameter.')
|
||||||
].includes(type)
|
return
|
||||||
) {
|
}
|
||||||
res.status(400).send('Invalid type parameter.')
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
const user = await getUser(id)
|
const user = await getUser(id)
|
||||||
|
|
||||||
if (!user) {
|
if (!user) {
|
||||||
res.send('This user is not currently subscribed or does not exist.')
|
res.send('This user is not currently subscribed or does not exist.')
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
const { name } = user
|
const { name } = user
|
||||||
|
|
||||||
const update: Partial<PrivateUser> = {
|
const update: Partial<PrivateUser> = {
|
||||||
...(type === 'market-resolve' && {
|
...(type === 'market-resolve' && {
|
||||||
unsubscribedFromResolutionEmails: true,
|
unsubscribedFromResolutionEmails: true,
|
||||||
}),
|
}),
|
||||||
...(type === 'market-comment' && {
|
...(type === 'market-comment' && {
|
||||||
unsubscribedFromCommentEmails: true,
|
unsubscribedFromCommentEmails: true,
|
||||||
}),
|
}),
|
||||||
...(type === 'market-answer' && {
|
...(type === 'market-answer' && {
|
||||||
unsubscribedFromAnswerEmails: true,
|
unsubscribedFromAnswerEmails: true,
|
||||||
}),
|
}),
|
||||||
...(type === 'generic' && {
|
...(type === 'generic' && {
|
||||||
unsubscribedFromGenericEmails: true,
|
unsubscribedFromGenericEmails: true,
|
||||||
}),
|
}),
|
||||||
}
|
}
|
||||||
|
|
||||||
await firestore.collection('private-users').doc(id).update(update)
|
await firestore.collection('private-users').doc(id).update(update)
|
||||||
|
|
||||||
if (type === 'market-resolve')
|
if (type === 'market-resolve')
|
||||||
res.send(
|
res.send(
|
||||||
`${name}, you have been unsubscribed from market resolution emails on Manifold Markets.`
|
`${name}, you have been unsubscribed from market resolution emails on Manifold Markets.`
|
||||||
)
|
)
|
||||||
else if (type === 'market-comment')
|
else if (type === 'market-comment')
|
||||||
res.send(
|
res.send(
|
||||||
`${name}, you have been unsubscribed from market comment emails on Manifold Markets.`
|
`${name}, you have been unsubscribed from market comment emails on Manifold Markets.`
|
||||||
)
|
)
|
||||||
else if (type === 'market-answer')
|
else if (type === 'market-answer')
|
||||||
res.send(
|
res.send(
|
||||||
`${name}, you have been unsubscribed from market answer emails on Manifold Markets.`
|
`${name}, you have been unsubscribed from market answer emails on Manifold Markets.`
|
||||||
)
|
)
|
||||||
else res.send(`${name}, you have been unsubscribed.`)
|
else res.send(`${name}, you have been unsubscribed.`)
|
||||||
})
|
})
|
||||||
|
|
||||||
const firestore = admin.firestore()
|
const firestore = admin.firestore()
|
||||||
|
|
Loading…
Reference in New Issue
Block a user