activity.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import { getOrCreateModel, getModelSafely } from '@growi/core';
  2. import {
  3. Types, Document, Model, Schema,
  4. } from 'mongoose';
  5. import { AllSupportedTargetModelType, AllSupportedEventModelType, AllSupportedActionType } from '~/interfaces/activity';
  6. import loggerFactory from '../../utils/logger';
  7. import activityEvent from '../events/activity';
  8. import Subscription from './subscription';
  9. const logger = loggerFactory('growi:models:activity');
  10. export interface ActivityDocument extends Document {
  11. _id: Types.ObjectId
  12. user: Types.ObjectId | any
  13. targetModel: string
  14. target: Types.ObjectId
  15. action: string
  16. event: Types.ObjectId
  17. eventModel: string
  18. getNotificationTargetUsers(): Promise<any[]>
  19. }
  20. export interface ActivityModel extends Model<ActivityDocument> {
  21. getActionUsersFromActivities(activities: ActivityDocument[]): any[]
  22. }
  23. // TODO: add revision id
  24. const activitySchema = new Schema<ActivityDocument, ActivityModel>({
  25. user: {
  26. type: Schema.Types.ObjectId,
  27. ref: 'User',
  28. index: true,
  29. require: true,
  30. },
  31. targetModel: {
  32. type: String,
  33. require: true,
  34. enum: AllSupportedTargetModelType,
  35. },
  36. target: {
  37. type: Schema.Types.ObjectId,
  38. refPath: 'targetModel',
  39. require: true,
  40. },
  41. action: {
  42. type: String,
  43. require: true,
  44. enum: AllSupportedActionType,
  45. },
  46. event: {
  47. type: Schema.Types.ObjectId,
  48. refPath: 'eventModel',
  49. },
  50. eventModel: {
  51. type: String,
  52. enum: AllSupportedEventModelType,
  53. },
  54. }, {
  55. timestamps: true,
  56. });
  57. activitySchema.index({ target: 1, action: 1 });
  58. activitySchema.index({
  59. user: 1, target: 1, action: 1, createdAt: 1,
  60. }, { unique: true });
  61. activitySchema.methods.getNotificationTargetUsers = async function() {
  62. const User = getModelSafely('User') || require('~/server/models/user')();
  63. const { user: actionUser, target } = this;
  64. const [subscribeUsers, unsubscribeUsers] = await Promise.all([
  65. Subscription.getSubscription((target as any) as Types.ObjectId),
  66. Subscription.getUnsubscription((target as any) as Types.ObjectId),
  67. ]);
  68. const unique = array => Object.values(array.reduce((objects, object) => ({ ...objects, [object.toString()]: object }), {}));
  69. const filter = (array, pull) => {
  70. const ids = pull.map(object => object.toString());
  71. return array.filter(object => !ids.includes(object.toString()));
  72. };
  73. const notificationUsers = filter(unique([...subscribeUsers]), [...unsubscribeUsers, actionUser]);
  74. const activeNotificationUsers = await User.find({
  75. _id: { $in: notificationUsers },
  76. status: User.STATUS_ACTIVE,
  77. }).distinct('_id');
  78. return activeNotificationUsers;
  79. };
  80. activitySchema.post('save', async(savedActivity: ActivityDocument) => {
  81. let targetUsers: Types.ObjectId[] = [];
  82. try {
  83. targetUsers = await savedActivity.getNotificationTargetUsers();
  84. }
  85. catch (err) {
  86. logger.error(err);
  87. }
  88. activityEvent.emit('create', targetUsers, savedActivity);
  89. });
  90. export default getOrCreateModel<ActivityDocument, ActivityModel>('Activity', activitySchema);