thread.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. let express = require('express')
  2. let router = express.Router()
  3. const Errors = require('../lib/errors.js')
  4. let { User, Thread, Category, Post } = 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 = await 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. if(req.body.name === undefined) {
  45. validationErrors.push(Errors.missingParameter('name'))
  46. } else if(typeof req.body.name !== 'string') {
  47. validationErrors.push(Errors.invalidParameterType('name', 'string'))
  48. } else if(req.body.name.length === 0) {
  49. validationErrors.push(Errors.missingParameter('name'))
  50. }
  51. if(req.body.category === undefined) {
  52. validationErrors.push(Errors.missingParameter('category'))
  53. } else if(typeof req.body.category !== 'string') {
  54. validationErrors.push(Errors.invalidParameterType('category', 'string'))
  55. }
  56. if(validationErrors.length) throw Errors.VALIDATION_ERROR
  57. let category = await Category.findOne({ where: {
  58. value: req.body.category
  59. }})
  60. if(!category) throw Errors.invalidCategory
  61. let user = await User.findOne({ where: {
  62. username: req.session.username
  63. }})
  64. let thread = await Thread.create({
  65. name: req.body.name
  66. })
  67. await thread.setCategory(category)
  68. await thread.setUser(user)
  69. res.json(await thread.reload({
  70. include: [
  71. { model: User, attributes: ['username', 'createdAt', 'updatedAt', 'id'] },
  72. Category
  73. ]
  74. }))
  75. req.app.get('io').to('index').emit('new thread', {
  76. name: category.name,
  77. value: category.value
  78. })
  79. } catch (e) {
  80. if(e === Errors.VALIDATION_ERROR) {
  81. res.status(400)
  82. res.json({
  83. errors: validationErrors
  84. })
  85. } else if(e === Errors.invalidCategory) {
  86. res.status(400)
  87. res.json({
  88. errors: [Errors.invalidCategory]
  89. })
  90. } else {
  91. console.log(e)
  92. res.status(500)
  93. res.json({
  94. errors: [Errors.unknown]
  95. })
  96. }
  97. }
  98. })
  99. //Only admin routes
  100. router.all('*', (req, res, next) => {
  101. if(req.session.admin) {
  102. next()
  103. } else {
  104. res.status(401)
  105. res.json({
  106. errors: [Errors.requestNotAuthorized]
  107. })
  108. }
  109. })
  110. router.put('/:thread_id', async (req, res) => {
  111. try {
  112. let thread = await Thread.findById(req.params.thread_id)
  113. if(!thread) {
  114. res.status(400)
  115. res.json({ errors:
  116. [Errors.invalidParameter('threadId', 'thread does not exist')]
  117. })
  118. } else {
  119. if(req.body.locked) {
  120. await thread.update({ locked: true })
  121. } else {
  122. await thread.update({ locked: false })
  123. }
  124. res.json({ success: true })
  125. }
  126. } catch (e) {
  127. console.log(e)
  128. res.status(500)
  129. res.json({
  130. errors: [Errors.unknown]
  131. })
  132. }
  133. })
  134. module.exports = router