Просмотр исходного кода

Commonality of generatedefaultprenotify and generateprenotifyalsodescendants

WNomunomu 2 лет назад
Родитель
Сommit
5a1f2ed0e3
1 измененных файлов с 56 добавлено и 21 удалено
  1. 56 21
      apps/app/src/server/service/preNotify.ts

+ 56 - 21
apps/app/src/server/service/preNotify.ts

@@ -11,6 +11,9 @@ export type PreNotifyProps = {
 }
 
 export type PreNotify = (props: PreNotifyProps) => Promise<void>;
+export type GeneratePreNotify = (activity: ActivityDocument, getAditionalTargetUsers?: (activity?: ActivityDocument) => Ref<IUser>[]) => PreNotify;
+
+export type GetAditionalTargetUsers = (activity: ActivityDocument) => Ref<IUser>[];
 
 export const generateInitialPreNotifyProps = (): PreNotifyProps => {
 
@@ -19,7 +22,7 @@ export const generateInitialPreNotifyProps = (): PreNotifyProps => {
   return { notificationTargetUsers: initialPreNotifyProps };
 };
 
-export const generateDefaultPreNotify = (activity: ActivityDocument): PreNotify => {
+export const generatePreNotify = (activity: ActivityDocument, getAditionalTargetUsers?: GetAditionalTargetUsers): PreNotify => {
 
   const preNotify = async(props: PreNotifyProps) => {
     const { notificationTargetUsers } = props;
@@ -34,33 +37,65 @@ export const generateDefaultPreNotify = (activity: ActivityDocument): PreNotify
       status: User.STATUS_ACTIVE,
     }).distinct('_id');
 
-    notificationTargetUsers?.push(...activeNotificationUsers);
+    if (getAditionalTargetUsers == null) {
+      notificationTargetUsers?.push(...activeNotificationUsers);
+    }
+    else {
+      const aditionalTargetUsers = getAditionalTargetUsers(activity);
+
+      notificationTargetUsers?.push(
+        ...activeNotificationUsers,
+        ...aditionalTargetUsers,
+      );
+    }
+
   };
 
   return preNotify;
 };
 
-export const generatePreNotifyAlsoDescendants = (activity: ActivityDocument, descendantsSubscribedUsers: Ref<IUser>[]): PreNotify => {
+// export const generateDefaultPreNotify = (activity: ActivityDocument): PreNotify => {
 
-  const preNotify = async(props: PreNotifyProps) => {
-    const { notificationTargetUsers } = props;
+//   const preNotify = async(props: PreNotifyProps) => {
+//     const { notificationTargetUsers } = props;
 
-    const User = getModelSafely('User') || require('~/server/models/user')();
-    const actionUser = activity.user;
-    const target = activity.target;
-    const subscribedUsers = await Subscription.getSubscription(target as unknown as Ref<IPage>);
-    const notificationUsers = subscribedUsers.filter(item => (item.toString() !== actionUser._id.toString()));
-    const activeNotificationUsers = await User.find({
-      _id: { $in: notificationUsers },
-      status: User.STATUS_ACTIVE,
-    }).distinct('_id');
+//     const User = getModelSafely('User') || require('~/server/models/user')();
+//     const actionUser = activity.user;
+//     const target = activity.target;
+//     const subscribedUsers = await Subscription.getSubscription(target as unknown as Ref<IPage>);
+//     const notificationUsers = subscribedUsers.filter(item => (item.toString() !== actionUser._id.toString()));
+//     const activeNotificationUsers = await User.find({
+//       _id: { $in: notificationUsers },
+//       status: User.STATUS_ACTIVE,
+//     }).distinct('_id');
 
-    notificationTargetUsers?.push(
-      ...activeNotificationUsers,
-      ...descendantsSubscribedUsers,
-    );
+//     notificationTargetUsers?.push(...activeNotificationUsers);
+//   };
 
-  };
+//   return preNotify;
+// };
 
-  return preNotify;
-};
+// export const generatePreNotifyAlsoDescendants = (activity: ActivityDocument, descendantsSubscribedUsers: Ref<IUser>[]): PreNotify => {
+
+//   const preNotify = async(props: PreNotifyProps) => {
+//     const { notificationTargetUsers } = props;
+
+//     const User = getModelSafely('User') || require('~/server/models/user')();
+//     const actionUser = activity.user;
+//     const target = activity.target;
+//     const subscribedUsers = await Subscription.getSubscription(target as unknown as Ref<IPage>);
+//     const notificationUsers = subscribedUsers.filter(item => (item.toString() !== actionUser._id.toString()));
+//     const activeNotificationUsers = await User.find({
+//       _id: { $in: notificationUsers },
+//       status: User.STATUS_ACTIVE,
+//     }).distinct('_id');
+
+//     notificationTargetUsers?.push(
+//       ...activeNotificationUsers,
+//       ...descendantsSubscribedUsers,
+//     );
+
+//   };
+
+//   return preNotify;
+// };