2022-02-23 01:35:25 +00:00
|
|
|
import * as functions from 'firebase-functions'
|
|
|
|
import * as admin from 'firebase-admin'
|
2022-09-12 16:34:56 +00:00
|
|
|
import { compact } from 'lodash'
|
2022-02-23 01:35:25 +00:00
|
|
|
import { getContract, getUser, getValues } from './utils'
|
2022-08-19 08:06:40 +00:00
|
|
|
import { ContractComment } from '../../common/comment'
|
2022-05-15 17:39:42 +00:00
|
|
|
import { Bet } from '../../common/bet'
|
|
|
|
import { Answer } from '../../common/answer'
|
2022-09-12 16:34:56 +00:00
|
|
|
import { createCommentOrAnswerOrUpdatedContractNotification } from './create-notification'
|
2022-08-06 20:39:52 +00:00
|
|
|
import { parseMentions, richTextToString } from '../../common/util/parse'
|
2022-08-24 16:49:53 +00:00
|
|
|
import { addUserToContractFollowers } from './follow-market'
|
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
|
|
|
|
2022-08-16 05:43:46 +00:00
|
|
|
await change.ref.update({
|
|
|
|
contractSlug: contract.slug,
|
|
|
|
contractQuestion: contract.question,
|
|
|
|
})
|
|
|
|
|
2022-08-19 08:06:40 +00:00
|
|
|
const comment = change.data() as ContractComment
|
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-08-24 16:49:53 +00:00
|
|
|
await addUserToContractFollowers(contract.id, commentCreator.id)
|
|
|
|
|
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-09-04 21:28:45 +00:00
|
|
|
|
|
|
|
await change.ref.update({
|
|
|
|
betOutcome: bet.outcome,
|
|
|
|
betAmount: bet.amount,
|
|
|
|
})
|
2022-04-21 17:09:06 +00:00
|
|
|
}
|
2022-02-24 06:36:27 +00:00
|
|
|
|
2022-08-19 08:06:40 +00:00
|
|
|
const comments = await getValues<ContractComment>(
|
2022-02-23 01:35:25 +00:00
|
|
|
firestore.collection('contracts').doc(contractId).collection('comments')
|
|
|
|
)
|
2022-09-12 16:34:56 +00:00
|
|
|
const repliedToType = answer
|
2022-06-06 17:36:59 +00:00
|
|
|
? 'answer'
|
2022-09-12 16:34:56 +00:00
|
|
|
: comment.replyToCommentId
|
|
|
|
? 'comment'
|
2022-06-06 17:36:59 +00:00
|
|
|
: 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-06 20:39:52 +00:00
|
|
|
|
2022-08-24 16:49:53 +00:00
|
|
|
await createCommentOrAnswerOrUpdatedContractNotification(
|
2022-06-06 17:36:59 +00:00
|
|
|
comment.id,
|
|
|
|
'comment',
|
|
|
|
'created',
|
|
|
|
commentCreator,
|
|
|
|
eventId,
|
2022-08-06 20:39:52 +00:00
|
|
|
richTextToString(comment.content),
|
2022-08-24 16:49:53 +00:00
|
|
|
contract,
|
|
|
|
{
|
2022-09-12 16:34:56 +00:00
|
|
|
repliedToType,
|
|
|
|
repliedToId: comment.replyToCommentId || answer?.id,
|
|
|
|
repliedToContent: answer ? answer.text : undefined,
|
2022-08-24 16:49:53 +00:00
|
|
|
repliedUserId,
|
|
|
|
taggedUserIds: compact(parseMentions(comment.content)),
|
|
|
|
}
|
2022-06-06 17:36:59 +00:00
|
|
|
)
|
2022-02-23 01:35:25 +00:00
|
|
|
})
|