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