Skip to content
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

Remove q #1564

Open
wants to merge 3 commits into
base: dev
Choose a base branch
from
Open

Remove q #1564

wants to merge 3 commits into from

Conversation

PatrickTle924
Copy link

@PatrickTle924 PatrickTle924 commented Jan 15, 2025

Followed the instructions in setting up the Messaging Component and the Form to send the username in the request. The request is then passed through and used in the endpoint to avoid querying the DB if user is not using an APIKey. Apologies if my Prettier messed up the formatting.

concering issue #1555

@PatrickTle924 PatrickTle924 self-assigned this Jan 15, 2025
Comment on lines 1 to 36
const { UNAUTHORIZED, BAD_REQUEST, SERVER_ERROR, OK } =
require('../../util/constants').STATUS_CODES;
const { MAX_AMOUNT_OF_CONNECTIONS } =
require('../../util/constants').MESSAGES_API;
const express = require('express');
const router = express.Router();
const bodyParser = require('body-parser');
const User = require('../models/User.js');
const logger = require('../../util/logger');
const client = require('prom-client');
const { decodeToken, decodeTokenFromBodyOrQuery } = require('../util/token-functions.js');
const {
decodeToken,
decodeTokenFromBodyOrQuery,
} = require('../util/token-functions.js');
const { MetricsHandler, register } = require('../../util/metrics.js');


router.use(bodyParser.json());



const clients = {};
const numberOfConnections = {};
const lastMessageSent = {};

const writeMessage = ((roomId, message, username) => {

const writeMessage = (roomId, message, username) => {
const messageObj = {
timestamp: Date.now(),
message,
username
username,
};

if (clients[roomId]) {
clients[roomId].forEach(res => res.write(`data: ${JSON.stringify(messageObj)}\n\n`));
clients[roomId].forEach((res) =>
res.write(`data: ${JSON.stringify(messageObj)}\n\n`)
);
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there are a bunch of random changes in this file. lets disable prettier on vscode, and undo these changes

the only diff in this file should be the logic to remove the mongodb query

@PatrickTle924
Copy link
Author

Sorry about the Prettier formatting issues. Looking over the change log I can see it caused a ton of unnecessary changes that probably makes reviewing the PR pretty tough. I rolled back the commits, disabled Prettier, and pushed a new commit that should have the changes cleaner, and without the random formatting changes.

});
} catch (error) {
logger.error('Error in /send: ', error);
res.sendStatus(SERVER_ERROR);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
res.sendStatus(SERVER_ERROR);
return res.sendStatus(SERVER_ERROR);

}
filterQuery._id = userObj._id;
} else {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can remove the entire else indentation, if we use return statements inside if (apiKey) {

let me know if this makes sense. it makes the code easier to follow

if (thing) {
  // stuff
  return idk;
}
// no more else block
// just the original code that wouldve been in the else ere

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Understood, removed the else block. I had to change the code to make the User.findOne() function call async since the code ended up sending multiple responses (I think it was running the code after the if (apiKey) block as it waited for the User.findOne() function?). Please let me know if that's okay or there was an alternative I should have done. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants