Notify people mentioned in comment
This commit is contained in:
parent
e91a859c5c
commit
19608d3368
|
@ -7,7 +7,7 @@ import {
|
||||||
} from '../../common/notification'
|
} from '../../common/notification'
|
||||||
import { User } from '../../common/user'
|
import { User } from '../../common/user'
|
||||||
import { Contract } from '../../common/contract'
|
import { Contract } from '../../common/contract'
|
||||||
import { getUserByUsername, getValues } from './utils'
|
import { getValues } from './utils'
|
||||||
import { Comment } from '../../common/comment'
|
import { Comment } from '../../common/comment'
|
||||||
import { uniq } from 'lodash'
|
import { uniq } from 'lodash'
|
||||||
import { Bet, LimitBet } from '../../common/bet'
|
import { Bet, LimitBet } from '../../common/bet'
|
||||||
|
@ -17,6 +17,7 @@ import { removeUndefinedProps } from '../../common/util/object'
|
||||||
import { TipTxn } from '../../common/txn'
|
import { TipTxn } from '../../common/txn'
|
||||||
import { Group, GROUP_CHAT_SLUG } from '../../common/group'
|
import { Group, GROUP_CHAT_SLUG } from '../../common/group'
|
||||||
import { Challenge } from '../../common/challenge'
|
import { Challenge } from '../../common/challenge'
|
||||||
|
import { richTextToString } from 'common/util/parse'
|
||||||
const firestore = admin.firestore()
|
const firestore = admin.firestore()
|
||||||
|
|
||||||
type user_to_reason_texts = {
|
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 = (
|
const notifyTaggedUsers = (
|
||||||
userToReasonTexts: user_to_reason_texts,
|
userToReasonTexts: user_to_reason_texts,
|
||||||
userIds: (string | undefined)[]
|
userIds: (string | undefined)[]
|
||||||
|
@ -301,8 +291,7 @@ export const createNotification = async (
|
||||||
if (sourceType === 'comment') {
|
if (sourceType === 'comment') {
|
||||||
if (recipients?.[0] && relatedSourceType)
|
if (recipients?.[0] && relatedSourceType)
|
||||||
notifyRepliedUser(userToReasonTexts, recipients[0], relatedSourceType)
|
notifyRepliedUser(userToReasonTexts, recipients[0], relatedSourceType)
|
||||||
if (sourceText)
|
if (sourceText) notifyTaggedUsers(userToReasonTexts, recipients ?? [])
|
||||||
notifyTaggedUsers(userToReasonTexts, await parseMentions(sourceText))
|
|
||||||
}
|
}
|
||||||
await notifyContractCreator(userToReasonTexts, sourceContract)
|
await notifyContractCreator(userToReasonTexts, sourceContract)
|
||||||
await notifyOtherAnswerersOnContract(userToReasonTexts, sourceContract)
|
await notifyOtherAnswerersOnContract(userToReasonTexts, sourceContract)
|
||||||
|
@ -427,7 +416,7 @@ export const createGroupCommentNotification = async (
|
||||||
sourceUserName: fromUser.name,
|
sourceUserName: fromUser.name,
|
||||||
sourceUserUsername: fromUser.username,
|
sourceUserUsername: fromUser.username,
|
||||||
sourceUserAvatarUrl: fromUser.avatarUrl,
|
sourceUserAvatarUrl: fromUser.avatarUrl,
|
||||||
sourceText: comment.text,
|
sourceText: richTextToString(comment.content),
|
||||||
sourceSlug,
|
sourceSlug,
|
||||||
sourceTitle: `${group.name}`,
|
sourceTitle: `${group.name}`,
|
||||||
isSeenOnHref: sourceSlug,
|
isSeenOnHref: sourceSlug,
|
||||||
|
|
|
@ -1,13 +1,13 @@
|
||||||
import * as functions from 'firebase-functions'
|
import * as functions from 'firebase-functions'
|
||||||
import * as admin from 'firebase-admin'
|
import * as admin from 'firebase-admin'
|
||||||
import { uniq } from 'lodash'
|
import { compact, uniq } from 'lodash'
|
||||||
|
|
||||||
import { getContract, getUser, getValues } from './utils'
|
import { getContract, getUser, getValues } from './utils'
|
||||||
import { Comment } from '../../common/comment'
|
import { Comment } from '../../common/comment'
|
||||||
import { sendNewCommentEmail } from './emails'
|
import { sendNewCommentEmail } from './emails'
|
||||||
import { Bet } from '../../common/bet'
|
import { Bet } from '../../common/bet'
|
||||||
import { Answer } from '../../common/answer'
|
import { Answer } from '../../common/answer'
|
||||||
import { createNotification } from './create-notification'
|
import { createNotification } from './create-notification'
|
||||||
|
import { parseMentions, richTextToString } from 'common/util/parse'
|
||||||
|
|
||||||
const firestore = admin.firestore()
|
const firestore = admin.firestore()
|
||||||
|
|
||||||
|
@ -71,7 +71,10 @@ export const onCreateCommentOnContract = functions
|
||||||
const repliedUserId = comment.replyToCommentId
|
const repliedUserId = comment.replyToCommentId
|
||||||
? comments.find((c) => c.id === comment.replyToCommentId)?.userId
|
? comments.find((c) => c.id === comment.replyToCommentId)?.userId
|
||||||
: answer?.userId
|
: answer?.userId
|
||||||
const recipients = repliedUserId ? [repliedUserId] : []
|
|
||||||
|
const recipients = uniq(
|
||||||
|
compact([...parseMentions(comment.content), repliedUserId])
|
||||||
|
)
|
||||||
|
|
||||||
await createNotification(
|
await createNotification(
|
||||||
comment.id,
|
comment.id,
|
||||||
|
@ -79,7 +82,7 @@ export const onCreateCommentOnContract = functions
|
||||||
'created',
|
'created',
|
||||||
commentCreator,
|
commentCreator,
|
||||||
eventId,
|
eventId,
|
||||||
comment.text,
|
richTextToString(comment.content),
|
||||||
{ contract, relatedSourceType, recipients }
|
{ contract, relatedSourceType, recipients }
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user