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
1 change: 1 addition & 0 deletions appinfo/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@

$app = new \OCA\CustomGroups\Application();
$app->registerGroupBackend();
$app->registerNotifier();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

cries for info.xml enhancement ....

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

if we had a generic service registration we wouldn't need to enhance core for everything out there

<services><service type="\OC\INotifier">\OCA\CustomGroups\Notifier</service></services>

then core (or any app) could just find all services of a given type/namespace.

That was what I hoped to achieve with https://central.owncloud.org/t/service-based-app-imlpementation-to-solve-inter-app-dependencies/1615

16 changes: 16 additions & 0 deletions lib/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,20 @@ public function registerGroupBackend() {
$backend = $this->getContainer()->query('\OCA\CustomGroups\CustomGroupsBackend');
$this->getContainer()->getServer()->getGroupManager()->addBackend($backend);
}

/**
* Registers the notifier
*/
public function registerNotifier() {
$manager = $this->getContainer()->getServer()->getNotificationManager();
$manager->registerNotifier(function() use ($manager) {
return $this->getContainer()->query('\OCA\CustomGroups\Notifier');
}, function() {
$l = \OC::$server->getL10N('customgroups');
return [
'id' => 'customgroups',
'name' => $l->t('Custom groups'),
];
});
}
}
2 changes: 2 additions & 0 deletions lib/Dav/GroupMembershipCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,8 @@ public function createFile($userId, $data = null) {
if (!$this->groupsHandler->addToGroup($userId, $groupId, CustomGroupsDatabaseHandler::ROLE_MEMBER)) {
throw new PreconditionFailed("The user \"$userId\" is already member of this group");
}

$this->helper->notifyUser($userId, $this->groupInfo);
}

/**
Expand Down
8 changes: 1 addition & 7 deletions lib/Dav/UsersCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,7 @@ public function createFile($name, $data = null) {
* @throws MethodNotAllowed if the group already exists
*/
public function createDirectory($name) {
$groupId = $this->groupsHandler->createGroup($name, $name);
if (is_null($groupId)) {
throw new MethodNotAllowed("Group with uri \"$name\" already exists");
}

// add current user as admin
$this->groupsHandler->addToGroup($this->helper->getUserId(), $groupId, true);
throw new MethodNotAllowed('Cannot create user nodes');
}

/**
Expand Down
80 changes: 80 additions & 0 deletions lib/Notifier.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php
/**
* @author Vincent Petry <pvince81@owncloud.com>
*
* @copyright Copyright (c) 2017, ownCloud GmbH
* @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/>
*
*/

namespace OCA\CustomGroups;

use OCP\L10N\IFactory;
use OCP\Notification\IManager;
use OCP\Notification\INotification;
use OCP\Notification\INotifier;
use OCA\CustomGroups\CustomGroupsDatabaseHandler;

class Notifier implements INotifier {

/**
* @var IFactory
*/
protected $l10NFactory;

/**
* @var CustomGroupsDatabaseHandler
*/
protected $handler;

/**
* Notifier constructor.
*
* @param IFactory $l10NFactory
* @param CustomGroupsDatabaseHandler $handler
*/
public function __construct(IFactory $l10NFactory, CustomGroupsDatabaseHandler $handler) {
$this->l10NFactory = $l10NFactory;
$this->handler = $handler;
}

/**
* @param INotification $notification
* @param string $languageCode The code of the language that should be used to prepare the notification
* @return INotification
* @throws \InvalidArgumentException When the notification was not prepared by a notifier
*/
public function prepare(INotification $notification, $languageCode) {
if ($notification->getApp() !== 'customgroups') {
throw new \InvalidArgumentException();
}

$l = $this->l10NFactory->get('customgroups', $languageCode);
if ($notification->getObjectType() === 'customgroup') {
if ($notification->getSubject() === 'added_member') {
$notification->setParsedSubject(
$l->t('Added to group "%1$s" by "%2$s".', $notification->getSubjectParameters())
);
}
if ($notification->getMessage() === 'added_member') {
$notification->setParsedMessage(
$l->t('You have been added to the group "%1$s" by "%2$s".', $notification->getMessageParameters())
);
}
}

return $notification;
}
}
43 changes: 42 additions & 1 deletion lib/Service/MembershipHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
use OCP\IGroupManager;
use OCA\CustomGroups\Search;
use OCP\IUser;
use OCP\Notification\IManager;
use OCP\IURLGenerator;

