Skip to content

Commit

Permalink
Added temp/getsinglecomment API
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebastian-Webster committed Oct 15, 2023
1 parent 8dd8c78 commit 81f881e
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 1 deletion.
76 changes: 76 additions & 0 deletions controllers/Temp.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ const PollVote = require('../models/PollVote');

const { Expo } = require('expo-server-sdk')

const POST_DATABASE_MODELS = {
Image: ImagePost,
Poll,
Thread
}

class TempController {
static #sendnotificationkey = (userId, notificationKey, refreshTokenId) => {
return new Promise(resolve => {
Expand Down Expand Up @@ -5791,6 +5797,72 @@ class TempController {
})
}

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

if (commentId.length === 0) {
return resolve(HTTPWTHandler.badInput('commentId cannot be blank'))
}

User.findOne({_id: {$eq: userId}}).lean().then(userFound => {
if (!userFound) return resolve(HTTPWTHandler.notFound('Could not find user with provided userId'))

Comment.findOne({_id: {$eq: commentId}}).lean().then(commentFound => {
if (!commentFound) return resolve(HTTPWTHandler.notFound('Could not find comment.'))

let commentOwner;

try {
commentOwner = commentFound.commenterId == userId ? userFound : User.findOne({_id: {$eq: commentFound.commenterId}}).lean()
} catch (error) {
console.log('An error occurred while finding one user with id:', commentFound.commenterId, '. The error was:', error)
return resolve(HTTPWTHandler.serverError('An error occurred while finding comment owner. Please try again.'))
}

const requesterIsBlockedByCommentOwner = commentOwner.blockedAccounts.includes(userFound.secondId)
if (requesterIsBlockedByCommentOwner) return resolve(HTTPWTHandler.notFound('Could not find comment.'))

const postDatabaseModel = POST_DATABASE_MODELS[commentFound.postFormat]
postDatabaseModel.findOne({_id: {$eq: commentFound.postId}}).lean().then(postFound => {
let postOwner;

try {
postOwner = postFound.creatorId == userId ? userFound : User.findOne({_id: {$eq: postFound.creatorId}}).lean()
} catch (error) {
console.error('An error occurred while finding one user with id:', postFound.creatorId, '. The error was:', error)
return resolve(HTTPWTHandler.serverError('An error occurred while finding user that owns the post that the comment was made on. Please try again.'))
}

const requesterIsBlockedByPostOwner = postOwner.blockedAccounts.includes(userFound.secondId);
if (requesterIsBlockedByPostOwner) return resolve(HTTPWTHandler.notFound('Comment not found.'))

const postOwnerAccountIsPrivateAndRequesterDoesNotFollowAccount = postOwner.privateAccount === true && !postOwner.followers.includes(userFound.secondId)
if (postOwnerAccountIsPrivateAndRequesterDoesNotFollowAccount) return resolve(HTTPWTHandler.notFound('Comment not found.'))

commentHandler.processMultipleCommentsFromOneOwner([commentFound], commentOwner, userFound).then(comments => {
return resolve(HTTPWTHandler.OK('Successfully found comment', comments))
}).catch(error => {
console.error('An error occurred while processing comment data:', error)
return resolve(HTTPWTHandler.serverError('An error occurred while getting comment data. Please try again.'))
})
}).catch(error => {
console.error('An error occurred while finding', commentFound.postFormat, 'post with id:', commentFound.postId, '. The error was:', error)
return resolve(HTTPWTHandler.serverError('An error occurred while finding the post that is associated with the comment. Please try again.'))
})
}).catch(error => {
console.error('An error occurred while finding one comment with id:', commentId, '. The error was:', error)
return resolve(HTTPWTHandler.serverError('An error occurred while finding comment. Please try again.'))
})
}).catch(error => {
console.error('An error occurred while finding one user with id:', userId, '. The error was:', error)
return resolve(HTTPWTHandler.serverError('An error occurred while finding user. Please try again.'))
})
})
}

static sendnotificationkey = async (userId, notificationKey, refreshTokenId) => {
return await this.#sendnotificationkey(userId, notificationKey, refreshTokenId)
}
Expand Down Expand Up @@ -6142,6 +6214,10 @@ class TempController {
static deletecomment = async (userId, commentId) => {
return await this.#deletecomment(userId, commentId);
}

static getsinglecomment = async (userId, commentId) => {
return await this.#getsinglecomment(userId, commentId);
}
}

module.exports = TempController;
29 changes: 28 additions & 1 deletion routes/Temp.js
Original file line number Diff line number Diff line change
Expand Up @@ -829,7 +829,16 @@ const rateLimiters = {
message: {status: "FAILED", message: "You have deleted too many comments in the last minute. Please try again in 60 seconds."},
skipFailedRequests: true,
keyGenerator: (req, res) => req.tokenData //Use req.tokenData (account _id in MongoDB) to identify clients and rate limit
})
}),
'/getsinglecomment': rateLimit({
windowMs: 1000 * 60, //1 minute
max: 30,
standardHeaders: false,
legacyHeaders: false,
message: {status: "FAILED", message: "You have requested too many single comments in the last minute. Please try again in 60 seconds."},
skipFailedRequests: true,
keyGenerator: (req, res) => req.tokenData //Use req.tokenData (account _id in MongoDB) to identify clients and rate limit
}),
}


Expand Down Expand Up @@ -2450,4 +2459,22 @@ router.post('/deletecomment', rateLimiters['/deletecomment'], (req, res) => {
})
});

router.post('/getsinglecomment', rateLimiters['/getsinglecomment'], (req, res) => {
const worker = new Worker(workerPath, {
workerData: {
functionName: 'getsinglecomment',
functionArgs: [req.tokenData, req.body.commentId]
}
})

worker.on('message', (result) => {
res.status(result.statusCode).json(result.data)
})

worker.on('error', (error) => {
console.error('An error occurred from TempWorker for POST /getsinglecomment:', error)
HTTPHandler.serverError(res, String(error))
})
});

module.exports = router;

0 comments on commit 81f881e

Please sign in to comment.