From c99566410afdcbcb4fd61ac37567704009ccc963 Mon Sep 17 00:00:00 2001 From: Yuri Kuznetsov Date: Fri, 8 Oct 2021 15:35:05 +0300 Subject: [PATCH] note acl refactoring and period change --- application/Espo/Entities/Note.php | 7 ++++ application/Espo/Services/Stream.php | 52 +++++++++++++++++++--------- 2 files changed, 43 insertions(+), 16 deletions(-) diff --git a/application/Espo/Entities/Note.php b/application/Espo/Entities/Note.php index 935f5067cb..7f2ddef52d 100644 --- a/application/Espo/Entities/Note.php +++ b/application/Espo/Entities/Note.php @@ -31,6 +31,8 @@ namespace Espo\Entities; use Espo\Core\ORM\Entity; +use Espo\Core\Field\DateTime; + use stdClass; class Note extends Entity @@ -117,6 +119,11 @@ class Note extends Entity return $this->get('post'); } + public function getCreatedAt(): ?DateTime + { + return $this->getValueObject('createdAt'); + } + public function setAclIsProcessed(): void { $this->aclIsProcessed = true; diff --git a/application/Espo/Services/Stream.php b/application/Espo/Services/Stream.php index 148ea92976..e03c126acb 100644 --- a/application/Espo/Services/Stream.php +++ b/application/Espo/Services/Stream.php @@ -108,7 +108,18 @@ class Stream private $recordServiceContainer; - private const NOTE_ACL_PERIOD = '1 hour'; + /** + * When a record is re-assigned, ACL will be recalculated for related notes + * created within the period. + */ + private const NOTE_ACL_PERIOD = '3 days'; + + private const NOTE_ACL_LIMIT = 50; + + /** + * Not used currently. + */ + private const NOTE_NOTIFICATION_PERIOD = '1 hour'; public function __construct( EntityManager $entityManager, @@ -2110,6 +2121,8 @@ class Stream return; } + $limit = $this->config->get('noteAclLimit', self::NOTE_ACL_LIMIT); + $noteList = $this->entityManager ->getRDBRepository('Note') ->where([ @@ -2137,6 +2150,8 @@ class Stream 'relatedId', 'createdAt', ]) + ->order('number', 'DESC') + ->limit(0, $limit) ->find(); $noteOptions = []; @@ -2145,9 +2160,11 @@ class Stream $noteOptions['forceProcessNotifications'] = true; } - $period = '-' . $this->config->get('noteNotificationPeriod', self::NOTE_ACL_PERIOD); + $notificationPeriod = '-' . $this->config->get('noteNotificationPeriod', self::NOTE_NOTIFICATION_PERIOD); + $aclPeriod = '-' . $this->config->get('noteAclPeriod', self::NOTE_ACL_PERIOD); - $threshold = (new DateTime())->modify($period); + $notificationThreshold = (new DateTime())->modify($notificationPeriod); + $aclThreshold = (new DateTime())->modify($aclPeriod); foreach ($noteList as $note) { $this->processNoteAclItem($entity, $note, [ @@ -2156,7 +2173,8 @@ class Stream 'forceProcessNoteNotifications' => $forceProcessNoteNotifications, 'teamIdList' => $teamIdList, 'userIdList' => $userIdList, - 'threshold' => $threshold, + 'notificationThreshold' => $notificationThreshold, + 'aclThreshold' => $aclThreshold, ]); } } @@ -2170,21 +2188,21 @@ class Stream $teamIdList = $params['teamIdList']; $userIdList = $params['userIdList']; - $threshold = $params['threshold']; + $notificationThreshold = $params['notificationThreshold']; + $aclThreshold = $params['aclThreshold']; - $noteOptions = [ - 'forceProcessNotifications' => $forceProcessNoteNotifications, - ]; + $createdAt = $note->getCreatedAt(); - if (!$entity->isNew() && $note->get('createdAt')) { - try { - $createdAtDt = new DateTime($note->get('createdAt')); - } - catch (Exception $e) { - return; + if (!$createdAt) { + return; + } + + if (!$entity->isNew()) { + if ($createdAt->getTimestamp() < $notificationThreshold->getTimestamp()) { + $forceProcessNoteNotifications = false; } - if ($createdAtDt->getTimestamp() < $threshold->getTimestamp()) { + if ($createdAt->getTimestamp() < $aclThreshold->getTimestamp()) { return; } } @@ -2197,7 +2215,9 @@ class Stream $note->set('usersIds', $userIdList); } - $this->entityManager->saveEntity($note, $noteOptions); + $this->entityManager->saveEntity($note, [ + 'forceProcessNotifications' => $forceProcessNoteNotifications, + ]); } public function applyAccessControlToNote(NoteEntity $note, ?User $user = null): void