mirror of
https://github.com/espocrm/espocrm.git
synced 2026-06-28 06:56:05 +00:00
Auto follow refactoring. bypassAssignedUserFollow parameter.
This commit is contained in:
@@ -362,45 +362,47 @@ class HookProcessor
|
||||
*/
|
||||
private function afterSaveStreamNew(CoreEntity $entity, array $options): void
|
||||
{
|
||||
$entityType = $entity->getEntityType();
|
||||
$multipleField = $this->getFollowingUsersField($entity->getEntityType());
|
||||
|
||||
$multipleField = $this->metadata->get(['streamDefs', $entityType, 'followingUsersField']) ??
|
||||
Field::ASSIGNED_USERS;
|
||||
|
||||
$hasAssignedUsersField = $entity->hasLinkMultipleField($multipleField);
|
||||
$createdById = $entity->get(Field::CREATED_BY . 'Id');
|
||||
|
||||
$userIdList = [];
|
||||
|
||||
$assignedUserId = $entity->get('assignedUserId');
|
||||
$createdById = $entity->get('createdById');
|
||||
|
||||
/** @var string[] $assignedUserIdList */
|
||||
$assignedUserIdList = $hasAssignedUsersField ? $entity->getLinkMultipleIdList($multipleField) : [];
|
||||
|
||||
if (
|
||||
!$this->user->isSystem() &&
|
||||
!$this->user->isApi() &&
|
||||
$createdById &&
|
||||
$createdById === $this->user->getId() &&
|
||||
(
|
||||
$this->user->isPortal() ||
|
||||
$this->preferences->get('followCreatedEntities') ||
|
||||
in_array($entityType, $this->preferences->get('followCreatedEntityTypeList') ?? [])
|
||||
)
|
||||
) {
|
||||
if ($this->toMakeCreatorFollow($createdById, $entity->getEntityType())) {
|
||||
$userIdList[] = $createdById;
|
||||
}
|
||||
|
||||
if ($hasAssignedUsersField) {
|
||||
$userIdList = array_unique(
|
||||
array_merge($userIdList, $assignedUserIdList)
|
||||
|
||||
if (
|
||||
!$this->helper->hasAssignedUsersField($entity->getEntityType()) &&
|
||||
!$this->bypassAssignedUserFollow($entity->getEntityType())
|
||||
) {
|
||||
$assignedUserId = $entity->get(Field::ASSIGNED_USER . 'Id');
|
||||
|
||||
if ($assignedUserId) {
|
||||
$userIdList[] = $assignedUserId;
|
||||
}
|
||||
}
|
||||
|
||||
if (
|
||||
$this->helper->hasAssignedUsersField($entity->getEntityType()) &&
|
||||
!$this->bypassAssignedUserFollow($entity->getEntityType())
|
||||
) {
|
||||
$userIdList = array_merge(
|
||||
$userIdList,
|
||||
$entity->getLinkMultipleIdList(Field::ASSIGNED_USERS)
|
||||
);
|
||||
}
|
||||
|
||||
if ($assignedUserId && !in_array($assignedUserId, $userIdList)) {
|
||||
$userIdList[] = $assignedUserId;
|
||||
if ($multipleField && $entity->hasLinkMultipleField($multipleField)) {
|
||||
$userIdList = array_merge(
|
||||
$userIdList,
|
||||
$entity->getLinkMultipleIdList($multipleField)
|
||||
);
|
||||
}
|
||||
|
||||
$userIdList = array_unique($userIdList);
|
||||
|
||||
if (count($userIdList)) {
|
||||
$this->service->followEntityMass($entity, $userIdList);
|
||||
}
|
||||
@@ -413,20 +415,7 @@ class HookProcessor
|
||||
$entity->set(Field::IS_FOLLOWED, true);
|
||||
}
|
||||
|
||||
$autofollowUserIdList = $this->getAutofollowUserIdList($entity->getEntityType(), $userIdList);
|
||||
|
||||
if (count($autofollowUserIdList)) {
|
||||
$this->jobSchedulerFactory
|
||||
->create()
|
||||
->setClassName(AutoFollowJob::class)
|
||||
->setQueue(QueueName::Q1)
|
||||
->setData([
|
||||
'userIdList' => $autofollowUserIdList,
|
||||
'entityType' => $entity->getEntityType(),
|
||||
'entityId' => $entity->getId(),
|
||||
])
|
||||
->schedule();
|
||||
}
|
||||
$this->createAutoFollowJob($entity, $userIdList);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -447,40 +436,29 @@ class HookProcessor
|
||||
return;
|
||||
}
|
||||
|
||||
if ($entity->isAttributeChanged('assignedUserId')) {
|
||||
$hasAssignedUsersField = $this->helper->hasAssignedUsersField($entity->getEntityType());
|
||||
|
||||
if (!$hasAssignedUsersField && $entity->isAttributeChanged(Field::ASSIGNED_USER . 'Id')) {
|
||||
$this->afterSaveStreamNotNewAssignedUserIdChanged($entity, $options);
|
||||
} else if (
|
||||
}
|
||||
|
||||
if (
|
||||
$hasAssignedUsersField &&
|
||||
$entity->hasLinkMultipleField(self::FIELD_ASSIGNED_USERS) &&
|
||||
$entity->isAttributeChanged(self::FIELD_ASSIGNED_USERS . 'Ids')
|
||||
) {
|
||||
$this->afterSaveStreamNotNewAssignedUsersIdsChanged($entity, $options);
|
||||
}
|
||||
|
||||
$multipleField = $this->getFollowingUsersField($entity->getEntityType());
|
||||
|
||||
if ($multipleField && $entity->hasLinkMultipleField($multipleField)) {
|
||||
$this->afterSaveStreamFollowingUsers($entity, $multipleField);
|
||||
}
|
||||
|
||||
if (empty($options[SaveOption::SKIP_AUDITED])) {
|
||||
$this->service->handleAudited($entity, $options);
|
||||
}
|
||||
|
||||
$multipleField = $this->metadata->get(['streamDefs', $entity->getEntityType(), 'followingUsersField']) ??
|
||||
Field::ASSIGNED_USERS;
|
||||
|
||||
if (!$entity->hasLinkMultipleField($multipleField)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$assignedUserIdList = $entity->getLinkMultipleIdList($multipleField);
|
||||
$fetchedAssignedUserIdList = $entity->getFetched($multipleField . 'Ids') ?? [];
|
||||
|
||||
foreach ($assignedUserIdList as $userId) {
|
||||
if (in_array($userId, $fetchedAssignedUserIdList)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$this->service->followEntity($entity, $userId);
|
||||
|
||||
if ($this->user->getId() === $userId) {
|
||||
$entity->set(Field::IS_FOLLOWED, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -488,7 +466,7 @@ class HookProcessor
|
||||
*/
|
||||
private function afterSaveStreamNotNewAssignedUserIdChanged(Entity $entity, array $options): void
|
||||
{
|
||||
$assignedUserId = $entity->get('assignedUserId');
|
||||
$assignedUserId = $entity->get(Field::ASSIGNED_USER . 'Id');
|
||||
|
||||
if (!$assignedUserId) {
|
||||
$this->service->noteAssign($entity, $options);
|
||||
@@ -496,12 +474,15 @@ class HookProcessor
|
||||
return;
|
||||
}
|
||||
|
||||
$this->service->followEntity($entity, $assignedUserId);
|
||||
$this->service->noteAssign($entity, $options);
|
||||
if (!$this->bypassAssignedUserFollow($entity->getEntityType())) {
|
||||
$this->service->followEntity($entity, $assignedUserId);
|
||||
|
||||
if ($this->user->getId() === $assignedUserId) {
|
||||
$entity->set(Field::IS_FOLLOWED, true);
|
||||
if ($this->user->getId() === $assignedUserId) {
|
||||
$entity->set(Field::IS_FOLLOWED, true);
|
||||
}
|
||||
}
|
||||
|
||||
$this->service->noteAssign($entity, $options);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -509,20 +490,26 @@ class HookProcessor
|
||||
*/
|
||||
private function afterSaveStreamNotNewAssignedUsersIdsChanged(CoreEntity $entity, array $options): void
|
||||
{
|
||||
$userIds = $entity->getLinkMultipleIdList(self::FIELD_ASSIGNED_USERS);
|
||||
if ($this->bypassAssignedUserFollow($entity->getEntityType())) {
|
||||
$this->service->noteAssign($entity, $options);
|
||||
|
||||
/** @var string[] $prevUserIds */
|
||||
$prevUserIds = $entity->getFetched(self::FIELD_ASSIGNED_USERS . 'Ids') ?? [];
|
||||
return;
|
||||
}
|
||||
|
||||
foreach (array_diff($userIds, $prevUserIds) as $userId) {
|
||||
$userIdsList = array_diff(
|
||||
$entity->getLinkMultipleIdList(self::FIELD_ASSIGNED_USERS),
|
||||
$entity->getFetchedLinkMultipleIdList(self::FIELD_ASSIGNED_USERS),
|
||||
);
|
||||
|
||||
foreach ($userIdsList as $userId) {
|
||||
$this->service->followEntity($entity, $userId);
|
||||
}
|
||||
|
||||
$this->service->noteAssign($entity, $options);
|
||||
|
||||
if (in_array($this->user->getId(), $userIds)) {
|
||||
if (in_array($this->user->getId(), $userIdsList)) {
|
||||
$entity->set(Field::IS_FOLLOWED, true);
|
||||
}
|
||||
|
||||
$this->service->noteAssign($entity, $options);
|
||||
}
|
||||
|
||||
private function afterSaveStreamNotNew2(CoreEntity $entity): void
|
||||
@@ -711,4 +698,67 @@ class HookProcessor
|
||||
|
||||
return $preferences?->get('followAsCollaborator') === true;
|
||||
}
|
||||
|
||||
|
||||
private function afterSaveStreamFollowingUsers(CoreEntity $entity, string $multipleField): void
|
||||
{
|
||||
$userIdsList = array_diff(
|
||||
$entity->getLinkMultipleIdList($multipleField),
|
||||
$entity->getFetchedLinkMultipleIdList($multipleField),
|
||||
);
|
||||
|
||||
foreach ($userIdsList as $userId) {
|
||||
$this->service->followEntity($entity, $userId);
|
||||
|
||||
if ($this->user->getId() === $userId) {
|
||||
$entity->set(Field::IS_FOLLOWED, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private function bypassAssignedUserFollow(string $entityType): bool
|
||||
{
|
||||
return (bool) $this->metadata->get("streamDefs.$entityType.bypassAssignedUserFollow");
|
||||
}
|
||||
|
||||
private function getFollowingUsersField(string $entityType): ?string
|
||||
{
|
||||
return $this->metadata->get("streamDefs.$entityType.followingUsersField");
|
||||
}
|
||||
|
||||
private function toMakeCreatorFollow(?string $createdById, string $entityType): bool
|
||||
{
|
||||
return !$this->user->isSystem() &&
|
||||
!$this->user->isApi() &&
|
||||
$createdById &&
|
||||
$createdById === $this->user->getId() &&
|
||||
(
|
||||
$this->user->isPortal() ||
|
||||
$this->preferences->get('followCreatedEntities') ||
|
||||
in_array($entityType, $this->preferences->get('followCreatedEntityTypeList') ?? [])
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string[] $userIdList
|
||||
*/
|
||||
private function createAutoFollowJob(CoreEntity $entity, array $userIdList): void
|
||||
{
|
||||
$autoFollowUserIdList = $this->getAutofollowUserIdList($entity->getEntityType(), $userIdList);
|
||||
|
||||
if (!count($autoFollowUserIdList)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->jobSchedulerFactory
|
||||
->create()
|
||||
->setClassName(AutoFollowJob::class)
|
||||
->setQueue(QueueName::Q1)
|
||||
->setData([
|
||||
'userIdList' => $autoFollowUserIdList,
|
||||
'entityType' => $entity->getEntityType(),
|
||||
'entityId' => $entity->getId(),
|
||||
])
|
||||
->schedule();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,6 +29,10 @@
|
||||
}
|
||||
},
|
||||
"description": "Subscribers cleanup."
|
||||
},
|
||||
"bypassAssignedUserFollow": {
|
||||
"type": "boolean",
|
||||
"description": "Assigned users won't follow the record when they get assigned to it. If enabling, consider also enabling `forceAssignmentNotificator` in notificationDefs – otherwise, the assignee won't receive any notification.. As of v10.0."
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
133
tests/integration/Espo/Stream/FollowTest.php
Normal file
133
tests/integration/Espo/Stream/FollowTest.php
Normal file
@@ -0,0 +1,133 @@
|
||||
<?php
|
||||
/************************************************************************
|
||||
* This file is part of EspoCRM.
|
||||
*
|
||||
* EspoCRM – Open Source CRM application.
|
||||
* Copyright (C) 2014-2026 EspoCRM, Inc.
|
||||
* Website: https://www.espocrm.com
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*
|
||||
* The interactive user interfaces in modified source and object code versions
|
||||
* of this program must display Appropriate Legal Notices, as required under
|
||||
* Section 5 of the GNU Affero General Public License version 3.
|
||||
*
|
||||
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
|
||||
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
|
||||
************************************************************************/
|
||||
|
||||
namespace integration\Espo\Stream;
|
||||
|
||||
use Espo\Core\Acl\Table;
|
||||
use Espo\Core\Field\LinkMultiple;
|
||||
use Espo\Core\Name\Field;
|
||||
use Espo\Modules\Crm\Entities\Account;
|
||||
use Espo\Modules\Crm\Entities\Contact;
|
||||
use Espo\Modules\Crm\Entities\Meeting;
|
||||
use Espo\Tools\EntityManager\EntityManager;
|
||||
use Espo\Tools\Stream\Service;
|
||||
use tests\integration\Core\BaseTestCase;
|
||||
|
||||
class FollowTest extends BaseTestCase
|
||||
{
|
||||
/**
|
||||
* @noinspection PhpUnhandledExceptionInspection
|
||||
*/
|
||||
public function testFollow(): void
|
||||
{
|
||||
$roleData = [
|
||||
Account::ENTITY_TYPE => [
|
||||
'create' => Table::LEVEL_NO,
|
||||
'read' => Table::LEVEL_ALL,
|
||||
'stream' => Table::LEVEL_ALL,
|
||||
],
|
||||
Contact::ENTITY_TYPE => [
|
||||
'create' => Table::LEVEL_NO,
|
||||
'read' => Table::LEVEL_ALL,
|
||||
'stream' => Table::LEVEL_ALL,
|
||||
],
|
||||
Meeting::ENTITY_TYPE => [
|
||||
'create' => Table::LEVEL_NO,
|
||||
'read' => Table::LEVEL_ALL,
|
||||
'stream' => Table::LEVEL_ALL,
|
||||
],
|
||||
];
|
||||
|
||||
$user1 = $this->createUser('test-1', [
|
||||
'data' => $roleData,
|
||||
]);
|
||||
|
||||
$user2 = $this->createUser('test-2', [
|
||||
'data' => $roleData,
|
||||
]);
|
||||
|
||||
$user3 = $this->createUser('test-3', [
|
||||
'data' => $roleData,
|
||||
]);
|
||||
|
||||
$tool = $this->getInjectableFactory()->create(EntityManager::class);
|
||||
|
||||
/** @noinspection PhpArrayKeyDoesNotMatchArrayShapeInspection */
|
||||
$tool->update(Contact::ENTITY_TYPE, ['assignedUsers' => true]);
|
||||
|
||||
$this->reCreateApplication();
|
||||
|
||||
$streamService = $this->getInjectableFactory()->create(Service::class);
|
||||
|
||||
$em = $this->getEntityManager();
|
||||
|
||||
//
|
||||
|
||||
$account = $em->getRepositoryByClass(Account::class)->getNew();
|
||||
$account->setAssignedUser($user1);
|
||||
$em->saveEntity($account);
|
||||
|
||||
$this->assertTrue($streamService->checkIsFollowed($account, $user1->getId()));
|
||||
|
||||
$account->setAssignedUser($user2);
|
||||
$em->saveEntity($account);
|
||||
|
||||
$this->assertTrue($streamService->checkIsFollowed($account, $user2->getId()));
|
||||
|
||||
//
|
||||
|
||||
$contact = $em->getRepositoryByClass(Contact::class)->getNew();
|
||||
$contact->setLinkMultipleIdList(Field::ASSIGNED_USERS, [$user1->getId()]);
|
||||
$em->saveEntity($contact);
|
||||
|
||||
$this->assertTrue($streamService->checkIsFollowed($contact, $user1->getId()));
|
||||
|
||||
$contact->setLinkMultipleIdList(Field::ASSIGNED_USERS, [$user2->getId()]);
|
||||
$em->saveEntity($contact);
|
||||
|
||||
$this->assertTrue($streamService->checkIsFollowed($contact, $user2->getId()));
|
||||
|
||||
//
|
||||
|
||||
$meeting = $em->getRepositoryByClass(Meeting::class)->getNew();
|
||||
$meeting->setAssignedUser($user1);
|
||||
$meeting->setUsers(LinkMultiple::create()->withAddedId($user2->getId()));
|
||||
|
||||
$em->saveEntity($meeting);
|
||||
|
||||
$this->assertTrue($streamService->checkIsFollowed($meeting, $user1->getId()));
|
||||
$this->assertTrue($streamService->checkIsFollowed($meeting, $user2->getId()));
|
||||
|
||||
$meeting->setUsers(LinkMultiple::create()->withAddedId($user3->getId()));
|
||||
|
||||
$em->saveEntity($meeting);
|
||||
|
||||
$this->assertTrue($streamService->checkIsFollowed($meeting, $user3->getId()));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user