-
Notifications
You must be signed in to change notification settings - Fork 329
Sending to nextcloud groups #810
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
1aa8a4d
a79dc2f
123350d
b9756bb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -31,3 +31,6 @@ translations.js | |
|
|
||
| # JS coverage | ||
| coverage/ | ||
|
|
||
| # vim | ||
| .*.sw? | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * @author Matthias Rella <mrella@pisys.eu> | ||
| * | ||
| * | ||
| * 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\Mail\Service\Group; | ||
|
|
||
| interface IGroupService { | ||
|
|
||
| /** | ||
| * Search the service's groups. | ||
| * | ||
| * @param string $term | ||
| * @return array of matched groups as associative arrays | ||
| */ | ||
| public function search($term); | ||
|
|
||
| /** | ||
| * Get the group's namespace. | ||
| * | ||
| * @return string | ||
| */ | ||
| public function getNamespace(); | ||
|
|
||
| /** | ||
| * Get the group's users. | ||
| * | ||
| * @param string $groupId | ||
| * @return array with group's users as associative arrays | ||
| */ | ||
| public function getUsers($groupId); | ||
|
|
||
| } | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * @author Matthias Rella <mrella@pisys.eu> | ||
| * | ||
| * | ||
| * 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\Mail\Service\Group; | ||
|
|
||
| use OCP\IGroupManager; | ||
| use OCA\Mail\Exception\ServiceException; | ||
|
|
||
| class NextcloudGroupService implements IGroupService { | ||
|
|
||
| /** | ||
| * Nextcloud's group manager | ||
| * | ||
| * @var IGroupManager | ||
| */ | ||
| private $groupManager; | ||
|
|
||
| /** | ||
| * Group's namespace | ||
| * | ||
| * @var string | ||
| */ | ||
| private $namespace = "Nextcloud"; | ||
|
|
||
| public function __construct(IGroupManager $groupManager) { | ||
| $this->groupManager = $groupManager; | ||
| } | ||
|
|
||
| public function getNamespace() { | ||
| return $this->namespace; | ||
| } | ||
|
|
||
| public function search($term) { | ||
| $groups = $this->groupManager->search($term); | ||
|
|
||
| return array_map( | ||
| function($g) { | ||
| return [ | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks like a good candidate to implement PHP's
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I ported this pattern from
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nvm, just realized that |
||
| 'id' => $g->getGID(), | ||
| 'name' => $g->getDisplayName() | ||
| ]; | ||
| }, | ||
| $groups | ||
| ); | ||
| } | ||
|
|
||
| public function getUsers($groupId) { | ||
| if(!$this->groupManager->groupExists($groupId)) { | ||
| throw new ServiceException("$groupId ({$this->getNamespace()}) does not exist"); | ||
| } | ||
| $users = $this->groupManager->get($groupId)->getUsers(); | ||
| return array_map( | ||
| function($user) { | ||
| return [ | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same here |
||
| 'id' => $user->getUID(), | ||
| 'name' => $user->getDisplayName(), | ||
| 'email' => $user->getEMailAddress() | ||
| ]; | ||
| }, | ||
| $users | ||
| ); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * @author Matthias Rella <mrella@pisys.eu> | ||
| * | ||
| * | ||
| * 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\Mail\Service; | ||
|
|
||
| use OCA\Mail\Service\Group\IGroupService; | ||
| use OCA\Mail\Exception\ServiceException; | ||
|
|
||
| class GroupsIntegration { | ||
|
|
||
| /** | ||
| * The services to get groups from | ||
| * | ||
| * @var IGroupService[] | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does this interface really exist?
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It did :-) But |
||
| */ | ||
| private $groupServices = []; | ||
|
|
||
| public function __construct(IGroupService $groupService) { | ||
| $this->groupServices[] = $groupService; | ||
| } | ||
|
|
||
| /** | ||
| * Extracts all matching contacts with email address and name | ||
| * | ||
| * @param string $term | ||
| * @return array | ||
| */ | ||
| public function getMatchingGroups($term) { | ||
| $receivers = []; | ||
| foreach ($this->groupServices as $gs) { | ||
| $result = $gs->search($term); | ||
| foreach ($result as $g) { | ||
| $gid = $this->servicePrefix($gs) . $g['id']; | ||
| $receivers[] = [ | ||
| 'id' => $gid, | ||
| 'label' => $g['name'] . " (" . $gs->getNamespace() . ")", | ||
| 'value' => $gid, | ||
| 'photo' => null, | ||
| ]; | ||
| } | ||
| } | ||
|
|
||
| return $receivers; | ||
| } | ||
|
|
||
| /** | ||
| * Returns the prefix for the group service. | ||
| * | ||
| * @param IGroupService $gs | ||
| * @return string | ||
| */ | ||
| public function servicePrefix(IGroupService $gs) { | ||
| if (empty($gs->getNamespace())) { | ||
| throw new ServiceException('GroupService has no namespace'); | ||
| } | ||
| return strtolower($gs->getNamespace()) . ":"; | ||
| } | ||
|
|
||
| /** | ||
| * Expands a string of group names to its members email addresses. | ||
| * | ||
| * @param string $recipients | ||
| * @return string | ||
| */ | ||
| public function expand($recipients) { | ||
| return array_reduce($this->groupServices, | ||
| function($carry, $service) { | ||
| return preg_replace_callback( | ||
| '/' . preg_quote($this->servicePrefix($service)) . '([\w\d ]+)(,?)/', | ||
| function($matches) use ($service) { | ||
| if (empty($matches[1])) { | ||
| return ''; | ||
| } | ||
| $members = $service->getUsers($matches[1]); | ||
| if (empty($members)) { | ||
| throw new ServiceException($matches[1] . " ({$service->getNamespace()}) has no members"); | ||
| } | ||
| $addresses = []; | ||
| foreach ($members as $m) { | ||
| if (!empty($m['email'])) { | ||
| $addresses[] = $m['email']; | ||
| } | ||
| } | ||
| return implode(',', $addresses) | ||
| . (!empty($matches[2]) && !empty($addresses) ? ',' : ''); | ||
| }, $carry); | ||
| }, $recipients); | ||
| } | ||
|
|
||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd prefer to have this logic moved to a service class, maybe even introduce a facade for that. But that's a minor enhancement - this can be done later on 😉