From ef8157025fb69cf4a8ee99727837e4c35e1f0d73 Mon Sep 17 00:00:00 2001 From: jahooma Date: Wed, 26 Jan 2022 14:27:13 -0600 Subject: [PATCH] Compute fold follower count from cloud function. --- common/fold.ts | 2 ++ functions/src/create-fold.ts | 3 ++ functions/src/index.ts | 1 + functions/src/on-fold-follow.ts | 17 +++++++++ web/pages/folds.tsx | 62 ++++++++++++++++++++------------- 5 files changed, 60 insertions(+), 25 deletions(-) create mode 100644 functions/src/on-fold-follow.ts diff --git a/common/fold.ts b/common/fold.ts index 46be7499..b91772a5 100644 --- a/common/fold.ts +++ b/common/fold.ts @@ -15,4 +15,6 @@ export type Fold = { // Default: creatorIds: undefined, excludedCreatorIds: [] creatorIds?: string[] excludedCreatorIds?: string[] + + followCount: number } diff --git a/functions/src/create-fold.ts b/functions/src/create-fold.ts index 368a6b03..5d65e3de 100644 --- a/functions/src/create-fold.ts +++ b/functions/src/create-fold.ts @@ -62,10 +62,13 @@ export const createFold = functions.runWith({ minInstances: 1 }).https.onCall( contractIds: [], excludedContractIds: [], excludedCreatorIds: [], + followCount: 0, } await foldRef.create(fold) + await foldRef.collection('followers').doc(userId).set({ userId }) + return { status: 'success', fold } } ) diff --git a/functions/src/index.ts b/functions/src/index.ts index f191a771..f14b4100 100644 --- a/functions/src/index.ts +++ b/functions/src/index.ts @@ -11,6 +11,7 @@ export * from './sell-bet' export * from './create-contract' export * from './create-user' export * from './create-fold' +export * from './on-fold-follow' export * from './unsubscribe' export * from './update-contract-metrics' export * from './update-user-metrics' diff --git a/functions/src/on-fold-follow.ts b/functions/src/on-fold-follow.ts new file mode 100644 index 00000000..4d72fcfb --- /dev/null +++ b/functions/src/on-fold-follow.ts @@ -0,0 +1,17 @@ +import * as functions from 'firebase-functions' +import * as admin from 'firebase-admin' + +const firestore = admin.firestore() + +export const onFoldFollow = functions.firestore + .document('folds/{foldId}/followers/{userId}') + .onWrite(async (change, context) => { + const { foldId } = context.params + + const snapshot = await firestore + .collection(`folds/${foldId}/followers`) + .get() + const followCount = snapshot.size + + await firestore.doc(`folds/${foldId}`).update({ followCount }) + }) diff --git a/web/pages/folds.tsx b/web/pages/folds.tsx index ac240def..eeda8435 100644 --- a/web/pages/folds.tsx +++ b/web/pages/folds.tsx @@ -41,7 +41,8 @@ export default function Folds(props: { }) { const [curatorsDict, setCuratorsDict] = useState(props.curatorsDict) - const folds = useFolds() ?? props.folds + let folds = useFolds() ?? props.folds + folds = _.sortBy(folds, (fold) => -1 * fold.followCount) const user = useUser() useEffect(() => { @@ -74,31 +75,11 @@ export default function Folds(props: { {folds.map((fold) => ( - - - - - - {fold.name} - - - -
12 followers
-
- -
Curated by
- -
-
-
{fold.about}
- + fold={fold} + curator={curatorsDict[fold.curatorId]} + /> ))} @@ -106,3 +87,34 @@ export default function Folds(props: { ) } + +function FoldCard(props: { fold: Fold; curator: User | undefined }) { + const { fold, curator } = props + return ( + + +
+ + + {fold.name} + + + +
{fold.followCount} followers
+
+ +
Curated by
+ +
+
+
{fold.about}
+ + ) +}