Prechádzať zdrojové kódy

add generateDefaultPreNotify and generatePreNotifyAlsoDescendants

WNomunomu 2 rokov pred
rodič
commit
4734b16dcb
1 zmenil súbory, kde vykonal 52 pridanie a 11 odobranie
  1. 52 11
      apps/app/src/server/service/preNotify.ts

+ 52 - 11
apps/app/src/server/service/preNotify.ts

@@ -10,15 +10,56 @@ export type PreNotifyProps = {
 
 export type PreNotify = (props: PreNotifyProps) => Promise<void>;
 
-export const generateInitialPreNotifyProps = async(activity: ActivityDocument): Promise<IUser[]> => {
-  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');
-  return activeNotificationUsers;
+export const generateInitialPreNotifyProps = (): PreNotifyProps => {
+
+  const initialPreNotifyProps: IUser[] = [];
+
+  return { notificationTargetUsers: initialPreNotifyProps };
+};
+
+export const generateDefaultPreNotify = (activity: ActivityDocument): 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?.concat(activeNotificationUsers);
+
+  };
+
+  return preNotify;
+};
+
+export const generatePreNotifyAlsoDescendants = (activity: ActivityDocument, descendantsSubscribedUsers): 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?.concat(
+      activeNotificationUsers,
+      descendantsSubscribedUsers as IUser[],
+    );
+
+  };
+
+  return preNotify;
 };