-
-
Notifications
You must be signed in to change notification settings - Fork 56
Birmingham | 26-SDC-Mar | Chioma Okeke | Sprint 2 | Chat App #84
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
JanefrancessC
wants to merge
8
commits into
CodeYourFuture:main
Choose a base branch
from
JanefrancessC:sprint2-chat-app
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
8 commits
Select commit
Hold shift + click to select a range
90e2c3b
feat: Implement basic chat-app concept
JanefrancessC 1518e8b
feat: Implement like and dislike buttons logic
JanefrancessC c310b9c
chore: cleanup code
JanefrancessC 9b80f19
fix: Remove dotenv since not used
JanefrancessC 85ad07e
fix: Modify endpoints to accomodate like feature
JanefrancessC b393959
fix: edit URL
JanefrancessC 3627170
fix: Refactor code to adjust review suggestions
JanefrancessC 82ee927
fix: Trim client input on the server-side
JanefrancessC 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 |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| node_modules/ | ||
| .DS_Store |
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 |
|---|---|---|
| @@ -0,0 +1,114 @@ | ||
| import express from "express"; | ||
| import cors from "cors"; | ||
|
|
||
| const port = process.env.PORT || 3000; | ||
| // Auto-increment IDS | ||
| let nextMessageId = 1; | ||
| // In-memory message store, shared by all clients | ||
| const messages = []; | ||
| // Stores pending longPoll requests waiting for new messages | ||
| const callbacksForNewMessages = []; | ||
|
|
||
| // App setup | ||
| const app = express(); | ||
| app.use(cors()); | ||
| app.use(express.json()); | ||
|
|
||
| // Routes | ||
| /** | ||
| * GET / | ||
| * Returns messages to client | ||
| * | ||
| * Query params: | ||
| * since - timestamps (ms). If provided, only messages newer than this are returned | ||
| * longPoll - If true and there are no new messages, holds the connection open | ||
| * until there are new messages instead of returning empty array. | ||
| */ | ||
| app.get("/", (req, res) => { | ||
| let since = Number(req.query.since); | ||
| let longPoll = req.query.longPoll === "true"; | ||
|
|
||
| if (since) { | ||
| const messagesToSend = messages.filter((msg) => msg.time > since); | ||
|
|
||
| // callback until a new message is posted | ||
| // No new messages and client wants long-polling - park the response | ||
| if (messagesToSend.length === 0 && longPoll) { | ||
| callbacksForNewMessages.push((val) => res.json(val)); | ||
| return; | ||
| } | ||
| res.json(messagesToSend); | ||
| return; | ||
| } | ||
| // No "since" param, return the full message history | ||
| res.json(messages); | ||
| }); | ||
|
|
||
| /** | ||
| * POST / | ||
| * Accepts a new message from a client. | ||
| * Trims whitespace from user and message before saving so that | ||
| * values like " hello " are stored as "hello". | ||
| */ | ||
| app.post("/", (req, res) => { | ||
| const { message, user } = req.body; | ||
|
cjyuan marked this conversation as resolved.
|
||
|
|
||
| if ( | ||
| typeof message !== "string" || | ||
| typeof user !== "string" || | ||
| !message.trim() || | ||
| !user.trim() | ||
| ) { | ||
| res.status(400).json({ error: `Message and user are required!` }); | ||
| return; | ||
| } | ||
|
|
||
| const newMessage = { | ||
| id: nextMessageId++, | ||
| message: message.trim(), | ||
| user: user.trim(), | ||
| time: Date.now(), | ||
| likes: 0, | ||
| dislikes: 0, | ||
| }; | ||
|
|
||
| messages.push(newMessage); | ||
|
|
||
| // Resolve all parked long-poll clients with new messages | ||
| while (callbacksForNewMessages.length > 0) { | ||
| const callback = callbacksForNewMessages.pop(); | ||
| callback([newMessage]); | ||
| } | ||
| res.status(201).json({ success: true }); | ||
| }); | ||
|
|
||
| /** | ||
| * POST /:id/:reaction | ||
| * Increments the like or dislike count on a message. | ||
| * Returns the updated message object. | ||
| */ | ||
| app.post("/:id/:reaction", (req, res) => { | ||
| const id = Number(req.params.id); | ||
| const reaction = req.params.reaction; | ||
|
|
||
| const message = messages.find((msg) => msg.id === id); | ||
| if (!message) { | ||
| res.status(404).json({ error: "Message not found" }); | ||
| return; | ||
| } | ||
|
|
||
| if (reaction === "like") { | ||
| message.likes++; | ||
| } else if (reaction === "dislike") { | ||
| message.dislikes++; | ||
| } else { | ||
| res.status(400).json({ error: "Invalid reaction!" }); | ||
| return; | ||
| } | ||
| res.json(message); | ||
| }); | ||
|
Comment on lines
+90
to
+109
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could consider rearrange the code this way to prevent unnecessary operations from being performed when input is invalid: |
||
|
|
||
| // start the server | ||
| app.listen(port, () => { | ||
| console.log(`Chat server listening on port ${port}`); | ||
| }); | ||
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I missed this in my previous review. Could you use an AI to find out