stream note unrelate fix

This commit is contained in:
Yuri Kuznetsov
2023-02-17 10:31:41 +02:00
parent 8f8eb4807d
commit 1c1042cd75
3 changed files with 56 additions and 30 deletions
+1 -1
View File
@@ -69,7 +69,7 @@ class Stream implements AfterSave, AfterRemove, AfterRelate, AfterUnrelate
return;
}
$this->processor->afterRemove($entity);
$this->processor->afterRemove($entity, $options);
}
public function afterRelate(
+10 -29
View File
@@ -45,6 +45,7 @@ use Espo\ORM\EntityManager;
use Espo\ORM\Entity;
use Espo\ORM\Defs\RelationDefs;
use Espo\ORM\Repository\Option\RemoveOptions;
use Espo\Tools\Stream\Service as Service;
use Espo\Tools\Stream\Jobs\AutoFollow as AutoFollowJob;
use Espo\Tools\Stream\Jobs\ControlFollowers as ControlFollowersJob;
@@ -89,13 +90,17 @@ class HookProcessor
}
}
public function afterRemove(Entity $entity): void
public function afterRemove(Entity $entity, RemoveOptions $options): void
{
if ($this->checkHasStream($entity->getEntityType())) {
$this->service->unfollowAllUsersFromEntity($entity);
}
$query = $this->entityManager
if (!$options->has('noStream')) {
return;
}
/*$query = $this->entityManager
->getQueryBuilder()
->update()
->in(Note::ENTITY_TYPE)
@@ -109,7 +114,7 @@ class HookProcessor
])
->build();
$this->entityManager->getQueryExecutor()->execute($query);
$this->entityManager->getQueryExecutor()->execute($query);*/
}
private function checkHasStream(string $entityType): bool
@@ -609,38 +614,14 @@ class HookProcessor
$auditedForeign = $this->metadata->get(['entityDefs', $foreignEntityType, 'links', $foreignLink, 'audited']);
if ($audited) {
$note1 = $this->entityManager
->getRDBRepositoryByClass(Note::class)
->getNew();
$note1->set([
'type' => Note::TYPE_UNRELATE,
'parentId' => $entity->getId(),
'parentType' => $entityType,
'relatedId' => $foreignEntity->getId(),
'relatedType' => $foreignEntityType,
]);
$this->entityManager->saveEntity($note1);
$this->service->noteUnrelate($foreignEntity, $entityType, $entity->getId());
// @todo
// Add time period (a few minutes). If before, remove RELATE note, don't create 'unrelate' if before.
}
if ($auditedForeign) {
$note2 = $this->entityManager
->getRDBRepositoryByClass(Note::class)
->getNew();
$note2->set([
'type' => Note::TYPE_UNRELATE,
'parentId' => $foreignEntity->getId(),
'parentType' => $foreignEntityType,
'relatedId' => $entity->getId(),
'relatedType' => $entityType,
]);
$this->entityManager->saveEntity($note2);
$this->service->noteUnrelate($entity, $foreignEntity->getEntityType(), $foreignEntity->getId());
// @todo
// Add time period (a few minutes). If before, remove RELATE note, don't create 'unrelate' if before.
+45
View File
@@ -764,6 +764,51 @@ class Service
$this->entityManager->saveEntity($note, $o);
}
/**
* @param array<string, mixed> $options
*/
public function noteUnrelate(Entity $entity, string $parentType, string $parentId, array $options = []): void
{
$entityType = $entity->getEntityType();
$existing = $this->entityManager
->getRDBRepository(Note::ENTITY_TYPE)
->select(['id'])
->where([
'type' => Note::TYPE_UNRELATE,
'parentId' => $parentId,
'parentType' => $parentType,
'relatedId' => $entity->getId(),
'relatedType' => $entityType,
])
->findOne();
if ($existing) {
return;
}
/** @var Note $note */
$note = $this->entityManager->getNewEntity(Note::ENTITY_TYPE);
$note->set([
'type' => Note::TYPE_UNRELATE,
'parentId' => $parentId,
'parentType' => $parentType,
'relatedType' => $entityType,
'relatedId' => $entity->getId(),
]);
$this->processNoteTeamsUsers($note, $entity);
$o = [];
if (!empty($options['modifiedById'])) {
$o['createdById'] = $options['modifiedById'];
}
$this->entityManager->saveEntity($note, $o);
}
/**
* @param array<string,mixed> $options
*/