move autoFollow to job

This commit is contained in:
yuri
2015-02-20 11:51:31 +02:00
parent 6112a9d02d
commit 06bde4f1ce
2 changed files with 86 additions and 17 deletions
+24 -17
View File
@@ -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');
+62
View File
@@ -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)) {