From 06bde4f1cec57ecd8f218fa5a481b7ce053db07c Mon Sep 17 00:00:00 2001 From: yuri Date: Fri, 20 Feb 2015 11:51:31 +0200 Subject: [PATCH] move autoFollow to job --- application/Espo/Hooks/Common/Stream.php | 41 +++++++++------- application/Espo/Services/Stream.php | 62 ++++++++++++++++++++++++ 2 files changed, 86 insertions(+), 17 deletions(-) diff --git a/application/Espo/Hooks/Common/Stream.php b/application/Espo/Hooks/Common/Stream.php index 3648beb535..b1aedf238f 100644 --- a/application/Espo/Hooks/Common/Stream.php +++ b/application/Espo/Hooks/Common/Stream.php @@ -134,9 +134,7 @@ class Stream extends \Espo\Core\Hooks\Base protected function getAutofollowUserIdList(Entity $entity, array $ignoreList = array()) { $entityType = $entity->getEntityName(); - $pdo = $this->getEntityManager()->getPDO(); - $userIdList = []; $sql = " @@ -150,15 +148,7 @@ class Stream extends \Espo\Core\Hooks\Base if (in_array($userId, $ignoreList)) { continue; } - $user = $this->getEntityManager()->getEntity('User', $userId); - if (!$user) { - continue; - } - $acl = new \Espo\Core\Acl($user, $this->getConfig(), null, $this->getMetadata()); - - if ($acl->check($entity, 'read')) { - $userIdList[] = $userId; - } + $userIdList[] = $userId; } return $userIdList; @@ -182,12 +172,6 @@ class Stream extends \Espo\Core\Hooks\Base $userIdList[] = $assignedUserId; } - $autofollowUserIdList = $this->getAutofollowUserIdList($entity, $userIdList); - foreach ($autofollowUserIdList as $userId) { - if (!in_array($userId, $userIdList)) { - $userIdList[] = $userId; - } - } if (!empty($userIdList)) { $this->getStreamService()->followEntityMass($entity, $userIdList); @@ -195,6 +179,29 @@ class Stream extends \Espo\Core\Hooks\Base $this->getStreamService()->noteCreate($entity); + + $autofollowUserIdList = $this->getAutofollowUserIdList($entity, $userIdList); + foreach ($autofollowUserIdList as $i => $userId) { + if (in_array($userId, $userIdList)) { + unset($autofollowUserIdList[$i]); + } + } + $autofollowUserIdList = array_values($autofollowUserIdList); + + if (!empty($autofollowUserIdList)) { + $job = $this->getEntityManager()->getEntity('Job'); + $job->set(array( + 'serviceName' => 'Stream', + 'method' => 'afterRecordCreatedJob', + 'data' => array( + 'userIdList' => $autofollowUserIdList, + 'entityType' => $entity->getEntityName(), + 'entityId' => $entity->id + ) + )); + $this->getEntityManager()->saveEntity($job); + } + } else { if ($entity->isFieldChanged('assignedUserId')) { $assignedUserId = $entity->get('assignedUserId'); diff --git a/application/Espo/Services/Stream.php b/application/Espo/Services/Stream.php index 8d826ab32e..77d1f411eb 100644 --- a/application/Espo/Services/Stream.php +++ b/application/Espo/Services/Stream.php @@ -72,6 +72,8 @@ class Stream extends \Espo\Core\Services\Base protected $auditedFieldsCache = array(); + private $notificationService = null; + protected function getServiceFactory() { return $this->injections['container']->get('serviceFactory'); @@ -87,6 +89,66 @@ class Stream extends \Espo\Core\Services\Base return $this->injections['metadata']; } + protected function getNotificationService() + { + if (empty($this->notificationService)) { + $this->notificationService = $this->getServiceFactory()->create('Notification'); + } + return $this->notificationService; + } + + public function afterRecordCreatedJob($data) + { + if (empty($data)) { + return; + } + if (empty($data['entityId']) || empty($data['entityType']) || empty($data['userIdList'])) { + return; + } + $userIdList = $data['userIdList']; + $entityType = $data['entityType']; + $entityId = $data['entityId']; + + $entity = $this->getEntityManager()->getEntity($entityType, $entityId); + if (!$entity) { + return; + } + + foreach ($userIdList as $i => $userId) { + $user = $this->getEntityManager()->getEntity('User', $userId); + if (!$user){ + continue; + } + $acl = new \Espo\Core\Acl($user, $this->getConfig(), null, $this->getMetadata()); + if (!$acl->check($entity, 'read')) { + unset($userIdList[$i]); + } + } + $userIdList = array_values($userIdList); + + foreach ($userIdList as $i => $userId) { + if ($this->checkIsFollowed($entity, $userId)) { + unset($userIdList[$i]); + } + } + $userIdList = array_values($userIdList); + + if (empty($userIdList)) { + return; + } + + $this->followEntityMass($entity, $userIdList); + + $noteList = $this->getEntityManager()->getRepository('Note')->where(array( + 'parentType' => $entityType, + 'parentId' => $entityId + ))->order('number', 'ASC')->find(); + + foreach ($noteList as $note) { + $this->getNotificationService()->notifyAboutNote($userIdList, $note); + } + } + public function checkIsFollowed(Entity $entity, $userId = null) { if (empty($userId)) {