revision.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import type {
  2. HasObjectId,
  3. IRevision,
  4. Origin,
  5. } from '@growi/core';
  6. import { allOrigin } from '@growi/core';
  7. import {
  8. Schema, Types, type Document, type Model,
  9. } from 'mongoose';
  10. import mongoosePaginate from 'mongoose-paginate-v2';
  11. import loggerFactory from '~/utils/logger';
  12. import { getOrCreateModel } from '../util/mongoose-utils';
  13. import type { PageDocument } from './page';
  14. const logger = loggerFactory('growi:models:revision');
  15. export interface IRevisionDocument extends IRevision, Document {
  16. }
  17. type UpdateRevisionListByPageId = (pageId: string, updateData: Partial<IRevision>) => Promise<void>;
  18. type PrepareRevision = (
  19. pageData: PageDocument, body: string, previousBody: string | null, user: HasObjectId, origin?: Origin, options?: { format: string }
  20. ) => IRevisionDocument;
  21. export interface IRevisionModel extends Model<IRevisionDocument> {
  22. updateRevisionListByPageId: UpdateRevisionListByPageId,
  23. prepareRevision: PrepareRevision,
  24. }
  25. // Use this to allow empty strings to pass the `required` validator
  26. Schema.Types.String.checkRequired(v => typeof v === 'string');
  27. const revisionSchema = new Schema<IRevisionDocument, IRevisionModel>({
  28. // The type of pageId is always converted to String at server startup
  29. // Refer to this method (/src/server/service/normalize-data/convert-revision-page-id-to-string.ts) to change the pageId type
  30. pageId: {
  31. type: String, required: true, index: true,
  32. },
  33. body: {
  34. type: String,
  35. required: true,
  36. get: (data) => {
  37. // replace CR/CRLF to LF above v3.1.5
  38. // see https://github.com/weseek/growi/issues/463
  39. return data ? data.replace(/\r\n?/g, '\n') : '';
  40. },
  41. },
  42. format: { type: String, default: 'markdown' },
  43. author: { type: Types.ObjectId, ref: 'User' },
  44. hasDiffToPrev: { type: Boolean },
  45. origin: { type: String, enum: allOrigin },
  46. }, {
  47. timestamps: { createdAt: true, updatedAt: false },
  48. });
  49. revisionSchema.plugin(mongoosePaginate);
  50. const updateRevisionListByPageId: UpdateRevisionListByPageId = async function(this: IRevisionModel, pageId, updateData) {
  51. await this.updateMany({ pageId }, { $set: updateData });
  52. };
  53. revisionSchema.statics.updateRevisionListByPageId = updateRevisionListByPageId;
  54. const prepareRevision: PrepareRevision = function(this: IRevisionModel, pageData, body, previousBody, user, origin, options = { format: 'markdown' }) {
  55. if (!user._id) {
  56. throw new Error('Error: user should have _id');
  57. }
  58. const newRevision = new this();
  59. newRevision.pageId = pageData._id;
  60. newRevision.body = body;
  61. newRevision.format = options.format;
  62. newRevision.author = user._id;
  63. newRevision.origin = origin;
  64. if (pageData.revision != null) {
  65. newRevision.hasDiffToPrev = body !== previousBody;
  66. }
  67. return newRevision;
  68. };
  69. revisionSchema.statics.prepareRevision = prepareRevision;
  70. export const Revision = getOrCreateModel<IRevisionDocument, IRevisionModel>('Revision', revisionSchema);