-
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: random projects as cloud function
- Loading branch information
Showing
16 changed files
with
244 additions
and
180 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import admin from "firebase-admin"; | ||
import { onCall } from "firebase-functions/v2/https"; | ||
import { log } from "firebase-functions/logger"; | ||
import { shuffle } from "lodash"; | ||
|
||
let lastUpdate = Date.now(); | ||
let projects: any[] = []; | ||
|
||
export const randomProjects = onCall<{ count: number }>(async ({ data }) => { | ||
if (projects.length === 0 || Date.now() - lastUpdate > 1000 * 60 * 5) { | ||
lastUpdate = Date.now(); | ||
const db = admin.firestore(); | ||
const projectsCollection = await db | ||
.collection("projects") | ||
.where("public", "==", true) | ||
.orderBy("createdAt", "desc") | ||
.limit(100) | ||
.get(); | ||
projects = projectsCollection.docs.map((doc) => ({ | ||
...doc.data(), | ||
projectUid: doc.id | ||
})); | ||
log("projectsLength: " + projects.length); | ||
} | ||
|
||
return shuffle(projects).slice(0, data.count); | ||
}); |
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
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.