thread.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. let express = require('express')
  2. let router = express.Router()
  3. const Errors = require('../lib/errors.js')
  4. let { User, Thread, Category, Post, Ban, 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.put('/:thread_id', async (req, res) => {
  97. try {
  98. let thread = await Thread.findById(req.params.thread_id)
  99. if(!thread) {
  100. res.status(400)
  101. res.json({ errors:
  102. [Errors.invalidParameter('threadId', 'thread does not exist')]
  103. })
  104. } else {
  105. if(req.body.locked) {
  106. await thread.update({ locked: true })
  107. } else {
  108. await thread.update({ locked: false })
  109. }
  110. res.json({ success: true })
  111. }
  112. } catch (e) {
  113. console.log(e)
  114. res.status(500)
  115. res.json({
  116. errors: [Errors.unknown]
  117. })
  118. }
  119. })
  120. module.exports = router