thread.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. let express = require('express')
  2. let router = express.Router()
  3. const Errors = require('../lib/errors.js')
  4. let { User, Thread, Category, Post, Ban, Report, Sequelize } = require('../models')
  5. let pagination = require('../lib/pagination.js')
  6. router.get('/:thread_id', async (req, res) => {
  7. try {
  8. let { from, limit } = pagination.getPaginationProps(req.query)
  9. let thread = await Thread.findById(req.params.thread_id, {
  10. include: Thread.includeOptions(from, limit)
  11. })
  12. if(!thread) throw Errors.invalidParameter('id', 'thread does not exist')
  13. let meta = thread.getMeta(limit)
  14. res.json(Object.assign( thread.toJSON(), { meta } ))
  15. } catch (e) {
  16. if(e.name === 'invalidParameter') {
  17. res.status(400)
  18. res.json({
  19. errors: [e]
  20. })
  21. } else {
  22. console.log(e)
  23. res.status(500)
  24. res.json({
  25. errors: [Errors.unknown]
  26. })
  27. }
  28. }
  29. })
  30. //Only logged in routes
  31. router.all('*', (req, res, next) => {
  32. if(req.session.loggedIn) {
  33. next()
  34. } else {
  35. res.status(401)
  36. res.json({
  37. errors: [Errors.requestNotAuthorized]
  38. })
  39. }
  40. })
  41. router.post('/', async (req, res) => {
  42. let validationErrors = []
  43. try {
  44. await Ban.canCreateThreads(req.session.username)
  45. let category = await Category.findOne({ where: {
  46. value: req.body.category
  47. }})
  48. if(!category) throw Errors.invalidCategory
  49. let user = await User.findOne({ where: {
  50. username: req.session.username
  51. }})
  52. let thread = await Thread.create({
  53. name: req.body.name
  54. })
  55. await thread.setCategory(category)
  56. await thread.setUser(user)
  57. res.json(await thread.reload({
  58. include: [
  59. { model: User, attributes: ['username', 'createdAt', 'updatedAt', 'id'] },
  60. Category
  61. ]
  62. }))
  63. req.app.get('io').to('index').emit('new thread', {
  64. name: category.name,
  65. value: category.value
  66. })
  67. } catch (e) {
  68. if(e instanceof Sequelize.ValidationError) {
  69. res.status(400)
  70. res.json(e)
  71. } else if(e === Errors.invalidCategory) {
  72. res.status(400)
  73. res.json({
  74. errors: [e]
  75. })
  76. } else {
  77. console.log(e)
  78. res.status(500)
  79. res.json({
  80. errors: [Errors.unknown]
  81. })
  82. }
  83. }
  84. })
  85. //Only admin routes
  86. router.all('*', (req, res, next) => {
  87. if(req.session.admin) {
  88. next()
  89. } else {
  90. res.status(401)
  91. res.json({
  92. errors: [Errors.requestNotAuthorized]
  93. })
  94. }
  95. })
  96. router.delete('/:thread_id', async (req, res) => {
  97. try {
  98. let thread = await Thread.findById(req.params.thread_id)
  99. if(!thread) {
  100. throw Errors.sequelizeValidation(Sequelize, {
  101. error: 'invalid thread id',
  102. value: req.params.thread_id
  103. })
  104. } else {
  105. //Find all posts with reports and get reports
  106. //Then delete those reports
  107. //Temporary fix because cascade is not working
  108. let posts = await Post.findAll({
  109. where: {
  110. ThreadId: thread.id
  111. },
  112. include: [Report]
  113. })
  114. let reports = posts
  115. .map(post => post.Reports)
  116. .reduce((a, b) => a.concat(b), [])
  117. let destroyPromises = reports.map(report => report.destroy())
  118. await Promise.all(destroyPromises)
  119. await thread.destroy()
  120. res.json({ success: true })
  121. }
  122. } catch (e) {
  123. if(e instanceof Sequelize.ValidationError) {
  124. res.status(400)
  125. res.json(e)
  126. } else {
  127. console.log(e)
  128. res.status(500)
  129. res.json({
  130. errors: [Errors.unknown]
  131. })
  132. }
  133. }
  134. })
  135. router.put('/:thread_id', async (req, res) => {
  136. try {
  137. let thread = await Thread.findById(req.params.thread_id)
  138. if(!thread) {
  139. res.status(400)
  140. res.json({ errors:
  141. [Errors.invalidParameter('threadId', 'thread does not exist')]
  142. })
  143. } else {
  144. if(req.body.locked) {
  145. await thread.update({ locked: true })
  146. } else {
  147. await thread.update({ locked: false })
  148. }
  149. res.json({ success: true })
  150. }
  151. } catch (e) {
  152. console.log(e)
  153. res.status(500)
  154. res.json({
  155. errors: [Errors.unknown]
  156. })
  157. }
  158. })
  159. module.exports = router