Welcome to mirror list, hosted at ThFree Co, Russian Federation.

GroupsIntegration.php « Service « lib - github.com/nextcloud/mail.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: d440cd2e90057bc3994fa8ffb3b4117768121599 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<?php

declare(strict_types=1);

/**
 * @author Matthias Rella <mrella@pisys.eu>
 *
 * Mail
 *
 * 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\Db\Recipient;
use OCA\Mail\Service\Group\ContactsGroupService;
use OCA\Mail\Service\Group\IGroupService;
use OCA\Mail\Service\Group\NextcloudGroupService;
use OCA\Mail\Exception\ServiceException;
use function array_map;
use function mb_strlen;
use function mb_strpos;
use function mb_substr;
use function OCA\Mail\array_flat_map;

class GroupsIntegration {

	/**
	 * The services to get groups from
	 *
	 * @var IGroupService[]
	 */
	private $groupServices = [];

	public function __construct(ContactsGroupService $contactsGroupService, NextcloudGroupService $nextcloudGroupService) {
		$this->groupServices = [
			$contactsGroupService,
			$nextcloudGroupService,
		];
	}

	/**
	 * Extracts all matching contacts with email address and name
	 *
	 * @param string $term
	 * @return array
	 */
	public function getMatchingGroups(string $term): array {
		$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() . ")",
					'email' => $gid,
					'photo' => null,
				];
			}
		}

		return $receivers;
	}

	/**
	 * Returns the prefix for the group service.
	 *
	 * @param IGroupService $gs
	 * @return string
	 */
	public function servicePrefix(IGroupService $gs): string {
		if (empty($gs->getNamespace())) {
			throw new ServiceException('GroupService has no namespace');
		}
		return strtolower($gs->getNamespace()) . ":";
	}

	/**
	 * Expands group names to its members
	 *
	 * @param Recipient[] $recipients
	 *
	 * @return Recipient[]
	 */
	public function expand(array $recipients): array {
		return array_flat_map(function (Recipient $recipient) {
			foreach ($this->groupServices as $service) {
				if (mb_strpos($recipient->getEmail(), $this->servicePrefix($service)) !== false) {
					$groupId = mb_substr(
						$recipient->getEmail(),
						mb_strlen($this->servicePrefix($service))
					);
					$members = array_filter($service->getUsers($groupId), function (array $member) {
						return !empty($member['email']);
					});
					if (empty($members)) {
						throw new ServiceException($groupId . " ({$service->getNamespace()}) has no members with email addresses");
					}
					return array_map(function (array $member) use ($recipient) {
						return Recipient::fromParams([
							'messageId' => $recipient->getMessageId(),
							'type' => $recipient->getType(),
							'label' => $member['name'] ?? $member['email'],
							'email' => $member['email'],
						]);
					}, $members);
				}
			}

			return [$recipient];
		}, $recipients);
	}
}