activity.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. import type {
  2. Types, Document, Model, SortOrder,
  3. } from 'mongoose';
  4. import { Schema } from 'mongoose';
  5. import mongoosePaginate from 'mongoose-paginate-v2';
  6. import type {
  7. IActivity, ISnapshot, SupportedActionType, SupportedTargetModelType, SupportedEventModelType,
  8. } from '~/interfaces/activity';
  9. import {
  10. AllSupportedActions,
  11. AllSupportedTargetModels,
  12. AllSupportedEventModels,
  13. } from '~/interfaces/activity';
  14. import loggerFactory from '../../utils/logger';
  15. import { getOrCreateModel } from '../util/mongoose-utils';
  16. const logger = loggerFactory('growi:models:activity');
  17. export interface ActivityDocument extends Document {
  18. _id: Types.ObjectId
  19. user: Types.ObjectId
  20. ip: string
  21. endpoint: string
  22. targetModel: SupportedTargetModelType
  23. target: Types.ObjectId
  24. eventModel: SupportedEventModelType
  25. event: Types.ObjectId
  26. action: SupportedActionType
  27. snapshot: ISnapshot
  28. }
  29. export interface ActivityModel extends Model<ActivityDocument> {
  30. [x:string]: any
  31. getActionUsersFromActivities(activities: ActivityDocument[]): any[]
  32. }
  33. const snapshotSchema = new Schema<ISnapshot>({
  34. username: { type: String, index: true },
  35. });
  36. // TODO: add revision id
  37. const activitySchema = new Schema<ActivityDocument, ActivityModel>({
  38. user: {
  39. type: Schema.Types.ObjectId,
  40. ref: 'User',
  41. index: true,
  42. },
  43. ip: {
  44. type: String,
  45. },
  46. endpoint: {
  47. type: String,
  48. },
  49. targetModel: {
  50. type: String,
  51. enum: AllSupportedTargetModels,
  52. },
  53. target: {
  54. type: Schema.Types.ObjectId,
  55. refPath: 'targetModel',
  56. },
  57. eventModel: {
  58. type: String,
  59. enum: AllSupportedEventModels,
  60. },
  61. event: {
  62. type: Schema.Types.ObjectId,
  63. },
  64. action: {
  65. type: String,
  66. enum: AllSupportedActions,
  67. required: true,
  68. },
  69. snapshot: snapshotSchema,
  70. }, {
  71. timestamps: {
  72. createdAt: true,
  73. updatedAt: false,
  74. },
  75. });
  76. // activitySchema.index({ createdAt: 1 }); // Do not create index here because it is created by ActivityService as TTL index
  77. activitySchema.index({ target: 1, action: 1 });
  78. activitySchema.index({
  79. user: 1, target: 1, action: 1, createdAt: 1,
  80. }, { unique: true });
  81. activitySchema.plugin(mongoosePaginate);
  82. activitySchema.post('save', function() {
  83. logger.debug('activity has been created', this);
  84. });
  85. activitySchema.statics.createByParameters = async function(parameters): Promise<IActivity> {
  86. const activity = await this.create(parameters) as unknown as IActivity;
  87. return activity;
  88. };
  89. // When using this method, ensure that activity updates are allowed using ActivityService.shoudUpdateActivity
  90. activitySchema.statics.updateByParameters = async function(activityId: string, parameters): Promise<ActivityDocument | null> {
  91. const activity = await this.findOneAndUpdate({ _id: activityId }, parameters, { new: true }).exec();
  92. return activity;
  93. };
  94. activitySchema.statics.findSnapshotUsernamesByUsernameRegexWithTotalCount = async function(
  95. q: string, option: { sortOpt: SortOrder, offset: number, limit: number},
  96. ): Promise<{usernames: string[], totalCount: number}> {
  97. const opt = option || {};
  98. const sortOpt = opt.sortOpt || 1;
  99. const offset = opt.offset || 0;
  100. const limit = opt.limit || 10;
  101. const conditions = { 'snapshot.username': { $regex: q, $options: 'i' } };
  102. const usernames = await this.aggregate()
  103. .skip(0)
  104. .limit(10000) // Narrow down the search target
  105. .match(conditions)
  106. .group({ _id: '$snapshot.username' })
  107. .sort({ _id: sortOpt }) // Sort "snapshot.username" in ascending order
  108. .skip(offset)
  109. .limit(limit);
  110. const totalCount = (await this.find(conditions).distinct('snapshot.username')).length;
  111. return { usernames: usernames.map(r => r._id), totalCount };
  112. };
  113. export default getOrCreateModel<ActivityDocument, ActivityModel>('Activity', activitySchema);