Passed
Push — master ( 782554...5b604e )
by Morris
10:22
created

Notifier::getName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 2
rs 10
c 1
b 0
f 0
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') {
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 %1$s (on behalf of %2$s)', $params)
106
					);
107
					$notification->setRichSubject(
108
						$l->t('You received {share} as a remote share from {user} (on behalf of {behalf})'),
109
						[
110
							'share' => [
111
								'type' => 'pending-federated-share',
112
								'id' => $notification->getObjectId(),
113
								'name' => $params[2],
114
							],
115
							'user' => $this->createRemoteUser($params[0]),
116
							'behalf' => $this->createRemoteUser($params[1]),
117
						]
118
					);
119
				} else {
120
					$notification->setParsedSubject(
121
						$l->t('You received "%3$s" as a remote share from %1$s', $params)
122
					);
123
					$notification->setRichSubject(
124
						$l->t('You received {share} as a remote share from {user}'),
125
						[
126
							'share' => [
127
								'type' => 'pending-federated-share',
128
								'id' => $notification->getObjectId(),
129
								'name' => $params[2],
130
							],
131
							'user' => $this->createRemoteUser($params[0]),
132
						]
133
					);
134
				}
135
136
				// Deal with the actions for a known subject
137
				foreach ($notification->getActions() as $action) {
138
					switch ($action->getLabel()) {
139
						case 'accept':
140
							$action->setParsedLabel(
141
								(string) $l->t('Accept')
142
							)
143
							->setPrimary(true);
144
							break;
145
146
						case 'decline':
147
							$action->setParsedLabel(
148
								(string) $l->t('Decline')
149
							);
150
							break;
151
					}
152
153
					$notification->addParsedAction($action);
154
				}
155
				return $notification;
156
157
			default:
158
				// Unknown subject => Unknown notification => throw
159
				throw new \InvalidArgumentException();
160
		}
161
	}
162
163
	/**
164
	 * @param string $cloudId
165
	 * @return array
166
	 */
167
	protected function createRemoteUser($cloudId) {
168
		$displayName = $cloudId;
169
		try {
170
			$resolvedId = $this->cloudIdManager->resolveCloudId($cloudId);
171
			$displayName = $this->getDisplayName($resolvedId);
172
			$user = $resolvedId->getUser();
173
			$server = $resolvedId->getRemote();
174
		} catch (HintException $e) {
175
			$user = $cloudId;
176
			$server = '';
177
		}
178
179
		return [
180
			'type' => 'user',
181
			'id' => $user,
182
			'name' => $displayName,
183
			'server' => $server,
184
		];
185
	}
186
187
	/**
188
	 * Try to find the user in the contacts
189
	 *
190
	 * @param ICloudId $cloudId
191
	 * @return string
192
	 */
193
	protected function getDisplayName(ICloudId $cloudId) {
194
		$server = $cloudId->getRemote();
195
		$user = $cloudId->getUser();
196
		if (strpos($server, 'http://') === 0) {
197
			$server = substr($server, strlen('http://'));
198
		} else if (strpos($server, 'https://') === 0) {
199
			$server = substr($server, strlen('https://'));
200
		}
201
202
		try {
203
			return $this->getDisplayNameFromContact($cloudId->getId());
204
		} catch (\OutOfBoundsException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
205
		}
206
207
		try {
208
			$this->getDisplayNameFromContact($user . '@http://' . $server);
209
		} catch (\OutOfBoundsException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
210
		}
211
212
		try {
213
			$this->getDisplayNameFromContact($user . '@https://' . $server);
214
		} catch (\OutOfBoundsException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
215
		}
216
217
		return $cloudId->getId();
218
	}
219
220
	/**
221
	 * Try to find the user in the contacts
222
	 *
223
	 * @param string $federatedCloudId
224
	 * @return string
225
	 * @throws \OutOfBoundsException when there is no contact for the id
226
	 */
227
	protected function getDisplayNameFromContact($federatedCloudId) {
228
		if (isset($this->federatedContacts[$federatedCloudId])) {
229
			if ($this->federatedContacts[$federatedCloudId] !== '') {
230
				return $this->federatedContacts[$federatedCloudId];
231
			} else {
232
				throw new \OutOfBoundsException('No contact found for federated cloud id');
233
			}
234
		}
235
236
		$addressBookEntries = $this->contactsManager->search($federatedCloudId, ['CLOUD']);
237
		foreach ($addressBookEntries as $entry) {
238
			if (isset($entry['CLOUD'])) {
239
				foreach ($entry['CLOUD'] as $cloudID) {
240
					if ($cloudID === $federatedCloudId) {
241
						$this->federatedContacts[$federatedCloudId] = $entry['FN'];
242
						return $entry['FN'];
243
					}
244
				}
245
			}
246
		}
247
248
		$this->federatedContacts[$federatedCloudId] = '';
249
		throw new \OutOfBoundsException('No contact found for federated cloud id');
250
	}
251
}
252