|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @copyright Copyright (c) 2016 Joas Schilling <[email protected]> |
|
4
|
|
|
* |
|
5
|
|
|
* @license GNU AGPL version 3 or any later version |
|
6
|
|
|
* |
|
7
|
|
|
* This program is free software: you can redistribute it and/or modify |
|
8
|
|
|
* it under the terms of the GNU Affero General Public License as |
|
9
|
|
|
* published by the Free Software Foundation, either version 3 of the |
|
10
|
|
|
* License, or (at your option) any later version. |
|
11
|
|
|
* |
|
12
|
|
|
* This program is distributed in the hope that it will be useful, |
|
13
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
14
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
15
|
|
|
* GNU Affero General Public License for more details. |
|
16
|
|
|
* |
|
17
|
|
|
* You should have received a copy of the GNU Affero General Public License |
|
18
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
19
|
|
|
* |
|
20
|
|
|
*/ |
|
21
|
|
|
|
|
22
|
|
|
namespace OCA\ShareByMail; |
|
23
|
|
|
|
|
24
|
|
|
use OCP\Activity\IEvent; |
|
25
|
|
|
use OCP\Activity\IManager; |
|
26
|
|
|
use OCP\Activity\IProvider; |
|
27
|
|
|
use OCP\Contacts\IManager as IContactsManager; |
|
28
|
|
|
use OCP\IL10N; |
|
29
|
|
|
use OCP\IURLGenerator; |
|
30
|
|
|
use OCP\IUser; |
|
31
|
|
|
use OCP\IUserManager; |
|
32
|
|
|
use OCP\L10N\IFactory; |
|
33
|
|
|
|
|
34
|
|
|
class Activity implements IProvider { |
|
35
|
|
|
|
|
36
|
|
|
/** @var IFactory */ |
|
37
|
|
|
protected $languageFactory; |
|
38
|
|
|
|
|
39
|
|
|
/** @var IL10N */ |
|
40
|
|
|
protected $l; |
|
41
|
|
|
|
|
42
|
|
|
/** @var IURLGenerator */ |
|
43
|
|
|
protected $url; |
|
44
|
|
|
|
|
45
|
|
|
/** @var IManager */ |
|
46
|
|
|
protected $activityManager; |
|
47
|
|
|
|
|
48
|
|
|
/** @var IUserManager */ |
|
49
|
|
|
protected $userManager; |
|
50
|
|
|
/** @var IContactsManager */ |
|
51
|
|
|
protected $contactsManager; |
|
52
|
|
|
|
|
53
|
|
|
/** @var array */ |
|
54
|
|
|
protected $displayNames = []; |
|
55
|
|
|
|
|
56
|
|
|
/** @var array */ |
|
57
|
|
|
protected $contactNames = []; |
|
58
|
|
|
|
|
59
|
|
|
const SUBJECT_SHARED_EMAIL_SELF = 'shared_with_email_self'; |
|
60
|
|
|
const SUBJECT_SHARED_EMAIL_BY = 'shared_with_email_by'; |
|
61
|
|
|
const SUBJECT_SHARED_EMAIL_PASSWORD_SEND = 'shared_with_email_password_send'; |
|
62
|
|
|
const SUBJECT_SHARED_EMAIL_PASSWORD_SEND_SELF = 'shared_with_email_password_send_self'; |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @param IFactory $languageFactory |
|
66
|
|
|
* @param IURLGenerator $url |
|
67
|
|
|
* @param IManager $activityManager |
|
68
|
|
|
* @param IUserManager $userManager |
|
69
|
|
|
* @param IContactsManager $contactsManager |
|
70
|
|
|
*/ |
|
71
|
|
|
public function __construct(IFactory $languageFactory, IURLGenerator $url, IManager $activityManager, IUserManager $userManager, IContactsManager $contactsManager) { |
|
72
|
|
|
$this->languageFactory = $languageFactory; |
|
73
|
|
|
$this->url = $url; |
|
74
|
|
|
$this->activityManager = $activityManager; |
|
75
|
|
|
$this->userManager = $userManager; |
|
76
|
|
|
$this->contactsManager = $contactsManager; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @param string $language |
|
81
|
|
|
* @param IEvent $event |
|
82
|
|
|
* @param IEvent|null $previousEvent |
|
83
|
|
|
* @return IEvent |
|
84
|
|
|
* @throws \InvalidArgumentException |
|
85
|
|
|
* @since 11.0.0 |
|
86
|
|
|
*/ |
|
87
|
|
View Code Duplication |
public function parse($language, IEvent $event, IEvent $previousEvent = null) { |
|
|
|
|
|
|
88
|
|
|
if ($event->getApp() !== 'sharebymail') { |
|
89
|
|
|
throw new \InvalidArgumentException(); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
$this->l = $this->languageFactory->get('sharebymail', $language); |
|
93
|
|
|
|
|
94
|
|
|
if ($this->activityManager->isFormattingFilteredObject()) { |
|
95
|
|
|
try { |
|
96
|
|
|
return $this->parseShortVersion($event); |
|
97
|
|
|
} catch (\InvalidArgumentException $e) { |
|
98
|
|
|
// Ignore and simply use the long version... |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
return $this->parseLongVersion($event); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* @param IEvent $event |
|
107
|
|
|
* @return IEvent |
|
108
|
|
|
* @throws \InvalidArgumentException |
|
109
|
|
|
* @since 11.0.0 |
|
110
|
|
|
*/ |
|
111
|
|
|
public function parseShortVersion(IEvent $event) { |
|
112
|
|
|
$parsedParameters = $this->getParsedParameters($event); |
|
113
|
|
|
|
|
114
|
|
|
if ($event->getSubject() === self::SUBJECT_SHARED_EMAIL_SELF) { |
|
115
|
|
|
$event->setParsedSubject($this->l->t('Shared with %1$s', [ |
|
116
|
|
|
$parsedParameters['email']['name'], |
|
117
|
|
|
])) |
|
118
|
|
|
->setRichSubject($this->l->t('Shared with {email}'), [ |
|
119
|
|
|
'email' => $parsedParameters['email'], |
|
120
|
|
|
]); |
|
121
|
|
|
if ($this->activityManager->getRequirePNG()) { |
|
122
|
|
|
$event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.png'))); |
|
123
|
|
|
} else { |
|
124
|
|
|
$event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.svg'))); |
|
125
|
|
|
} |
|
126
|
|
|
} else if ($event->getSubject() === self::SUBJECT_SHARED_EMAIL_BY) { |
|
127
|
|
|
$event->setParsedSubject($this->l->t('Shared with %1$s by %2$s', [ |
|
128
|
|
|
$parsedParameters['email']['name'], |
|
129
|
|
|
$parsedParameters['actor']['name'], |
|
130
|
|
|
])) |
|
131
|
|
|
->setRichSubject($this->l->t('Shared with {email} by {actor}'), [ |
|
132
|
|
|
'email' => $parsedParameters['email'], |
|
133
|
|
|
'actor' => $parsedParameters['actor'], |
|
134
|
|
|
]); |
|
135
|
|
|
if ($this->activityManager->getRequirePNG()) { |
|
136
|
|
|
$event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.png'))); |
|
137
|
|
|
} else { |
|
138
|
|
|
$event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.svg'))); |
|
139
|
|
|
} |
|
140
|
|
|
} else if ($event->getSubject() === self::SUBJECT_SHARED_EMAIL_PASSWORD_SEND) { |
|
141
|
|
|
$event->setParsedSubject($this->l->t('Password for mail share sent to %1$s', [ |
|
142
|
|
|
$parsedParameters['email']['name'] |
|
143
|
|
|
])) |
|
144
|
|
|
->setRichSubject($this->l->t('Password for mail share sent to {email}'), [ |
|
145
|
|
|
'email' => $parsedParameters['email'] |
|
146
|
|
|
]); |
|
147
|
|
|
if ($this->activityManager->getRequirePNG()) { |
|
148
|
|
|
$event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.png'))); |
|
149
|
|
|
} else { |
|
150
|
|
|
$event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.svg'))); |
|
151
|
|
|
} |
|
152
|
|
|
} else if ($event->getSubject() === self::SUBJECT_SHARED_EMAIL_PASSWORD_SEND_SELF) { |
|
153
|
|
|
$event->setParsedSubject($this->l->t('Password for mail share sent to you')) |
|
154
|
|
|
->setRichSubject($this->l->t('Password for mail share sent to you')); |
|
155
|
|
|
if ($this->activityManager->getRequirePNG()) { |
|
156
|
|
|
$event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.png'))); |
|
157
|
|
|
} else { |
|
158
|
|
|
$event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.svg'))); |
|
159
|
|
|
} |
|
160
|
|
|
} else { |
|
161
|
|
|
throw new \InvalidArgumentException(); |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
return $event; |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
/** |
|
168
|
|
|
* @param IEvent $event |
|
169
|
|
|
* @return IEvent |
|
170
|
|
|
* @throws \InvalidArgumentException |
|
171
|
|
|
* @since 11.0.0 |
|
172
|
|
|
*/ |
|
173
|
|
|
public function parseLongVersion(IEvent $event) { |
|
174
|
|
|
$parsedParameters = $this->getParsedParameters($event); |
|
175
|
|
|
|
|
176
|
|
|
if ($event->getSubject() === self::SUBJECT_SHARED_EMAIL_SELF) { |
|
177
|
|
|
$event->setParsedSubject($this->l->t('You shared %1$s with %2$s by mail', [ |
|
178
|
|
|
$parsedParameters['file']['path'], |
|
179
|
|
|
$parsedParameters['email']['name'], |
|
180
|
|
|
])) |
|
181
|
|
|
->setRichSubject($this->l->t('You shared {file} with {email} by mail'), $parsedParameters); |
|
182
|
|
|
if ($this->activityManager->getRequirePNG()) { |
|
183
|
|
|
$event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.png'))); |
|
184
|
|
|
} else { |
|
185
|
|
|
$event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.svg'))); |
|
186
|
|
|
} |
|
187
|
|
|
} else if ($event->getSubject() === self::SUBJECT_SHARED_EMAIL_BY) { |
|
188
|
|
|
$event->setParsedSubject($this->l->t('%3$s shared %1$s with %2$s by mail', [ |
|
189
|
|
|
$parsedParameters['file']['path'], |
|
190
|
|
|
$parsedParameters['email']['name'], |
|
191
|
|
|
$parsedParameters['actor']['name'], |
|
192
|
|
|
])) |
|
193
|
|
|
->setRichSubject($this->l->t('{actor} shared {file} with {email} by mail'), $parsedParameters); |
|
194
|
|
|
if ($this->activityManager->getRequirePNG()) { |
|
195
|
|
|
$event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.png'))); |
|
196
|
|
|
} else { |
|
197
|
|
|
$event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.svg'))); |
|
198
|
|
|
} |
|
199
|
|
|
} else if ($event->getSubject() === self::SUBJECT_SHARED_EMAIL_PASSWORD_SEND) { |
|
200
|
|
|
$event->setParsedSubject($this->l->t('Password to access %1$s was sent to %2s', [ |
|
201
|
|
|
$parsedParameters['file']['path'], |
|
202
|
|
|
$parsedParameters['email']['name'] |
|
203
|
|
|
])) |
|
204
|
|
|
->setRichSubject($this->l->t('Password to access {file} was sent to {email}'), $parsedParameters); |
|
205
|
|
|
if ($this->activityManager->getRequirePNG()) { |
|
206
|
|
|
$event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.png'))); |
|
207
|
|
|
} else { |
|
208
|
|
|
$event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.svg'))); |
|
209
|
|
|
} |
|
210
|
|
|
} else if ($event->getSubject() === self::SUBJECT_SHARED_EMAIL_PASSWORD_SEND_SELF) { |
|
211
|
|
|
$event->setParsedSubject( |
|
212
|
|
|
$this->l->t('Password to access %1$s was sent to you', |
|
213
|
|
|
[$parsedParameters['file']['path']])) |
|
214
|
|
|
->setRichSubject($this->l->t('Password to access {file} was sent to you'), $parsedParameters); |
|
215
|
|
|
if ($this->activityManager->getRequirePNG()) { |
|
216
|
|
|
$event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.png'))); |
|
217
|
|
|
} else { |
|
218
|
|
|
$event->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.svg'))); |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
} else { |
|
222
|
|
|
throw new \InvalidArgumentException(); |
|
223
|
|
|
} |
|
224
|
|
|
|
|
225
|
|
|
return $event; |
|
226
|
|
|
} |
|
227
|
|
|
|
|
228
|
|
|
protected function getParsedParameters(IEvent $event) { |
|
229
|
|
|
$subject = $event->getSubject(); |
|
230
|
|
|
$parameters = $event->getSubjectParameters(); |
|
231
|
|
|
|
|
232
|
|
|
switch ($subject) { |
|
233
|
|
View Code Duplication |
case self::SUBJECT_SHARED_EMAIL_SELF: |
|
234
|
|
|
return [ |
|
235
|
|
|
'file' => $this->generateFileParameter((int) $event->getObjectId(), $parameters[0]), |
|
236
|
|
|
'email' => $this->generateEmailParameter($parameters[1]), |
|
237
|
|
|
]; |
|
238
|
|
View Code Duplication |
case self::SUBJECT_SHARED_EMAIL_BY: |
|
239
|
|
|
return [ |
|
240
|
|
|
'file' => $this->generateFileParameter((int) $event->getObjectId(), $parameters[0]), |
|
241
|
|
|
'email' => $this->generateEmailParameter($parameters[1]), |
|
242
|
|
|
'actor' => $this->generateUserParameter($parameters[2]), |
|
243
|
|
|
]; |
|
244
|
|
View Code Duplication |
case self::SUBJECT_SHARED_EMAIL_PASSWORD_SEND: |
|
245
|
|
|
return [ |
|
246
|
|
|
'file' => $this->generateFileParameter((int) $event->getObjectId(), $parameters[0]), |
|
247
|
|
|
'email' => $this->generateEmailParameter($parameters[1]), |
|
248
|
|
|
]; |
|
249
|
|
|
case self::SUBJECT_SHARED_EMAIL_PASSWORD_SEND_SELF: |
|
250
|
|
|
return [ |
|
251
|
|
|
'file' => $this->generateFileParameter((int) $event->getObjectId(), $parameters[0]), |
|
252
|
|
|
]; |
|
253
|
|
|
} |
|
254
|
|
|
throw new \InvalidArgumentException(); |
|
255
|
|
|
} |
|
256
|
|
|
|
|
257
|
|
|
/** |
|
258
|
|
|
* @param int $id |
|
259
|
|
|
* @param string $path |
|
260
|
|
|
* @return array |
|
261
|
|
|
*/ |
|
262
|
|
View Code Duplication |
protected function generateFileParameter($id, $path) { |
|
|
|
|
|
|
263
|
|
|
return [ |
|
264
|
|
|
'type' => 'file', |
|
265
|
|
|
'id' => $id, |
|
266
|
|
|
'name' => basename($path), |
|
267
|
|
|
'path' => trim($path, '/'), |
|
268
|
|
|
'link' => $this->url->linkToRouteAbsolute('files.viewcontroller.showFile', ['fileid' => $id]), |
|
269
|
|
|
]; |
|
270
|
|
|
} |
|
271
|
|
|
|
|
272
|
|
|
/** |
|
273
|
|
|
* @param string $email |
|
274
|
|
|
* @return array |
|
275
|
|
|
*/ |
|
276
|
|
|
protected function generateEmailParameter($email) { |
|
277
|
|
|
if (!isset($this->contactNames[$email])) { |
|
278
|
|
|
$this->contactNames[$email] = $this->getContactName($email); |
|
279
|
|
|
} |
|
280
|
|
|
|
|
281
|
|
|
return [ |
|
282
|
|
|
'type' => 'email', |
|
283
|
|
|
'id' => $email, |
|
284
|
|
|
'name' => $this->contactNames[$email], |
|
285
|
|
|
]; |
|
286
|
|
|
} |
|
287
|
|
|
|
|
288
|
|
|
/** |
|
289
|
|
|
* @param string $uid |
|
290
|
|
|
* @return array |
|
291
|
|
|
*/ |
|
292
|
|
View Code Duplication |
protected function generateUserParameter($uid) { |
|
|
|
|
|
|
293
|
|
|
if (!isset($this->displayNames[$uid])) { |
|
294
|
|
|
$this->displayNames[$uid] = $this->getDisplayName($uid); |
|
295
|
|
|
} |
|
296
|
|
|
|
|
297
|
|
|
return [ |
|
298
|
|
|
'type' => 'user', |
|
299
|
|
|
'id' => $uid, |
|
300
|
|
|
'name' => $this->displayNames[$uid], |
|
301
|
|
|
]; |
|
302
|
|
|
} |
|
303
|
|
|
|
|
304
|
|
|
/** |
|
305
|
|
|
* @param string $email |
|
306
|
|
|
* @return string |
|
307
|
|
|
*/ |
|
308
|
|
|
protected function getContactName($email) { |
|
309
|
|
|
$addressBookContacts = $this->contactsManager->search($email, ['EMAIL']); |
|
310
|
|
|
|
|
311
|
|
|
foreach ($addressBookContacts as $contact) { |
|
312
|
|
|
if (isset($contact['isLocalSystemBook'])) { |
|
313
|
|
|
continue; |
|
314
|
|
|
} |
|
315
|
|
|
|
|
316
|
|
|
if (in_array($email, $contact['EMAIL'])) { |
|
317
|
|
|
return $contact['FN']; |
|
318
|
|
|
} |
|
319
|
|
|
} |
|
320
|
|
|
|
|
321
|
|
|
return $email; |
|
322
|
|
|
} |
|
323
|
|
|
|
|
324
|
|
|
/** |
|
325
|
|
|
* @param string $uid |
|
326
|
|
|
* @return string |
|
327
|
|
|
*/ |
|
328
|
|
|
protected function getDisplayName($uid) { |
|
329
|
|
|
$user = $this->userManager->get($uid); |
|
330
|
|
|
if ($user instanceof IUser) { |
|
331
|
|
|
return $user->getDisplayName(); |
|
332
|
|
|
} else { |
|
333
|
|
|
return $uid; |
|
334
|
|
|
} |
|
335
|
|
|
} |
|
336
|
|
|
} |
|
337
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.