Notify people mentioned in comment

This commit is contained in:
Sinclair Chen 2022-08-04 16:00:12 -07:00
parent e91a859c5c
commit 19608d3368
2 changed files with 11 additions and 19 deletions

View File

@ -7,7 +7,7 @@ import {
} from '../../common/notification'
import { User } from '../../common/user'
import { Contract } from '../../common/contract'
import { getUserByUsername, getValues } from './utils'
import { getValues } from './utils'
import { Comment } from '../../common/comment'
import { uniq } from 'lodash'
import { Bet, LimitBet } from '../../common/bet'
@ -17,6 +17,7 @@ import { removeUndefinedProps } from '../../common/util/object'
import { TipTxn } from '../../common/txn'
import { Group, GROUP_CHAT_SLUG } from '../../common/group'
import { Challenge } from '../../common/challenge'
import { richTextToString } from 'common/util/parse'
const firestore = admin.firestore()
type user_to_reason_texts = {
@ -155,17 +156,6 @@ export const createNotification = async (
}
}
/** @deprecated parse from rich text instead */
const parseMentions = async (source: string) => {
const mentions = source.match(/@\w+/g)
if (!mentions) return []
return Promise.all(
mentions.map(
async (username) => (await getUserByUsername(username.slice(1)))?.id
)
)
}
const notifyTaggedUsers = (
userToReasonTexts: user_to_reason_texts,
userIds: (string | undefined)[]
@ -301,8 +291,7 @@ export const createNotification = async (
if (sourceType === 'comment') {
if (recipients?.[0] && relatedSourceType)
notifyRepliedUser(userToReasonTexts, recipients[0], relatedSourceType)
if (sourceText)
notifyTaggedUsers(userToReasonTexts, await parseMentions(sourceText))
if (sourceText) notifyTaggedUsers(userToReasonTexts, recipients ?? [])
}
await notifyContractCreator(userToReasonTexts, sourceContract)
await notifyOtherAnswerersOnContract(userToReasonTexts, sourceContract)
@ -427,7 +416,7 @@ export const createGroupCommentNotification = async (
sourceUserName: fromUser.name,
sourceUserUsername: fromUser.username,
sourceUserAvatarUrl: fromUser.avatarUrl,
sourceText: comment.text,
sourceText: richTextToString(comment.content),
sourceSlug,
sourceTitle: `${group.name}`,
isSeenOnHref: sourceSlug,

View File

@ -1,13 +1,13 @@
import * as functions from 'firebase-functions'
import * as admin from 'firebase-admin'
import { uniq } from 'lodash'
import { compact, uniq } from 'lodash'
import { getContract, getUser, getValues } from './utils'
import { Comment } from '../../common/comment'
import { sendNewCommentEmail } from './emails'
import { Bet } from '../../common/bet'
import { Answer } from '../../common/answer'
import { createNotification } from './create-notification'
import { parseMentions, richTextToString } from 'common/util/parse'
const firestore = admin.firestore()
@ -71,7 +71,10 @@ export const onCreateCommentOnContract = functions
const repliedUserId = comment.replyToCommentId
? comments.find((c) => c.id === comment.replyToCommentId)?.userId
: answer?.userId
const recipients = repliedUserId ? [repliedUserId] : []
const recipients = uniq(
compact([...parseMentions(comment.content), repliedUserId])
)
await createNotification(
comment.id,
@ -79,7 +82,7 @@ export const onCreateCommentOnContract = functions
'created',
commentCreator,
eventId,
comment.text,
richTextToString(comment.content),
{ contract, relatedSourceType, recipients }
)