ソースを参照

setup models including independent crowi

kaori 4 年 前
コミット
6a9a12820e

+ 11 - 1
packages/app/src/server/crowi/index.js

@@ -19,6 +19,8 @@ import AttachmentService from '../service/attachment';
 import { SlackIntegrationService } from '../service/slack-integration';
 import { UserNotificationService } from '../service/user-notification';
 
+import Actiity from '../models/activity';
+
 const logger = loggerFactory('growi:crowi');
 const httpErrorHandler = require('../middlewares/http-error-handler');
 
@@ -300,7 +302,15 @@ Crowi.prototype.setupSocketIoService = async function() {
 };
 
 Crowi.prototype.setupModels = async function() {
-  Object.keys(models).forEach((key) => {
+  let allModels = {};
+
+  // include models that dependent on crowi
+  allModels = models;
+
+  // include models that independent from crowi
+  allModels.Activity = Actiity;
+
+  Object.keys(allModels).forEach((key) => {
     return this.model(key, models[key](this));
   });
 };

+ 61 - 64
packages/app/src/server/models/activity.ts

@@ -25,74 +25,71 @@ export interface ActivityDocument extends Document {
 
 export type ActivityModel = Model<ActivityDocument>
 
-module.exports = function(crowi: Crowi) {
 
-  // TODO: add revision id
-  const activitySchema = new Schema<ActivityDocument, ActivityModel>({
-    user: {
-      type: Schema.Types.ObjectId,
-      ref: 'User',
-      index: true,
-      require: true,
-    },
-    targetModel: {
-      type: String,
-      require: true,
-      enum: ActivityDefine.getSupportTargetModelNames(),
-    },
-    target: {
-      type: Schema.Types.ObjectId,
-      refPath: 'targetModel',
-      require: true,
-    },
-    action: {
-      type: String,
-      require: true,
-      enum: ActivityDefine.getSupportActionNames(),
-    },
-    event: {
-      type: Schema.Types.ObjectId,
-      refPath: 'eventModel',
-    },
-    eventModel: {
-      type: String,
-      enum: ActivityDefine.getSupportEventModelNames(),
-    },
-    createdAt: {
-      type: Date,
-      default: Date.now,
-    },
-  });
-  activitySchema.index({ target: 1, action: 1 });
-  activitySchema.index({
-    user: 1, target: 1, action: 1, createdAt: 1,
-  }, { unique: true });
+// TODO: add revision id
+const activitySchema = new Schema<ActivityDocument, ActivityModel>({
+  user: {
+    type: Schema.Types.ObjectId,
+    ref: 'User',
+    index: true,
+    require: true,
+  },
+  targetModel: {
+    type: String,
+    require: true,
+    enum: ActivityDefine.getSupportTargetModelNames(),
+  },
+  target: {
+    type: Schema.Types.ObjectId,
+    refPath: 'targetModel',
+    require: true,
+  },
+  action: {
+    type: String,
+    require: true,
+    enum: ActivityDefine.getSupportActionNames(),
+  },
+  event: {
+    type: Schema.Types.ObjectId,
+    refPath: 'eventModel',
+  },
+  eventModel: {
+    type: String,
+    enum: ActivityDefine.getSupportEventModelNames(),
+  },
+  createdAt: {
+    type: Date,
+    default: Date.now,
+  },
+});
+activitySchema.index({ target: 1, action: 1 });
+activitySchema.index({
+  user: 1, target: 1, action: 1, createdAt: 1,
+}, { unique: true });
 
 
-  activitySchema.methods.getNotificationTargetUsers = async function() {
-    const User = getModelSafely('User') || require('~/server/models/user')();
-    const { user: actionUser, targetModel, target } = this;
+activitySchema.methods.getNotificationTargetUsers = async function() {
+  const User = getModelSafely('User') || require('~/server/models/user')();
+  const { user: actionUser, targetModel, target } = this;
 
-    const model: any = await this.model(targetModel).findById(target);
-    const [targetUsers, subscribeUsers, unsubscribeUsers] = await Promise.all([
-      model.getNotificationTargetUsers(),
-      Subscription.getSubscription((target as any) as Types.ObjectId),
-      Subscription.getUnsubscription((target as any) as Types.ObjectId),
-    ]);
+  const model: any = await this.model(targetModel).findById(target);
+  const [targetUsers, subscribeUsers, unsubscribeUsers] = await Promise.all([
+    model.getNotificationTargetUsers(),
+    Subscription.getSubscription((target as any) as Types.ObjectId),
+    Subscription.getUnsubscription((target as any) as Types.ObjectId),
+  ]);
 
-    const unique = array => Object.values(array.reduce((objects, object) => ({ ...objects, [object.toString()]: object }), {}));
-    const filter = (array, pull) => {
-      const ids = pull.map(object => object.toString());
-      return array.filter(object => !ids.includes(object.toString()));
-    };
-    const notificationUsers = filter(unique([...targetUsers, ...subscribeUsers]), [...unsubscribeUsers, actionUser]);
-    const activeNotificationUsers = await User.find({
-      _id: { $in: notificationUsers },
-      status: User.STATUS_ACTIVE,
-    }).distinct('_id');
-    return activeNotificationUsers;
+  const unique = array => Object.values(array.reduce((objects, object) => ({ ...objects, [object.toString()]: object }), {}));
+  const filter = (array, pull) => {
+    const ids = pull.map(object => object.toString());
+    return array.filter(object => !ids.includes(object.toString()));
   };
-
-  return getOrCreateModel<ActivityDocument, ActivityModel>('Activity', activitySchema);
-
+  const notificationUsers = filter(unique([...targetUsers, ...subscribeUsers]), [...unsubscribeUsers, actionUser]);
+  const activeNotificationUsers = await User.find({
+    _id: { $in: notificationUsers },
+    status: User.STATUS_ACTIVE,
+  }).distinct('_id');
+  return activeNotificationUsers;
 };
+
+export default getOrCreateModel<ActivityDocument, ActivityModel>('Activity', activitySchema);

+ 0 - 1
packages/app/src/server/models/index.js

@@ -1,5 +1,4 @@
 module.exports = {
-  Activity: require('./activity'),
   Page: require('./page'),
   // TODO GW-2746 bulk export pages
   // PageArchive: require('./page-archive'),