/**
* Membership helper
Expand Down Expand Up @@ -63,6 +65,20 @@ class MembershipHelper {
*/
private $groupManager;

/**
* Notification manager
*
* @var IManager
*/
private $notificationManager;

/**
* URL generator
*
* @var IURLGenerator
*/
private $urlGenerator;

/**
* Membership info for the currently logged in user
*
Expand All @@ -77,17 +93,23 @@ class MembershipHelper {
* @param IUserSession $userSession user session
* @param IUserManager $userManager user manager
* @param IGroupManager $groupManager group manager
* @param IManager $notificationManager notification manager
* @param IURLGenerator $urlGenerator URL generator
*/
public function __construct(
CustomGroupsDatabaseHandler $groupsHandler,
IUserSession $userSession,
IUserManager $userManager,
IGroupManager $groupManager
IGroupManager $groupManager,
IManager $notificationManager,
IURLGenerator $urlGenerator
) {
$this->groupsHandler = $groupsHandler;
$this->userSession = $userSession;
$this->userManager = $userManager;
$this->groupManager = $groupManager;
$this->notificationManager = $notificationManager;
$this->urlGenerator = $urlGenerator;
}

/**
Expand Down Expand Up @@ -233,4 +255,23 @@ public function searchForNewMembers($groupId, $pattern, $limit = 200) {

return $totalResults;
}

/**
* Notify the given user about the given group
*
* @param string $targetUserId user to notify
* @param array $groupInfo group info
*/
public function notifyUser($targetUserId, $groupInfo) {
$link = $this->urlGenerator->linkToRouteAbsolute('settings.SettingsPage.getPersonal', ['sectionid' => 'customgroups', 'group' => $groupInfo['uri']]);
$notification = $this->notificationManager->createNotification();
$notification->setApp('customgroups')
->setDateTime(new \DateTime())
->setObject('customgroup', $groupInfo['group_id'])
->setSubject('added_member', [$this->getUserId(), $groupInfo['display_name']])
->setMessage('added_member', [$this->getUserId(), $groupInfo['display_name']])
->setUser($targetUserId)
->setLink($link);
$this->notificationManager->notify($notification);
}
}
20 changes: 14 additions & 6 deletions tests/unit/Dav/GroupMembershipCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
use OCA\CustomGroups\Service\MembershipHelper;
use OCP\IGroupManager;
use OCA\CustomGroups\Search;
use OCP\IURLGenerator;
use OCP\Notification\IManager;

