thread.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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, resThread
  10. thread = await Thread.findById(req.params.thread_id, {
  11. include: Thread.includeOptions(from, limit)
  12. })
  13. if(!thread) throw Errors.invalidParameter('id', 'thread does not exist')
  14. resThread = thread.toJSON()
  15. resThread.meta = {}
  16. let lastPost = thread.Posts.slice(-1)[0]
  17. let firstPost = thread.Posts[0]
  18. if(!lastPost || lastPost.postNumber+1 === thread.postsCount) {
  19. resThread.meta.nextURL = null
  20. } else {
  21. resThread.meta.nextURL =
  22. `/api/v1/thread/${thread.id}?limit=${limit}&from=${lastPost.postNumber + 1}`
  23. }
  24. if(!firstPost || firstPost.postNumber === 0) {
  25. resThread.meta.previousURL = null
  26. } else if(firstPost.postNumber - limit < 0) {
  27. resThread.meta.previousURL =
  28. `/api/v1/thread/${thread.id}?limit=${firstPost.postNumber}&from=0`
  29. } else {
  30. resThread.meta.previousURL =
  31. `/api/v1/thread/${thread.id}?limit=${limit}&from=${firstPost.postNumber - limit}`
  32. }
  33. if(lastPost === undefined) {
  34. resThread.meta.postsRemaining = 0
  35. resThread.meta.nextPostsCount = 0
  36. resThread.meta.previousPostsCount = 0
  37. } else {
  38. resThread.meta.postsRemaining =
  39. resThread.postsCount - lastPost.postNumber - 1
  40. if(resThread.meta.postsRemaining < limit) {
  41. resThread.meta.nextPostsCount = resThread.meta.postsRemaining
  42. } else {
  43. resThread.meta.nextPostsCount = limit
  44. }
  45. if(firstPost.postNumber === 0) {
  46. resThread.meta.previousPostsCount = 0
  47. } else if(firstPost.postNumber - limit < 0) {
  48. resThread.meta.previousPostsCount = firstPost.postNumber
  49. } else {
  50. resThread.meta.previousPostsCount = limit
  51. }
  52. }
  53. res.json(resThread)
  54. } catch (e) {
  55. if(e.name === 'invalidParameter') {
  56. res.status(400)
  57. res.json({
  58. errors: [e]
  59. })
  60. } else {
  61. console.log(e)
  62. res.status(500)
  63. res.json({
  64. errors: [Errors.unknown]
  65. })
  66. }
  67. }
  68. })
  69. router.all('*', (req, res, next) => {
  70. if(req.session.loggedIn) {
  71. next()
  72. } else {
  73. res.status(401)
  74. res.json({
  75. errors: [Errors.requestNotAuthorized]
  76. })
  77. }
  78. })
  79. router.post('/', async (req, res) => {
  80. let validationErrors = []
  81. try {
  82. if(req.body.name === undefined) {
  83. validationErrors.push(Errors.missingParameter('name'))
  84. } else if(typeof req.body.name !== 'string') {
  85. validationErrors.push(Errors.invalidParameterType('name', 'string'))
  86. }
  87. if(req.body.category === undefined) {
  88. validationErrors.push(Errors.missingParameter('category'))
  89. } else if(typeof req.body.category !== 'string') {
  90. validationErrors.push(Errors.invalidParameterType('category', 'string'))
  91. }
  92. if(validationErrors.length) throw Errors.VALIDATION_ERROR
  93. let category = await Category.findOne({ where: {
  94. name: req.body.category
  95. }})
  96. if(!category) throw Errors.invalidCategory
  97. let user = await User.findOne({ where: {
  98. username: req.session.username
  99. }})
  100. let thread = await Thread.create({
  101. name: req.body.name
  102. })
  103. await thread.setCategory(category)
  104. await thread.setUser(user)
  105. res.json(await thread.reload({
  106. include: [
  107. { model: User, attributes: ['username', 'createdAt', 'updatedAt', 'id'] },
  108. Category
  109. ]
  110. }))
  111. req.app.get('io').emit('new thread', {
  112. name: category.name,
  113. value: category.value
  114. })
  115. } catch (e) {
  116. if(e === Errors.VALIDATION_ERROR) {
  117. res.status(400)
  118. res.json({
  119. errors: validationErrors
  120. })
  121. } else if(e === Errors.invalidCategory) {
  122. res.status(400)
  123. res.json({
  124. errors: [Errors.invalidCategory]
  125. })
  126. } else {
  127. console.log(e)
  128. res.status(500)
  129. res.json({
  130. errors: [Errors.unknown]
  131. })
  132. }
  133. }
  134. })
  135. module.exports = router