Skip to content

Commit

Permalink
Removed userName check from temp/threadpostcommentreply
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebastian-Webster committed Oct 10, 2023
1 parent 34bec59 commit fc092cf
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 37 deletions.
64 changes: 28 additions & 36 deletions controllers/Temp.js
Original file line number Diff line number Diff line change
Expand Up @@ -3580,16 +3580,12 @@ class TempController {
})
}

static #threadpostcommentreply = (userId, comment, userName, postId, commentId) => {
static #threadpostcommentreply = (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 @@ -3615,38 +3611,34 @@ class TempController {
//Find User
User.findOne({_id: {$eq: userId}}).lean().then(result => {
if (result) {
if (result.name == userName) {
Thread.findOne({_id: {$eq: postId}}).lean().then(data => {
if (data) {
const comments = data.comments;

const commentIndex = comments.findIndex(item => String(item.commentId) === commentId)
Thread.findOne({_id: {$eq: postId}}).lean().then(data => {
if (data) {
const comments = data.comments;

if (commentIndex === -1) {
return resolve(HTTPWTHandler.badInput("Couldn't find comment"))
}
const commentIndex = comments.findIndex(item => String(item.commentId) === commentId)

const objectId = new mongoose.Types.ObjectId()
console.log(objectId)
var commentForPost = {commentId: objectId, commenterId: userId, commentsText: comment, commentUpVotes: [], commentDownVotes: [], datePosted: Date.now()}
Thread.findOneAndUpdate({_id: {$eq: postId}}, { $push: { [`comments.${sentIndex}.commentReplies`]: commentForPost } }).then(function(){
console.log("SUCCESS1")
return resolve(HTTPWTHandler.OK('Comment upload successful'))
})
.catch(err => {
console.error('An error occurred while pushing:', commentForPost, 'to:', `comments.${sentIndex}.commentReplies`, ' for thread with id:', postId, '. The error was:', err)
return resolve(HTTPWTHandler.serverError('An error occurred while adding comment. Please try again.'))
});
} else {
return resolve(HTTPWTHandler.notFound('Could not find thread'))
if (commentIndex === -1) {
return resolve(HTTPWTHandler.badInput("Couldn't find comment"))
}
}).catch(error => {
console.error('An error occurred while finding thread with id:', postId, '. The error was:', error)
return resolve(HTTPWTHandler.serverError('An error occurred while finding thread. Please try again.'))
})
} else {
return resolve(HTTPWTHandler.badInput('name in database does not match up with userName provided'))
}

const objectId = new mongoose.Types.ObjectId()
console.log(objectId)
var commentForPost = {commentId: objectId, commenterId: userId, commentsText: comment, commentUpVotes: [], commentDownVotes: [], datePosted: Date.now()}
Thread.findOneAndUpdate({_id: {$eq: postId}}, { $push: { [`comments.${sentIndex}.commentReplies`]: commentForPost } }).then(function(){
console.log("SUCCESS1")
return resolve(HTTPWTHandler.OK('Comment upload successful'))
})
.catch(err => {
console.error('An error occurred while pushing:', commentForPost, 'to:', `comments.${sentIndex}.commentReplies`, ' for thread with id:', postId, '. The error was:', err)
return resolve(HTTPWTHandler.serverError('An error occurred while adding comment. Please try again.'))
});
} else {
return resolve(HTTPWTHandler.notFound('Could not find thread'))
}
}).catch(error => {
console.error('An error occurred while finding thread with id:', postId, '. The error was:', error)
return resolve(HTTPWTHandler.serverError('An error occurred while finding thread. Please try again.'))
})
} else {
return resolve(HTTPWTHandler.notFound('Could not find user with provided userId'))
}
Expand Down Expand Up @@ -6619,8 +6611,8 @@ class TempController {
return await this.#threadpostcomment(userId, comment, userName, postId)
}

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

static searchforthreadcomments = async (userId, threadId) => {
Expand Down
2 changes: 1 addition & 1 deletion routes/Temp.js
Original file line number Diff line number Diff line change
Expand Up @@ -1737,7 +1737,7 @@ router.post('/threadpostcommentreply', rateLimiters['/threadpostcommentreply'],
const worker = new Worker(workerPath, {
workerData: {
functionName: 'threadpostcommentreply',
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 fc092cf

Please sign in to comment.