From 8402ceb916afb3a41e27f5d170e7dd85a4c92eee Mon Sep 17 00:00:00 2001 From: Ian Philips Date: Wed, 24 Aug 2022 08:59:40 -0600 Subject: [PATCH] Add user to followers in place-bet and sell-shares --- functions/src/follow-market.ts | 39 ++++++++++++++----- functions/src/on-create-answer.ts | 2 +- functions/src/on-create-bet.ts | 2 +- .../src/on-create-comment-on-contract.ts | 2 +- functions/src/on-create-contract.ts | 2 +- .../src/on-create-liquidity-provision.ts | 2 +- functions/src/place-bet.ts | 3 ++ functions/src/sell-shares.ts | 6 ++- 8 files changed, 42 insertions(+), 16 deletions(-) diff --git a/functions/src/follow-market.ts b/functions/src/follow-market.ts index 1dc2941b..597b7d71 100644 --- a/functions/src/follow-market.ts +++ b/functions/src/follow-market.ts @@ -1,30 +1,49 @@ -import { Contract } from '../../common/lib/contract' -import { User } from '../../common/lib/user' import * as admin from 'firebase-admin' import { FieldValue } from 'firebase-admin/firestore' const firestore = admin.firestore() export const addUserToContractFollowers = async ( - contract: Contract, - user: User + contractId: string, + userId: string ) => { const followerDoc = await firestore - .collection(`contracts/${contract.id}/follows`) - .doc(user.id) + .collection(`contracts/${contractId}/follows`) + .doc(userId) .get() if (followerDoc.exists) return await firestore - .collection(`contracts/${contract.id}/follows`) - .doc(user.id) + .collection(`contracts/${contractId}/follows`) + .doc(userId) .set({ - id: user.id, + id: userId, createdTime: Date.now(), }) await firestore .collection(`contracts`) - .doc(contract.id) + .doc(contractId) .update({ followerCount: FieldValue.increment(1), }) } + +export const removeUserFromContractFollowers = async ( + contractId: string, + userId: string +) => { + const followerDoc = await firestore + .collection(`contracts/${contractId}/follows`) + .doc(userId) + .get() + if (!followerDoc.exists) return + await firestore + .collection(`contracts/${contractId}/follows`) + .doc(userId) + .delete() + await firestore + .collection(`contracts`) + .doc(contractId) + .update({ + followerCount: FieldValue.increment(-1), + }) +} diff --git a/functions/src/on-create-answer.ts b/functions/src/on-create-answer.ts index d3e3e1eb..1f08ca16 100644 --- a/functions/src/on-create-answer.ts +++ b/functions/src/on-create-answer.ts @@ -21,7 +21,7 @@ export const onCreateAnswer = functions.firestore const answerCreator = await getUser(answer.userId) if (!answerCreator) throw new Error('Could not find answer creator') - await addUserToContractFollowers(contract, answerCreator) + await addUserToContractFollowers(contract.id, answerCreator.id) await createCommentOrAnswerOrUpdatedContractNotification( answer.id, 'answer', diff --git a/functions/src/on-create-bet.ts b/functions/src/on-create-bet.ts index 767add88..6170dc5c 100644 --- a/functions/src/on-create-bet.ts +++ b/functions/src/on-create-bet.ts @@ -58,7 +58,7 @@ export const onCreateBet = functions.firestore const bettor = await getUser(bet.userId) if (!bettor) return - await addUserToContractFollowers(contract, bettor) + await addUserToContractFollowers(contract.id, bettor.id) await updateUniqueBettorsAndGiveCreatorBonus(contract, eventId, bet.userId) await notifyFills(bet, contract, eventId, bettor) await updateBettingStreak(bettor, bet, contract, eventId) diff --git a/functions/src/on-create-comment-on-contract.ts b/functions/src/on-create-comment-on-contract.ts index a44487cc..8651bde0 100644 --- a/functions/src/on-create-comment-on-contract.ts +++ b/functions/src/on-create-comment-on-contract.ts @@ -36,7 +36,7 @@ export const onCreateCommentOnContract = functions const commentCreator = await getUser(comment.userId) if (!commentCreator) throw new Error('Could not find comment creator') - await addUserToContractFollowers(contract, commentCreator) + await addUserToContractFollowers(contract.id, commentCreator.id) await firestore .collection('contracts') diff --git a/functions/src/on-create-contract.ts b/functions/src/on-create-contract.ts index e5a5bb51..d9826f6c 100644 --- a/functions/src/on-create-contract.ts +++ b/functions/src/on-create-contract.ts @@ -19,7 +19,7 @@ export const onCreateContract = functions const desc = contract.description as JSONContent const mentioned = parseMentions(desc) - await addUserToContractFollowers(contract, contractCreator) + await addUserToContractFollowers(contract.id, contractCreator.id) await createNotification( contract.id, diff --git a/functions/src/on-create-liquidity-provision.ts b/functions/src/on-create-liquidity-provision.ts index 0473fb68..56a01bbb 100644 --- a/functions/src/on-create-liquidity-provision.ts +++ b/functions/src/on-create-liquidity-provision.ts @@ -19,7 +19,7 @@ export const onCreateLiquidityProvision = functions.firestore const liquidityProvider = await getUser(liquidity.userId) if (!liquidityProvider) throw new Error('Could not find liquidity provider') - await addUserToContractFollowers(contract, liquidityProvider) + await addUserToContractFollowers(contract.id, liquidityProvider.id) await createNotification( contract.id, diff --git a/functions/src/place-bet.ts b/functions/src/place-bet.ts index 44a96210..237019a4 100644 --- a/functions/src/place-bet.ts +++ b/functions/src/place-bet.ts @@ -22,6 +22,7 @@ import { LimitBet } from '../../common/bet' import { floatingEqual } from '../../common/util/math' import { redeemShares } from './redeem-shares' import { log } from './utils' +import { addUserToContractFollowers } from 'functions/src/follow-market' const bodySchema = z.object({ contractId: z.string(), @@ -167,6 +168,8 @@ export const placebet = newEndpoint({}, async (req, auth) => { return { betId: betDoc.id, makers, newBet } }) + await addUserToContractFollowers(contractId, auth.uid) + log('Main transaction finished.') if (result.newBet.amount !== 0) { diff --git a/functions/src/sell-shares.ts b/functions/src/sell-shares.ts index d9f99de3..0e669f39 100644 --- a/functions/src/sell-shares.ts +++ b/functions/src/sell-shares.ts @@ -13,6 +13,7 @@ import { floatingEqual, floatingLesserEqual } from '../../common/util/math' import { getUnfilledBetsQuery, updateMakers } from './place-bet' import { FieldValue } from 'firebase-admin/firestore' import { redeemShares } from './redeem-shares' +import { removeUserFromContractFollowers } from 'functions/src/follow-market' const bodySchema = z.object({ contractId: z.string(), @@ -123,9 +124,12 @@ export const sellshares = newEndpoint({}, async (req, auth) => { }) ) - return { newBet, makers } + return { newBet, makers, maxShares, soldShares } }) + if (result.maxShares === result.soldShares) { + await removeUserFromContractFollowers(contractId, auth.uid) + } const userIds = uniq(result.makers.map((maker) => maker.bet.userId)) await Promise.all(userIds.map((userId) => redeemShares(userId, contractId))) log('Share redemption transaction finished.')