Skip to content

Commit

Permalink
feat: popular projects (wip)
Browse files Browse the repository at this point in the history
  • Loading branch information
hlolli committed Dec 15, 2024
1 parent f47f798 commit 98352fa
Show file tree
Hide file tree
Showing 21 changed files with 291 additions and 188 deletions.
1 change: 1 addition & 0 deletions functions/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules/
index.html
dist
_local
3 changes: 2 additions & 1 deletion functions/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
"@types/lodash-es": "^4.17.12",
"eslint": "^9.16.0",
"eslint-plugin-promise": "^7.2.1",
"firebase-functions-test": "^3.3.0"
"firebase-functions-test": "^3.3.0",
"tsx": "^4.19.2"
},
"private": true
}
6 changes: 1 addition & 5 deletions functions/src/add_project_file_on_storage_upload.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
import admin from "firebase-admin";
import { initializeApp } from "firebase-admin/app";
import { onObjectFinalized } from "firebase-functions/v2/storage";
import { makeLogger } from "./logger.js";
import { log } from "firebase-functions/logger";

const functionName = "addProjectFileOnStorageUpload";
initializeApp(undefined, functionName);
const log = makeLogger(functionName);
const newTimestamp = admin.firestore.FieldValue.serverTimestamp();

export const addProjectFileOnStorageUploadCallback = onObjectFinalized(
Expand Down
7 changes: 1 addition & 6 deletions functions/src/delete_user.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
import admin from "firebase-admin";
import { initializeApp } from "firebase-admin/app";
import functions from "firebase-functions/v1";
import { makeLogger } from "./logger.js";

const functionName = "deleteUser";
initializeApp(undefined, functionName);
const log = makeLogger(functionName);
import { log } from "firebase-functions/logger";

const deleteUserDocument = async (
user: admin.auth.UserRecord
Expand Down
6 changes: 0 additions & 6 deletions functions/src/followers_counter.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
import admin from "firebase-admin";
import { initializeApp } from "firebase-admin/app";
import { onDocumentWritten } from "firebase-functions/v2/firestore";
import { makeLogger } from "./logger.js";

const functionName = "followersCounter";
initializeApp(undefined, functionName);
const log = makeLogger("followersCounter");

export const followersCounter = onDocumentWritten(
"followers/{userUid}",
Expand Down
7 changes: 1 addition & 6 deletions functions/src/following_counter.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
import admin from "firebase-admin";
import { initializeApp } from "firebase-admin/app";
import { onDocumentWritten } from "firebase-functions/v2/firestore";
import { makeLogger } from "./logger.js";

const functionName = "followingCounter";
initializeApp(undefined, functionName);
const log = makeLogger(functionName);
import { log } from "firebase-functions/logger";

export const followingCounter = onDocumentWritten(
"following/{userUid}",
Expand Down
4 changes: 0 additions & 4 deletions functions/src/host.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
import admin from "firebase-admin";
import { initializeApp } from "firebase-admin/app";
import { onRequest } from "firebase-functions/v2/https";
import { isbot } from "isbot";
import fs from "node:fs";
import path from "node:path";
import * as R from "ramda";

const functionName = "host";
initializeApp(undefined, functionName);

function printTree(dirPath: string, indent = "") {
const files = fs.readdirSync(dirPath);

Expand Down
29 changes: 0 additions & 29 deletions functions/src/logger.ts

This file was deleted.

1 change: 1 addition & 0 deletions functions/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ export { newUserCallback as new_user_callback } from "./new_user.js";
export { projectFileStorageDeleteCallback as project_file_storage_delete_callback } from "./project_file_storage_delete.js";
export { projectsCounter as projects_counter } from "./projects_counter.js";
export { randomProjects as random_projects } from "./random_projects.js";
export { popularProjects as popular_projects } from "./popular_projects.js";
7 changes: 1 addition & 6 deletions functions/src/new_user.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
import { WriteResult } from "@google-cloud/firestore";
import { Timestamp } from "firebase-admin/firestore";
import admin from "firebase-admin";
import { initializeApp } from "firebase-admin/app";
import functions from "firebase-functions/v1";
import { makeLogger } from "./logger.js";

const functionName = "newUser";
initializeApp(undefined, functionName);
const log = makeLogger(functionName);
import { log } from "firebase-functions/logger";

async function createProfileDocument(
user: admin.auth.UserRecord
Expand Down
22 changes: 22 additions & 0 deletions functions/src/popular_projects.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import admin from "firebase-admin";
import { onCall } from "firebase-functions/v2/https";
import { log } from "firebase-functions/logger";
import { shuffle } from "lodash-es";

let lastUpdate = Date.now();
let projects: any[] = [];

export const popularProjects = onCall<{ count: number }>(async ({ data }) => {
if (projects.length === 0 || Date.now() - lastUpdate > 1000 * 60 * 5) {
lastUpdate = Date.now();
const db = admin.firestore();
const randomStars = await db.collection("stars").limit(200).get();
// log("randomStarsLength: " + randomStars.docs.length);
const projectUids = randomStars.docs.map((doc) => doc.id);
// log("projectUids: " + JSON.stringify(projectUids, null, 2));
// log("projectsLength: " + projects.length);
projects = projectUids;
}

return shuffle(projects).slice(0, data.count);
});
7 changes: 1 addition & 6 deletions functions/src/project_file_storage_delete.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
import admin from "firebase-admin";
import { initializeApp } from "firebase-admin/app";
import { onDocumentDeleted } from "firebase-functions/v2/firestore";
import { config } from "firebase-functions/v1";
import { makeLogger } from "./logger.js";

const functionName = "projectFileStorageDelete";
initializeApp(undefined, functionName);
const log = makeLogger(functionName);
import { log } from "firebase-functions/logger";

async function projectFileStorageDelete(binaryUrl: string): Promise<void> {
log(
Expand Down
7 changes: 1 addition & 6 deletions functions/src/projects_counter.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
import admin from "firebase-admin";
import { initializeApp } from "firebase-admin/app";
import { FieldValue } from "firebase-admin/firestore";
import { onDocumentWritten } from "firebase-functions/v2/firestore";
import { makeLogger } from "./logger.js";

const functionName = "projectsCounter";
initializeApp(undefined, functionName);
const log = makeLogger(functionName);
import { log } from "firebase-functions/logger";

export const projectsCounter = onDocumentWritten(
"projects/{projectUid}",
Expand Down
Loading

0 comments on commit 98352fa

Please sign in to comment.