2016-05-09 11:02:07 +03:00
|
|
|
<?php
|
|
|
|
/**
|
2017-11-06 17:56:42 +03:00
|
|
|
*
|
|
|
|
*
|
2016-05-09 11:02:07 +03:00
|
|
|
* @author Arthur Schiwon <blizzz@arthur-schiwon.de>
|
2020-04-29 12:57:22 +03:00
|
|
|
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
|
2019-12-03 21:57:53 +03:00
|
|
|
* @author Joas Schilling <coding@schilljs.com>
|
2016-05-09 11:02:07 +03:00
|
|
|
*
|
|
|
|
* @license GNU AGPL version 3 or any later version
|
|
|
|
*
|
2017-11-06 17:56:42 +03:00
|
|
|
* 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.
|
2016-05-09 11:02:07 +03:00
|
|
|
*
|
2017-11-06 17:56:42 +03:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
2016-05-09 11:02:07 +03:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2017-11-06 17:56:42 +03:00
|
|
|
* GNU Affero General Public License for more details.
|
2016-05-09 11:02:07 +03:00
|
|
|
*
|
2017-11-06 17:56:42 +03:00
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
2019-12-03 21:57:53 +03:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2016-05-09 11:02:07 +03:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace OCA\Comments\Controller;
|
|
|
|
|
|
|
|
use OCP\AppFramework\Controller;
|
|
|
|
use OCP\AppFramework\Http\NotFoundResponse;
|
|
|
|
use OCP\AppFramework\Http\RedirectResponse;
|
|
|
|
use OCP\AppFramework\Http\Response;
|
|
|
|
use OCP\Comments\IComment;
|
|
|
|
use OCP\Comments\ICommentsManager;
|
2018-09-12 12:23:38 +03:00
|
|
|
use OCP\Files\IRootFolder;
|
2016-05-09 11:02:07 +03:00
|
|
|
use OCP\IRequest;
|
|
|
|
use OCP\IURLGenerator;
|
2018-09-12 12:23:38 +03:00
|
|
|
use OCP\IUser;
|
2016-05-09 11:02:07 +03:00
|
|
|
use OCP\IUserSession;
|
|
|
|
use OCP\Notification\IManager;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class Notifications
|
|
|
|
*
|
|
|
|
* @package OCA\Comments\Controller
|
|
|
|
*/
|
|
|
|
class Notifications extends Controller {
|
2018-09-12 12:23:38 +03:00
|
|
|
/** @var IRootFolder */
|
|
|
|
protected $rootFolder;
|
2016-05-09 11:02:07 +03:00
|
|
|
|
|
|
|
/** @var ICommentsManager */
|
|
|
|
protected $commentsManager;
|
|
|
|
|
|
|
|
/** @var IURLGenerator */
|
|
|
|
protected $urlGenerator;
|
|
|
|
|
|
|
|
/** @var IManager */
|
|
|
|
protected $notificationManager;
|
|
|
|
|
|
|
|
/** @var IUserSession */
|
|
|
|
protected $userSession;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Notifications constructor.
|
|
|
|
*
|
|
|
|
* @param string $appName
|
|
|
|
* @param IRequest $request
|
|
|
|
* @param ICommentsManager $commentsManager
|
2018-09-12 12:23:38 +03:00
|
|
|
* @param IRootFolder $rootFolder
|
2016-05-09 11:02:07 +03:00
|
|
|
* @param IURLGenerator $urlGenerator
|
|
|
|
* @param IManager $notificationManager
|
|
|
|
* @param IUserSession $userSession
|
|
|
|
*/
|
|
|
|
public function __construct(
|
|
|
|
$appName,
|
|
|
|
IRequest $request,
|
|
|
|
ICommentsManager $commentsManager,
|
2018-09-12 12:23:38 +03:00
|
|
|
IRootFolder $rootFolder,
|
2016-05-09 11:02:07 +03:00
|
|
|
IURLGenerator $urlGenerator,
|
|
|
|
IManager $notificationManager,
|
|
|
|
IUserSession $userSession
|
|
|
|
) {
|
|
|
|
parent::__construct($appName, $request);
|
|
|
|
$this->commentsManager = $commentsManager;
|
2018-09-12 12:23:38 +03:00
|
|
|
$this->rootFolder = $rootFolder;
|
2016-05-09 11:02:07 +03:00
|
|
|
$this->urlGenerator = $urlGenerator;
|
|
|
|
$this->notificationManager = $notificationManager;
|
|
|
|
$this->userSession = $userSession;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-09-12 12:23:38 +03:00
|
|
|
* @PublicPage
|
2016-05-09 11:02:07 +03:00
|
|
|
* @NoCSRFRequired
|
|
|
|
*
|
|
|
|
* @param string $id the comment ID
|
|
|
|
* @return Response
|
|
|
|
*/
|
|
|
|
public function view($id) {
|
2018-09-12 12:23:38 +03:00
|
|
|
$currentUser = $this->userSession->getUser();
|
|
|
|
if (!$currentUser instanceof IUser) {
|
|
|
|
return new RedirectResponse(
|
|
|
|
$this->urlGenerator->linkToRoute('core.login.showLoginForm', [
|
|
|
|
'redirect_url' => $this->urlGenerator->linkToRoute(
|
|
|
|
'comments.Notifications.view',
|
|
|
|
['id' => $id]
|
|
|
|
),
|
|
|
|
])
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-05-09 11:02:07 +03:00
|
|
|
try {
|
|
|
|
$comment = $this->commentsManager->get($id);
|
2020-04-10 15:19:56 +03:00
|
|
|
if ($comment->getObjectType() !== 'files') {
|
2016-05-09 11:02:07 +03:00
|
|
|
return new NotFoundResponse();
|
|
|
|
}
|
2018-09-12 12:23:38 +03:00
|
|
|
$userFolder = $this->rootFolder->getUserFolder($currentUser->getUID());
|
|
|
|
$files = $userFolder->getById((int)$comment->getObjectId());
|
|
|
|
|
|
|
|
$this->markProcessed($comment, $currentUser);
|
|
|
|
|
|
|
|
if (empty($files)) {
|
2016-05-09 11:02:07 +03:00
|
|
|
return new NotFoundResponse();
|
|
|
|
}
|
|
|
|
|
|
|
|
$url = $this->urlGenerator->linkToRouteAbsolute(
|
2016-10-07 18:27:33 +03:00
|
|
|
'files.viewcontroller.showFile',
|
|
|
|
[ 'fileid' => $comment->getObjectId() ]
|
2016-05-09 11:02:07 +03:00
|
|
|
);
|
|
|
|
|
|
|
|
return new RedirectResponse($url);
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
return new NotFoundResponse();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Marks the notification about a comment as processed
|
|
|
|
* @param IComment $comment
|
2018-09-12 12:23:38 +03:00
|
|
|
* @param IUser $currentUser
|
2016-05-09 11:02:07 +03:00
|
|
|
*/
|
2018-09-12 12:23:38 +03:00
|
|
|
protected function markProcessed(IComment $comment, IUser $currentUser) {
|
2016-05-09 11:02:07 +03:00
|
|
|
$notification = $this->notificationManager->createNotification();
|
|
|
|
$notification->setApp('comments')
|
|
|
|
->setObject('comment', $comment->getId())
|
|
|
|
->setSubject('mention')
|
2018-09-12 12:23:38 +03:00
|
|
|
->setUser($currentUser->getUID());
|
2016-05-09 11:02:07 +03:00
|
|
|
$this->notificationManager->markProcessed($notification);
|
|
|
|
}
|
|
|
|
}
|