post.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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(!user.canCreatePosts) throw Errors.sequelizeValidation(Sequelize, {
  94. error: 'You have been banned from posting'
  95. })
  96. if(!thread) throw Errors.sequelizeValidation(Sequelize, {
  97. error: 'thread does not exist',
  98. path: 'id'
  99. })
  100. if(thread.locked) throw Errors.threadLocked
  101. if(req.body.replyingToId) {
  102. replyingToPost = await Post.getReplyingToPost(
  103. req.body.replyingToId, thread
  104. )
  105. post = await Post.create({ content: req.body.content, postNumber: thread.postsCount })
  106. await post.setReplyingTo(replyingToPost)
  107. await replyingToPost.addReplies(post)
  108. let replyNotification = await Notification.createPostNotification({
  109. usernameTo: replyingToPost.User.username,
  110. userFrom: user,
  111. type: 'reply',
  112. post: post
  113. })
  114. await replyNotification.emitNotificationMessage(
  115. req.app.get('io-users'),
  116. req.app.get('io')
  117. )
  118. } else {
  119. post = await Post.create({ content: req.body.content, postNumber: thread.postsCount })
  120. }
  121. await post.setUser(user)
  122. await post.setThread(thread)
  123. await thread.increment('postsCount')
  124. if(uniqueMentions.length) {
  125. let ioUsers = req.app.get('io-users')
  126. let io = req.app.get('io')
  127. uniqueMentions.forEach(async mention => {
  128. let mentionNotification = await Notification.createPostNotification({
  129. usernameTo: mention,
  130. userFrom: user,
  131. type: 'mention',
  132. post
  133. })
  134. await mentionNotification.emitNotificationMessage(ioUsers, io)
  135. })
  136. }
  137. res.json(await post.reload({
  138. include: Post.includeOptions()
  139. }))
  140. req.app.get('io').to('thread/' + thread.id).emit('new post', {
  141. postNumber: thread.postsCount
  142. })
  143. } catch (e) {
  144. if(e instanceof Sequelize.ValidationError) {
  145. res.status(400)
  146. res.json(e)
  147. } else if(e.name in Errors) {
  148. res.status(400)
  149. res.json({
  150. errors: [e]
  151. })
  152. } else {
  153. console.log(e)
  154. res.status(500)
  155. res.json({
  156. errors: [Errors.unknown]
  157. })
  158. }
  159. }
  160. })
  161. router.all('*', (req, res, next) => {
  162. if(!req.session.admin) {
  163. res.status(401)
  164. res.json({
  165. errors: [Errors.requestNotAuthorized]
  166. })
  167. } else {
  168. next()
  169. }
  170. })
  171. router.delete('/:post_id', async (req, res) => {
  172. try {
  173. let post = await Post.findById(req.params.post_id)
  174. if(!post) throw Errors.sequelizeValidation(Sequelize, {
  175. error: 'post does not exist',
  176. path: 'id'
  177. })
  178. await post.update({ content: '[This post has been removed by an administrator]', removed: true })
  179. res.json({ success: true })
  180. } catch (e) {
  181. if(e instanceof Sequelize.ValidationError) {
  182. res.status(400)
  183. res.json(e)
  184. } else {
  185. console.log(e)
  186. res.status(500)
  187. res.json({
  188. errors: [Errors.unknown]
  189. })
  190. }
  191. }
  192. })
  193. module.exports = router