Skip to content

Commit

Permalink
Merge pull request #177 from SquareTable/171-remove-username-check-fr…
Browse files Browse the repository at this point in the history
…om-temppollpostcommentreply

Removed userName check from temp/pollpostcommentreply
  • Loading branch information
Sebastian-Webster authored Oct 10, 2023
2 parents 7682f1b + b63e451 commit 6386fc2
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 51 deletions.
92 changes: 42 additions & 50 deletions controllers/Temp.js
Original file line number Diff line number Diff line change
Expand Up @@ -733,16 +733,12 @@ class TempController {
})
}

static #pollpostcommentreply = (userId, comment, userName, postId, commentId) => {
static #pollpostcommentreply = (userId, comment, postId, commentId) => {
return new Promise(resolve => {
if (typeof comment !== 'string') {
return resolve(HTTPWTHandler.badInput(`comment must be a string. Provided type: ${typeof comment}`))
}

if (typeof userName !== 'string') {
return resolve(HTTPWTHandler.badInput(`userName must be a string. Provided type: ${typeof userName}`))
}

if (typeof postId !== 'string') {
return resolve(HTTPWTHandler.badInput(`postId must be a string. Provided type: ${typeof postId}`))
}
Expand All @@ -768,52 +764,48 @@ class TempController {
//Find User
User.findOne({_id: {$eq: userId}}).lean().then(result => {
if (result) {
if (result.name == userName) {
Poll.findOne({_id: {$eq: postId}}).lean().then(data => {
if (data) {
var comments = data.comments
async function findThreads(sentIndex) {
var objectId = new mongoose.Types.ObjectId()
console.log(objectId)
var commentForPost = {commentId: objectId, commenterId: userId, commentsText: comment, commentUpVotes: [], commentDownVotes: [], datePosted: Date.now()}
Poll.findOneAndUpdate({_id: {$eq: postId}}, { $push: { [`comments.${sentIndex}.commentReplies`]: commentForPost } }).then(function(){
return resolve(HTTPWTHandler.OK('Comment upload successful'))
})
.catch(err => {
console.error('An error occured while adding reply to poll comment. Comment reply was:', commentForPost, '. The error was:', err)
return resolve(HTTPWTHandler.serverError('An error occurred while adding comment reply. Please try again.'))
});
}
var itemsProcessed = 0
comments.forEach(function (item, index) {
console.log(comments[index].commentId)
console.log(commentId)
if (comments[index].commentId == commentId) {
if (itemsProcessed !== null) {
console.log("Found at index:")
console.log(index)
findThreads(index)
itemsProcessed = null
}
} else {
if (itemsProcessed !== null) {
itemsProcessed++;
if(itemsProcessed == comments.length) {
return resolve(HTTPWTHandler.notFound("Couldn't find comment"))
}
}
}
Poll.findOne({_id: {$eq: postId}}).lean().then(data => {
if (data) {
var comments = data.comments
async function findThreads(sentIndex) {
var objectId = new mongoose.Types.ObjectId()
console.log(objectId)
var commentForPost = {commentId: objectId, commenterId: userId, commentsText: comment, commentUpVotes: [], commentDownVotes: [], datePosted: Date.now()}
Poll.findOneAndUpdate({_id: {$eq: postId}}, { $push: { [`comments.${sentIndex}.commentReplies`]: commentForPost } }).then(function(){
return resolve(HTTPWTHandler.OK('Comment upload successful'))
})
.catch(err => {
console.error('An error occured while adding reply to poll comment. Comment reply was:', commentForPost, '. The error was:', err)
return resolve(HTTPWTHandler.serverError('An error occurred while adding comment reply. Please try again.'))
});
} else {
return resolve(HTTPWTHandler.notFound('Could not find poll'))
}
}).catch(error => {
console.error('An error occurred while finding one poll with id:', postId, '. The error was:', error)
return resolve(HTTPWTHandler.serverError('An error occurred while finding poll. Please try again.'))
var itemsProcessed = 0
comments.forEach(function (item, index) {
console.log(comments[index].commentId)
console.log(commentId)
if (comments[index].commentId == commentId) {
if (itemsProcessed !== null) {
console.log("Found at index:")
console.log(index)
findThreads(index)
itemsProcessed = null
}
} else {
if (itemsProcessed !== null) {
itemsProcessed++;
if(itemsProcessed == comments.length) {
return resolve(HTTPWTHandler.notFound("Couldn't find comment"))
}
}
}
});
} else {
return resolve(HTTPWTHandler.notFound('Could not find poll'))
}
}).catch(error => {
console.error('An error occurred while finding one poll with id:', postId, '. The error was:', error)
return resolve(HTTPWTHandler.serverError('An error occurred while finding poll. Please try again.'))
})
} else {
return resolve(HTTPWTHandler.badInput('Provided userName does not match username in database.'))
}
} else {
return resolve(HTTPWTHandler.badInput('Could not find user from userId provided'))
}
Expand Down Expand Up @@ -6429,8 +6421,8 @@ class TempController {
return await this.#pollpostcomment(userId, comment, postId)
}

static pollpostcommentreply = async (userId, comment, userName, postId, commentId) => {
return await this.#pollpostcommentreply(userId, comment, userName, postId, commentId)
static pollpostcommentreply = async (userId, comment, postId, commentId) => {
return await this.#pollpostcommentreply(userId, comment, postId, commentId)
}

static searchforpollcomments = async (userId, pollId) => {
Expand Down
2 changes: 1 addition & 1 deletion routes/Temp.js
Original file line number Diff line number Diff line change
Expand Up @@ -1082,7 +1082,7 @@ router.post('/pollpostcommentreply', rateLimiters['/pollpostcommentreply'], (req
const worker = new Worker(workerPath, {
workerData: {
functionName: 'pollpostcommentreply',
functionArgs: [req.tokenData, req.body.comment, req.body.userName, req.body.postId, req.body.commentId]
functionArgs: [req.tokenData, req.body.comment, req.body.postId, req.body.commentId]
}
})

Expand Down

0 comments on commit 6386fc2

Please sign in to comment.