Add notifications for users that are added to the group

Signed-off-by: Joas Schilling <coding@schilljs.com>
This commit is contained in:
Joas Schilling 2019-09-06 13:06:21 +02:00
parent 520042bbd0
commit 8986910a7d
No known key found for this signature in database
GPG Key ID: 7076EA9751AACDDA
2 changed files with 51 additions and 3 deletions

View File

@ -43,6 +43,7 @@ use OCP\AppFramework\Utility\IControllerMethodReflector;
use OCP\Defaults;
use OCP\Federation\ICloudIdManager;
use \OCP\IContainer;
use OCP\IGroup;
use OCP\IServerContainer;
use OCA\Files_Sharing\Capabilities;
use OCA\Files_Sharing\External\Manager;
@ -188,5 +189,10 @@ class Application extends App {
$listener = $this->getContainer()->query(Listener::class);
$listener->shareNotification($event);
});
$dispatcher->addListener(IGroup::class . '::postAddUser', function(GenericEvent $event) {
/** @var Listener $listener */
$listener = $this->getContainer()->query(Listener::class);
$listener->userAddedToGroup($event);
});
}
}

View File

@ -25,24 +25,31 @@ declare(strict_types=1);
namespace OCA\Files_Sharing\Notification;
use OC\Share\Share;
use OCP\IGroup;
use OCP\IGroupManager;
use OCP\Notification\IManager;
use OCP\IUser;
use OCP\Notification\IManager as INotificationManager;
use OCP\Notification\INotification;
use OCP\Share\IManager as IShareManager;
use OCP\Share\IShare;
use Symfony\Component\EventDispatcher\GenericEvent;
class Listener {
/** @var IManager */
/** @var INotificationManager */
protected $notificationManager;
/** @var IShareManager */
protected $shareManager;
/** @var IGroupManager */
protected $groupManager;
public function __construct(
IManager $notificationManager,
INotificationManager $notificationManager,
IShareManager $shareManager,
IGroupManager $groupManager
) {
$this->notificationManager = $notificationManager;
$this->shareManager = $shareManager;
$this->groupManager = $groupManager;
}
@ -74,6 +81,41 @@ class Listener {
}
}
/**
* @param GenericEvent $event
*/
public function userAddedToGroup(GenericEvent $event): void {
/** @var IGroup $group */
$group = $event->getSubject();
/** @var IUser $user */
$user = $event->getArgument('user');
$offset = 0;
while (true) {
$shares = $this->shareManager->getSharedWith($user->getUID(), IShare::TYPE_GROUP, null, 50, $offset);
if (empty($shares)) {
break;
}
foreach ($shares as $share) {
if ($share->getSharedWith() !== $group->getGID()) {
continue;
}
if ($user->getUID() === $share->getShareOwner() ||
$user->getUID() === $share->getSharedBy()) {
continue;
}
$notification = $this->instantiateNotification($share);
$notification->setSubject('incoming_group_share')
->setUser($user->getUID());
$this->notificationManager->notify($notification);
}
$offset += 50;
}
}
/**
* @param IShare $share
* @return INotification