-
-
Notifications
You must be signed in to change notification settings - Fork 120
Add activity tracking system for backend events #170
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Shweta-281
wants to merge
3
commits into
AOSSIE-Org:main
Choose a base branch
from
Shweta-281:feature/activity-tracking
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,66 +1,106 @@ | ||
| import AppwriteService from "./appwrite.js"; | ||
| import LivekitService from "./livekit.js"; | ||
| import { throwIfMissing } from "./utils.js"; | ||
| import { throwIfMissing, parseBody } from "./utils.js"; | ||
|
|
||
| export default async ({ req, res, log, error }) => { | ||
| throwIfMissing(process.env, [ | ||
| "APPWRITE_API_KEY", | ||
| "MASTER_DATABASE_ID", | ||
| "ROOMS_COLLECTION_ID", | ||
| "LIVEKIT_HOST", | ||
| "LIVEKIT_API_KEY", | ||
| "LIVEKIT_API_SECRET", | ||
| "LIVEKIT_SOCKET_URL", | ||
| ]); | ||
| // Validate environment variables | ||
| try { | ||
| throwIfMissing(process.env, [ | ||
| "APPWRITE_API_KEY", | ||
| "MASTER_DATABASE_ID", | ||
| "ROOMS_COLLECTION_ID", | ||
| "LIVEKIT_HOST", | ||
| "LIVEKIT_API_KEY", | ||
| "LIVEKIT_API_SECRET", | ||
| "LIVEKIT_SOCKET_URL", | ||
| ]); | ||
| } catch (err) { | ||
| error("[ENV_ERROR] " + err.message); | ||
| return res.json({ | ||
| success: false, | ||
| message: err.message, | ||
| }, 500); | ||
| } | ||
|
|
||
| const appwrite = new AppwriteService(); | ||
| const livekit = new LivekitService(); | ||
|
|
||
| let data; | ||
|
|
||
| // ✅ Safe parsing + validation | ||
| try { | ||
| throwIfMissing(JSON.parse(req.body), ["name", "adminUid", "tags"]); | ||
| data = parseBody(req.body); | ||
| throwIfMissing(data, ["name", "adminUid", "tags"]); | ||
| } catch (err) { | ||
| error(err.message); | ||
| return res.json({ msg: err.message }, 400); | ||
| error("[VALIDATION_ERROR] " + err.message); | ||
| return res.json({ | ||
| success: false, | ||
| message: err.message, | ||
| }, 400); | ||
| } | ||
|
|
||
| const { name, description = "", adminUid, tags } = data; | ||
|
|
||
| try { | ||
| log(req); | ||
| const { name, description, adminUid, tags } = JSON.parse(req.body); | ||
| log("[CREATE_ROOM_REQUEST]", { name, adminUid, tags }); | ||
|
|
||
| // create a new room on appwrite | ||
| // Create room in Appwrite | ||
| const newRoomdata = { | ||
| name, | ||
| description, | ||
| adminUid, | ||
| tags, | ||
| totalParticipants: 1, | ||
| }; | ||
|
|
||
| const appwriteRoomId = await appwrite.createRoom(newRoomdata); | ||
| log(appwriteRoomId); | ||
| log("[APPWRITE_ROOM_CREATED]", appwriteRoomId); | ||
|
|
||
| // create a new livekit room | ||
| // Create room in LiveKit | ||
| const livekitRoomOptions = { | ||
| name: appwriteRoomId, // using appwrite room doc id as livekit room name | ||
| emptyTimeout: 300, // timeout in seconds | ||
| name: appwriteRoomId, | ||
| emptyTimeout: 300, | ||
| }; | ||
|
|
||
| const livekitRoom = await livekit.createRoom(livekitRoomOptions); | ||
| log(livekitRoom); | ||
| log("[LIVEKIT_ROOM_CREATED]", livekitRoom); | ||
|
|
||
| // Creating a token for the admin | ||
| // Generate token for admin | ||
| const accessToken = livekit.generateToken( | ||
| appwriteRoomId, | ||
| adminUid, | ||
| true | ||
| ); | ||
|
|
||
| // Add track-activity | ||
| await fetch("http://localhost/track-activity", { | ||
| method: "POST", | ||
| body: JSON.stringify({ | ||
| eventType: "ROOM_CREATED", | ||
| userId: adminUid, | ||
| metadata: { | ||
| roomId: appwriteRoomId, | ||
| }, | ||
| }), | ||
| }); | ||
|
|
||
| return res.json({ | ||
| msg: "Room created Successfully", | ||
| livekit_room: livekitRoom, | ||
| livekit_socket_url: `${process.env.LIVEKIT_SOCKET_URL}`, | ||
| access_token: accessToken, | ||
| success: true, | ||
| message: "Room created successfully", | ||
| data: { | ||
| roomId: appwriteRoomId, | ||
| livekit_room: livekitRoom, | ||
| livekit_socket_url: process.env.LIVEKIT_SOCKET_URL, | ||
| access_token: accessToken, | ||
| }, | ||
| }); | ||
| } catch (e) { | ||
| error(String(e)); | ||
| return res.json({ msg: "Room creation failed" }, 500); | ||
| error("[CREATE_ROOM_ERROR] " + String(e)); | ||
|
|
||
| return res.json({ | ||
| success: false, | ||
| message: "Room creation failed", | ||
| }, 500); | ||
| } | ||
|
|
||
| }; | ||
This file contains hidden or 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,11 +1,25 @@ | ||
| export const throwIfMissing = (obj, keys) => { | ||
| const missing = []; | ||
|
|
||
| for (let key of keys) { | ||
| if (!(key in obj) || !obj[key]) { | ||
| if (!(key in obj) || obj[key] === undefined || obj[key] === null) { | ||
| missing.push(key); | ||
| } | ||
| } | ||
|
|
||
| if (missing.length > 0) { | ||
| throw new Error(`Missing required fields: ${missing.join(", ")}`); | ||
| } | ||
| }; | ||
|
|
||
| export const parseBody = (body) => { | ||
| if (!body) { | ||
| throw new Error("Request body is empty"); | ||
| } | ||
|
|
||
| try { | ||
| return JSON.parse(body); | ||
| } catch (err) { | ||
| throw new Error("Invalid JSON body"); | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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,18 +1,32 @@ | ||
| export const throwIfMissing = (obj, keys) => { | ||
| const missing = []; | ||
|
|
||
| for (let key of keys) { | ||
| if (!(key in obj) || (!obj[key] && obj[key] != 0)) { | ||
| if (!(key in obj) || obj[key] === undefined || obj[key] === null) { | ||
| missing.push(key); | ||
| } | ||
| } | ||
|
|
||
| if (missing.length > 0) { | ||
| throw new Error(`Missing required fields: ${missing.join(", ")}`); | ||
| } | ||
| }; | ||
|
|
||
| export const parseBody = (body) => { | ||
| if (!body) { | ||
| throw new Error("Request body is empty"); | ||
| } | ||
|
|
||
| try { | ||
| return JSON.parse(body); | ||
| } catch (err) { | ||
| throw new Error("Invalid JSON body"); | ||
| } | ||
| }; | ||
|
|
||
| export const getExpiryDate = () => { | ||
| const retentionPeriod = +(process.env.RETENTION_PERIOD_DAYS ?? 1); | ||
| const expiryDate = new Date(); | ||
| expiryDate.setDate(expiryDate.getDate() - retentionPeriod); | ||
| return expiryDate.toISOString(); | ||
| }; | ||
| }; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.