page.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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 globalNotificationService = crowi.getGlobalNotificationService();
  113. const { Page, GlobalNotificationSetting } = crowi.models;
  114. const { exportService } = crowi;
  115. const validator = {
  116. likes: [
  117. body('pageId').isString(),
  118. body('bool').isBoolean(),
  119. ],
  120. export: [
  121. query('format').isString().isIn(['md', 'pdf']),
  122. query('revisionId').isString(),
  123. ],
  124. archive: [
  125. body('rootPagePath').isString(),
  126. body('isCommentDownload').isBoolean(),
  127. body('isAttachmentFileDownload').isBoolean(),
  128. body('isSubordinatedPageDownload').isBoolean(),
  129. body('fileType').isString().isIn(['pdf', 'markdown']),
  130. body('hierarchyType').isString().isIn(['allSubordinatedPage', 'decideHierarchy']),
  131. body('hierarchyValue').isNumeric(),
  132. ],
  133. };
  134. /**
  135. * @swagger
  136. *
  137. * /page/likes:
  138. * put:
  139. * tags: [Page]
  140. * summary: /page/likes
  141. * description: Update liked status
  142. * operationId: updateLikedStatus
  143. * requestBody:
  144. * content:
  145. * application/json:
  146. * schema:
  147. * $ref: '#/components/schemas/LikeParams'
  148. * responses:
  149. * 200:
  150. * description: Succeeded to update liked status.
  151. * content:
  152. * application/json:
  153. * schema:
  154. * $ref: '#/components/schemas/Page'
  155. */
  156. router.put('/likes', accessTokenParser, loginRequired, csrf, validator.likes, apiV3FormValidator, async(req, res) => {
  157. const { pageId, bool } = req.body;
  158. let page;
  159. try {
  160. page = await Page.findByIdAndViewer(pageId, req.user);
  161. if (page == null) {
  162. return res.apiv3Err(`Page '${pageId}' is not found or forbidden`);
  163. }
  164. if (bool) {
  165. page = await page.like(req.user);
  166. }
  167. else {
  168. page = await page.unlike(req.user);
  169. }
  170. }
  171. catch (err) {
  172. logger.error('update-like-failed', err);
  173. return res.apiv3Err(err, 500);
  174. }
  175. try {
  176. // global notification
  177. await globalNotificationService.fire(GlobalNotificationSetting.EVENT.PAGE_LIKE, page, req.user);
  178. }
  179. catch (err) {
  180. logger.error('Like notification failed', err);
  181. }
  182. const result = { page };
  183. result.seenUser = page.seenUsers;
  184. return res.apiv3({ result });
  185. });
  186. /**
  187. * @swagger
  188. *
  189. * /pages/export:
  190. * get:
  191. * tags: [Export]
  192. * description: return page's markdown
  193. * responses:
  194. * 200:
  195. * description: Return page's markdown
  196. */
  197. router.get('/export/:pageId', loginRequired, validator.export, async(req, res) => {
  198. const { pageId } = req.params;
  199. const { format, revisionId = null } = req.query;
  200. let revision;
  201. try {
  202. const Page = crowi.model('Page');
  203. const page = await Page.findByIdAndViewer(pageId, req.user);
  204. if (page == null) {
  205. const isPageExist = await Page.count({ _id: pageId }) > 0;
  206. if (isPageExist) {
  207. // This page exists but req.user has not read permission
  208. return res.apiv3Err(new ErrorV3(`Haven't the right to see the page ${pageId}.`), 403);
  209. }
  210. return res.apiv3Err(new ErrorV3(`Page ${pageId} is not exist.`), 404);
  211. }
  212. const revisionIdForFind = revisionId || page.revision;
  213. const Revision = crowi.model('Revision');
  214. revision = await Revision.findById(revisionIdForFind);
  215. }
  216. catch (err) {
  217. logger.error('Failed to get page data', err);
  218. return res.apiv3Err(err, 500);
  219. }
  220. const fileName = revision.id;
  221. let stream;
  222. try {
  223. stream = exportService.getReadStreamFromRevision(revision, format);
  224. }
  225. catch (err) {
  226. logger.error('Failed to create readStream', err);
  227. return res.apiv3Err(err, 500);
  228. }
  229. res.set({
  230. 'Content-Disposition': `attachment;filename*=UTF-8''${fileName}.${format}`,
  231. });
  232. return stream.pipe(res);
  233. });
  234. // TODO GW-2746 bulk export pages
  235. // /**
  236. // * @swagger
  237. // *
  238. // * /page/archive:
  239. // * post:
  240. // * tags: [Page]
  241. // * summary: /page/archive
  242. // * description: create page archive
  243. // * requestBody:
  244. // * content:
  245. // * application/json:
  246. // * schema:
  247. // * properties:
  248. // * rootPagePath:
  249. // * type: string
  250. // * description: path of the root page
  251. // * isCommentDownload:
  252. // * type: boolean
  253. // * description: whether archive data contains comments
  254. // * isAttachmentFileDownload:
  255. // * type: boolean
  256. // * description: whether archive data contains attachments
  257. // * isSubordinatedPageDownload:
  258. // * type: boolean
  259. // * description: whether archive data children pages
  260. // * fileType:
  261. // * type: string
  262. // * description: file type of archive data(.md, .pdf)
  263. // * hierarchyType:
  264. // * type: string
  265. // * description: method of select children pages archive data contains('allSubordinatedPage', 'decideHierarchy')
  266. // * hierarchyValue:
  267. // * type: number
  268. // * description: depth of hierarchy(use when hierarchyType is 'decideHierarchy')
  269. // * responses:
  270. // * 200:
  271. // * description: create page archive
  272. // * content:
  273. // * application/json:
  274. // * schema:
  275. // * $ref: '#/components/schemas/Page'
  276. // */
  277. // router.post('/archive', accessTokenParser, loginRequired, csrf, validator.archive, apiV3FormValidator, async(req, res) => {
  278. // const PageArchive = crowi.model('PageArchive');
  279. // const {
  280. // rootPagePath,
  281. // isCommentDownload,
  282. // isAttachmentFileDownload,
  283. // fileType,
  284. // } = req.body;
  285. // const owner = req.user._id;
  286. // const numOfPages = 1; // TODO 最終的にzipファイルに取り込むページ数を入れる
  287. // const createdPageArchive = PageArchive.create({
  288. // owner,
  289. // fileType,
  290. // rootPagePath,
  291. // numOfPages,
  292. // hasComment: isCommentDownload,
  293. // hasAttachment: isAttachmentFileDownload,
  294. // });
  295. // console.log(createdPageArchive);
  296. // return res.apiv3({ });
  297. // });
  298. // router.get('/count-children-pages', accessTokenParser, loginRequired, async(req, res) => {
  299. // // TO DO implement correct number at another task
  300. // const { pageId } = req.query;
  301. // console.log(pageId);
  302. // const dummy = 6;
  303. // return res.apiv3({ dummy });
  304. // });
  305. return router;
  306. };