revision.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /**
  2. * @swagger
  3. * tags:
  4. * name: Revisions
  5. */
  6. /**
  7. * @swagger
  8. *
  9. * components:
  10. * schemas:
  11. * Revision:
  12. * description: Revision
  13. * type: object
  14. * properties:
  15. * _id:
  16. * type: string
  17. * description: revision ID
  18. * example: 5e0734e472560e001761fa68
  19. * __v:
  20. * type: number
  21. * description: DB record version
  22. * example: 0
  23. * author:
  24. * $ref: '#/components/schemas/User/properties/_id'
  25. * body:
  26. * type: string
  27. * description: content body
  28. * example: |
  29. * # test
  30. *
  31. * test
  32. * format:
  33. * type: string
  34. * description: format
  35. * example: markdown
  36. * path:
  37. * type: string
  38. * description: path
  39. * example: /user/alice/test
  40. * createdAt:
  41. * type: string
  42. * description: date created at
  43. * example: 2010-01-01T00:00:00.000Z
  44. */
  45. module.exports = function(crowi, app) {
  46. const logger = require('@alias/logger')('growi:routes:revision');
  47. const Page = crowi.model('Page');
  48. const Revision = crowi.model('Revision');
  49. const User = crowi.model('User');
  50. const ApiResponse = require('../util/apiResponse');
  51. const actions = {};
  52. actions.api = {};
  53. /**
  54. * @swagger
  55. *
  56. * /revisions.get:
  57. * get:
  58. * tags: [Revisions, CrowiCompatibles]
  59. * operationId: revisions.get
  60. * summary: /revisions.get
  61. * description: Get revision
  62. * parameters:
  63. * - in: query
  64. * name: page_id
  65. * schema:
  66. * $ref: '#/components/schemas/Page/properties/_id'
  67. * required: true
  68. * - in: query
  69. * name: revision_id
  70. * schema:
  71. * $ref: '#/components/schemas/Revision/properties/_id'
  72. * required: true
  73. * responses:
  74. * 200:
  75. * description: Succeeded to get revision.
  76. * content:
  77. * application/json:
  78. * schema:
  79. * properties:
  80. * ok:
  81. * $ref: '#/components/schemas/V1Response/properties/ok'
  82. * revision:
  83. * $ref: '#/components/schemas/Revision'
  84. * 403:
  85. * $ref: '#/components/responses/403'
  86. * 500:
  87. * $ref: '#/components/responses/500'
  88. */
  89. /**
  90. * @api {get} /revisions.get Get revision
  91. * @apiName GetRevision
  92. * @apiGroup Revision
  93. *
  94. * @apiParam {String} page_id Page Id.
  95. * @apiParam {String} revision_id Revision Id.
  96. */
  97. actions.api.get = async function(req, res) {
  98. const pageId = req.query.page_id;
  99. const revisionId = req.query.revision_id;
  100. const { isSharedPage } = req;
  101. if (!pageId || !revisionId) {
  102. return res.json(ApiResponse.error('Parameter page_id and revision_id are required.'));
  103. }
  104. // check whether accessible
  105. if (!isSharedPage && !(await Page.isAccessiblePageByViewer(pageId, req.user))) {
  106. return res.json(ApiResponse.error('Current user is not accessible to this page.'));
  107. }
  108. try {
  109. const revision = await Revision.findById(revisionId).populate('author', User.USER_PUBLIC_FIELDS);
  110. return res.json(ApiResponse.success({ revision }));
  111. }
  112. catch (err) {
  113. logger.error('Error revisios.get', err);
  114. return res.json(ApiResponse.error(err));
  115. }
  116. };
  117. /**
  118. * @swagger
  119. *
  120. * /revisions.ids:
  121. * get:
  122. * tags: [Revisions, CrowiCompatibles]
  123. * operationId: revisions.ids
  124. * summary: /revisions.ids
  125. * description: Get revision id list of the page
  126. * parameters:
  127. * - in: query
  128. * name: page_id
  129. * schema:
  130. * $ref: '#/components/schemas/Page/properties/_id'
  131. * required: true
  132. * responses:
  133. * 200:
  134. * description: Succeeded to get revision id list of the page.
  135. * content:
  136. * application/json:
  137. * schema:
  138. * properties:
  139. * ok:
  140. * $ref: '#/components/schemas/V1Response/properties/ok'
  141. * revisions:
  142. * type: array
  143. * items:
  144. * $ref: '#/components/schemas/Revision'
  145. * 403:
  146. * $ref: '#/components/responses/403'
  147. * 500:
  148. * $ref: '#/components/responses/500'
  149. */
  150. /**
  151. * @api {get} /revisions.ids Get revision id list of the page
  152. * @apiName ids
  153. * @apiGroup Revision
  154. *
  155. * @apiParam {String} page_id Page Id.
  156. */
  157. actions.api.ids = async function(req, res) {
  158. const pageId = req.query.page_id;
  159. const { isSharedPage } = req;
  160. if (pageId == null) {
  161. return res.json(ApiResponse.error('Parameter page_id is required.'));
  162. }
  163. // check whether accessible
  164. if (!isSharedPage && !(await Page.isAccessiblePageByViewer(pageId, req.user))) {
  165. return res.json(ApiResponse.error('Current user is not accessible to this page.'));
  166. }
  167. try {
  168. const page = await Page.findOne({ _id: pageId });
  169. const revisions = await Revision.findRevisionIdList(page.path);
  170. return res.json(ApiResponse.success({ revisions }));
  171. }
  172. catch (err) {
  173. logger.error('Error revisios.ids', err);
  174. return res.json(ApiResponse.error(err));
  175. }
  176. };
  177. return actions;
  178. };