Skip to content

EspoCRM – Open Source CRM Application

notification: show only not read on first fetch

Yurii7ad455f11484
diff --git a/application/Espo/Tools/Notification/GroupAllService.php b/application/Espo/Tools/Notification/GroupAllService.php
index 6795c658b7..4319236c65 100644
--- a/application/Espo/Tools/Notification/GroupAllService.php
+++ b/application/Espo/Tools/Notification/GroupAllService.php
@@ -63,6 +63,22 @@ class GroupAllService
* @throws Forbidden
*/
public function get(string $type, string $groupId, SearchParams $searchParams): RecordCollection
+ {
+ $searchParams = $this->amendMaxSize($type, $groupId, $searchParams);
+
+ $collection = $this->getInternal($type, $groupId, $searchParams);
+
+ $this->markAsRead($collection);
+
+ return $collection;
+ }
+
+ /**
+ * @return RecordCollection<Notification>
+ * @throws BadRequest
+ * @throws Forbidden
+ */
+ private function getInternal(string $type, string $groupId, SearchParams $searchParams): RecordCollection
{
$collection = null;
@@ -76,8 +92,6 @@ class GroupAllService
throw new BadRequest("Bad group.");
}
- $this->markAsRead($collection);
-
return $collection;
}
@@ -354,4 +368,52 @@ class GroupAllService
$this->entityManager->getQueryExecutor()->execute($query);
}
+
+ private function hasBeforeNumber(SearchParams $searchParams): bool
+ {
+ foreach ($searchParams->getWhere()?->getItemList() ?? [] as $item) {
+ if ($item->getAttribute() === Notification::ATTR_NUMBER) {
+ return true;
+ }
+ }
+
+ return false;
+ }
+
+ /**
+ * @throws BadRequest
+ * @throws Forbidden
+ */
+ private function amendMaxSize(string $type, string $groupId, SearchParams $searchParams): SearchParams
+ {
+ if ($this->hasBeforeNumber($searchParams)) {
+ return $searchParams;
+ }
+
+ $collection = $this->getInternal($type, $groupId, $searchParams);
+
+ $firstReadIndex = -1;
+ $hasNotRead = false;
+ $hasNotReadAfterRead = false;
+
+ foreach ($collection->getCollection() as $i => $entity) {
+ if ($entity->isRead() && $firstReadIndex === -1) {
+ $firstReadIndex = $i;
+ }
+
+ if (!$entity->isRead()) {
+ $hasNotRead = true;
+ }
+
+ if ($firstReadIndex !== -1 && !$entity->isRead()) {
+ $hasNotReadAfterRead = true;
+ }
+ }
+
+ if ($firstReadIndex !== -1 && $hasNotRead && !$hasNotReadAfterRead) {
+ $searchParams = $searchParams->withMaxSize($firstReadIndex);
+ }
+
+ return $searchParams;
+ }
}