|
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
|
|
|
|
|
33
|
|
|
class Activity implements IProvider { |
|
34
|
|
|
|
|
35
|
|
|
/** @var IL10N */ |
|
36
|
|
|
protected $l; |
|
37
|
|
|
|
|
38
|
|
|
/** @var IURLGenerator */ |
|
39
|
|
|
protected $url; |
|
40
|
|
|
|
|
41
|
|
|
/** @var IManager */ |
|
42
|
|
|
protected $activityManager; |
|
43
|
|
|
|
|
44
|
|
|
/** @var IUserManager */ |
|
45
|
|
|
protected $userManager; |
|
46
|
|
|
/** @var IContactsManager */ |
|
47
|
|
|
protected $contactsManager; |
|
48
|
|
|
|
|
49
|
|
|
/** @var array */ |
|
50
|
|
|
protected $displayNames = []; |
|
51
|
|
|
|
|
52
|
|
|
/** @var array */ |
|
53
|
|
|
protected $contactNames = []; |
|
54
|
|
|
|
|
55
|
|
|
const SUBJECT_SHARED_EMAIL_SELF = 'shared_with_email_self'; |
|
56
|
|
|
const SUBJECT_SHARED_EMAIL_BY = 'shared_with_email_by'; |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @param IL10N $l |
|
60
|
|
|
* @param IURLGenerator $url |
|
61
|
|
|
* @param IManager $activityManager |
|
62
|
|
|
* @param IUserManager $userManager |
|
63
|
|
|
* @param IContactsManager $contactsManager |
|
64
|
|
|
*/ |
|
65
|
|
View Code Duplication |
public function __construct(IL10N $l, IURLGenerator $url, IManager $activityManager, IUserManager $userManager, IContactsManager $contactsManager) { |
|
|
|
|
|
|
66
|
|
|
$this->l = $l; |
|
67
|
|
|
$this->url = $url; |
|
68
|
|
|
$this->activityManager = $activityManager; |
|
69
|
|
|
$this->userManager = $userManager; |
|
70
|
|
|
$this->contactsManager = $contactsManager; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @param IEvent $event |
|
75
|
|
|
* @param IEvent|null $previousEvent |
|
76
|
|
|
* @return IEvent |
|
77
|
|
|
* @throws \InvalidArgumentException |
|
78
|
|
|
* @since 11.0.0 |
|
79
|
|
|
*/ |
|
80
|
|
View Code Duplication |
public function parse(IEvent $event, IEvent $previousEvent = null) { |
|
|
|
|
|
|
81
|
|
|
if ($event->getApp() !== 'sharebymail') { |
|
82
|
|
|
throw new \InvalidArgumentException(); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
if ($this->activityManager->isFormattingFilteredObject()) { |
|
86
|
|
|
try { |
|
87
|
|
|
return $this->parseShortVersion($event); |
|
88
|
|
|
} catch (\InvalidArgumentException $e) { |
|
89
|
|
|
// Ignore and simply use the long version... |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
return $this->parseLongVersion($event); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* @param IEvent $event |
|
98
|
|
|
* @return IEvent |
|
99
|
|
|
* @throws \InvalidArgumentException |
|
100
|
|
|
* @since 11.0.0 |
|
101
|
|
|
*/ |
|
102
|
|
|
public function parseShortVersion(IEvent $event) { |
|
103
|
|
|
$parsedParameters = $this->getParsedParameters($event); |
|
104
|
|
|
|
|
105
|
|
|
if ($event->getSubject() === self::SUBJECT_SHARED_EMAIL_SELF) { |
|
106
|
|
|
$event->setParsedSubject($this->l->t('Shared with %1$s', [ |
|
107
|
|
|
$parsedParameters['email']['name'], |
|
108
|
|
|
])) |
|
109
|
|
|
->setRichSubject($this->l->t('Shared with {email}'), [ |
|
110
|
|
|
'email' => $parsedParameters['email'], |
|
111
|
|
|
]) |
|
112
|
|
|
->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.svg'))); |
|
113
|
|
|
} else if ($event->getSubject() === self::SUBJECT_SHARED_EMAIL_BY) { |
|
114
|
|
|
$event->setParsedSubject($this->l->t('Shared with %1$s by %2$s', [ |
|
115
|
|
|
$parsedParameters['email']['name'], |
|
116
|
|
|
$parsedParameters['actor']['name'], |
|
117
|
|
|
])) |
|
118
|
|
|
->setRichSubject($this->l->t('Shared with {email} by {actor}'), [ |
|
119
|
|
|
'email' => $parsedParameters['email'], |
|
120
|
|
|
'actor' => $parsedParameters['actor'], |
|
121
|
|
|
]) |
|
122
|
|
|
->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.svg'))); |
|
123
|
|
|
|
|
124
|
|
|
} else { |
|
125
|
|
|
throw new \InvalidArgumentException(); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
return $event; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* @param IEvent $event |
|
133
|
|
|
* @return IEvent |
|
134
|
|
|
* @throws \InvalidArgumentException |
|
135
|
|
|
* @since 11.0.0 |
|
136
|
|
|
*/ |
|
137
|
|
|
public function parseLongVersion(IEvent $event) { |
|
138
|
|
|
$parsedParameters = $this->getParsedParameters($event); |
|
139
|
|
|
|
|
140
|
|
|
if ($event->getSubject() === self::SUBJECT_SHARED_EMAIL_SELF) { |
|
141
|
|
|
$event->setParsedSubject($this->l->t('You shared %1$s with %2$s by mail', [ |
|
142
|
|
|
$parsedParameters['file']['path'], |
|
143
|
|
|
$parsedParameters['email']['name'], |
|
144
|
|
|
])) |
|
145
|
|
|
->setRichSubject($this->l->t('You shared {file} with {email} by mail'), $parsedParameters) |
|
146
|
|
|
->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.svg'))); |
|
147
|
|
|
} else if ($event->getSubject() === self::SUBJECT_SHARED_EMAIL_BY) { |
|
148
|
|
|
$event->setParsedSubject($this->l->t('%3$s shared %1$s with %2$s by mail', [ |
|
149
|
|
|
$parsedParameters['file']['path'], |
|
150
|
|
|
$parsedParameters['email']['name'], |
|
151
|
|
|
$parsedParameters['actor']['name'], |
|
152
|
|
|
])) |
|
153
|
|
|
->setRichSubject($this->l->t('{actor} shared {file} with {email} by mail'), $parsedParameters) |
|
154
|
|
|
->setIcon($this->url->getAbsoluteURL($this->url->imagePath('core', 'actions/share.svg'))); |
|
155
|
|
|
} else { |
|
156
|
|
|
throw new \InvalidArgumentException(); |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
return $event; |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
protected function getParsedParameters(IEvent $event) { |
|
163
|
|
|
$subject = $event->getSubject(); |
|
164
|
|
|
$parameters = $event->getSubjectParameters(); |
|
165
|
|
|
|
|
166
|
|
|
switch ($subject) { |
|
167
|
|
View Code Duplication |
case self::SUBJECT_SHARED_EMAIL_SELF: |
|
|
|
|
|
|
168
|
|
|
return [ |
|
169
|
|
|
'file' => $this->generateFileParameter((int) $event->getObjectId(), $parameters[0]), |
|
170
|
|
|
'email' => $this->generateEmailParameter($parameters[1]), |
|
171
|
|
|
]; |
|
172
|
|
View Code Duplication |
case self::SUBJECT_SHARED_EMAIL_BY: |
|
|
|
|
|
|
173
|
|
|
return [ |
|
174
|
|
|
'file' => $this->generateFileParameter((int) $event->getObjectId(), $parameters[0]), |
|
175
|
|
|
'email' => $this->generateEmailParameter($parameters[1]), |
|
176
|
|
|
'actor' => $this->generateUserParameter($parameters[2]), |
|
177
|
|
|
]; |
|
178
|
|
|
} |
|
179
|
|
|
throw new \InvalidArgumentException(); |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
/** |
|
183
|
|
|
* @param int $id |
|
184
|
|
|
* @param string $path |
|
185
|
|
|
* @return array |
|
186
|
|
|
*/ |
|
187
|
|
View Code Duplication |
protected function generateFileParameter($id, $path) { |
|
|
|
|
|
|
188
|
|
|
return [ |
|
189
|
|
|
'type' => 'file', |
|
190
|
|
|
'id' => $id, |
|
191
|
|
|
'name' => basename($path), |
|
192
|
|
|
'path' => $path, |
|
193
|
|
|
'link' => $this->url->linkToRouteAbsolute('files.viewcontroller.showFile', ['fileid' => $id]), |
|
194
|
|
|
]; |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
/** |
|
198
|
|
|
* @param string $email |
|
199
|
|
|
* @return array |
|
200
|
|
|
*/ |
|
201
|
|
|
protected function generateEmailParameter($email) { |
|
202
|
|
|
if (!isset($this->contactNames[$email])) { |
|
203
|
|
|
$this->contactNames[$email] = $this->getContactName($email); |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
return [ |
|
207
|
|
|
'type' => 'email', |
|
208
|
|
|
'id' => $email, |
|
209
|
|
|
'name' => $this->contactNames[$email], |
|
210
|
|
|
]; |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
/** |
|
214
|
|
|
* @param string $uid |
|
215
|
|
|
* @return array |
|
216
|
|
|
*/ |
|
217
|
|
View Code Duplication |
protected function generateUserParameter($uid) { |
|
|
|
|
|
|
218
|
|
|
if (!isset($this->displayNames[$uid])) { |
|
219
|
|
|
$this->displayNames[$uid] = $this->getDisplayName($uid); |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
|
|
return [ |
|
223
|
|
|
'type' => 'user', |
|
224
|
|
|
'id' => $uid, |
|
225
|
|
|
'name' => $this->displayNames[$uid], |
|
226
|
|
|
]; |
|
227
|
|
|
} |
|
228
|
|
|
|
|
229
|
|
|
/** |
|
230
|
|
|
* @param string $email |
|
231
|
|
|
* @return string |
|
232
|
|
|
*/ |
|
233
|
|
|
protected function getContactName($email) { |
|
234
|
|
|
$addressBookContacts = $this->contactsManager->search($email, ['EMAIL']); |
|
235
|
|
|
|
|
236
|
|
|
foreach ($addressBookContacts as $contact) { |
|
237
|
|
|
if (isset($contact['isLocalSystemBook'])) { |
|
238
|
|
|
continue; |
|
239
|
|
|
} |
|
240
|
|
|
|
|
241
|
|
|
if (in_array($email, $contact['EMAIL'])) { |
|
242
|
|
|
return $contact['FN']; |
|
243
|
|
|
} |
|
244
|
|
|
} |
|
245
|
|
|
|
|
246
|
|
|
return $email; |
|
247
|
|
|
} |
|
248
|
|
|
|
|
249
|
|
|
/** |
|
250
|
|
|
* @param string $uid |
|
251
|
|
|
* @return string |
|
252
|
|
|
*/ |
|
253
|
|
|
protected function getDisplayName($uid) { |
|
254
|
|
|
$user = $this->userManager->get($uid); |
|
255
|
|
|
if ($user instanceof IUser) { |
|
256
|
|
|
return $user->getDisplayName(); |
|
257
|
|
|
} else { |
|
258
|
|
|
return $uid; |
|
259
|
|
|
} |
|
260
|
|
|
} |
|
261
|
|
|
} |
|
262
|
|
|
|
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.