2016-02-05 13:20:16 +03:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
2016-07-21 17:49:16 +03:00
|
|
|
*
|
2019-12-03 21:57:53 +03:00
|
|
|
* @author Arthur Schiwon <blizzz@arthur-schiwon.de>
|
2016-07-21 17:49:16 +03:00
|
|
|
* @author Joas Schilling <coding@schilljs.com>
|
2019-12-03 21:57:53 +03:00
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
2017-11-06 17:56:42 +03:00
|
|
|
* @author Roeland Jago Douma <roeland@famdouma.nl>
|
2016-07-21 17:49:16 +03:00
|
|
|
*
|
2016-02-05 13:20:16 +03:00
|
|
|
* @license AGPL-3.0
|
|
|
|
*
|
|
|
|
* This code is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License, version 3,
|
|
|
|
* as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* 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, version 3,
|
2019-12-03 21:57:53 +03:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
2016-02-05 13:20:16 +03:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace OCA\Comments\Activity;
|
|
|
|
|
|
|
|
use OCP\Activity\IManager;
|
|
|
|
use OCP\App\IAppManager;
|
|
|
|
use OCP\Comments\CommentsEvent;
|
|
|
|
use OCP\Files\Config\IMountProviderCollection;
|
|
|
|
use OCP\Files\IRootFolder;
|
|
|
|
use OCP\Files\Node;
|
|
|
|
use OCP\IUser;
|
|
|
|
use OCP\IUserSession;
|
2017-04-20 15:34:28 +03:00
|
|
|
use OCP\Share\IShareHelper;
|
2016-02-05 13:20:16 +03:00
|
|
|
|
|
|
|
class Listener {
|
|
|
|
/** @var IManager */
|
|
|
|
protected $activityManager;
|
|
|
|
/** @var IUserSession */
|
|
|
|
protected $session;
|
|
|
|
/** @var \OCP\App\IAppManager */
|
|
|
|
protected $appManager;
|
|
|
|
/** @var \OCP\Files\Config\IMountProviderCollection */
|
|
|
|
protected $mountCollection;
|
|
|
|
/** @var \OCP\Files\IRootFolder */
|
|
|
|
protected $rootFolder;
|
2017-04-20 15:34:28 +03:00
|
|
|
/** @var IShareHelper */
|
|
|
|
protected $shareHelper;
|
2016-02-05 13:20:16 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Listener constructor.
|
|
|
|
*
|
|
|
|
* @param IManager $activityManager
|
|
|
|
* @param IUserSession $session
|
|
|
|
* @param IAppManager $appManager
|
|
|
|
* @param IMountProviderCollection $mountCollection
|
|
|
|
* @param IRootFolder $rootFolder
|
2017-04-20 15:34:28 +03:00
|
|
|
* @param IShareHelper $shareHelper
|
2016-02-05 13:20:16 +03:00
|
|
|
*/
|
|
|
|
public function __construct(IManager $activityManager,
|
|
|
|
IUserSession $session,
|
|
|
|
IAppManager $appManager,
|
|
|
|
IMountProviderCollection $mountCollection,
|
2017-04-20 15:34:28 +03:00
|
|
|
IRootFolder $rootFolder,
|
|
|
|
IShareHelper $shareHelper) {
|
2016-02-05 13:20:16 +03:00
|
|
|
$this->activityManager = $activityManager;
|
|
|
|
$this->session = $session;
|
|
|
|
$this->appManager = $appManager;
|
|
|
|
$this->mountCollection = $mountCollection;
|
|
|
|
$this->rootFolder = $rootFolder;
|
2017-04-20 15:34:28 +03:00
|
|
|
$this->shareHelper = $shareHelper;
|
2016-02-05 13:20:16 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param CommentsEvent $event
|
|
|
|
*/
|
|
|
|
public function commentEvent(CommentsEvent $event) {
|
|
|
|
if ($event->getComment()->getObjectType() !== 'files'
|
2018-01-26 00:20:10 +03:00
|
|
|
|| $event->getEvent() !== CommentsEvent::EVENT_ADD
|
2016-02-05 13:20:16 +03:00
|
|
|
|| !$this->appManager->isInstalled('activity')) {
|
|
|
|
// Comment not for file, not adding a comment or no activity-app enabled (save the energy)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get all mount point owners
|
|
|
|
$cache = $this->mountCollection->getMountCache();
|
2017-07-20 10:04:56 +03:00
|
|
|
$mounts = $cache->getMountsForFileId((int)$event->getComment()->getObjectId());
|
2016-02-05 13:20:16 +03:00
|
|
|
if (empty($mounts)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$users = [];
|
|
|
|
foreach ($mounts as $mount) {
|
|
|
|
$owner = $mount->getUser()->getUID();
|
|
|
|
$ownerFolder = $this->rootFolder->getUserFolder($owner);
|
2017-07-20 10:04:56 +03:00
|
|
|
$nodes = $ownerFolder->getById((int)$event->getComment()->getObjectId());
|
2016-02-05 13:20:16 +03:00
|
|
|
if (!empty($nodes)) {
|
|
|
|
/** @var Node $node */
|
|
|
|
$node = array_shift($nodes);
|
2017-04-20 15:34:28 +03:00
|
|
|
$al = $this->shareHelper->getPathsForAccessList($node);
|
2018-02-14 16:39:39 +03:00
|
|
|
$users += $al['users'];
|
2016-02-05 13:20:16 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$actor = $this->session->getUser();
|
|
|
|
if ($actor instanceof IUser) {
|
|
|
|
$actor = $actor->getUID();
|
|
|
|
} else {
|
|
|
|
$actor = '';
|
|
|
|
}
|
|
|
|
|
|
|
|
$activity = $this->activityManager->generateEvent();
|
2016-11-16 13:39:06 +03:00
|
|
|
$activity->setApp('comments')
|
|
|
|
->setType('comments')
|
2016-02-05 13:20:16 +03:00
|
|
|
->setAuthor($actor)
|
2016-11-16 13:39:06 +03:00
|
|
|
->setObject($event->getComment()->getObjectType(), (int) $event->getComment()->getObjectId())
|
|
|
|
->setMessage('add_comment_message', [
|
2017-09-05 19:10:32 +03:00
|
|
|
'commentId' => $event->getComment()->getId(),
|
2016-02-05 13:20:16 +03:00
|
|
|
]);
|
|
|
|
|
|
|
|
foreach ($users as $user => $path) {
|
2018-02-14 16:39:39 +03:00
|
|
|
// numerical user ids end up as integers from array keys, but string
|
|
|
|
// is required
|
|
|
|
$activity->setAffectedUser((string)$user);
|
2016-02-05 13:20:16 +03:00
|
|
|
|
2016-11-16 13:39:06 +03:00
|
|
|
$activity->setSubject('add_comment_subject', [
|
2017-09-05 19:10:32 +03:00
|
|
|
'actor' => $actor,
|
|
|
|
'fileId' => (int) $event->getComment()->getObjectId(),
|
|
|
|
'filePath' => trim($path, '/'),
|
2016-02-05 13:20:16 +03:00
|
|
|
]);
|
|
|
|
$this->activityManager->publish($activity);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|