bookmarks.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. const loggerFactory = require('@alias/logger');
  2. const logger = loggerFactory('growi:routes:apiv3:bookmarks'); // eslint-disable-line no-unused-vars
  3. const express = require('express');
  4. const { body } = require('express-validator');
  5. const router = express.Router();
  6. /**
  7. * @swagger
  8. * tags:
  9. * name: Bookmarks
  10. */
  11. /**
  12. * @swagger
  13. *
  14. * components:
  15. * schemas:
  16. * Bookmark:
  17. * description: Bookmark
  18. * type: object
  19. * properties:
  20. * _id:
  21. * type: string
  22. * description: page ID
  23. * example: 5e07345972560e001761fa63
  24. * __v:
  25. * type: number
  26. * description: DB record version
  27. * example: 0
  28. * createdAt:
  29. * type: string
  30. * description: date created at
  31. * example: 2010-01-01T00:00:00.000Z
  32. * page:
  33. * $ref: '#/components/schemas/Page/properties/_id'
  34. * user:
  35. * $ref: '#/components/schemas/User/properties/_id'
  36. *
  37. * BookmarkParams:
  38. * description: BookmarkParams
  39. * type: object
  40. * properties:
  41. * pageId:
  42. * type: string
  43. * description: page ID
  44. * example: 5e07345972560e001761fa63
  45. * bool:
  46. * type: boolean
  47. * description: boolean for bookmark status
  48. */
  49. module.exports = (crowi) => {
  50. const accessTokenParser = require('../../middlewares/access-token-parser')(crowi);
  51. const loginRequired = require('../../middlewares/login-required')(crowi);
  52. const csrf = require('../../middlewares/csrf')(crowi);
  53. const apiV3FormValidator = require('../../middlewares/apiv3-form-validator')(crowi);
  54. const { Page, Bookmark } = crowi.models;
  55. const validator = {
  56. bookmarks: [
  57. body('pageId').isString(),
  58. body('bool').isBoolean(),
  59. ],
  60. };
  61. /**
  62. * @swagger
  63. *
  64. * /bookmarks:
  65. * get:
  66. * tags: [Bookmarks]
  67. * summary: /bookmarks
  68. * description: Get bookmarked status
  69. * operationId: getBookmarkedStatus
  70. * parameters:
  71. * - name: pageId
  72. * in: query
  73. * description: page id
  74. * schema:
  75. * type: string
  76. * responses:
  77. * 200:
  78. * description: Succeeded to get bookmarked status.
  79. * content:
  80. * application/json:
  81. * schema:
  82. * $ref: '#/components/schemas/Bookmark'
  83. */
  84. router.get('/', accessTokenParser, loginRequired, async(req, res) => {
  85. const { pageId } = req.query;
  86. try {
  87. const bookmark = await Bookmark.findByPageIdAndUserId(pageId, req.user);
  88. return res.apiv3({ bookmark });
  89. }
  90. catch (err) {
  91. logger.error('get-bookmark-failed', err);
  92. return res.apiv3Err(err, 500);
  93. }
  94. });
  95. /**
  96. * @swagger
  97. *
  98. * /bookmarks:
  99. * put:
  100. * tags: [Bookmarks]
  101. * summary: /bookmarks
  102. * description: Update bookmarked status
  103. * operationId: updateBookmarkedStatus
  104. * requestBody:
  105. * content:
  106. * application/json:
  107. * schema:
  108. * $ref: '#/components/schemas/BookmarkParams'
  109. * responses:
  110. * 200:
  111. * description: Succeeded to update bookmarked status.
  112. * content:
  113. * application/json:
  114. * schema:
  115. * $ref: '#/components/schemas/Bookmark'
  116. */
  117. router.put('/', accessTokenParser, loginRequired, csrf, validator.bookmarks, apiV3FormValidator, async(req, res) => {
  118. const { pageId, bool } = req.body;
  119. let bookmark;
  120. try {
  121. const page = await Page.findByIdAndViewer(pageId, req.user);
  122. if (page == null) {
  123. return res.apiv3Err(`Page '${pageId}' is not found or forbidden`);
  124. }
  125. if (bool) {
  126. bookmark = await Bookmark.add(page, req.user);
  127. }
  128. else {
  129. bookmark = await Bookmark.removeBookmark(page, req.user);
  130. }
  131. }
  132. catch (err) {
  133. logger.error('update-bookmark-failed', err);
  134. return res.apiv3Err(err, 500);
  135. }
  136. bookmark.depopulate('page');
  137. bookmark.depopulate('user');
  138. return res.apiv3({ bookmark });
  139. });
  140. return router;
  141. };