page.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. const loggerFactory = require('@alias/logger');
  2. const logger = loggerFactory('growi:routes:apiv3:page'); // eslint-disable-line no-unused-vars
  3. const express = require('express');
  4. const { body, query } = require('express-validator');
  5. const router = express.Router();
  6. const ErrorV3 = require('../../models/vo/error-apiv3');
  7. /**
  8. * @swagger
  9. * tags:
  10. * name: Page
  11. */
  12. /**
  13. * @swagger
  14. *
  15. * components:
  16. * schemas:
  17. * Page:
  18. * description: Page
  19. * type: object
  20. * properties:
  21. * _id:
  22. * type: string
  23. * description: page ID
  24. * example: 5e07345972560e001761fa63
  25. * __v:
  26. * type: number
  27. * description: DB record version
  28. * example: 0
  29. * commentCount:
  30. * type: number
  31. * description: count of comments
  32. * example: 3
  33. * createdAt:
  34. * type: string
  35. * description: date created at
  36. * example: 2010-01-01T00:00:00.000Z
  37. * creator:
  38. * $ref: '#/components/schemas/User'
  39. * extended:
  40. * type: object
  41. * description: extend data
  42. * example: {}
  43. * grant:
  44. * type: number
  45. * description: grant
  46. * example: 1
  47. * grantedUsers:
  48. * type: array
  49. * description: granted users
  50. * items:
  51. * type: string
  52. * description: user ID
  53. * example: ["5ae5fccfc5577b0004dbd8ab"]
  54. * lastUpdateUser:
  55. * $ref: '#/components/schemas/User'
  56. * liker:
  57. * type: array
  58. * description: granted users
  59. * items:
  60. * type: string
  61. * description: user ID
  62. * example: []
  63. * path:
  64. * type: string
  65. * description: page path
  66. * example: /
  67. * redirectTo:
  68. * type: string
  69. * description: redirect path
  70. * example: ""
  71. * revision:
  72. * type: string
  73. * description: page revision
  74. * seenUsers:
  75. * type: array
  76. * description: granted users
  77. * items:
  78. * type: string
  79. * description: user ID
  80. * example: ["5ae5fccfc5577b0004dbd8ab"]
  81. * status:
  82. * type: string
  83. * description: status
  84. * enum:
  85. * - 'wip'
  86. * - 'published'
  87. * - 'deleted'
  88. * - 'deprecated'
  89. * example: published
  90. * updatedAt:
  91. * type: string
  92. * description: date updated at
  93. * example: 2010-01-01T00:00:00.000Z
  94. *
  95. * LikeParams:
  96. * description: LikeParams
  97. * type: object
  98. * properties:
  99. * pageId:
  100. * type: string
  101. * description: page ID
  102. * example: 5e07345972560e001761fa63
  103. * bool:
  104. * type: boolean
  105. * description: boolean for like status
  106. */
  107. module.exports = (crowi) => {
  108. const accessTokenParser = require('../../middlewares/access-token-parser')(crowi);
  109. const loginRequired = require('../../middlewares/login-required')(crowi);
  110. const csrf = require('../../middlewares/csrf')(crowi);
  111. const apiV3FormValidator = require('../../middlewares/apiv3-form-validator')(crowi);
  112. const { exportService } = crowi;
  113. const globalNotificationService = crowi.getGlobalNotificationService();
  114. const { Page, GlobalNotificationSetting } = crowi.models;
  115. const validator = {
  116. likes: [
  117. body('pageId').isString(),
  118. body('bool').isBoolean(),
  119. ],
  120. export: [
  121. query('pageId').isString(),
  122. query('revisionId').isString(),
  123. ],
  124. };
  125. /**
  126. * @swagger
  127. *
  128. * /page/likes:
  129. * put:
  130. * tags: [Page]
  131. * summary: /page/likes
  132. * description: Update liked status
  133. * operationId: updateLikedStatus
  134. * requestBody:
  135. * content:
  136. * application/json:
  137. * schema:
  138. * $ref: '#/components/schemas/LikeParams'
  139. * responses:
  140. * 200:
  141. * description: Succeeded to update liked status.
  142. * content:
  143. * application/json:
  144. * schema:
  145. * $ref: '#/components/schemas/Page'
  146. */
  147. router.put('/likes', accessTokenParser, loginRequired, csrf, validator.likes, apiV3FormValidator, async(req, res) => {
  148. const { pageId, bool } = req.body;
  149. let page;
  150. try {
  151. page = await Page.findByIdAndViewer(pageId, req.user);
  152. if (page == null) {
  153. return res.apiv3Err(`Page '${pageId}' is not found or forbidden`);
  154. }
  155. if (bool) {
  156. page = await page.like(req.user);
  157. }
  158. else {
  159. page = await page.unlike(req.user);
  160. }
  161. }
  162. catch (err) {
  163. logger.error('update-like-failed', err);
  164. return res.apiv3Err(err, 500);
  165. }
  166. try {
  167. // global notification
  168. await globalNotificationService.fire(GlobalNotificationSetting.EVENT.PAGE_LIKE, page, req.user);
  169. }
  170. catch (err) {
  171. logger.error('Like notification failed', err);
  172. }
  173. const result = { page };
  174. result.seenUser = page.seenUsers;
  175. return res.apiv3({ result });
  176. });
  177. /**
  178. * @swagger
  179. *
  180. * /pages/export:
  181. * get:
  182. * tags: [Export]
  183. * description: return page's markdown
  184. * responses:
  185. * 200:
  186. * description: Return page's markdown
  187. */
  188. router.get('/export', validator.export, async(req, res) => {
  189. try {
  190. const { type, pageId = null, revisionId = null } = req.query;
  191. if (pageId == null) {
  192. return res.apiv3Err(new ErrorV3('Should provided pageId or both pageId and revisionId.'));
  193. }
  194. const isPageExist = await Page.count({ _id: pageId }) > 0;
  195. if (!isPageExist) {
  196. return res.apiv3Err(new ErrorV3(`Page ${pageId} is not exist.`), 404);
  197. }
  198. const isAccessible = await Page.isAccessiblePageByViewer(pageId, req.user);
  199. if (!isAccessible) {
  200. return res.apiv3Err(new ErrorV3(`Haven't the right to see the page ${pageId}.`), 403);
  201. }
  202. let revisionIdForFind;
  203. if (revisionId == null) {
  204. const Page = crowi.model('Page');
  205. const page = await Page.findByIdAndViewer(pageId);
  206. revisionIdForFind = page.revision;
  207. }
  208. else {
  209. revisionIdForFind = revisionId;
  210. }
  211. const Revision = crowi.model('Revision');
  212. const revision = await Revision.findById(revisionIdForFind);
  213. const markdown = revision.body;
  214. const data = type === 'pdf' ? await exportService.convertMdToPdf(markdown) : markdown;
  215. return res.send(data);
  216. }
  217. catch (err) {
  218. logger.error('Failed to get page', err);
  219. return res.apiv3Err(err, 500);
  220. }
  221. });
  222. return router;
  223. };