post.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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) => {
  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 thread, replyingToPost, post, uniqueMentions = []
  82. try {
  83. //Will throw an error if banned
  84. await Ban.canCreatePosts(req.session.username)
  85. if(req.body.mentions) {
  86. uniqueMentions = Notification.filterMentions(req.body.mentions)
  87. }
  88. thread = await Thread.findOne({ where: {
  89. id: req.body.threadId
  90. }})
  91. user = await User.findOne({ where: {
  92. username: req.session.username
  93. }})
  94. if(!thread) throw Errors.sequelizeValidation(Sequelize, {
  95. error: 'thread does not exist',
  96. path: 'id'
  97. })
  98. if(thread.locked) throw Errors.threadLocked
  99. if(req.body.replyingToId) {
  100. replyingToPost = await Post.getReplyingToPost(
  101. req.body.replyingToId, thread
  102. )
  103. post = await Post.create({ content: req.body.content, postNumber: thread.postsCount })
  104. await post.setReplyingTo(replyingToPost)
  105. await replyingToPost.addReplies(post)
  106. let replyNotification = await Notification.createPostNotification({
  107. usernameTo: replyingToPost.User.username,
  108. userFrom: user,
  109. type: 'reply',
  110. post: post
  111. })
  112. await replyNotification.emitNotificationMessage(
  113. req.app.get('io-users'),
  114. req.app.get('io')
  115. )
  116. } else {
  117. post = await Post.create({ content: req.body.content, postNumber: thread.postsCount })
  118. }
  119. await post.setUser(user)
  120. await post.setThread(thread)
  121. await thread.increment('postsCount')
  122. if(uniqueMentions.length) {
  123. let ioUsers = req.app.get('io-users')
  124. let io = req.app.get('io')
  125. uniqueMentions.forEach(async mention => {
  126. let mentionNotification = await Notification.createPostNotification({
  127. usernameTo: mention,
  128. userFrom: user,
  129. type: 'mention',
  130. post
  131. })
  132. await mentionNotification.emitNotificationMessage(ioUsers, io)
  133. })
  134. }
  135. res.json(await post.reload({
  136. include: Post.includeOptions()
  137. }))
  138. req.app.get('io').to('thread/' + thread.id).emit('new post', {
  139. postNumber: thread.postsCount,
  140. content: post.content,
  141. username: user.username
  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