thread.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. //Only logged in routes
  70. router.all('*', (req, res, next) => {
  71. if(req.session.loggedIn) {
  72. next()
  73. } else {
  74. res.status(401)
  75. res.json({
  76. errors: [Errors.requestNotAuthorized]
  77. })
  78. }
  79. })
  80. router.post('/', async (req, res) => {
  81. let validationErrors = []
  82. try {
  83. if(req.body.name === undefined) {
  84. validationErrors.push(Errors.missingParameter('name'))
  85. } else if(typeof req.body.name !== 'string') {
  86. validationErrors.push(Errors.invalidParameterType('name', 'string'))
  87. } else if(req.body.name.length === 0) {
  88. validationErrors.push(Errors.missingParameter('name'))
  89. }
  90. if(req.body.category === undefined) {
  91. validationErrors.push(Errors.missingParameter('category'))
  92. } else if(typeof req.body.category !== 'string') {
  93. validationErrors.push(Errors.invalidParameterType('category', 'string'))
  94. }
  95. if(validationErrors.length) throw Errors.VALIDATION_ERROR
  96. let category = await Category.findOne({ where: {
  97. value: req.body.category
  98. }})
  99. if(!category) throw Errors.invalidCategory
  100. let user = await User.findOne({ where: {
  101. username: req.session.username
  102. }})
  103. let thread = await Thread.create({
  104. name: req.body.name
  105. })
  106. await thread.setCategory(category)
  107. await thread.setUser(user)
  108. res.json(await thread.reload({
  109. include: [
  110. { model: User, attributes: ['username', 'createdAt', 'updatedAt', 'id'] },
  111. Category
  112. ]
  113. }))
  114. req.app.get('io').to('index').emit('new thread', {
  115. name: category.name,
  116. value: category.value
  117. })
  118. } catch (e) {
  119. if(e === Errors.VALIDATION_ERROR) {
  120. res.status(400)
  121. res.json({
  122. errors: validationErrors
  123. })
  124. } else if(e === Errors.invalidCategory) {
  125. res.status(400)
  126. res.json({
  127. errors: [Errors.invalidCategory]
  128. })
  129. } else {
  130. console.log(e)
  131. res.status(500)
  132. res.json({
  133. errors: [Errors.unknown]
  134. })
  135. }
  136. }
  137. })
  138. //Only admin routes
  139. router.all('*', (req, res, next) => {
  140. if(req.session.admin) {
  141. next()
  142. } else {
  143. res.status(401)
  144. res.json({
  145. errors: [Errors.requestNotAuthorized]
  146. })
  147. }
  148. })
  149. router.put('/:thread_id', async (req, res) => {
  150. try {
  151. let thread = await Thread.findById(req.params.thread_id)
  152. if(!thread) {
  153. res.status(400)
  154. res.json({ errors:
  155. [Errors.invalidParameter('threadId', 'thread does not exist')]
  156. })
  157. } else {
  158. if(req.body.locked) {
  159. await thread.update({ locked: true })
  160. } else {
  161. await thread.update({ locked: false })
  162. }
  163. res.json({ success: true })
  164. }
  165. } catch (e) {
  166. console.log(e)
  167. res.status(500)
  168. res.json({
  169. errors: [Errors.unknown]
  170. })
  171. }
  172. })
  173. module.exports = router