bookmarks.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. // select page from bookmark where userid = userid
  96. router.get('/byUser', /* accessTokenParser, loginRequired, csrf, */ apiV3FormValidator, async(req, res) => {
  97. const { userId } = req.query;
  98. try {
  99. const bookmarks = await Bookmark
  100. .find({ user: userId })
  101. .populate({ path: 'page', model: 'Page' });
  102. const pageList = bookmarks.map((bookmark) => {
  103. return bookmark.page;
  104. });
  105. return res.apiv3({ pageList });
  106. }
  107. catch (err) {
  108. logger.error('get-bookmark-failed', err);
  109. return res.apiv3Err(err, 500);
  110. }
  111. });
  112. /**
  113. * @swagger
  114. *
  115. * /bookmarks:
  116. * put:
  117. * tags: [Bookmarks]
  118. * summary: /bookmarks
  119. * description: Update bookmarked status
  120. * operationId: updateBookmarkedStatus
  121. * requestBody:
  122. * content:
  123. * application/json:
  124. * schema:
  125. * $ref: '#/components/schemas/BookmarkParams'
  126. * responses:
  127. * 200:
  128. * description: Succeeded to update bookmarked status.
  129. * content:
  130. * application/json:
  131. * schema:
  132. * $ref: '#/components/schemas/Bookmark'
  133. */
  134. router.put('/', accessTokenParser, loginRequired, csrf, validator.bookmarks, apiV3FormValidator, async(req, res) => {
  135. const { pageId, bool } = req.body;
  136. let bookmark;
  137. try {
  138. const page = await Page.findByIdAndViewer(pageId, req.user);
  139. if (page == null) {
  140. return res.apiv3Err(`Page '${pageId}' is not found or forbidden`);
  141. }
  142. if (bool) {
  143. bookmark = await Bookmark.add(page, req.user);
  144. }
  145. else {
  146. bookmark = await Bookmark.removeBookmark(page, req.user);
  147. }
  148. }
  149. catch (err) {
  150. logger.error('update-bookmark-failed', err);
  151. return res.apiv3Err(err, 500);
  152. }
  153. bookmark.depopulate('page');
  154. bookmark.depopulate('user');
  155. return res.apiv3({ bookmark });
  156. });
  157. return router;
  158. };