metadata = $metadata; $this->entityManager = $entityManager; $this->serviceFactory = $serviceFactory; $this->user = $user; $this->internalAclManager = $internalAclManager; } protected function getMentionedUserIdList($entity) { $mentionedUserList = []; $data = $entity->get('data'); if (($data instanceof StdClass) && ($data->mentions instanceof StdClass)) { $mentions = get_object_vars($data->mentions); foreach ($mentions as $d) { $mentionedUserList[] = $d->id; } } return $mentionedUserList; } protected function getSubscriberList(string $parentType, string $parentId, bool $isInternal = false) { return $this->getStreamService()->getSubscriberList($parentType, $parentId, $isInternal); } public function afterSave(Entity $entity, array $options = []) { if (!$entity->isNew() && empty($options['forceProcessNotifications'])) { return; } $parentType = $entity->get('parentType'); $parentId = $entity->get('parentId'); $superParentType = $entity->get('superParentType'); $superParentId = $entity->get('superParentId'); $notifyUserIdList = []; if ($parentType && $parentId) { $userList = $this->getSubscriberList($parentType, $parentId, $entity->get('isInternal')); $userIdMetList = []; foreach ($userList as $user) { $userIdMetList[] = $user->id; } if ($superParentType && $superParentId) { $additionalUserList = $this ->getSubscriberList($superParentType, $superParentId, $entity->get('isInternal')); foreach ($additionalUserList as $user) { if ($user->isPortal()) { continue; } if (in_array($user->id, $userIdMetList)) { continue; } $userIdMetList[] = $user->id; $userList[] = $user; } } if ($entity->get('relatedType')) { $targetType = $entity->get('relatedType'); } else { $targetType = $parentType; } $skipAclCheck = false; if (!$entity->isAclProcessed()) { $skipAclCheck = true; } else { $teamIdList = $entity->getLinkMultipleIdList('teams'); $userIdList = $entity->getLinkMultipleIdList('users'); } foreach ($userList as $user) { if ($skipAclCheck) { $notifyUserIdList[] = $user->id; continue; } if ($user->isAdmin()) { $notifyUserIdList[] = $user->id; continue; } if ($user->isPortal()) { if ($entity->get('relatedType')) { continue; } else { $notifyUserIdList[] = $user->id; } continue; } $level = $this->internalAclManager->getLevel($user, $targetType, 'read'); if ($level === 'all') { $notifyUserIdList[] = $user->id; continue; } else if ($level === 'team') { if (in_array($user->id, $userIdList)) { $notifyUserIdList[] = $user->id; continue; } if (!empty($teamIdList)) { $userTeamIdList = $user->getLinkMultipleIdList('teams'); foreach ($teamIdList as $teamId) { if (in_array($teamId, $userTeamIdList)) { $notifyUserIdList[] = $user->id; break; } } } continue; } else if ($level === 'own') { if (in_array($user->id, $userIdList)) { $notifyUserIdList[] = $user->id; continue; } } } } else { $targetType = $entity->get('targetType'); if ($targetType === 'users') { $targetUserIdList = $entity->get('usersIds'); if (is_array($targetUserIdList)) { foreach ($targetUserIdList as $userId) { if ($userId === $this->user->id) { continue; } if (in_array($userId, $notifyUserIdList)) { continue; } $notifyUserIdList[] = $userId; } } } else if ($targetType === 'teams') { $targetTeamIdList = $entity->get('teamsIds'); if (is_array($targetTeamIdList)) { foreach ($targetTeamIdList as $teamId) { $team = $this->entityManager->getEntity('Team', $teamId); if (!$team) { continue; } $targetUserList = $this->entityManager ->getRDBRepository('Team') ->getRelation($team, 'users') ->where([ 'isActive' => true, ]) ->find(); foreach ($targetUserList as $user) { if ($user->id === $this->user->id) { continue; } if (in_array($user->id, $notifyUserIdList)) { continue; } $notifyUserIdList[] = $user->id; } } } } else if ($targetType === 'portals') { $targetPortalIdList = $entity->get('portalsIds'); if (is_array($targetPortalIdList)) { foreach ($targetPortalIdList as $portalId) { $portal = $this->entityManager->getEntity('Portal', $portalId); if (!$portal) { continue; } $targetUserList = $this->entityManager ->getRDBRepository('Portal') ->getRelation($portal, 'users') ->where([ 'isActive' => true, ]) ->find(); foreach ($targetUserList as $user) { if ($user->id === $this->user->id) { continue; } if (in_array($user->id, $notifyUserIdList)) { continue; } $notifyUserIdList[] = $user->id; } } } } else if ($targetType === 'all') { $targetUserList = $this->entityManager ->getRepository('User') ->where([ 'isActive' => true, 'type' => ['regular', 'admin'], ]) ->find(); foreach ($targetUserList as $user) { if ($user->id === $this->user->id) { continue; } $notifyUserIdList[] = $user->id; } } } $notifyUserIdList = array_unique($notifyUserIdList); foreach ($notifyUserIdList as $i => $userId) { if ($entity->isUserIdNotified($userId)) { unset($notifyUserIdList[$i]); continue; } if (!$entity->isNew()) { if ( $this->entityManager ->getRepository('Notification') ->select(['id']) ->where([ 'type' => 'Note', 'relatedType' => 'Note', 'relatedId' => $entity->id, 'userId' => $userId, ]) ->findOne() ) { unset($notifyUserIdList[$i]); continue; } } } $notifyUserIdList = array_values($notifyUserIdList); if (!empty($notifyUserIdList)) { $this->getNotificationService()->notifyAboutNote($notifyUserIdList, $entity); } } protected function getNotificationService() { if (empty($this->notificationService)) { $this->notificationService = $this->serviceFactory->create('Notification'); } return $this->notificationService; } protected function getStreamService() { if (empty($this->streamService)) { $this->streamService = $this->serviceFactory->create('Stream'); } return $this->streamService; } }