bookmarks.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. const loggerFactory = require('@alias/logger');
  2. const logger = loggerFactory('growi:routes:apiv3:bookmark'); // 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. module.exports = (crowi) => {
  38. const accessTokenParser = require('../../middleware/access-token-parser')(crowi);
  39. const loginRequired = require('../../middleware/login-required')(crowi);
  40. const csrf = require('../../middleware/csrf')(crowi);
  41. const { Page, Bookmark } = crowi.models;
  42. const { ApiV3FormValidator } = crowi.middlewares;
  43. const validator = {
  44. bookmarks: [
  45. body('pageId').isString(),
  46. body('isBookmarked').isBoolean(),
  47. ],
  48. };
  49. /**
  50. * @swagger
  51. *
  52. * /bookmarks.get:
  53. * get:
  54. * tags: [Bookmarks, CrowiCompatibles]
  55. * operationId: getBookmark
  56. * summary: /bookmarks.get
  57. * description: Get bookmark of the page with the user
  58. * parameters:
  59. * - in: query
  60. * name: page_id
  61. * required: true
  62. * schema:
  63. * $ref: '#/components/schemas/Page/properties/_id'
  64. * responses:
  65. * 200:
  66. * description: Succeeded to get bookmark of the page with the user.
  67. * content:
  68. * application/json:
  69. * schema:
  70. * properties:
  71. * ok:
  72. * $ref: '#/components/schemas/V1Response/properties/ok'
  73. * bookmark:
  74. * $ref: '#/components/schemas/Bookmark'
  75. * 403:
  76. * $ref: '#/components/responses/403'
  77. * 500:
  78. * $ref: '#/components/responses/500'
  79. */
  80. // actions.api.get = function(req, res) {
  81. // const pageId = req.query.page_id;
  82. // Bookmark.findByPageIdAndUserId(pageId, req.user)
  83. // .then((data) => {
  84. // debug('bookmark found', pageId, data);
  85. // const result = {};
  86. // result.bookmark = data;
  87. // return res.json(ApiResponse.success(result));
  88. // })
  89. // .catch((err) => {
  90. // return res.json(ApiResponse.error(err));
  91. // });
  92. // };
  93. // actions.api.list = function(req, res) {
  94. // const paginateOptions = ApiPaginate.parseOptions(req.query);
  95. // const options = Object.assign(paginateOptions, { populatePage: true });
  96. // Bookmark.findByUserId(req.user._id, options)
  97. // .then((result) => {
  98. // return res.json(ApiResponse.success(result));
  99. // })
  100. // .catch((err) => {
  101. // return res.json(ApiResponse.error(err));
  102. // });
  103. // };
  104. /**
  105. * @swagger
  106. *
  107. * /bookmarks.add:
  108. * post:
  109. * tags: [Bookmarks, CrowiCompatibles]
  110. * operationId: addBookmark
  111. * summary: /bookmarks.add
  112. * description: Add bookmark of the page
  113. * parameters:
  114. * - in: query
  115. * name: page_id
  116. * schema:
  117. * $ref: '#/components/schemas/Page/properties/_id'
  118. * required: true
  119. * responses:
  120. * 200:
  121. * description: Succeeded to add bookmark of the page.
  122. * content:
  123. * application/json:
  124. * schema:
  125. * properties:
  126. * ok:
  127. * $ref: '#/components/schemas/V1Response/properties/ok'
  128. * bookmark:
  129. * $ref: '#/components/schemas/Bookmark'
  130. * 403:
  131. * $ref: '#/components/responses/403'
  132. * 500:
  133. * $ref: '#/components/responses/500'
  134. */
  135. router.put('/bookmarks', accessTokenParser, loginRequired, csrf, validator.bookmarks, ApiV3FormValidator, async(req, res) => {
  136. const { pageId, isBookmarked } = req.body;
  137. let bookmark;
  138. try {
  139. const page = await Page.findByIdAndViewer(pageId, req.user);
  140. if (page == null) {
  141. return res.apiv3Err(`Page '${pageId}' is not found or forbidden`);
  142. }
  143. if (isBookmarked) {
  144. bookmark = await Bookmark.removeBookmark(page, req.user);
  145. }
  146. else {
  147. bookmark = await Bookmark.add(page, req.user);
  148. }
  149. }
  150. catch (err) {
  151. logger.error('update-bookmark-failed', err);
  152. return res.apiv3Err(err, 500);
  153. }
  154. bookmark.depopulate('page');
  155. bookmark.depopulate('user');
  156. return res.apiv3({ bookmark });
  157. });
  158. /**
  159. * @swagger
  160. *
  161. * /bookmarks.remove:
  162. * post:
  163. * tags: [Bookmarks, CrowiCompatibles]
  164. * operationId: removeBookmark
  165. * summary: /bookmarks.remove
  166. * description: Remove bookmark of the page
  167. * requestBody:
  168. * content:
  169. * application/json:
  170. * schema:
  171. * properties:
  172. * page_id:
  173. * $ref: '#/components/schemas/Page/properties/_id'
  174. * required:
  175. * - page_id
  176. * responses:
  177. * 200:
  178. * description: Succeeded to remove bookmark of the page.
  179. * content:
  180. * application/json:
  181. * schema:
  182. * properties:
  183. * ok:
  184. * $ref: '#/components/schemas/V1Response/properties/ok'
  185. * 403:
  186. * $ref: '#/components/responses/403'
  187. * 500:
  188. * $ref: '#/components/responses/500'
  189. */
  190. /**
  191. * @api {post} /bookmarks.remove Remove bookmark of the page
  192. * @apiName RemoveBookmark
  193. * @apiGroup Bookmark
  194. *
  195. * @apiParam {String} page_id Page Id.
  196. */
  197. // actions.api.remove = function(req, res) {
  198. // const pageId = req.body.page_id;
  199. // Bookmark.removeBookmark(pageId, req.user)
  200. // .then((data) => {
  201. // debug('Bookmark removed.', data); // if the bookmark is not exists, this 'data' is null
  202. // return res.json(ApiResponse.success());
  203. // })
  204. // .catch((err) => {
  205. // return res.json(ApiResponse.error(err));
  206. // });
  207. // };
  208. return router;
  209. };