-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(functions): update and migrate outdated firebase functions package
- Loading branch information
Showing
10 changed files
with
904 additions
and
651 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,40 @@ | ||
import * as admin from "firebase-admin"; | ||
import * as functions from "firebase-functions"; | ||
import { onDocumentWritten } from "firebase-functions/v2/firestore"; | ||
import { makeLogger } from "./logger.js"; | ||
|
||
admin.initializeApp(); | ||
const log = makeLogger("followersCounter"); | ||
|
||
export const followersCounter = functions.firestore | ||
.document("followers/{userUid}") | ||
.onWrite(async (change, context) => { | ||
const userUid = context.params.userUid; | ||
export const followersCounter = onDocumentWritten( | ||
"followers/{userUid}", | ||
async (event) => { | ||
const before = event.data.before; | ||
const after = event.data.after; | ||
const userUid = event.params.userUid; | ||
|
||
const followersCountRef = admin | ||
.firestore() | ||
.collection("followersCount") | ||
.doc(userUid); | ||
|
||
if (!change.before.exists) { | ||
// Document created | ||
if (!before.exists && after.exists) { | ||
// New followers doc created = 1 new follower | ||
await followersCountRef.set({ followersCount: 1 }); | ||
} else if (change.before.exists && change.after.exists) { | ||
// Follower count changed | ||
const dataAfter = change.after.data(); | ||
} | ||
// Document updated | ||
else if (before.exists && after.exists) { | ||
const dataAfter = after.data(); | ||
await followersCountRef.set({ | ||
followersCount: Object.keys(dataAfter || {}).length | ||
}); | ||
} else if (!change.after.exists) { | ||
// No followers left, do nothing | ||
} | ||
// Document deleted | ||
else if (!after.exists) { | ||
// No followers left, do nothing (or you could remove the doc) | ||
// await followersCountRef.delete(); | ||
} | ||
|
||
return; | ||
}); | ||
} | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,37 @@ | ||
import * as admin from "firebase-admin"; | ||
import * as functions from "firebase-functions"; | ||
import { onDocumentWritten } from "firebase-functions/v2/firestore"; | ||
import { makeLogger } from "./logger.js"; | ||
|
||
admin.initializeApp(); | ||
const log = makeLogger("followersCounter"); | ||
|
||
export const followingCounter = functions.firestore | ||
.document("following/{userUid}") | ||
.onWrite(async (change, context) => { | ||
const userUid = context.params.userUid; | ||
export const followingCounter = onDocumentWritten( | ||
"following/{userUid}", | ||
async (event) => { | ||
const before = event.data.before; | ||
const after = event.data.after; | ||
const userUid = event.params.userUid; | ||
|
||
const followingCountRef = admin | ||
.firestore() | ||
.collection("followingCount") | ||
.doc(userUid); | ||
|
||
if (!change.before.exists) { | ||
if (!before.exists && after.exists) { | ||
// First time following = 1 following | ||
await followingCountRef.set({ followingCount: 1 }); | ||
} else if (change.before.exists && change.after.exists) { | ||
} else if (before.exists && after.exists) { | ||
// Following count changed | ||
const dataAfter = change.after.data(); | ||
const dataAfter = after.data(); | ||
await followingCountRef.set({ | ||
followingCount: Object.keys(dataAfter || {}).length | ||
}); | ||
} else if (!change.after.exists) { | ||
// Not following anyone anymore, do nothing | ||
} else if (!after.exists) { | ||
// No longer following anyone | ||
// You can leave it as is, or optionally remove the doc | ||
// await followingCountRef.delete(); | ||
} | ||
|
||
return; | ||
}); | ||
} | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.