post.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. let express = require('express')
  2. let router = express.Router()
  3. const Errors = require('../lib/errors')
  4. let { User, Thread, Post, Notification, Ban, Sequelize, sequelize } = require('../models')
  5. router.get('/:post_id', async (req, res, next) => {
  6. try {
  7. let post = await Post.findById(req.params.post_id, { include: Post.includeOptions() })
  8. if(!post) throw Errors.sequelizeValidation(Sequelize, {
  9. error: 'post does not exist',
  10. path: 'id'
  11. })
  12. res.json(post.toJSON())
  13. } catch (e) { next(e) }
  14. })
  15. router.all('*', (req, res, next) => {
  16. if(req.session.loggedIn) {
  17. next()
  18. } else {
  19. res.status(401)
  20. res.json({
  21. errors: [Errors.requestNotAuthorized]
  22. })
  23. }
  24. })
  25. router.put('/:post_id/like', async (req, res, next) => {
  26. try {
  27. let post = await Post.findById(req.params.post_id)
  28. let user = await User.findOne({ where: { username: req.session.username }})
  29. if(!post) throw Errors.invalidParameter('id', 'post does not exist')
  30. if(post.UserId === user.id) throw Errors.cannotLikeOwnPost
  31. await post.addLikes(user)
  32. res.json({ success: true })
  33. } catch (e) { next(e) }
  34. })
  35. router.delete('/:post_id/like', async (req, res, next) => {
  36. try {
  37. let post = await Post.findById(req.params.post_id)
  38. let user = await User.findOne({ where: { username: req.session.username }})
  39. if(!post) throw Errors.invalidParameter('id', 'post does not exist')
  40. await post.removeLikes(user)
  41. res.json({ success: true })
  42. } catch (e) { next(e) }
  43. })
  44. router.post('/', async (req, res, next) => {
  45. let thread, replyingToPost, post, uniqueMentions = []
  46. try {
  47. //Will throw an error if banned
  48. await Ban.canCreatePosts(req.session.username)
  49. if(req.body.mentions) {
  50. uniqueMentions = Notification.filterMentions(req.body.mentions)
  51. }
  52. thread = await Thread.findOne({ where: {
  53. id: req.body.threadId
  54. }})
  55. user = await User.findOne({ where: {
  56. username: req.session.username
  57. }})
  58. if(!thread) throw Errors.sequelizeValidation(Sequelize, {
  59. error: 'thread does not exist',
  60. path: 'id'
  61. })
  62. if(thread.locked) throw Errors.threadLocked
  63. if(req.body.replyingToId) {
  64. replyingToPost = await Post.getReplyingToPost(
  65. req.body.replyingToId, thread
  66. )
  67. post = await Post.create({ content: req.body.content, postNumber: thread.postsCount })
  68. await post.setReplyingTo(replyingToPost)
  69. await replyingToPost.addReplies(post)
  70. let replyNotification = await Notification.createPostNotification({
  71. usernameTo: replyingToPost.User.username,
  72. userFrom: user,
  73. type: 'reply',
  74. post: post
  75. })
  76. await replyNotification.emitNotificationMessage(
  77. req.app.get('io-users'),
  78. req.app.get('io')
  79. )
  80. } else {
  81. post = await Post.create({ content: req.body.content, postNumber: thread.postsCount })
  82. }
  83. await post.setUser(user)
  84. await post.setThread(thread)
  85. await thread.increment('postsCount')
  86. if(uniqueMentions.length) {
  87. let ioUsers = req.app.get('io-users')
  88. let io = req.app.get('io')
  89. for(const mention of uniqueMentions) {
  90. let mentionNotification = await Notification.createPostNotification({
  91. usernameTo: mention,
  92. userFrom: user,
  93. type: 'mention',
  94. post
  95. })
  96. if(mentionNotification) {
  97. await mentionNotification.emitNotificationMessage(ioUsers, io)
  98. }
  99. }
  100. }
  101. res.json(await post.reload({
  102. include: Post.includeOptions()
  103. }))
  104. req.app.get('io').to('thread/' + thread.id).emit('new post', {
  105. postNumber: thread.postsCount,
  106. content: post.content,
  107. username: user.username
  108. })
  109. } catch (e) { next(e) }
  110. })
  111. router.all('*', (req, res, next) => {
  112. if(!req.session.admin) {
  113. res.status(401)
  114. res.json({
  115. errors: [Errors.requestNotAuthorized]
  116. })
  117. } else {
  118. next()
  119. }
  120. })
  121. router.delete('/:post_id', async (req, res, next) => {
  122. try {
  123. let post = await Post.findById(req.params.post_id)
  124. if(!post) throw Errors.sequelizeValidation(Sequelize, {
  125. error: 'post does not exist',
  126. path: 'id'
  127. })
  128. await post.update({ content: '[This post has been removed by an administrator]', removed: true })
  129. res.json({ success: true })
  130. } catch (e) { next(e) }
  131. })
  132. module.exports = router