|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @copyright Copyright (c) 2016 Joas Schilling <[email protected]> |
|
4
|
|
|
* |
|
5
|
|
|
* @author Joas Schilling <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* @license GNU AGPL version 3 or any later version |
|
8
|
|
|
* |
|
9
|
|
|
* This program is free software: you can redistribute it and/or modify |
|
10
|
|
|
* it under the terms of the GNU Affero General Public License as |
|
11
|
|
|
* published by the Free Software Foundation, either version 3 of the |
|
12
|
|
|
* License, or (at your option) any later version. |
|
13
|
|
|
* |
|
14
|
|
|
* This program is distributed in the hope that it will be useful, |
|
15
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
16
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
17
|
|
|
* GNU Affero General Public License for more details. |
|
18
|
|
|
* |
|
19
|
|
|
* You should have received a copy of the GNU Affero General Public License |
|
20
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
21
|
|
|
* |
|
22
|
|
|
*/ |
|
23
|
|
|
|
|
24
|
|
|
namespace OCA\Files_Sharing\Activity\Providers; |
|
25
|
|
|
|
|
26
|
|
|
use OCP\Activity\IEvent; |
|
27
|
|
|
use OCP\Activity\IManager; |
|
28
|
|
|
use OCP\Activity\IProvider; |
|
29
|
|
|
use OCP\Contacts\IManager as IContactsManager; |
|
30
|
|
|
use OCP\Federation\ICloudIdManager; |
|
31
|
|
|
use OCP\IL10N; |
|
32
|
|
|
use OCP\IURLGenerator; |
|
33
|
|
|
use OCP\IUser; |
|
34
|
|
|
use OCP\IUserManager; |
|
35
|
|
|
use OCP\L10N\IFactory; |
|
36
|
|
|
|
|
37
|
|
|
abstract class Base implements IProvider { |
|
38
|
|
|
|
|
39
|
|
|
/** @var IFactory */ |
|
40
|
|
|
protected $languageFactory; |
|
41
|
|
|
|
|
42
|
|
|
/** @var IL10N */ |
|
43
|
|
|
protected $l; |
|
44
|
|
|
|
|
45
|
|
|
/** @var IURLGenerator */ |
|
46
|
|
|
protected $url; |
|
47
|
|
|
|
|
48
|
|
|
/** @var IManager */ |
|
49
|
|
|
protected $activityManager; |
|
50
|
|
|
|
|
51
|
|
|
/** @var IUserManager */ |
|
52
|
|
|
protected $userManager; |
|
53
|
|
|
|
|
54
|
|
|
/** @var IContactsManager */ |
|
55
|
|
|
protected $contactsManager; |
|
56
|
|
|
|
|
57
|
|
|
/** @var ICloudIdManager */ |
|
58
|
|
|
protected $cloudIdManager; |
|
59
|
|
|
|
|
60
|
|
|
/** @var array */ |
|
61
|
|
|
protected $displayNames = []; |
|
62
|
|
|
|
|
63
|
|
|
public function __construct(IFactory $languageFactory, |
|
64
|
|
|
IURLGenerator $url, |
|
65
|
|
|
IManager $activityManager, |
|
66
|
|
|
IUserManager $userManager, |
|
67
|
|
|
ICloudIdManager $cloudIdManager, |
|
68
|
|
|
IContactsManager $contactsManager) { |
|
69
|
|
|
$this->languageFactory = $languageFactory; |
|
70
|
|
|
$this->url = $url; |
|
71
|
|
|
$this->activityManager = $activityManager; |
|
72
|
|
|
$this->userManager = $userManager; |
|
73
|
|
|
$this->cloudIdManager = $cloudIdManager; |
|
74
|
|
|
$this->contactsManager = $contactsManager; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @param string $language |
|
79
|
|
|
* @param IEvent $event |
|
80
|
|
|
* @param IEvent|null $previousEvent |
|
81
|
|
|
* @return IEvent |
|
82
|
|
|
* @throws \InvalidArgumentException |
|
83
|
|
|
* @since 11.0.0 |
|
84
|
|
|
*/ |
|
85
|
|
|
public function parse($language, IEvent $event, IEvent $previousEvent = null) { |
|
86
|
|
|
if ($event->getApp() !== 'files_sharing') { |
|
87
|
|
|
throw new \InvalidArgumentException(); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
$this->l = $this->languageFactory->get('files_sharing', $language); |
|
91
|
|
|
|
|
92
|
|
|
if ($this->activityManager->isFormattingFilteredObject()) { |
|
93
|
|
|
try { |
|
94
|
|
|
return $this->parseShortVersion($event); |
|
95
|
|
|
} catch (\InvalidArgumentException $e) { |
|
96
|
|
|
// Ignore and simply use the long version... |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
return $this->parseLongVersion($event); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* @param IEvent $event |
|
105
|
|
|
* @return IEvent |
|
106
|
|
|
* @throws \InvalidArgumentException |
|
107
|
|
|
* @since 11.0.0 |
|
108
|
|
|
*/ |
|
109
|
|
|
abstract protected function parseShortVersion(IEvent $event); |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* @param IEvent $event |
|
113
|
|
|
* @return IEvent |
|
114
|
|
|
* @throws \InvalidArgumentException |
|
115
|
|
|
* @since 11.0.0 |
|
116
|
|
|
*/ |
|
117
|
|
|
abstract protected function parseLongVersion(IEvent $event); |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* @param IEvent $event |
|
121
|
|
|
* @param string $subject |
|
122
|
|
|
* @param array $parameters |
|
123
|
|
|
* @throws \InvalidArgumentException |
|
124
|
|
|
*/ |
|
125
|
|
|
protected function setSubjects(IEvent $event, $subject, array $parameters) { |
|
126
|
|
|
$placeholders = $replacements = []; |
|
127
|
|
|
foreach ($parameters as $placeholder => $parameter) { |
|
128
|
|
|
$placeholders[] = '{' . $placeholder . '}'; |
|
129
|
|
|
if ($parameter['type'] === 'file') { |
|
130
|
|
|
$replacements[] = $parameter['path']; |
|
131
|
|
|
} else { |
|
132
|
|
|
$replacements[] = $parameter['name']; |
|
133
|
|
|
} |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
$event->setParsedSubject(str_replace($placeholders, $replacements, $subject)) |
|
137
|
|
|
->setRichSubject($subject, $parameters); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* @param array|string $parameter |
|
142
|
|
|
* @param IEvent|null $event |
|
143
|
|
|
* @return array |
|
144
|
|
|
* @throws \InvalidArgumentException |
|
145
|
|
|
*/ |
|
146
|
|
|
protected function getFile($parameter, IEvent $event = null) { |
|
147
|
|
|
if (is_array($parameter)) { |
|
148
|
|
|
$path = reset($parameter); |
|
149
|
|
|
$id = (string) key($parameter); |
|
150
|
|
|
} else if ($event !== null) { |
|
151
|
|
|
// Legacy from before ownCloud 8.2 |
|
152
|
|
|
$path = $parameter; |
|
153
|
|
|
$id = $event->getObjectId(); |
|
154
|
|
|
} else { |
|
155
|
|
|
throw new \InvalidArgumentException('Could not generate file parameter'); |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
return [ |
|
159
|
|
|
'type' => 'file', |
|
160
|
|
|
'id' => $id, |
|
161
|
|
|
'name' => basename($path), |
|
162
|
|
|
'path' => trim($path, '/'), |
|
163
|
|
|
'link' => $this->url->linkToRouteAbsolute('files.viewcontroller.showFile', ['fileid' => $id]), |
|
164
|
|
|
]; |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
/** |
|
168
|
|
|
* @param string $uid |
|
169
|
|
|
* @return array |
|
170
|
|
|
*/ |
|
171
|
|
|
protected function getUser($uid) { |
|
172
|
|
|
// First try local user |
|
173
|
|
|
$user = $this->userManager->get($uid); |
|
174
|
|
|
if ($user instanceof IUser) { |
|
175
|
|
|
return [ |
|
176
|
|
|
'type' => 'user', |
|
177
|
|
|
'id' => $user->getUID(), |
|
178
|
|
|
'name' => $user->getDisplayName(), |
|
179
|
|
|
]; |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
// Then a contact from the addressbook |
|
183
|
|
|
if ($this->cloudIdManager->isValidCloudId($uid)) { |
|
184
|
|
|
$cloudId = $this->cloudIdManager->resolveCloudId($uid); |
|
185
|
|
|
return [ |
|
186
|
|
|
'type' => 'user', |
|
187
|
|
|
'id' => $cloudId->getUser(), |
|
188
|
|
|
'name' => $this->getDisplayNameFromAddressBook($cloudId->getDisplayId()), |
|
189
|
|
|
'server' => $cloudId->getRemote(), |
|
190
|
|
|
]; |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
// Fallback to empty dummy data |
|
194
|
|
|
return [ |
|
195
|
|
|
'type' => 'user', |
|
196
|
|
|
'id' => $uid, |
|
197
|
|
|
'name' => $uid, |
|
198
|
|
|
]; |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
protected function getDisplayNameFromAddressBook(string $search): string { |
|
202
|
|
|
if (isset($this->displayNames[$search])) { |
|
203
|
|
|
return $this->displayNames[$search]; |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
$addressBookContacts = $this->contactsManager->search($search, ['CLOUD']); |
|
207
|
|
|
foreach ($addressBookContacts as $contact) { |
|
208
|
|
|
if (isset($contact['isLocalSystemBook'])) { |
|
209
|
|
|
continue; |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
if (isset($contact['CLOUD'])) { |
|
213
|
|
|
$cloudIds = $contact['CLOUD']; |
|
214
|
|
|
if (is_string($cloudIds)) { |
|
215
|
|
|
$cloudIds = [$cloudIds]; |
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
|
|
$lowerSearch = strtolower($search); |
|
219
|
|
|
foreach ($cloudIds as $cloudId) { |
|
220
|
|
|
if (strtolower($cloudId) === $lowerSearch) { |
|
221
|
|
|
$this->displayNames[$search] = $contact['FN'] . " ($cloudId)"; |
|
222
|
|
|
return $this->displayNames[$search]; |
|
223
|
|
|
} |
|
224
|
|
|
} |
|
225
|
|
|
} |
|
226
|
|
|
} |
|
227
|
|
|
|
|
228
|
|
|
return $search; |
|
229
|
|
|
} |
|
230
|
|
|
} |
|
231
|
|
|
|