manifold/functions/src/on-unfollow-user.ts
Ben Congdon 67d0a6c0c2
Create Top Followed Users leaderboard (#531)
* Create Top Followed Users leaderboard

* Switch to increment/decrement approach for caching user follower counts

* Backfill script for user follow counts

* Appease ESLint

* Address review comment

Co-authored-by: James Grugett <jahooma@gmail.com>
2022-06-22 11:05:54 -05:00

19 lines
539 B
TypeScript

import * as functions from 'firebase-functions'
import * as admin from 'firebase-admin'
import { FieldValue } from 'firebase-admin/firestore'
export const onUnfollowUser = functions.firestore
.document('users/{userId}/follows/{followedUserId}')
.onDelete(async (change, context) => {
const { followedUserId } = context.params as {
followedUserId: string
}
await firestore.doc(`users/${followedUserId}`).update({
followerCountCached: FieldValue.increment(-1),
})
})
const firestore = admin.firestore()