Passed
Push — master ( ccc0a5...5195be )
by Roeland
10:43 queued 11s
created

Notifier   A

Complexity

Total Complexity 31

Size/Duplication

Total Lines 225
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 106
dl 0
loc 225
rs 9.92
c 0
b 0
f 0
wmc 31

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getID() 0 2 1
A getName() 0 2 1
A getDisplayName() 0 25 6
C prepare() 0 82 12
B getDisplayNameFromContact() 0 23 7
A createRemoteUser() 0 19 3
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016, ownCloud, Inc.
4
 *
5
 * @author Björn Schießle <[email protected]>
6
 * @author Joas Schilling <[email protected]>
7
 * @author Robin Appelman <[email protected]>
8
 *
9
 * @license AGPL-3.0
10
 *
11
 * This code is free software: you can redistribute it and/or modify
12
 * it under the terms of the GNU Affero General Public License, version 3,
13
 * as published by the Free Software Foundation.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
 * GNU Affero General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU Affero General Public License, version 3,
21
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
22
 *
23
 */
24
25
namespace OCA\FederatedFileSharing;
26
27
28
use OC\HintException;
29
use OCP\Contacts\IManager;
30
use OCP\Federation\ICloudId;
31
use OCP\Federation\ICloudIdManager;
32
use OCP\IURLGenerator;
33
use OCP\L10N\IFactory;
34
use OCP\Notification\INotification;
35
use OCP\Notification\INotifier;
36
37
class Notifier implements INotifier {
38
	/** @var IFactory */
39
	protected $factory;
40
	/** @var IManager */
41
	protected $contactsManager;
42
	/** @var IURLGenerator */
43
	protected $url;
44
	/** @var array */
45
	protected $federatedContacts;
46
	/** @var ICloudIdManager */
47
	protected $cloudIdManager;
48
49
	/**
50
	 * @param IFactory $factory
51
	 * @param IManager $contactsManager
52
	 * @param IURLGenerator $url
53
	 * @param ICloudIdManager $cloudIdManager
54
	 */
55
	public function __construct(IFactory $factory, IManager $contactsManager, IURLGenerator $url, ICloudIdManager $cloudIdManager) {
56
		$this->factory = $factory;
57
		$this->contactsManager = $contactsManager;
58
		$this->url = $url;
59
		$this->cloudIdManager = $cloudIdManager;
60
	}
61
62
	/**
63
	 * Identifier of the notifier, only use [a-z0-9_]
64
	 *
65
	 * @return string
66
	 * @since 17.0.0
67
	 */
68
	public function getID(): string {
69
		return 'federatedfilesharing';
70
	}
71
72
	/**
73
	 * Human readable name describing the notifier
74
	 *
75
	 * @return string
76
	 * @since 17.0.0
77
	 */
78
	public function getName(): string {
79
		return $this->factory->get('federatedfilesharing')->t('Federated sharing');
80
	}
81
82
	/**
83
	 * @param INotification $notification
84
	 * @param string $languageCode The code of the language that should be used to prepare the notification
85
	 * @return INotification
86
	 * @throws \InvalidArgumentException
87
	 */
88
	public function prepare(INotification $notification, string $languageCode): INotification {
89
		if ($notification->getApp() !== 'files_sharing' || $notification->getObjectType() !== 'remote_share') {
90
			// Not my app => throw
91
			throw new \InvalidArgumentException();
92
		}
93
94
		// Read the language from the notification
95
		$l = $this->factory->get('files_sharing', $languageCode);
96
97
		switch ($notification->getSubject()) {
98
			// Deal with known subjects
99
			case 'remote_share':
100
				$notification->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.svg')));
101
102
				$params = $notification->getSubjectParameters();
103
				if ($params[0] !== $params[1] && $params[1] !== null) {
104
					$notification->setParsedSubject(
105
						$l->t('You received "%3$s" as a remote share from %4$s (%1$s) (on behalf of %5$s (%2$s))', $params)
106
					);
107
108
					$initiator = $params[0];
109
					$initiatorDisplay = isset($params[3]) ? $params[3] : null;
110
					$owner = $params[1];
111
					$ownerDisplay = isset($params[4]) ? $params[4] : null;
112
113
					$notification->setRichSubject(
114
						$l->t('You received {share} as a remote share from {user} (on behalf of {behalf})'),
115
						[
116
							'share' => [
117
								'type' => 'pending-federated-share',
118
								'id' => $notification->getObjectId(),
119
								'name' => $params[2],
120
							],
121
							'user' => $this->createRemoteUser($initiator, $initiatorDisplay),
122
							'behalf' => $this->createRemoteUser($owner, $ownerDisplay),
123
						]
124
					);
125
				} else {
126
					$notification->setParsedSubject(
127
						$l->t('You received "%3$s" as a remote share from %4$s (%1$s)', $params)
128
					);
129
130
					$owner = $params[0];
131
					$ownerDisplay = isset($params[3]) ? $params[3] : null;
132
133
					$notification->setRichSubject(
134
						$l->t('You received {share} as a remote share from {user}'),
135
						[
136
							'share' => [
137
								'type' => 'pending-federated-share',
138
								'id' => $notification->getObjectId(),
139
								'name' => $params[2],
140
							],
141
							'user' => $this->createRemoteUser($owner, $ownerDisplay),
142
						]
143
					);
144
				}
145
146
				// Deal with the actions for a known subject
147
				foreach ($notification->getActions() as $action) {
148
					switch ($action->getLabel()) {
149
						case 'accept':
150
							$action->setParsedLabel(
151
								(string) $l->t('Accept')
152
							)
153
							->setPrimary(true);
154
							break;
155
156
						case 'decline':
157
							$action->setParsedLabel(
158
								(string) $l->t('Decline')
159
							);
160
							break;
161
					}
162
163
					$notification->addParsedAction($action);
164
				}
165
				return $notification;
166
167
			default:
168
				// Unknown subject => Unknown notification => throw
169
				throw new \InvalidArgumentException();
170
		}
171
	}
172
173
	/**
174
	 * @param string $cloudId
175
	 * @return array
176
	 */
177
	protected function createRemoteUser($cloudId, $displayName = null) {
178
		try {
179
			$resolvedId = $this->cloudIdManager->resolveCloudId($cloudId);
180
			if ($displayName === null) {
181
				$displayName = $this->getDisplayName($resolvedId);
182
			}
183
			$user = $resolvedId->getUser();
184
			$server = $resolvedId->getRemote();
185
		} catch (HintException $e) {
186
			$user = $cloudId;
187
			$displayName = $cloudId;
188
			$server = '';
189
		}
190
191
		return [
192
			'type' => 'user',
193
			'id' => $user,
194
			'name' => $displayName,
195
			'server' => $server,
196
		];
197
	}
198
199
	/**
200
	 * Try to find the user in the contacts
201
	 *
202
	 * @param ICloudId $cloudId
203
	 * @return string
204
	 */
205
	protected function getDisplayName(ICloudId $cloudId) {
206
		$server = $cloudId->getRemote();
207
		$user = $cloudId->getUser();
208
		if (strpos($server, 'http://') === 0) {
209
			$server = substr($server, strlen('http://'));
210
		} else if (strpos($server, 'https://') === 0) {
211
			$server = substr($server, strlen('https://'));
212
		}
213
214
		try {
215
			return $this->getDisplayNameFromContact($cloudId->getId());
216
		} catch (\OutOfBoundsException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
217
		}
218
219
		try {
220
			$this->getDisplayNameFromContact($user . '@http://' . $server);
221
		} catch (\OutOfBoundsException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
222
		}
223
224
		try {
225
			$this->getDisplayNameFromContact($user . '@https://' . $server);
226
		} catch (\OutOfBoundsException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
227
		}
228
229
		return $cloudId->getId();
230
	}
231
232
	/**
233
	 * Try to find the user in the contacts
234
	 *
235
	 * @param string $federatedCloudId
236
	 * @return string
237
	 * @throws \OutOfBoundsException when there is no contact for the id
238
	 */
239
	protected function getDisplayNameFromContact($federatedCloudId) {
240
		if (isset($this->federatedContacts[$federatedCloudId])) {
241
			if ($this->federatedContacts[$federatedCloudId] !== '') {
242
				return $this->federatedContacts[$federatedCloudId];
243
			} else {
244
				throw new \OutOfBoundsException('No contact found for federated cloud id');
245
			}
246
		}
247
248
		$addressBookEntries = $this->contactsManager->search($federatedCloudId, ['CLOUD']);
249
		foreach ($addressBookEntries as $entry) {
250
			if (isset($entry['CLOUD'])) {
251
				foreach ($entry['CLOUD'] as $cloudID) {
252
					if ($cloudID === $federatedCloudId) {
253
						$this->federatedContacts[$federatedCloudId] = $entry['FN'];
254
						return $entry['FN'];
255
					}
256
				}
257
			}
258
		}
259
260
		$this->federatedContacts[$federatedCloudId] = '';
261
		throw new \OutOfBoundsException('No contact found for federated cloud id');
262
	}
263
}
264