activity.ts 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. import type { IPage } from '@growi/core';
  2. import mongoose from 'mongoose';
  3. import {
  4. IActivity, SupportedActionType, AllSupportedActions, ActionGroupSize,
  5. AllEssentialActions, AllSmallGroupActions, AllMediumGroupActions, AllLargeGroupActions,
  6. } from '~/interfaces/activity';
  7. import Activity, { ActivityDocument } from '~/server/models/activity';
  8. import loggerFactory from '../../utils/logger';
  9. import Crowi from '../crowi';
  10. import type { GeneratePreNotify, GetAditionalTargetUsers } from './preNotify';
  11. const logger = loggerFactory('growi:service:ActivityService');
  12. const parseActionString = (actionsString: string): SupportedActionType[] => {
  13. if (actionsString == null) {
  14. return [];
  15. }
  16. const actions = actionsString.split(',').map(value => value.trim());
  17. return actions.filter(action => (AllSupportedActions as string[]).includes(action)) as SupportedActionType[];
  18. };
  19. class ActivityService {
  20. crowi!: Crowi;
  21. activityEvent: any;
  22. constructor(crowi: Crowi) {
  23. this.crowi = crowi;
  24. this.activityEvent = crowi.event('activity');
  25. this.getAvailableActions = this.getAvailableActions.bind(this);
  26. this.shoudUpdateActivity = this.shoudUpdateActivity.bind(this);
  27. this.initActivityEventListeners();
  28. }
  29. initActivityEventListeners(): void {
  30. this.activityEvent.on('update', async(
  31. activityId: string, parameters, target: IPage, generatePreNotify: GeneratePreNotify, getAditionalTargetUsers?: GetAditionalTargetUsers,
  32. ) => {
  33. let activity: ActivityDocument;
  34. const shoudUpdate = this.shoudUpdateActivity(parameters.action);
  35. if (shoudUpdate) {
  36. try {
  37. activity = await Activity.updateByParameters(activityId, parameters);
  38. }
  39. catch (err) {
  40. logger.error('Update activity failed', err);
  41. return;
  42. }
  43. const preNotify = generatePreNotify(activity, getAditionalTargetUsers);
  44. this.activityEvent.emit('updated', activity, target, preNotify);
  45. }
  46. });
  47. }
  48. getAvailableActions = function(isIncludeEssentialActions = true): SupportedActionType[] {
  49. const auditLogEnabled = this.crowi.configManager.getConfig('crowi', 'app:auditLogEnabled') || false;
  50. const auditLogActionGroupSize = this.crowi.configManager.getConfig('crowi', 'app:auditLogActionGroupSize') || ActionGroupSize.Small;
  51. const auditLogAdditionalActions = this.crowi.configManager.getConfig('crowi', 'app:auditLogAdditionalActions');
  52. const auditLogExcludeActions = this.crowi.configManager.getConfig('crowi', 'app:auditLogExcludeActions');
  53. if (!auditLogEnabled) {
  54. return AllEssentialActions;
  55. }
  56. const availableActionsSet = new Set<SupportedActionType>();
  57. // Set base action group
  58. switch (auditLogActionGroupSize) {
  59. case ActionGroupSize.Small:
  60. AllSmallGroupActions.forEach(action => availableActionsSet.add(action));
  61. break;
  62. case ActionGroupSize.Medium:
  63. AllMediumGroupActions.forEach(action => availableActionsSet.add(action));
  64. break;
  65. case ActionGroupSize.Large:
  66. AllLargeGroupActions.forEach(action => availableActionsSet.add(action));
  67. break;
  68. }
  69. // Add additionalActions
  70. const additionalActions = parseActionString(auditLogAdditionalActions);
  71. additionalActions.forEach(action => availableActionsSet.add(action));
  72. // Delete excludeActions
  73. const excludeActions = parseActionString(auditLogExcludeActions);
  74. excludeActions.forEach(action => availableActionsSet.delete(action));
  75. // Add essentialActions
  76. if (isIncludeEssentialActions) {
  77. AllEssentialActions.forEach(action => availableActionsSet.add(action));
  78. }
  79. return Array.from(availableActionsSet);
  80. };
  81. shoudUpdateActivity = function(action: SupportedActionType): boolean {
  82. return this.getAvailableActions().includes(action);
  83. };
  84. // for GET request
  85. createActivity = async function(parameters): Promise<IActivity | null> {
  86. const shoudCreateActivity = this.crowi.activityService.shoudUpdateActivity(parameters.action);
  87. if (shoudCreateActivity) {
  88. let activity: IActivity;
  89. try {
  90. activity = await Activity.createByParameters(parameters);
  91. return activity;
  92. }
  93. catch (err) {
  94. logger.error('Create activity failed', err);
  95. }
  96. }
  97. return null;
  98. };
  99. createTtlIndex = async function() {
  100. const configManager = this.crowi.configManager;
  101. const activityExpirationSeconds = configManager != null ? configManager.getConfig('crowi', 'app:activityExpirationSeconds') : 2592000;
  102. const collection = mongoose.connection.collection('activities');
  103. try {
  104. const targetField = 'createdAt_1';
  105. const indexes = await collection.indexes();
  106. const foundCreatedAt = indexes.find(i => i.name === targetField);
  107. const isNotSpec = foundCreatedAt?.expireAfterSeconds == null || foundCreatedAt?.expireAfterSeconds !== activityExpirationSeconds;
  108. const shoudDropIndex = foundCreatedAt != null && isNotSpec;
  109. const shoudCreateIndex = foundCreatedAt == null || shoudDropIndex;
  110. if (shoudDropIndex) {
  111. await collection.dropIndex(targetField);
  112. }
  113. if (shoudCreateIndex) {
  114. await collection.createIndex({ createdAt: 1 }, { expireAfterSeconds: activityExpirationSeconds });
  115. }
  116. }
  117. catch (err) {
  118. logger.error('Failed to create TTL Index', err);
  119. throw err;
  120. }
  121. };
  122. }
  123. module.exports = ActivityService;