thread.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. } else if(req.body.name.length === 0) {
  87. validationErrors.push(Errors.missingParameter('name'))
  88. }
  89. if(req.body.category === undefined) {
  90. validationErrors.push(Errors.missingParameter('category'))
  91. } else if(typeof req.body.category !== 'string') {
  92. validationErrors.push(Errors.invalidParameterType('category', 'string'))
  93. }
  94. if(validationErrors.length) throw Errors.VALIDATION_ERROR
  95. let category = await Category.findOne({ where: {
  96. value: req.body.category
  97. }})
  98. if(!category) throw Errors.invalidCategory
  99. let user = await User.findOne({ where: {
  100. username: req.session.username
  101. }})
  102. let thread = await Thread.create({
  103. name: req.body.name
  104. })
  105. await thread.setCategory(category)
  106. await thread.setUser(user)
  107. res.json(await thread.reload({
  108. include: [
  109. { model: User, attributes: ['username', 'createdAt', 'updatedAt', 'id'] },
  110. Category
  111. ]
  112. }))
  113. req.app.get('io').to('index').emit('new thread', {
  114. name: category.name,
  115. value: category.value
  116. })
  117. } catch (e) {
  118. if(e === Errors.VALIDATION_ERROR) {
  119. res.status(400)
  120. res.json({
  121. errors: validationErrors
  122. })
  123. } else if(e === Errors.invalidCategory) {
  124. res.status(400)
  125. res.json({
  126. errors: [Errors.invalidCategory]
  127. })
  128. } else {
  129. console.log(e)
  130. res.status(500)
  131. res.json({
  132. errors: [Errors.unknown]
  133. })
  134. }
  135. }
  136. })
  137. module.exports = router