2022-02-23 01:35:25 +00:00
|
|
|
import * as functions from 'firebase-functions'
|
|
|
|
import * as admin from 'firebase-admin'
|
2022-08-04 23:34:04 +00:00
|
|
|
import { compact, uniq } from 'lodash'
|
2022-02-23 01:35:25 +00:00
|
|
|
import { getContract, getUser, getValues } from './utils'
|
2022-05-15 17:39:42 +00:00
|
|
|
import { Comment } from '../../common/comment'
|
2022-02-23 01:35:25 +00:00
|
|
|
import { sendNewCommentEmail } from './emails'
|
2022-05-15 17:39:42 +00:00
|
|
|
import { Bet } from '../../common/bet'
|
|
|
|
import { Answer } from '../../common/answer'
|
2022-06-01 13:11:25 +00:00
|
|
|
import { createNotification } from './create-notification'
|
2022-08-04 23:34:04 +00:00
|
|
|
import { parseMentions, richTextToString } from 'common/util/parse'
|
2022-02-23 01:35:25 +00:00
|
|
|
|
|
|
|
const firestore = admin.firestore()
|
|
|
|
|
2022-07-05 23:18:37 +00:00
|
|
|
export const onCreateCommentOnContract = functions
|
2022-06-04 21:39:25 +00:00
|
|
|
.runWith({ secrets: ['MAILGUN_KEY'] })
|
|
|
|
.firestore.document('contracts/{contractId}/comments/{commentId}')
|
2022-02-23 01:35:25 +00:00
|
|
|
.onCreate(async (change, context) => {
|
|
|
|
const { contractId } = context.params as {
|
|
|
|
contractId: string
|
|
|
|
}
|
2022-06-01 13:11:25 +00:00
|
|
|
const { eventId } = context
|
2022-02-23 01:35:25 +00:00
|
|
|
|
|
|
|
const contract = await getContract(contractId)
|
2022-05-01 16:36:54 +00:00
|
|
|
if (!contract)
|
|
|
|
throw new Error('Could not find contract corresponding with comment')
|
2022-02-23 01:35:25 +00:00
|
|
|
|
|
|
|
const comment = change.data() as Comment
|
2022-05-01 16:36:54 +00:00
|
|
|
const lastCommentTime = comment.createdTime
|
2022-02-23 01:35:25 +00:00
|
|
|
|
|
|
|
const commentCreator = await getUser(comment.userId)
|
2022-06-01 13:11:25 +00:00
|
|
|
if (!commentCreator) throw new Error('Could not find comment creator')
|
|
|
|
|
2022-05-01 16:36:54 +00:00
|
|
|
await firestore
|
|
|
|
.collection('contracts')
|
|
|
|
.doc(contract.id)
|
|
|
|
.update({ lastCommentTime, lastUpdatedTime: Date.now() })
|
2022-02-23 01:35:25 +00:00
|
|
|
|
2022-04-21 17:09:06 +00:00
|
|
|
let bet: Bet | undefined
|
|
|
|
let answer: Answer | undefined
|
2022-05-18 13:25:38 +00:00
|
|
|
if (comment.answerOutcome) {
|
|
|
|
answer =
|
|
|
|
contract.outcomeType === 'FREE_RESPONSE' && contract.answers
|
|
|
|
? contract.answers?.find(
|
|
|
|
(answer) => answer.id === comment.answerOutcome
|
|
|
|
)
|
|
|
|
: undefined
|
|
|
|
} else if (comment.betId) {
|
2022-04-21 17:09:06 +00:00
|
|
|
const betSnapshot = await firestore
|
|
|
|
.collection('contracts')
|
|
|
|
.doc(contractId)
|
|
|
|
.collection('bets')
|
|
|
|
.doc(comment.betId)
|
|
|
|
.get()
|
|
|
|
bet = betSnapshot.data() as Bet
|
|
|
|
|
|
|
|
answer =
|
|
|
|
contract.outcomeType === 'FREE_RESPONSE' && contract.answers
|
|
|
|
? contract.answers.find((answer) => answer.id === bet?.outcome)
|
|
|
|
: undefined
|
|
|
|
}
|
2022-02-24 06:36:27 +00:00
|
|
|
|
2022-02-23 01:35:25 +00:00
|
|
|
const comments = await getValues<Comment>(
|
|
|
|
firestore.collection('contracts').doc(contractId).collection('comments')
|
|
|
|
)
|
2022-06-06 17:36:59 +00:00
|
|
|
const relatedSourceType = comment.replyToCommentId
|
|
|
|
? 'comment'
|
|
|
|
: comment.answerOutcome
|
|
|
|
? 'answer'
|
|
|
|
: undefined
|
|
|
|
|
2022-08-04 22:35:55 +00:00
|
|
|
const repliedUserId = comment.replyToCommentId
|
2022-06-06 17:36:59 +00:00
|
|
|
? comments.find((c) => c.id === comment.replyToCommentId)?.userId
|
|
|
|
: answer?.userId
|
2022-08-04 23:34:04 +00:00
|
|
|
|
|
|
|
const recipients = uniq(
|
|
|
|
compact([...parseMentions(comment.content), repliedUserId])
|
|
|
|
)
|
2022-06-06 17:36:59 +00:00
|
|
|
|
|
|
|
await createNotification(
|
|
|
|
comment.id,
|
|
|
|
'comment',
|
|
|
|
'created',
|
|
|
|
commentCreator,
|
|
|
|
eventId,
|
2022-08-04 23:34:04 +00:00
|
|
|
richTextToString(comment.content),
|
2022-08-04 22:35:55 +00:00
|
|
|
{ contract, relatedSourceType, recipients }
|
2022-06-06 17:36:59 +00:00
|
|
|
)
|
2022-02-23 01:35:25 +00:00
|
|
|
|
2022-05-22 08:36:05 +00:00
|
|
|
const recipientUserIds = uniq([
|
2022-02-23 01:35:25 +00:00
|
|
|
contract.creatorId,
|
|
|
|
...comments.map((comment) => comment.userId),
|
|
|
|
]).filter((id) => id !== comment.userId)
|
|
|
|
|
|
|
|
await Promise.all(
|
|
|
|
recipientUserIds.map((userId) =>
|
2022-02-24 06:36:27 +00:00
|
|
|
sendNewCommentEmail(
|
|
|
|
userId,
|
|
|
|
commentCreator,
|
|
|
|
contract,
|
|
|
|
comment,
|
|
|
|
bet,
|
2022-05-18 13:25:38 +00:00
|
|
|
answer?.text,
|
|
|
|
answer?.id
|
2022-02-24 06:36:27 +00:00
|
|
|
)
|
2022-02-23 01:35:25 +00:00
|
|
|
)
|
|
|
|
)
|
|
|
|
})
|