customize-setting.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /* eslint-disable no-unused-vars */
  2. const loggerFactory = require('@alias/logger');
  3. const logger = loggerFactory('growi:routes:apiv3:customize-setting');
  4. const express = require('express');
  5. const router = express.Router();
  6. const { body } = require('express-validator/check');
  7. const ErrorV3 = require('../../models/vo/error-apiv3');
  8. const validator = {};
  9. /**
  10. * @swagger
  11. * tags:
  12. * name: CustomizeSetting
  13. */
  14. module.exports = (crowi) => {
  15. const loginRequiredStrictly = require('../../middleware/login-required')(crowi);
  16. const adminRequired = require('../../middleware/admin-required')(crowi);
  17. const csrf = require('../../middleware/csrf')(crowi);
  18. const { ApiV3FormValidator } = crowi.middlewares;
  19. // TODO GW-533 implement accurate validation
  20. const validator = {
  21. layoutTheme: [
  22. body('layoutType').isString(),
  23. body('themeType').isString(),
  24. ],
  25. behavior: [
  26. body('behaviorType').isString(),
  27. ],
  28. function: [
  29. body('isEnabledTimeline').isBoolean(),
  30. body('isSavedStatesOfTabChanges').isBoolean(),
  31. body('isEnabledAttachTitleHeader').isBoolean(),
  32. body('recentCreatedLimit').isInt(),
  33. ],
  34. highlight: [
  35. ],
  36. };
  37. // TODO GW-575 writte swagger
  38. router.get('/', loginRequiredStrictly, adminRequired, async(req, res) => {
  39. // TODO GW-575 return others customize settings
  40. /* eslint-disable quote-props, no-multi-spaces */
  41. const highlightJsCssSelectorOptions = {
  42. 'github': { name: '[Light] GitHub', border: false },
  43. 'github-gist': { name: '[Light] GitHub Gist', border: true },
  44. 'atom-one-light': { name: '[Light] Atom One Light', border: true },
  45. 'xcode': { name: '[Light] Xcode', border: true },
  46. 'vs': { name: '[Light] Vs', border: true },
  47. 'atom-one-dark': { name: '[Dark] Atom One Dark', border: false },
  48. 'hybrid': { name: '[Dark] Hybrid', border: false },
  49. 'monokai': { name: '[Dark] Monokai', border: false },
  50. 'tomorrow-night': { name: '[Dark] Tomorrow Night', border: false },
  51. 'vs2015': { name: '[Dark] Vs 2015', border: false },
  52. };
  53. /* eslint-enable quote-props, no-multi-spaces */
  54. return res.apiv3({ highlightJsCssSelectorOptions });
  55. });
  56. /**
  57. * @swagger
  58. *
  59. * /customize-setting/layoutTheme:
  60. * put:
  61. * tags: [CustomizeSetting]
  62. * description: Update layout and theme
  63. * requestBody:
  64. * required: true
  65. * content:
  66. * application/json:
  67. * schama:
  68. * type: object
  69. * properties:
  70. * layoutType:
  71. * description: type of layout
  72. * type: string
  73. * themeType:
  74. * description: type of theme
  75. * type: string
  76. * responses:
  77. * 200:
  78. * description: Succeeded to update layout and theme
  79. */
  80. router.put('/layoutTheme', loginRequiredStrictly, adminRequired, csrf, validator.layoutTheme, ApiV3FormValidator, async(req, res) => {
  81. const requestParams = {
  82. 'customize:layout': req.body.layoutType,
  83. 'customize:theme': req.body.themeType,
  84. };
  85. try {
  86. await crowi.configManager.updateConfigsInTheSameNamespace('crowi', requestParams);
  87. const customizedParams = {
  88. layoutType: await crowi.configManager.getConfig('crowi', 'customize:layout'),
  89. themeType: await crowi.configManager.getConfig('crowi', 'customize:theme'),
  90. };
  91. return res.apiv3({ customizedParams });
  92. }
  93. catch (err) {
  94. const msg = 'Error occurred in updating layout and theme';
  95. logger.error('Error', err);
  96. return res.apiv3Err(new ErrorV3(msg, 'update-layoutTheme-failed'));
  97. }
  98. });
  99. /**
  100. * @swagger
  101. *
  102. * /customize-setting/behavior:
  103. * put:
  104. * tags: [CustomizeSetting]
  105. * description: Update behavior
  106. * requestBody:
  107. * required: true
  108. * content:
  109. * application/json:
  110. * schama:
  111. * type: object
  112. * properties:
  113. * behaviorType:
  114. * description: type of behavior
  115. * type: string
  116. * responses:
  117. * 200:
  118. * description: Succeeded to update behavior
  119. */
  120. router.put('/behavior', loginRequiredStrictly, adminRequired, csrf, validator.behavior, ApiV3FormValidator, async(req, res) => {
  121. const requestParams = {
  122. 'customize:behavior': req.body.behaviorType,
  123. };
  124. try {
  125. await crowi.configManager.updateConfigsInTheSameNamespace('crowi', requestParams);
  126. const customizedParams = {
  127. behaviorType: await crowi.configManager.getConfig('crowi', 'customize:behavior'),
  128. };
  129. return res.apiv3({ customizedParams });
  130. }
  131. catch (err) {
  132. const msg = 'Error occurred in updating behavior';
  133. logger.error('Error', err);
  134. return res.apiv3Err(new ErrorV3(msg, 'update-behavior-failed'));
  135. }
  136. });
  137. /**
  138. * @swagger
  139. *
  140. * /customize-setting/function:
  141. * put:
  142. * tags: [CustomizeSetting]
  143. * description: Update function
  144. * requestBody:
  145. * required: true
  146. * content:
  147. * application/json:
  148. * schama:
  149. * type: object
  150. * properties:
  151. * isEnabledTimeline:
  152. * description: is enabled timeline
  153. * type: boolean
  154. * isSavedStatesOfTabChanges:
  155. * description: is saved states of tabChanges
  156. * type: boolean
  157. * isEnabledAttachTitleHeader:
  158. * description: is enabled attach titleHeader
  159. * type: boolean
  160. * recentCreatedLimit:
  161. * description: limit of recent created
  162. * type: number
  163. * responses:
  164. * 200:
  165. * description: Succeeded to update function
  166. */
  167. router.put('/function', loginRequiredStrictly, adminRequired, csrf, validator.function, ApiV3FormValidator, async(req, res) => {
  168. const requestParams = {
  169. 'customize:isEnabledTimeline': req.body.isEnabledTimeline,
  170. 'customize:isSavedStatesOfTabChanges': req.body.isSavedStatesOfTabChanges,
  171. 'customize:isEnabledAttachTitleHeader': req.body.isEnabledAttachTitleHeader,
  172. 'customize:showRecentCreatedNumber': req.body.recentCreatedLimit,
  173. };
  174. try {
  175. await crowi.configManager.updateConfigsInTheSameNamespace('crowi', requestParams);
  176. const customizedParams = {
  177. isEnabledTimeline: await crowi.configManager.getConfig('crowi', 'customize:isEnabledTimeline'),
  178. isSavedStatesOfTabChanges: await crowi.configManager.getConfig('crowi', 'customize:isSavedStatesOfTabChanges'),
  179. isEnabledAttachTitleHeader: await crowi.configManager.getConfig('crowi', 'customize:isEnabledAttachTitleHeader'),
  180. recentCreatedLimit: await crowi.configManager.getConfig('crowi', 'customize:showRecentCreatedNumber'),
  181. };
  182. return res.apiv3({ customizedParams });
  183. }
  184. catch (err) {
  185. const msg = 'Error occurred in updating function';
  186. logger.error('Error', err);
  187. return res.apiv3Err(new ErrorV3(msg, 'update-function-failed'));
  188. }
  189. });
  190. // TODO writte swagger
  191. router.put('/highlight', loginRequiredStrictly, adminRequired, csrf, validator.highlight, ApiV3FormValidator, async(req, res) => {
  192. const requestParams = {
  193. 'customize:highlightJsStyle': req.body.highlightJsStyle,
  194. 'customize:highlightJsStyleBorder': req.body.highlightJsStyleBorder,
  195. };
  196. try {
  197. await crowi.configManager.updateConfigsInTheSameNamespace('crowi', requestParams);
  198. const customizedParams = {
  199. styleName: await crowi.configManager.getConfig('crowi', 'customize:highlightJsStyle'),
  200. styleBorder: await crowi.configManager.getConfig('crowi', 'customize:highlightJsStyleBorder'),
  201. };
  202. return res.apiv3({ customizedParams });
  203. }
  204. catch (err) {
  205. const msg = 'Error occurred in updating highlight';
  206. logger.error('Error', err);
  207. return res.apiv3Err(new ErrorV3(msg, 'update-highlight-failed'));
  208. }
  209. });
  210. return router;
  211. };