2016-05-09 11:02:07 +03:00
|
|
|
<?php
|
|
|
|
/**
|
2017-11-06 17:56:42 +03:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
|
|
|
*
|
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>
|
2017-11-06 17:56:42 +03:00
|
|
|
* @author Joas Schilling <coding@schilljs.com>
|
2016-05-09 11:02:07 +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-05-09 11:02:07 +03:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace OCA\Comments\Notification;
|
|
|
|
|
|
|
|
use OCP\Comments\CommentsEvent;
|
|
|
|
use OCP\Comments\IComment;
|
|
|
|
use OCP\IUserManager;
|
|
|
|
use OCP\Notification\IManager;
|
|
|
|
|
|
|
|
class Listener {
|
|
|
|
/** @var IManager */
|
|
|
|
protected $notificationManager;
|
|
|
|
|
|
|
|
/** @var IUserManager */
|
|
|
|
protected $userManager;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Listener constructor.
|
|
|
|
*
|
|
|
|
* @param IManager $notificationManager
|
|
|
|
* @param IUserManager $userManager
|
|
|
|
*/
|
|
|
|
public function __construct(
|
|
|
|
IManager $notificationManager,
|
2017-01-09 14:03:34 +03:00
|
|
|
IUserManager $userManager
|
2016-05-09 11:02:07 +03:00
|
|
|
) {
|
|
|
|
$this->notificationManager = $notificationManager;
|
|
|
|
$this->userManager = $userManager;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param CommentsEvent $event
|
|
|
|
*/
|
|
|
|
public function evaluate(CommentsEvent $event) {
|
|
|
|
$comment = $event->getComment();
|
|
|
|
|
2016-10-14 01:19:31 +03:00
|
|
|
$mentions = $this->extractMentions($comment->getMentions());
|
2020-04-10 15:19:56 +03:00
|
|
|
if (empty($mentions)) {
|
2016-05-09 11:02:07 +03:00
|
|
|
// no one to notify
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$notification = $this->instantiateNotification($comment);
|
|
|
|
|
2020-04-10 15:19:56 +03:00
|
|
|
foreach ($mentions as $uid) {
|
|
|
|
if (($comment->getActorType() === 'users' && $uid === $comment->getActorId())
|
2016-10-14 01:19:31 +03:00
|
|
|
|| !$this->userManager->userExists($uid)
|
2016-05-09 11:02:07 +03:00
|
|
|
) {
|
|
|
|
// do not notify unknown users or yourself
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-10-14 01:19:31 +03:00
|
|
|
$notification->setUser($uid);
|
2020-04-10 15:19:56 +03:00
|
|
|
if ($event->getEvent() === CommentsEvent::EVENT_DELETE
|
|
|
|
|| $event->getEvent() === CommentsEvent::EVENT_PRE_UPDATE) {
|
2016-05-09 11:02:07 +03:00
|
|
|
$this->notificationManager->markProcessed($notification);
|
|
|
|
} else {
|
|
|
|
$this->notificationManager->notify($notification);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* creates a notification instance and fills it with comment data
|
|
|
|
*
|
|
|
|
* @param IComment $comment
|
|
|
|
* @return \OCP\Notification\INotification
|
|
|
|
*/
|
|
|
|
public function instantiateNotification(IComment $comment) {
|
|
|
|
$notification = $this->notificationManager->createNotification();
|
|
|
|
$notification
|
|
|
|
->setApp('comments')
|
|
|
|
->setObject('comment', $comment->getId())
|
|
|
|
->setSubject('mention', [ $comment->getObjectType(), $comment->getObjectId() ])
|
2017-01-09 13:18:33 +03:00
|
|
|
->setDateTime($comment->getCreationDateTime());
|
2016-05-09 11:02:07 +03:00
|
|
|
|
|
|
|
return $notification;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-10-14 01:19:31 +03:00
|
|
|
* flattens the mention array returned from comments to a list of user ids.
|
2016-05-09 11:02:07 +03:00
|
|
|
*
|
2016-10-14 01:19:31 +03:00
|
|
|
* @param array $mentions
|
|
|
|
* @return string[] containing the mentions, e.g. ['alice', 'bob']
|
2016-05-09 11:02:07 +03:00
|
|
|
*/
|
2016-10-14 01:19:31 +03:00
|
|
|
public function extractMentions(array $mentions) {
|
2020-04-10 15:19:56 +03:00
|
|
|
if (empty($mentions)) {
|
2016-05-09 11:02:07 +03:00
|
|
|
return [];
|
|
|
|
}
|
2016-10-14 01:19:31 +03:00
|
|
|
$uids = [];
|
2020-04-10 15:19:56 +03:00
|
|
|
foreach ($mentions as $mention) {
|
|
|
|
if ($mention['type'] === 'user') {
|
2016-10-14 01:19:31 +03:00
|
|
|
$uids[] = $mention['id'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $uids;
|
2016-05-09 11:02:07 +03:00
|
|
|
}
|
|
|
|
}
|