revision.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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 debug = require('debug')('growi:routes:revision');
  47. const logger = require('@alias/logger')('growi:routes:revision');
  48. const Page = crowi.model('Page');
  49. const Revision = crowi.model('Revision');
  50. const User = crowi.model('User');
  51. const ApiResponse = require('../util/apiResponse');
  52. const actions = {};
  53. actions.api = {};
  54. /**
  55. * @swagger
  56. *
  57. * /revisions.get:
  58. * get:
  59. * tags: [Revisions, CrowiCompatibles]
  60. * operationId: revisions.get
  61. * summary: /revisions.get
  62. * description: Get revision
  63. * parameters:
  64. * - in: query
  65. * name: page_id
  66. * schema:
  67. * $ref: '#/components/schemas/Page/properties/_id'
  68. * required: true
  69. * - in: query
  70. * name: revision_id
  71. * schema:
  72. * $ref: '#/components/schemas/Revision/properties/_id'
  73. * required: true
  74. * responses:
  75. * 200:
  76. * description: Succeeded to get revision.
  77. * content:
  78. * application/json:
  79. * schema:
  80. * properties:
  81. * ok:
  82. * $ref: '#/components/schemas/V1Response/properties/ok'
  83. * revision:
  84. * $ref: '#/components/schemas/Revision'
  85. * 403:
  86. * $ref: '#/components/responses/403'
  87. * 500:
  88. * $ref: '#/components/responses/500'
  89. */
  90. /**
  91. * @api {get} /revisions.get Get revision
  92. * @apiName GetRevision
  93. * @apiGroup Revision
  94. *
  95. * @apiParam {String} page_id Page Id.
  96. * @apiParam {String} revision_id Revision Id.
  97. */
  98. actions.api.get = async function(req, res) {
  99. const pageId = req.query.page_id;
  100. const revisionId = req.query.revision_id;
  101. if (!pageId || !revisionId) {
  102. return res.json(ApiResponse.error('Parameter page_id and revision_id are required.'));
  103. }
  104. // check whether accessible
  105. const isAccessible = await Page.isAccessiblePageByViewer(pageId, req.user);
  106. if (!isAccessible) {
  107. return res.json(ApiResponse.error('Current user is not accessible to this page.'));
  108. }
  109. try {
  110. const revision = await Revision.findById(revisionId).populate('author', User.USER_PUBLIC_FIELDS);
  111. return res.json(ApiResponse.success({ revision }));
  112. }
  113. catch (err) {
  114. logger.error('Error revisios.get', err);
  115. return res.json(ApiResponse.error(err));
  116. }
  117. };
  118. /**
  119. * @swagger
  120. *
  121. * /revisions.ids:
  122. * get:
  123. * tags: [Revisions, CrowiCompatibles]
  124. * operationId: revisions.ids
  125. * summary: /revisions.ids
  126. * description: Get revision id list of the page
  127. * parameters:
  128. * - in: query
  129. * name: page_id
  130. * schema:
  131. * $ref: '#/components/schemas/Page/properties/_id'
  132. * required: true
  133. * responses:
  134. * 200:
  135. * description: Succeeded to get revision id list of the page.
  136. * content:
  137. * application/json:
  138. * schema:
  139. * properties:
  140. * ok:
  141. * $ref: '#/components/schemas/V1Response/properties/ok'
  142. * revisions:
  143. * type: array
  144. * items:
  145. * $ref: '#/components/schemas/Revision'
  146. * 403:
  147. * $ref: '#/components/responses/403'
  148. * 500:
  149. * $ref: '#/components/responses/500'
  150. */
  151. /**
  152. * @api {get} /revisions.ids Get revision id list of the page
  153. * @apiName ids
  154. * @apiGroup Revision
  155. *
  156. * @apiParam {String} page_id Page Id.
  157. */
  158. actions.api.ids = function(req, res) {
  159. const pageId = req.query.page_id || null;
  160. if (pageId && crowi.isPageId(pageId)) {
  161. Page.findByIdAndViewer(pageId, req.user)
  162. .then((pageData) => {
  163. debug('Page found', pageData._id, pageData.path);
  164. return Revision.findRevisionIdList(pageData.path);
  165. })
  166. .then((revisions) => {
  167. return res.json(ApiResponse.success({ revisions }));
  168. })
  169. .catch((err) => {
  170. return res.json(ApiResponse.error(err));
  171. });
  172. }
  173. else {
  174. return res.json(ApiResponse.error('Parameter error.'));
  175. }
  176. };
  177. /**
  178. * @swagger
  179. *
  180. * /revisions.list:
  181. * get:
  182. * tags: [Revisions, CrowiCompatibles]
  183. * operationId: revisions.list
  184. * summary: /revisions.list
  185. * description: Get revisions
  186. * parameters:
  187. * - in: query
  188. * name: page_id
  189. * schema:
  190. * $ref: '#/components/schemas/Page/properties/_id'
  191. * - in: query
  192. * name: revision_ids
  193. * schema:
  194. * type: string
  195. * description: revision ids
  196. * example: 5e0734e472560e001761fa68,5e079a0a0afa6700170a75fb
  197. * responses:
  198. * 200:
  199. * description: Succeeded to get revisions.
  200. * content:
  201. * application/json:
  202. * schema:
  203. * properties:
  204. * ok:
  205. * $ref: '#/components/schemas/V1Response/properties/ok'
  206. * revisions:
  207. * type: array
  208. * items:
  209. * $ref: '#/components/schemas/Revision'
  210. * 403:
  211. * $ref: '#/components/responses/403'
  212. * 500:
  213. * $ref: '#/components/responses/500'
  214. */
  215. /**
  216. * @api {get} /revisions.list Get revisions
  217. * @apiName ListRevision
  218. * @apiGroup Revision
  219. *
  220. * @apiParam {String} revision_ids Revision Ids.
  221. * @apiParam {String} page_id Page Id.
  222. */
  223. actions.api.list = function(req, res) {
  224. const revisionIds = (req.query.revision_ids || '').split(',');
  225. const pageId = req.query.page_id || null;
  226. if (pageId) {
  227. Page.findByIdAndViewer(pageId, req.user)
  228. .then((pageData) => {
  229. debug('Page found', pageData._id, pageData.path);
  230. return Revision.findRevisionList(pageData.path, {});
  231. })
  232. .then((revisions) => {
  233. return res.json(ApiResponse.success(revisions));
  234. })
  235. .catch((err) => {
  236. return res.json(ApiResponse.error(err));
  237. });
  238. }
  239. else if (revisionIds.length > 0) {
  240. Revision.findRevisions(revisionIds)
  241. .then((revisions) => {
  242. return res.json(ApiResponse.success(revisions));
  243. })
  244. .catch((err) => {
  245. return res.json(ApiResponse.error(err));
  246. });
  247. }
  248. else {
  249. return res.json(ApiResponse.error('Parameter error.'));
  250. }
  251. };
  252. return actions;
  253. };