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