pages.js 1.6 KB

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