/**
* Class GroupMembershipCollectionTest
Expand Down Expand Up @@ -91,12 +93,18 @@ public function setUp() {
[strtoupper(self::NODE_USER), $nodeUser],
]));

$this->helper = new MembershipHelper(
$this->handler,
$this->userSession,
$this->userManager,
$this->groupManager
);
$this->helper = $this->getMockBuilder(MembershipHelper::class)
->setMethods(['notifyUser'])
->setConstructorArgs([
$this->handler,
$this->userSession,
$this->userManager,
$this->groupManager,
$this->createMock(IManager::class),
$this->createMock(IURLGenerator::class)
])
->getMock();

$this->node = new GroupMembershipCollection(
['group_id' => 1, 'uri' => 'group1', 'display_name' => 'Group One', 'role' => CustomGroupsDatabaseHandler::ROLE_ADMIN],
$this->handler,
Expand Down
6 changes: 5 additions & 1 deletion tests/unit/Dav/GroupsCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
use OCA\CustomGroups\Service\MembershipHelper;
use OCP\IGroupManager;
use OCA\CustomGroups\Search;
use OCP\IURLGenerator;
use OCP\Notification\IManager;

/**
* Class GroupsCollectionTest
Expand Down Expand Up @@ -78,7 +80,9 @@ public function setUp() {
$this->handler,
$this->userSession,
$this->userManager,
$this->groupManager
$this->groupManager,
$this->createMock(IManager::class),
$this->createMock(IURLGenerator::class)
);
$this->collection = new GroupsCollection($this->handler, $this->helper);
}
Expand Down
10 changes: 8 additions & 2 deletions tests/unit/Dav/MembershipNodeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
use OCP\IGroupManager;
use OCA\CustomGroups\Dav\Roles;
use OCA\CustomGroups\Search;
use OCP\IURLGenerator;
use OCP\Notification\IManager;

/**
* Class MembershipNodeTest
Expand Down Expand Up @@ -90,7 +92,9 @@ public function setUp() {
$this->handler,
$this->userSession,
$this->userManager,
$this->groupManager
$this->groupManager,
$this->createMock(IManager::class),
$this->createMock(IURLGenerator::class)
);
$this->node = new MembershipNode(
['group_id' => 1, 'user_id' => self::NODE_USER, 'role' => CustomGroupsDatabaseHandler::ROLE_ADMIN],
Expand Down Expand Up @@ -178,7 +182,9 @@ private function makeSelfNode($role) {
$this->handler,
$userSession,
$this->userManager,
$this->groupManager
$this->groupManager,
$this->createMock(IManager::class),
$this->createMock(IURLGenerator::class)
);

$memberInfo = ['group_id' => 1, 'user_id' => self::NODE_USER, 'role' => $role];
Expand Down
6 changes: 5 additions & 1 deletion tests/unit/Dav/UsersCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
use OCA\CustomGroups\Service\MembershipHelper;
use OCP\IGroupManager;
use OCA\CustomGroups\Dav\GroupsCollection;
use OCP\IURLGenerator;
use OCP\Notification\IManager;

/**
* Class UsersCollectionTest
Expand Down Expand Up @@ -84,7 +86,9 @@ public function setUp() {
$this->handler,
$this->userSession,
$this->userManager,
$this->groupManager
$this->groupManager,
$this->createMock(IManager::class),
$this->createMock(IURLGenerator::class)
);

$this->collection = new UsersCollection($this->handler, $this->helper);
Expand Down
77 changes: 77 additions & 0 deletions tests/unit/NotifierTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php
/**
* @author Vincent Petry <pvince81@owncloud.com>
*
* @copyright Copyright (c) 2017, ownCloud GmbH
* @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/>
*
*/
namespace OCA\CustomGroups\Tests\unit;

use OCP\Notification\IManager;
use OCP\L10N\IFactory;
use OCA\CustomGroups\Notifier;
use OCA\CustomGroups\CustomGroupsDatabaseHandler;
use OCP\Notification\INotification;

/**
* Class NotifierTest
*
* @package OCA\CustomGroups\Tests\Unit
*/
class NotifierTest extends \Test\TestCase {

/**
* @var Notifier
*/
private $notifier;

/**
* @var CustomGroupsDatabaseHandler
*/
private $handler;

public function setUp() {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

parent:setup?

parent::setUp();

$this->handler = $this->createMock(CustomGroupsDatabaseHandler::class);

$this->notifier = new Notifier(
\OC::$server->getL10NFactory(),

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

mock?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I don't want to mock it... too many calls to it and in the end the default one works fine as it will return the same texts untranslated but formatted

$this->handler
);
}

public function testPrepare() {
$notification = $this->createMock(INotification::class);

$notification->method('getApp')->willReturn('customgroups');
$notification->method('getObjectType')->willReturn('customgroup');
$notification->method('getSubject')->willReturn('added_member');
$notification->method('getMessage')->willReturn('added_member');
$notification->method('getSubjectParameters')->willReturn(['group1', 'user1']);
$notification->method('getMessageParameters')->willReturn(['group1', 'user1']);

$notification->expects($this->once())
->method('setParsedSubject')
->with('Added to group "group1" by "user1".');
$notification->expects($this->once())
->method('setParsedMessage')
->with('You have been added to the group "group1" by "user1".');

$notification = $this->notifier->prepare($notification, 'en_US');
}

}
Loading