pages.ts 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import { PageGrant } from '@growi/core';
  2. const mongoose = require('mongoose');
  3. // eslint-disable-next-line no-unused-vars
  4. const ImportOptionForPages = require('~/models/admin/import-option-for-pages');
  5. const { ObjectId } = mongoose.Types;
  6. class PageOverwriteParamsFactory {
  7. /**
  8. * generate overwrite params object
  9. * @param {string} operatorUserId
  10. * @param {ImportOptionForPages} option
  11. * @return {import('~/server/service/import').OverwriteParams}
  12. * key: property name
  13. * value: any value or a function `(value, { document, schema, propertyName }) => { return newValue }`
  14. */
  15. static generate(operatorUserId, option) {
  16. const params = {};
  17. if (option.isOverwriteAuthorWithCurrentUser) {
  18. const userId = ObjectId(operatorUserId);
  19. params.creator = userId;
  20. params.lastUpdateUser = userId;
  21. }
  22. params.grant = (value, { document, schema, propertyName }) => {
  23. if (option.makePublicForGrant2 && value === 2) {
  24. return PageGrant.GRANT_PUBLIC;
  25. }
  26. if (option.makePublicForGrant4 && value === 4) {
  27. return PageGrant.GRANT_PUBLIC;
  28. }
  29. if (option.makePublicForGrant5 && value === 5) {
  30. return PageGrant.GRANT_PUBLIC;
  31. }
  32. return value;
  33. };
  34. params.parent = (value, { document, schema, propertyName }) => {
  35. return null;
  36. };
  37. params.descendantCount = (value, { document, schema, propertyName }) => {
  38. return 0;
  39. };
  40. if (option.initPageMetadatas) {
  41. params.liker = [];
  42. params.seenUsers = [];
  43. params.commentCount = 0;
  44. params.extended = {};
  45. }
  46. return params;
  47. }
  48. }
  49. module.exports = (operatorUserId, option) => PageOverwriteParamsFactory.generate(operatorUserId, option);