Send notif to all users in reply chain as reply

This commit is contained in:
Ian Philips 2022-09-12 17:01:59 -06:00
parent f49cb9b399
commit 018eb8fbfc
2 changed files with 62 additions and 27 deletions

View File

@ -188,6 +188,15 @@ export const createNotification = async (
} }
} }
export type replied_users_info = {
[key: string]: {
repliedToType: 'comment' | 'answer'
repliedToAnswerText: string | undefined
repliedToId: string | undefined
bet: Bet | undefined
}
}
export const createCommentOrAnswerOrUpdatedContractNotification = async ( export const createCommentOrAnswerOrUpdatedContractNotification = async (
sourceId: string, sourceId: string,
sourceType: 'comment' | 'answer' | 'contract', sourceType: 'comment' | 'answer' | 'contract',
@ -197,11 +206,8 @@ export const createCommentOrAnswerOrUpdatedContractNotification = async (
sourceText: string, sourceText: string,
sourceContract: Contract, sourceContract: Contract,
miscData?: { miscData?: {
repliedToType?: 'comment' | 'answer' repliedUsersInfo: replied_users_info
repliedToId?: string taggedUserIds: string[]
repliedToContent?: string
repliedUserId?: string
taggedUserIds?: string[]
}, },
resolutionData?: { resolutionData?: {
bets: Bet[] bets: Bet[]
@ -215,13 +221,7 @@ export const createCommentOrAnswerOrUpdatedContractNotification = async (
resolutions?: { [outcome: string]: number } resolutions?: { [outcome: string]: number }
} }
) => { ) => {
const { const { repliedUsersInfo, taggedUserIds } = miscData ?? {}
repliedToType,
repliedToContent,
repliedUserId,
taggedUserIds,
repliedToId,
} = miscData ?? {}
const browserRecipientIdsList: string[] = [] const browserRecipientIdsList: string[] = []
const emailRecipientIdsList: string[] = [] const emailRecipientIdsList: string[] = []
@ -289,6 +289,8 @@ export const createCommentOrAnswerOrUpdatedContractNotification = async (
} }
if (sendToEmail && !emailRecipientIdsList.includes(userId)) { if (sendToEmail && !emailRecipientIdsList.includes(userId)) {
if (sourceType === 'comment') { if (sourceType === 'comment') {
const { repliedToType, repliedToAnswerText, repliedToId, bet } =
repliedUsersInfo?.[userId] ?? {}
// TODO: change subject of email title to be more specific, i.e.: replied to you on/tagged you on/comment // TODO: change subject of email title to be more specific, i.e.: replied to you on/tagged you on/comment
await sendNewCommentEmail( await sendNewCommentEmail(
reason, reason,
@ -297,9 +299,8 @@ export const createCommentOrAnswerOrUpdatedContractNotification = async (
sourceContract, sourceContract,
sourceText, sourceText,
sourceId, sourceId,
// TODO: Add any paired bets to the comment bet,
undefined, repliedToAnswerText,
repliedToType === 'answer' ? repliedToContent : undefined,
repliedToType === 'answer' ? repliedToId : undefined repliedToType === 'answer' ? repliedToId : undefined
) )
} else if (sourceType === 'answer') } else if (sourceType === 'answer')
@ -437,13 +438,17 @@ export const createCommentOrAnswerOrUpdatedContractNotification = async (
} }
const notifyRepliedUser = async () => { const notifyRepliedUser = async () => {
if (sourceType === 'comment' && repliedUserId && repliedToType) if (sourceType === 'comment' && repliedUsersInfo)
await sendNotificationsIfSettingsPermit( await Promise.all(
repliedUserId, Object.keys(repliedUsersInfo).map((userId) =>
repliedToType === 'answer' sendNotificationsIfSettingsPermit(
userId,
repliedUsersInfo[userId].repliedToType === 'answer'
? 'reply_to_users_answer' ? 'reply_to_users_answer'
: 'reply_to_users_comment' : 'reply_to_users_comment'
) )
)
)
} }
const notifyTaggedUsers = async () => { const notifyTaggedUsers = async () => {

View File

@ -5,7 +5,10 @@ import { getContract, getUser, getValues } from './utils'
import { ContractComment } from '../../common/comment' import { ContractComment } from '../../common/comment'
import { Bet } from '../../common/bet' import { Bet } from '../../common/bet'
import { Answer } from '../../common/answer' import { Answer } from '../../common/answer'
import { createCommentOrAnswerOrUpdatedContractNotification } from './create-notification' import {
createCommentOrAnswerOrUpdatedContractNotification,
replied_users_info,
} from './create-notification'
import { parseMentions, richTextToString } from '../../common/util/parse' import { parseMentions, richTextToString } from '../../common/util/parse'
import { addUserToContractFollowers } from './follow-market' import { addUserToContractFollowers } from './follow-market'
@ -83,6 +86,36 @@ export const onCreateCommentOnContract = functions
? comments.find((c) => c.id === comment.replyToCommentId)?.userId ? comments.find((c) => c.id === comment.replyToCommentId)?.userId
: answer?.userId : answer?.userId
const mentionedUsers = compact(parseMentions(comment.content))
const repliedUsers: replied_users_info = {}
// The parent of the reply chain could be a comment or an answer
if (repliedUserId && repliedToType)
repliedUsers[repliedUserId] = {
repliedToType,
repliedToAnswerText: answer ? answer.text : undefined,
repliedToId: comment.replyToCommentId || answer?.id,
bet: bet,
}
const commentsInSameReplyChain = comments.filter((c) =>
repliedToType === 'answer'
? c.answerOutcome === answer?.id
: repliedToType === 'comment'
? c.replyToCommentId === comment.replyToCommentId
: false
)
// The rest of the children in the chain are always comments
commentsInSameReplyChain.forEach((c) => {
if (c.userId !== comment.userId && c.userId !== repliedUserId) {
repliedUsers[c.userId] = {
repliedToType: 'comment',
repliedToAnswerText: undefined,
repliedToId: c.id,
bet: undefined,
}
}
})
await createCommentOrAnswerOrUpdatedContractNotification( await createCommentOrAnswerOrUpdatedContractNotification(
comment.id, comment.id,
'comment', 'comment',
@ -92,11 +125,8 @@ export const onCreateCommentOnContract = functions
richTextToString(comment.content), richTextToString(comment.content),
contract, contract,
{ {
repliedToType, repliedUsersInfo: repliedUsers,
repliedToId: comment.replyToCommentId || answer?.id, taggedUserIds: mentionedUsers,
repliedToContent: answer ? answer.text : undefined,
repliedUserId,
taggedUserIds: compact(parseMentions(comment.content)),
} }
) )
}) })