|
|
@@ -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);
|