Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 21 additions & 10 deletions apps/comments/appinfo/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,22 +41,33 @@ function() {
$activityManager->registerExtension(function() {
$application = new \OCP\AppFramework\App('comments');
/** @var \OCA\Comments\Activity\Extension $extension */
$extension = $application->getContainer()->query('OCA\Comments\Activity\Extension');
$extension = $application->getContainer()->query(\OCA\Comments\Activity\Extension::class);
return $extension;
});

$managerListener = function(\OCP\Comments\CommentsEvent $event) use ($activityManager) {
$application = new \OCP\AppFramework\App('comments');
/** @var \OCA\Comments\Activity\Listener $listener */
$listener = $application->getContainer()->query('OCA\Comments\Activity\Listener');
$listener->commentEvent($event);
};

$eventDispatcher->addListener(\OCP\Comments\CommentsEvent::EVENT_ADD, $managerListener);

$eventDispatcher->addListener(\OCP\Comments\CommentsEntityEvent::EVENT_ENTITY, function(\OCP\Comments\CommentsEntityEvent $event) {
$event->addEntityCollection('files', function($name) {
$nodes = \OC::$server->getUserFolder()->getById(intval($name));
return !empty($nodes);
});
});

$notificationManager = \OC::$server->getNotificationManager();
$notificationManager->registerNotifier(
function() {
$application = new \OCP\AppFramework\App('comments');
return $application->getContainer()->query(\OCA\Comments\Notification\Notifier::class);
},
function () {
$l = \OC::$server->getL10N('comments');
return ['id' => 'comments', 'name' => $l->t('Comments')];
}
);

$commentsManager = \OC::$server->getCommentsManager();
$commentsManager->registerEventHandler(function () {
$application = new \OCP\AppFramework\App('comments');
/** @var \OCA\Comments\Activity\Extension $extension */
$handler = $application->getContainer()->query(\OCA\Comments\EventHandler::class);
return $handler;
});
28 changes: 28 additions & 0 deletions apps/comments/appinfo/routes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

/**
* @author Arthur Schiwon <blizzz@arthur-schiwon.de>
*
* @copyright Copyright (c) 2016, ownCloud, Inc.
* @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,
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/

use \OCA\Comments\AppInfo\Application;

$application = new Application();
$application->registerRoutes($this, ['routes' => [
['name' => 'Notifications#view', 'url' => '/notifications/view/{id}', 'verb' => 'GET'],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please include the type in the url and dispatch an event on the endpoint.
Comments are already used by other apps, which would like to redirect to other parts then

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The ID is a unique comment ID, not coupled to Actor or Object type. And the URL is still within apps/comments/. Still an issue?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm should be fine then I guess

]]);
35 changes: 35 additions & 0 deletions apps/comments/lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

/**
* @author Arthur Schiwon <blizzz@arthur-schiwon.de>
*
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing @license

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done (this is for me, Github keeps showing me the dealt-with comments despite code changes, making it hard to track whats left…)

* @license GNU AGPL version 3 or any later version
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
* License as published by the Free Software Foundation; either
* version 3 of the License, or any later version.
*
* This library 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 library. If not, see <http://www.gnu.org/licenses/>.
*/

namespace OCA\Comments\AppInfo;

use OCA\Comments\Controller\Notifications;
use OCP\AppFramework\App;

class Application extends App {

public function __construct (array $urlParams = array()) {
parent::__construct('comments', $urlParams);
$container = $this->getContainer();

$container->registerAlias('NotificationsController', Notifications::class);
}
}
134 changes: 134 additions & 0 deletions apps/comments/lib/Controller/Notifications.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
<?php

/**
* @author Arthur Schiwon <blizzz@arthur-schiwon.de>
*
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing license

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

* @license GNU AGPL version 3 or any later version
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
* License as published by the Free Software Foundation; either
* version 3 of the License, or any later version.
*
* This library 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 library. If not, see <http://www.gnu.org/licenses/>.
*
*/

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;
use OCP\Files\Folder;
use OCP\IRequest;
use OCP\IURLGenerator;
use OCP\IUserSession;
use OCP\Notification\IManager;

/**
* Class Notifications
*
* @package OCA\Comments\Controller
*/
class Notifications extends Controller {
/** @var Folder */
protected $folder;

/** @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
* @param Folder $folder
* @param IURLGenerator $urlGenerator
* @param IManager $notificationManager
* @param IUserSession $userSession
*/
public function __construct(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing docs

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

$appName,
IRequest $request,
ICommentsManager $commentsManager,
Folder $folder,
IURLGenerator $urlGenerator,
IManager $notificationManager,
IUserSession $userSession
) {
parent::__construct($appName, $request);
$this->commentsManager = $commentsManager;
$this->folder = $folder;
$this->urlGenerator = $urlGenerator;
$this->notificationManager = $notificationManager;
$this->userSession = $userSession;
}

/**
* @NoAdminRequired
* @NoCSRFRequired
*
* @param string $id the comment ID
* @return Response
*/
public function view($id) {
try {
$comment = $this->commentsManager->get($id);
if($comment->getObjectType() !== 'files') {
return new NotFoundResponse();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as mentioned above, dispatch an event, so announcements can redirect to the given announcement

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now this should not be a problem anymore, because the code path is a result from a registered EventHandler. So, if a different app wants to work with Notifications, it will react upon any event, create the notification pointing to a controller of its own. Thus, to be considered done.

}
$files = $this->folder->getById($comment->getObjectId());
if(count($files) === 0) {
$this->markProcessed($comment);
return new NotFoundResponse();
}

$url = $this->urlGenerator->linkToRouteAbsolute(
'files.viewcontroller.showFile',
[ 'fileid' => $comment->getObjectId() ]
);

$this->markProcessed($comment);

return new RedirectResponse($url);
} catch (\Exception $e) {
return new NotFoundResponse();
}
}

/**
* Marks the notification about a comment as processed
* @param IComment $comment
*/
protected function markProcessed(IComment $comment) {
$user = $this->userSession->getUser();
if(is_null($user)) {
return;
}
$notification = $this->notificationManager->createNotification();
$notification->setApp('comments')
->setObject('comment', $comment->getId())
->setSubject('mention')
->setUser($user->getUID());
$this->notificationManager->markProcessed($notification);
}
}
91 changes: 91 additions & 0 deletions apps/comments/lib/EventHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php

/**
* @copyright Copyright (c) 2016 Arthur Schiwon <blizzz@arthur-schiwon.de>
*
* @author Arthur Schiwon <blizzz@arthur-schiwon.de>
*
* @license GNU AGPL version 3 or any later version
*
* 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 <http://www.gnu.org/licenses/>.
*
*/

namespace OCA\Comments;

use OCA\Comments\Activity\Listener as ActivityListener;
use OCA\Comments\AppInfo\Application;
use OCA\Comments\Notification\Listener as NotificationListener;
use OCP\Comments\CommentsEvent;
use OCP\Comments\ICommentsEventHandler;

/**
* Class EventHandler
*
* @package OCA\Comments
*/
class EventHandler implements ICommentsEventHandler {
/** @var ActivityListener */
private $activityListener;

/** @var NotificationListener */
private $notificationListener;

public function __construct(ActivityListener $activityListener, NotificationListener $notificationListener) {
$this->activityListener = $activityListener;
$this->notificationListener = $notificationListener;
}

/**
* @param CommentsEvent $event
*/
public function handle(CommentsEvent $event) {
if($event->getComment()->getObjectType() !== 'files') {
// this is a 'files'-specific Handler
return;
}

$eventType = $event->getEvent();
if( $eventType === CommentsEvent::EVENT_ADD
) {
$this->notificationHandler($event);
$this->activityHandler($event);
return;
}

$applicableEvents = [
CommentsEvent::EVENT_PRE_UPDATE,
CommentsEvent::EVENT_UPDATE,
CommentsEvent::EVENT_DELETE,
];
if(in_array($eventType, $applicableEvents)) {
$this->notificationHandler($event);
return;
}
}

/**
* @param CommentsEvent $event
*/
private function activityHandler(CommentsEvent $event) {
$this->activityListener->commentEvent($event);
}

/**
* @param CommentsEvent $event
*/
private function notificationHandler(CommentsEvent $event) {
$this->notificationListener->evaluate($event);
}
}
Loading