|
@@ -1,10 +1,13 @@
|
|
|
import { subDays } from 'date-fns';
|
|
import { subDays } from 'date-fns';
|
|
|
import { Types } from 'mongoose';
|
|
import { Types } from 'mongoose';
|
|
|
|
|
|
|
|
|
|
+import { AllSupportedActionToNotifiedType } from '~/interfaces/activity';
|
|
|
import { HasObjectId } from '~/interfaces/has-object-id';
|
|
import { HasObjectId } from '~/interfaces/has-object-id';
|
|
|
import { InAppNotificationStatuses, PaginateResult } from '~/interfaces/in-app-notification';
|
|
import { InAppNotificationStatuses, PaginateResult } from '~/interfaces/in-app-notification';
|
|
|
|
|
+import { IPage } from '~/interfaces/page';
|
|
|
import { SubscriptionStatusType } from '~/interfaces/subscription';
|
|
import { SubscriptionStatusType } from '~/interfaces/subscription';
|
|
|
import { IUser } from '~/interfaces/user';
|
|
import { IUser } from '~/interfaces/user';
|
|
|
|
|
+import { stringifySnapshot } from '~/models/serializers/in-app-notification-snapshot/page';
|
|
|
import { ActivityDocument } from '~/server/models/activity';
|
|
import { ActivityDocument } from '~/server/models/activity';
|
|
|
import {
|
|
import {
|
|
|
InAppNotification,
|
|
InAppNotification,
|
|
@@ -36,6 +39,8 @@ export default class InAppNotificationService {
|
|
|
this.crowi = crowi;
|
|
this.crowi = crowi;
|
|
|
this.socketIoService = crowi.socketIoService;
|
|
this.socketIoService = crowi.socketIoService;
|
|
|
|
|
|
|
|
|
|
+ this.emitSocketIo = this.emitSocketIo.bind(this);
|
|
|
|
|
+ this.upsertByActivity = this.upsertByActivity.bind(this);
|
|
|
this.getUnreadCountByUser = this.getUnreadCountByUser.bind(this);
|
|
this.getUnreadCountByUser = this.getUnreadCountByUser.bind(this);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -175,6 +180,20 @@ export default class InAppNotificationService {
|
|
|
return;
|
|
return;
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
|
|
+ createInAppNotification = async function(activity: ActivityDocument, target: IPage): Promise<void> {
|
|
|
|
|
+ const shouldNotification = activity != null && target != null && (AllSupportedActionToNotifiedType as ReadonlyArray<string>).includes(activity.action);
|
|
|
|
|
+ if (shouldNotification) {
|
|
|
|
|
+ const snapshot = stringifySnapshot(target as IPage);
|
|
|
|
|
+ const notificationTargetUsers = await activity?.getNotificationTargetUsers();
|
|
|
|
|
+ await this.upsertByActivity(notificationTargetUsers, activity, snapshot);
|
|
|
|
|
+ await this.emitSocketIo(notificationTargetUsers);
|
|
|
|
|
+ }
|
|
|
|
|
+ else {
|
|
|
|
|
+ throw Error('No activity to notify');
|
|
|
|
|
+ }
|
|
|
|
|
+ return;
|
|
|
|
|
+ };
|
|
|
|
|
+
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
module.exports = InAppNotificationService;
|
|
module.exports = InAppNotificationService;
|