2022-06-01 13:11:25 +00:00
|
|
|
import * as admin from 'firebase-admin'
|
|
|
|
import {
|
|
|
|
Notification,
|
|
|
|
notification_reason_types,
|
2022-06-06 17:36:59 +00:00
|
|
|
notification_source_update_types,
|
2022-06-01 13:11:25 +00:00
|
|
|
notification_source_types,
|
|
|
|
} from '../../common/notification'
|
|
|
|
import { User } from '../../common/user'
|
|
|
|
import { Contract } from '../../common/contract'
|
2022-08-06 20:39:52 +00:00
|
|
|
import { getValues } from './utils'
|
2022-06-01 13:11:25 +00:00
|
|
|
import { Comment } from '../../common/comment'
|
|
|
|
import { uniq } from 'lodash'
|
2022-07-10 18:05:44 +00:00
|
|
|
import { Bet, LimitBet } from '../../common/bet'
|
2022-06-01 13:11:25 +00:00
|
|
|
import { Answer } from '../../common/answer'
|
2022-06-06 17:36:59 +00:00
|
|
|
import { getContractBetMetrics } from '../../common/calculate'
|
|
|
|
import { removeUndefinedProps } from '../../common/util/object'
|
2022-07-07 23:23:13 +00:00
|
|
|
import { TipTxn } from '../../common/txn'
|
2022-07-15 12:52:08 +00:00
|
|
|
import { Group, GROUP_CHAT_SLUG } from '../../common/group'
|
2022-08-04 21:27:02 +00:00
|
|
|
import { Challenge } from '../../common/challenge'
|
2022-08-06 20:39:52 +00:00
|
|
|
import { richTextToString } from '../../common/util/parse'
|
2022-06-01 13:11:25 +00:00
|
|
|
const firestore = admin.firestore()
|
|
|
|
|
|
|
|
type user_to_reason_texts = {
|
2022-07-15 12:52:08 +00:00
|
|
|
[userId: string]: { reason: notification_reason_types }
|
2022-06-01 13:11:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export const createNotification = async (
|
|
|
|
sourceId: string,
|
|
|
|
sourceType: notification_source_types,
|
2022-06-06 17:36:59 +00:00
|
|
|
sourceUpdateType: notification_source_update_types,
|
2022-06-01 13:11:25 +00:00
|
|
|
sourceUser: User,
|
2022-06-06 17:36:59 +00:00
|
|
|
idempotencyKey: string,
|
2022-06-08 14:43:24 +00:00
|
|
|
sourceText: string,
|
2022-07-22 00:08:09 +00:00
|
|
|
miscData?: {
|
|
|
|
contract?: Contract
|
|
|
|
relatedSourceType?: notification_source_types
|
2022-08-04 22:35:55 +00:00
|
|
|
recipients?: string[]
|
2022-07-22 00:08:09 +00:00
|
|
|
slug?: string
|
|
|
|
title?: string
|
|
|
|
}
|
2022-06-01 13:11:25 +00:00
|
|
|
) => {
|
2022-07-22 00:08:09 +00:00
|
|
|
const {
|
|
|
|
contract: sourceContract,
|
|
|
|
relatedSourceType,
|
2022-08-04 22:35:55 +00:00
|
|
|
recipients,
|
2022-07-22 00:08:09 +00:00
|
|
|
slug,
|
|
|
|
title,
|
|
|
|
} = miscData ?? {}
|
|
|
|
|
2022-06-01 13:11:25 +00:00
|
|
|
const shouldGetNotification = (
|
|
|
|
userId: string,
|
|
|
|
userToReasonTexts: user_to_reason_texts
|
|
|
|
) => {
|
|
|
|
return (
|
|
|
|
sourceUser.id != userId &&
|
|
|
|
!Object.keys(userToReasonTexts).includes(userId)
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
const createUsersNotifications = async (
|
|
|
|
userToReasonTexts: user_to_reason_texts
|
|
|
|
) => {
|
|
|
|
await Promise.all(
|
|
|
|
Object.keys(userToReasonTexts).map(async (userId) => {
|
|
|
|
const notificationRef = firestore
|
|
|
|
.collection(`/users/${userId}/notifications`)
|
|
|
|
.doc(idempotencyKey)
|
|
|
|
const notification: Notification = {
|
|
|
|
id: idempotencyKey,
|
|
|
|
userId,
|
|
|
|
reason: userToReasonTexts[userId].reason,
|
|
|
|
createdTime: Date.now(),
|
|
|
|
isSeen: false,
|
|
|
|
sourceId,
|
|
|
|
sourceType,
|
2022-06-06 17:36:59 +00:00
|
|
|
sourceUpdateType,
|
|
|
|
sourceContractId: sourceContract?.id,
|
2022-06-01 13:11:25 +00:00
|
|
|
sourceUserName: sourceUser.name,
|
|
|
|
sourceUserUsername: sourceUser.username,
|
|
|
|
sourceUserAvatarUrl: sourceUser.avatarUrl,
|
2022-06-08 14:43:24 +00:00
|
|
|
sourceText,
|
|
|
|
sourceContractCreatorUsername: sourceContract?.creatorUsername,
|
2022-06-22 16:35:50 +00:00
|
|
|
sourceContractTitle: sourceContract?.question,
|
2022-06-08 14:43:24 +00:00
|
|
|
sourceContractSlug: sourceContract?.slug,
|
2022-07-22 00:08:09 +00:00
|
|
|
sourceSlug: slug ? slug : sourceContract?.slug,
|
|
|
|
sourceTitle: title ? title : sourceContract?.question,
|
2022-06-01 13:11:25 +00:00
|
|
|
}
|
2022-06-06 17:36:59 +00:00
|
|
|
await notificationRef.set(removeUndefinedProps(notification))
|
2022-06-01 13:11:25 +00:00
|
|
|
})
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-06-10 22:48:28 +00:00
|
|
|
const notifyLiquidityProviders = async (
|
|
|
|
userToReasonTexts: user_to_reason_texts,
|
|
|
|
contract: Contract
|
|
|
|
) => {
|
|
|
|
const liquidityProviders = await firestore
|
|
|
|
.collection(`contracts/${contract.id}/liquidity`)
|
|
|
|
.get()
|
|
|
|
const liquidityProvidersIds = uniq(
|
|
|
|
liquidityProviders.docs.map((doc) => doc.data().userId)
|
|
|
|
)
|
|
|
|
liquidityProvidersIds.forEach((userId) => {
|
|
|
|
if (!shouldGetNotification(userId, userToReasonTexts)) return
|
|
|
|
userToReasonTexts[userId] = {
|
|
|
|
reason: 'on_contract_with_users_shares_in',
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-06-08 14:43:24 +00:00
|
|
|
const notifyUsersFollowers = async (
|
|
|
|
userToReasonTexts: user_to_reason_texts
|
|
|
|
) => {
|
|
|
|
const followers = await firestore
|
|
|
|
.collectionGroup('follows')
|
|
|
|
.where('userId', '==', sourceUser.id)
|
|
|
|
.get()
|
|
|
|
|
|
|
|
followers.docs.forEach((doc) => {
|
|
|
|
const followerUserId = doc.ref.parent.parent?.id
|
|
|
|
if (
|
|
|
|
followerUserId &&
|
|
|
|
shouldGetNotification(followerUserId, userToReasonTexts)
|
|
|
|
) {
|
|
|
|
userToReasonTexts[followerUserId] = {
|
|
|
|
reason: 'you_follow_user',
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-08-04 22:35:55 +00:00
|
|
|
const notifyRepliedUser = (
|
2022-06-10 22:48:28 +00:00
|
|
|
userToReasonTexts: user_to_reason_texts,
|
|
|
|
relatedUserId: string,
|
|
|
|
relatedSourceType: notification_source_types
|
2022-06-06 17:36:59 +00:00
|
|
|
) => {
|
2022-06-10 22:48:28 +00:00
|
|
|
if (!shouldGetNotification(relatedUserId, userToReasonTexts)) return
|
2022-06-06 17:36:59 +00:00
|
|
|
if (relatedSourceType === 'comment') {
|
|
|
|
userToReasonTexts[relatedUserId] = {
|
|
|
|
reason: 'reply_to_users_comment',
|
|
|
|
}
|
|
|
|
} else if (relatedSourceType === 'answer') {
|
|
|
|
userToReasonTexts[relatedUserId] = {
|
|
|
|
reason: 'reply_to_users_answer',
|
|
|
|
}
|
2022-06-06 16:54:25 +00:00
|
|
|
}
|
2022-06-06 17:36:59 +00:00
|
|
|
}
|
2022-06-01 13:11:25 +00:00
|
|
|
|
2022-08-04 22:35:55 +00:00
|
|
|
const notifyFollowedUser = (
|
2022-06-06 17:36:59 +00:00
|
|
|
userToReasonTexts: user_to_reason_texts,
|
|
|
|
followedUserId: string
|
|
|
|
) => {
|
|
|
|
if (shouldGetNotification(followedUserId, userToReasonTexts))
|
|
|
|
userToReasonTexts[followedUserId] = {
|
|
|
|
reason: 'on_new_follow',
|
|
|
|
}
|
|
|
|
}
|
2022-06-01 13:11:25 +00:00
|
|
|
|
2022-08-04 22:35:55 +00:00
|
|
|
const notifyTaggedUsers = (
|
2022-06-10 22:48:28 +00:00
|
|
|
userToReasonTexts: user_to_reason_texts,
|
2022-08-04 22:35:55 +00:00
|
|
|
userIds: (string | undefined)[]
|
2022-06-10 22:48:28 +00:00
|
|
|
) => {
|
2022-08-04 22:35:55 +00:00
|
|
|
userIds.forEach((id) => {
|
|
|
|
if (id && shouldGetNotification(id, userToReasonTexts))
|
|
|
|
userToReasonTexts[id] = {
|
2022-06-06 17:36:59 +00:00
|
|
|
reason: 'tagged_user',
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2022-06-01 13:11:25 +00:00
|
|
|
|
2022-06-06 17:36:59 +00:00
|
|
|
const notifyContractCreator = async (
|
|
|
|
userToReasonTexts: user_to_reason_texts,
|
2022-06-10 22:48:28 +00:00
|
|
|
sourceContract: Contract,
|
|
|
|
options?: { force: boolean }
|
2022-06-06 17:36:59 +00:00
|
|
|
) => {
|
2022-06-10 22:48:28 +00:00
|
|
|
if (
|
|
|
|
options?.force ||
|
|
|
|
shouldGetNotification(sourceContract.creatorId, userToReasonTexts)
|
|
|
|
)
|
2022-06-06 17:36:59 +00:00
|
|
|
userToReasonTexts[sourceContract.creatorId] = {
|
|
|
|
reason: 'on_users_contract',
|
|
|
|
}
|
|
|
|
}
|
2022-06-01 13:11:25 +00:00
|
|
|
|
2022-06-06 17:36:59 +00:00
|
|
|
const notifyOtherAnswerersOnContract = async (
|
|
|
|
userToReasonTexts: user_to_reason_texts,
|
|
|
|
sourceContract: Contract
|
|
|
|
) => {
|
|
|
|
const answers = await getValues<Answer>(
|
|
|
|
firestore
|
|
|
|
.collection('contracts')
|
|
|
|
.doc(sourceContract.id)
|
|
|
|
.collection('answers')
|
|
|
|
)
|
|
|
|
const recipientUserIds = uniq(answers.map((answer) => answer.userId))
|
|
|
|
recipientUserIds.forEach((userId) => {
|
|
|
|
if (shouldGetNotification(userId, userToReasonTexts))
|
|
|
|
userToReasonTexts[userId] = {
|
|
|
|
reason: 'on_contract_with_users_answer',
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2022-06-01 13:11:25 +00:00
|
|
|
|
2022-06-06 17:36:59 +00:00
|
|
|
const notifyOtherCommentersOnContract = async (
|
|
|
|
userToReasonTexts: user_to_reason_texts,
|
|
|
|
sourceContract: Contract
|
|
|
|
) => {
|
|
|
|
const comments = await getValues<Comment>(
|
|
|
|
firestore
|
|
|
|
.collection('contracts')
|
|
|
|
.doc(sourceContract.id)
|
|
|
|
.collection('comments')
|
|
|
|
)
|
|
|
|
const recipientUserIds = uniq(comments.map((comment) => comment.userId))
|
|
|
|
recipientUserIds.forEach((userId) => {
|
|
|
|
if (shouldGetNotification(userId, userToReasonTexts))
|
|
|
|
userToReasonTexts[userId] = {
|
|
|
|
reason: 'on_contract_with_users_comment',
|
|
|
|
}
|
|
|
|
})
|
2022-06-01 13:11:25 +00:00
|
|
|
}
|
2022-06-06 16:52:11 +00:00
|
|
|
|
2022-06-10 22:48:28 +00:00
|
|
|
const notifyBettorsOnContract = async (
|
2022-06-06 17:36:59 +00:00
|
|
|
userToReasonTexts: user_to_reason_texts,
|
|
|
|
sourceContract: Contract
|
|
|
|
) => {
|
|
|
|
const betsSnap = await firestore
|
|
|
|
.collection(`contracts/${sourceContract.id}/bets`)
|
|
|
|
.get()
|
|
|
|
const bets = betsSnap.docs.map((doc) => doc.data() as Bet)
|
|
|
|
// filter bets for only users that have an amount invested still
|
|
|
|
const recipientUserIds = uniq(bets.map((bet) => bet.userId)).filter(
|
|
|
|
(userId) => {
|
|
|
|
return (
|
|
|
|
getContractBetMetrics(
|
|
|
|
sourceContract,
|
|
|
|
bets.filter((bet) => bet.userId === userId)
|
|
|
|
).invested > 0
|
|
|
|
)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
recipientUserIds.forEach((userId) => {
|
|
|
|
if (shouldGetNotification(userId, userToReasonTexts))
|
|
|
|
userToReasonTexts[userId] = {
|
|
|
|
reason: 'on_contract_with_users_shares_in',
|
|
|
|
}
|
|
|
|
})
|
2022-06-06 16:52:11 +00:00
|
|
|
}
|
2022-06-06 17:36:59 +00:00
|
|
|
|
2022-08-04 22:35:55 +00:00
|
|
|
const notifyUserAddedToGroup = (
|
2022-06-22 16:35:50 +00:00
|
|
|
userToReasonTexts: user_to_reason_texts,
|
|
|
|
relatedUserId: string
|
|
|
|
) => {
|
|
|
|
if (shouldGetNotification(relatedUserId, userToReasonTexts))
|
|
|
|
userToReasonTexts[relatedUserId] = {
|
|
|
|
reason: 'added_you_to_group',
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-05 17:29:26 +00:00
|
|
|
const notifyContractCreatorOfUniqueBettorsBonus = async (
|
|
|
|
userToReasonTexts: user_to_reason_texts,
|
|
|
|
userId: string
|
|
|
|
) => {
|
|
|
|
userToReasonTexts[userId] = {
|
|
|
|
reason: 'unique_bettors_on_your_contract',
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-06 17:36:59 +00:00
|
|
|
const getUsersToNotify = async () => {
|
|
|
|
const userToReasonTexts: user_to_reason_texts = {}
|
|
|
|
// The following functions modify the userToReasonTexts object in place.
|
2022-08-04 22:35:55 +00:00
|
|
|
if (sourceType === 'follow' && recipients?.[0]) {
|
|
|
|
notifyFollowedUser(userToReasonTexts, recipients[0])
|
|
|
|
} else if (
|
|
|
|
sourceType === 'group' &&
|
|
|
|
sourceUpdateType === 'created' &&
|
|
|
|
recipients
|
|
|
|
) {
|
|
|
|
recipients.forEach((r) => notifyUserAddedToGroup(userToReasonTexts, r))
|
2022-07-01 13:47:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// The following functions need sourceContract to be defined.
|
|
|
|
if (!sourceContract) return userToReasonTexts
|
2022-07-06 19:30:51 +00:00
|
|
|
|
2022-07-01 13:47:19 +00:00
|
|
|
if (
|
|
|
|
sourceType === 'comment' ||
|
|
|
|
sourceType === 'answer' ||
|
|
|
|
(sourceType === 'contract' &&
|
|
|
|
(sourceUpdateType === 'updated' || sourceUpdateType === 'resolved'))
|
|
|
|
) {
|
|
|
|
if (sourceType === 'comment') {
|
2022-08-04 22:35:55 +00:00
|
|
|
if (recipients?.[0] && relatedSourceType)
|
|
|
|
notifyRepliedUser(userToReasonTexts, recipients[0], relatedSourceType)
|
2022-08-06 20:39:52 +00:00
|
|
|
if (sourceText) notifyTaggedUsers(userToReasonTexts, recipients ?? [])
|
2022-07-01 13:47:19 +00:00
|
|
|
}
|
|
|
|
await notifyContractCreator(userToReasonTexts, sourceContract)
|
|
|
|
await notifyOtherAnswerersOnContract(userToReasonTexts, sourceContract)
|
|
|
|
await notifyLiquidityProviders(userToReasonTexts, sourceContract)
|
|
|
|
await notifyBettorsOnContract(userToReasonTexts, sourceContract)
|
|
|
|
await notifyOtherCommentersOnContract(userToReasonTexts, sourceContract)
|
|
|
|
} else if (sourceType === 'contract' && sourceUpdateType === 'created') {
|
|
|
|
await notifyUsersFollowers(userToReasonTexts)
|
2022-08-04 22:35:55 +00:00
|
|
|
notifyTaggedUsers(userToReasonTexts, recipients ?? [])
|
2022-07-01 13:47:19 +00:00
|
|
|
} else if (sourceType === 'contract' && sourceUpdateType === 'closed') {
|
|
|
|
await notifyContractCreator(userToReasonTexts, sourceContract, {
|
|
|
|
force: true,
|
|
|
|
})
|
|
|
|
} else if (sourceType === 'liquidity' && sourceUpdateType === 'created') {
|
|
|
|
await notifyContractCreator(userToReasonTexts, sourceContract)
|
2022-07-05 17:29:26 +00:00
|
|
|
} else if (sourceType === 'bonus' && sourceUpdateType === 'created') {
|
|
|
|
// Note: the daily bonus won't have a contract attached to it
|
|
|
|
await notifyContractCreatorOfUniqueBettorsBonus(
|
|
|
|
userToReasonTexts,
|
|
|
|
sourceContract.creatorId
|
|
|
|
)
|
2022-06-06 17:36:59 +00:00
|
|
|
}
|
|
|
|
return userToReasonTexts
|
|
|
|
}
|
|
|
|
|
|
|
|
const userToReasonTexts = await getUsersToNotify()
|
|
|
|
await createUsersNotifications(userToReasonTexts)
|
2022-06-01 13:11:25 +00:00
|
|
|
}
|
2022-07-07 23:23:13 +00:00
|
|
|
|
|
|
|
export const createTipNotification = async (
|
|
|
|
fromUser: User,
|
|
|
|
toUser: User,
|
|
|
|
tip: TipTxn,
|
|
|
|
idempotencyKey: string,
|
|
|
|
commentId: string,
|
|
|
|
contract?: Contract,
|
|
|
|
group?: Group
|
|
|
|
) => {
|
|
|
|
const slug = group ? group.slug + `#${commentId}` : commentId
|
|
|
|
|
|
|
|
const notificationRef = firestore
|
|
|
|
.collection(`/users/${toUser.id}/notifications`)
|
|
|
|
.doc(idempotencyKey)
|
|
|
|
const notification: Notification = {
|
|
|
|
id: idempotencyKey,
|
|
|
|
userId: toUser.id,
|
|
|
|
reason: 'tip_received',
|
|
|
|
createdTime: Date.now(),
|
|
|
|
isSeen: false,
|
|
|
|
sourceId: tip.id,
|
|
|
|
sourceType: 'tip',
|
|
|
|
sourceUpdateType: 'created',
|
|
|
|
sourceUserName: fromUser.name,
|
|
|
|
sourceUserUsername: fromUser.username,
|
|
|
|
sourceUserAvatarUrl: fromUser.avatarUrl,
|
|
|
|
sourceText: tip.amount.toString(),
|
|
|
|
sourceContractCreatorUsername: contract?.creatorUsername,
|
|
|
|
sourceContractTitle: contract?.question,
|
|
|
|
sourceContractSlug: contract?.slug,
|
|
|
|
sourceSlug: slug,
|
|
|
|
sourceTitle: group?.name,
|
|
|
|
}
|
|
|
|
return await notificationRef.set(removeUndefinedProps(notification))
|
|
|
|
}
|
2022-07-10 18:05:44 +00:00
|
|
|
|
|
|
|
export const createBetFillNotification = async (
|
|
|
|
fromUser: User,
|
|
|
|
toUser: User,
|
|
|
|
bet: Bet,
|
|
|
|
userBet: LimitBet,
|
|
|
|
contract: Contract,
|
|
|
|
idempotencyKey: string
|
|
|
|
) => {
|
|
|
|
const fill = userBet.fills.find((fill) => fill.matchedBetId === bet.id)
|
|
|
|
const fillAmount = fill?.amount ?? 0
|
|
|
|
|
|
|
|
const notificationRef = firestore
|
|
|
|
.collection(`/users/${toUser.id}/notifications`)
|
|
|
|
.doc(idempotencyKey)
|
|
|
|
const notification: Notification = {
|
|
|
|
id: idempotencyKey,
|
|
|
|
userId: toUser.id,
|
|
|
|
reason: 'bet_fill',
|
|
|
|
createdTime: Date.now(),
|
|
|
|
isSeen: false,
|
|
|
|
sourceId: userBet.id,
|
|
|
|
sourceType: 'bet',
|
|
|
|
sourceUpdateType: 'updated',
|
|
|
|
sourceUserName: fromUser.name,
|
|
|
|
sourceUserUsername: fromUser.username,
|
|
|
|
sourceUserAvatarUrl: fromUser.avatarUrl,
|
|
|
|
sourceText: fillAmount.toString(),
|
|
|
|
sourceContractCreatorUsername: contract.creatorUsername,
|
|
|
|
sourceContractTitle: contract.question,
|
|
|
|
sourceContractSlug: contract.slug,
|
2022-07-10 18:45:32 +00:00
|
|
|
sourceContractId: contract.id,
|
2022-07-10 18:05:44 +00:00
|
|
|
}
|
|
|
|
return await notificationRef.set(removeUndefinedProps(notification))
|
|
|
|
}
|
2022-07-15 12:52:08 +00:00
|
|
|
|
|
|
|
export const createGroupCommentNotification = async (
|
|
|
|
fromUser: User,
|
|
|
|
toUserId: string,
|
|
|
|
comment: Comment,
|
|
|
|
group: Group,
|
|
|
|
idempotencyKey: string
|
|
|
|
) => {
|
2022-07-18 13:59:21 +00:00
|
|
|
if (toUserId === fromUser.id) return
|
2022-07-15 12:52:08 +00:00
|
|
|
const notificationRef = firestore
|
|
|
|
.collection(`/users/${toUserId}/notifications`)
|
|
|
|
.doc(idempotencyKey)
|
|
|
|
const sourceSlug = `/group/${group.slug}/${GROUP_CHAT_SLUG}`
|
|
|
|
const notification: Notification = {
|
|
|
|
id: idempotencyKey,
|
|
|
|
userId: toUserId,
|
|
|
|
reason: 'on_group_you_are_member_of',
|
|
|
|
createdTime: Date.now(),
|
|
|
|
isSeen: false,
|
|
|
|
sourceId: comment.id,
|
|
|
|
sourceType: 'comment',
|
|
|
|
sourceUpdateType: 'created',
|
|
|
|
sourceUserName: fromUser.name,
|
|
|
|
sourceUserUsername: fromUser.username,
|
|
|
|
sourceUserAvatarUrl: fromUser.avatarUrl,
|
2022-08-06 20:39:52 +00:00
|
|
|
sourceText: richTextToString(comment.content),
|
2022-07-15 12:52:08 +00:00
|
|
|
sourceSlug,
|
|
|
|
sourceTitle: `${group.name}`,
|
|
|
|
isSeenOnHref: sourceSlug,
|
|
|
|
}
|
|
|
|
await notificationRef.set(removeUndefinedProps(notification))
|
|
|
|
}
|
2022-07-18 16:40:44 +00:00
|
|
|
|
|
|
|
export const createReferralNotification = async (
|
|
|
|
toUser: User,
|
|
|
|
referredUser: User,
|
|
|
|
idempotencyKey: string,
|
|
|
|
bonusAmount: string,
|
|
|
|
referredByContract?: Contract,
|
|
|
|
referredByGroup?: Group
|
|
|
|
) => {
|
|
|
|
const notificationRef = firestore
|
|
|
|
.collection(`/users/${toUser.id}/notifications`)
|
|
|
|
.doc(idempotencyKey)
|
|
|
|
const notification: Notification = {
|
|
|
|
id: idempotencyKey,
|
|
|
|
userId: toUser.id,
|
|
|
|
reason: referredByGroup
|
|
|
|
? 'user_joined_from_your_group_invite'
|
|
|
|
: referredByContract?.creatorId === toUser.id
|
|
|
|
? 'user_joined_to_bet_on_your_market'
|
|
|
|
: 'you_referred_user',
|
|
|
|
createdTime: Date.now(),
|
|
|
|
isSeen: false,
|
|
|
|
sourceId: referredUser.id,
|
|
|
|
sourceType: 'user',
|
|
|
|
sourceUpdateType: 'updated',
|
|
|
|
sourceContractId: referredByContract?.id,
|
|
|
|
sourceUserName: referredUser.name,
|
|
|
|
sourceUserUsername: referredUser.username,
|
|
|
|
sourceUserAvatarUrl: referredUser.avatarUrl,
|
|
|
|
sourceText: bonusAmount,
|
|
|
|
// Only pass the contract referral details if they weren't referred to a group
|
|
|
|
sourceContractCreatorUsername: !referredByGroup
|
|
|
|
? referredByContract?.creatorUsername
|
|
|
|
: undefined,
|
|
|
|
sourceContractTitle: !referredByGroup
|
|
|
|
? referredByContract?.question
|
|
|
|
: undefined,
|
|
|
|
sourceContractSlug: !referredByGroup ? referredByContract?.slug : undefined,
|
|
|
|
sourceSlug: referredByGroup
|
|
|
|
? groupPath(referredByGroup.slug)
|
|
|
|
: referredByContract?.slug,
|
|
|
|
sourceTitle: referredByGroup
|
|
|
|
? referredByGroup.name
|
|
|
|
: referredByContract?.question,
|
|
|
|
}
|
|
|
|
await notificationRef.set(removeUndefinedProps(notification))
|
|
|
|
}
|
|
|
|
|
|
|
|
const groupPath = (groupSlug: string) => `/group/${groupSlug}`
|
2022-08-04 21:27:02 +00:00
|
|
|
|
|
|
|
export const createChallengeAcceptedNotification = async (
|
|
|
|
challenger: User,
|
|
|
|
challengeCreator: User,
|
|
|
|
challenge: Challenge,
|
|
|
|
acceptedAmount: number,
|
|
|
|
contract: Contract
|
|
|
|
) => {
|
|
|
|
const notificationRef = firestore
|
|
|
|
.collection(`/users/${challengeCreator.id}/notifications`)
|
|
|
|
.doc()
|
|
|
|
const notification: Notification = {
|
|
|
|
id: notificationRef.id,
|
|
|
|
userId: challengeCreator.id,
|
|
|
|
reason: 'challenge_accepted',
|
|
|
|
createdTime: Date.now(),
|
|
|
|
isSeen: false,
|
|
|
|
sourceId: challenge.slug,
|
|
|
|
sourceType: 'challenge',
|
|
|
|
sourceUpdateType: 'updated',
|
|
|
|
sourceUserName: challenger.name,
|
|
|
|
sourceUserUsername: challenger.username,
|
|
|
|
sourceUserAvatarUrl: challenger.avatarUrl,
|
|
|
|
sourceText: acceptedAmount.toString(),
|
|
|
|
sourceContractCreatorUsername: contract.creatorUsername,
|
|
|
|
sourceContractTitle: contract.question,
|
|
|
|
sourceContractSlug: contract.slug,
|
|
|
|
sourceContractId: contract.id,
|
|
|
|
sourceSlug: `/challenges/${challengeCreator.username}/${challenge.contractSlug}/${challenge.slug}`,
|
|
|
|
}
|
|
|
|
return await notificationRef.set(removeUndefinedProps(notification))
|
|
|
|
}
|