post.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. let express = require('express')
  2. let router = express.Router()
  3. const Errors = require('../lib/errors')
  4. let { User, Thread, Post, Notification, Sequelize, sequelize } = require('../models')
  5. router.get('/:post_id', async (req, res) => {
  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) {
  14. if(e instanceof Sequelize.ValidationError) {
  15. res.status(400)
  16. res.json(e)
  17. } else {
  18. res.status(500)
  19. res.json({
  20. errors: [Errors.unknown]
  21. })
  22. }
  23. }
  24. })
  25. router.all('*', (req, res, next) => {
  26. if(req.session.loggedIn) {
  27. next()
  28. } else {
  29. res.status(401)
  30. res.json({
  31. errors: [Errors.requestNotAuthorized]
  32. })
  33. }
  34. })
  35. router.put('/:post_id/like', async (req, res) => {
  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. if(post.UserId === user.id) throw Errors.cannotLikeOwnPost
  41. await post.addLikes(user)
  42. res.json({ success: true })
  43. } catch (e) {
  44. if(e.name in Errors) {
  45. res.status(400)
  46. res.json({
  47. errors: [e]
  48. })
  49. } else{
  50. console.log(e)
  51. res.status(500)
  52. res.json({
  53. errors: [Errors.unknown]
  54. })
  55. }
  56. }
  57. })
  58. router.delete('/:post_id/like', async (req, res) => {
  59. try {
  60. let post = await Post.findById(req.params.post_id)
  61. let user = await User.findOne({ where: { username: req.session.username }})
  62. if(!post) throw Errors.invalidParameter('id', 'post does not exist')
  63. await post.removeLikes(user)
  64. res.json({ success: true })
  65. } catch (e) {
  66. if(e.name === 'invalidParameter') {
  67. res.status(400)
  68. res.json({
  69. errors: [e]
  70. })
  71. } else{
  72. console.log(e)
  73. res.status(500)
  74. res.json({
  75. errors: [Errors.unknown]
  76. })
  77. }
  78. }
  79. })
  80. router.post('/', async (req, res) => {
  81. let validationErrors = []
  82. let thread, replyingToPost, post, uniqueMentions = []
  83. try {
  84. if(req.body.mentions) {
  85. uniqueMentions = Notification.filterMentions(req.body.mentions)
  86. }
  87. thread = await Thread.findOne({ where: {
  88. id: req.body.threadId
  89. }})
  90. user = await User.findOne({ where: {
  91. username: req.session.username
  92. }})
  93. if(!thread) throw Errors.sequelizeValidation(Sequelize, {
  94. error: 'thread does not exist',
  95. path: 'id'
  96. })
  97. if(thread.locked) throw Errors.threadLocked
  98. if(req.body.replyingToId) {
  99. replyingToPost = await Post.getReplyingToPost(
  100. req.body.replyingToId, thread
  101. )
  102. post = await Post.create({ content: req.body.content, postNumber: thread.postsCount })
  103. await post.setReplyingTo(replyingToPost)
  104. await replyingToPost.addReplies(post)
  105. let replyNotification = await Notification.createPostNotification({
  106. usernameTo: replyingToPost.User.username,
  107. userFrom: user,
  108. type: 'reply',
  109. post: post
  110. })
  111. await replyNotification.emitNotificationMessage(
  112. req.app.get('io-users'),
  113. req.app.get('io')
  114. )
  115. } else {
  116. post = await Post.create({ content: req.body.content, postNumber: thread.postsCount })
  117. }
  118. await post.setUser(user)
  119. await post.setThread(thread)
  120. await thread.increment('postsCount')
  121. if(uniqueMentions.length) {
  122. let ioUsers = req.app.get('io-users')
  123. let io = req.app.get('io')
  124. uniqueMentions.forEach(async mention => {
  125. let mentionNotification = await Notification.createPostNotification({
  126. usernameTo: mention,
  127. userFrom: user,
  128. type: 'mention',
  129. post
  130. })
  131. await mentionNotification.emitNotificationMessage(ioUsers, io)
  132. })
  133. }
  134. res.json(await post.reload({
  135. include: Post.includeOptions()
  136. }))
  137. req.app.get('io').to('thread/' + thread.id).emit('new post', {
  138. postNumber: thread.postsCount
  139. })
  140. } catch (e) {
  141. if(e instanceof Sequelize.ValidationError) {
  142. res.status(400)
  143. res.json(e)
  144. } else if(e.name in Errors) {
  145. res.status(400)
  146. res.json({
  147. errors: [e]
  148. })
  149. } else {
  150. console.log(e)
  151. res.status(500)
  152. res.json({
  153. errors: [Errors.unknown]
  154. })
  155. }
  156. }
  157. })
  158. router.all('*', (req, res, next) => {
  159. if(!req.session.admin) {
  160. res.status(401)
  161. res.json({
  162. errors: [Errors.requestNotAuthorized]
  163. })
  164. } else {
  165. next()
  166. }
  167. })
  168. router.delete('/:post_id', async (req, res) => {
  169. try {
  170. let post = await Post.findById(req.params.post_id)
  171. if(!post) throw Errors.sequelizeValidation(Sequelize, {
  172. error: 'post does not exist',
  173. path: 'id'
  174. })
  175. await post.update({ content: '[This post has been removed by an administrator]', removed: true })
  176. res.json({ success: true })
  177. } catch (e) {
  178. if(e instanceof Sequelize.ValidationError) {
  179. res.status(400)
  180. res.json(e)
  181. } else {
  182. console.log(e)
  183. res.status(500)
  184. res.json({
  185. errors: [Errors.unknown]
  186. })
  187. }
  188. }
  189. })
  190. module.exports = router