1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* ownCloud |
4
|
|
|
* |
5
|
|
|
* @author Joas Schilling |
6
|
|
|
* @copyright 2015 Joas Schilling [email protected] |
7
|
|
|
* |
8
|
|
|
* This file is licensed under the Affero General Public License version 3 or |
9
|
|
|
* later. |
10
|
|
|
* See the COPYING-README file. |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace OCA\Activity\Controller; |
14
|
|
|
|
15
|
|
|
use OCA\Activity\Data; |
16
|
|
|
use OCA\Activity\GroupHelper; |
17
|
|
|
use OCA\Activity\UserSettings; |
18
|
|
|
use OCP\Activity\IManager; |
19
|
|
|
use OCP\AppFramework\Controller; |
20
|
|
|
use OCP\AppFramework\Http\TemplateResponse; |
21
|
|
|
use OCP\IConfig; |
22
|
|
|
use OCP\IL10N; |
23
|
|
|
use OCP\IRequest; |
24
|
|
|
use OCP\IURLGenerator; |
25
|
|
|
use OCP\L10N\IFactory; |
26
|
|
|
use OCP\Util; |
27
|
|
|
|
28
|
|
|
class Feed extends Controller { |
29
|
|
|
const DEFAULT_PAGE_SIZE = 30; |
30
|
|
|
|
31
|
|
|
/** @var \OCA\Activity\Data */ |
32
|
|
|
protected $data; |
33
|
|
|
|
34
|
|
|
/** @var \OCA\Activity\GroupHelper */ |
35
|
|
|
protected $helper; |
36
|
|
|
|
37
|
|
|
/** @var \OCA\Activity\UserSettings */ |
38
|
|
|
protected $settings; |
39
|
|
|
|
40
|
|
|
/** @var IURLGenerator */ |
41
|
|
|
protected $urlGenerator; |
42
|
|
|
|
43
|
|
|
/** @var IManager */ |
44
|
|
|
protected $activityManager; |
45
|
|
|
|
46
|
|
|
/** @var IConfig */ |
47
|
|
|
protected $config; |
48
|
|
|
|
49
|
|
|
/** @var IFactory */ |
50
|
|
|
protected $l10nFactory; |
51
|
|
|
|
52
|
|
|
/** @var IL10N */ |
53
|
|
|
protected $l; |
54
|
|
|
|
55
|
|
|
/** @var string */ |
56
|
|
|
protected $user; |
57
|
|
|
|
58
|
|
|
/** @var string */ |
59
|
|
|
protected $tokenUser; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* constructor of the controller |
63
|
|
|
* |
64
|
|
|
* @param string $appName |
65
|
|
|
* @param IRequest $request |
66
|
|
|
* @param Data $data |
67
|
|
|
* @param GroupHelper $helper |
68
|
|
|
* @param UserSettings $settings |
69
|
|
|
* @param IURLGenerator $urlGenerator |
70
|
|
|
* @param IManager $activityManager |
71
|
|
|
* @param IFactory $l10nFactory |
72
|
|
|
* @param IConfig $config |
73
|
|
|
* @param string $user |
74
|
|
|
*/ |
75
|
5 |
|
public function __construct($appName, |
76
|
|
|
IRequest $request, |
77
|
|
|
Data $data, |
78
|
|
|
GroupHelper $helper, |
79
|
|
|
UserSettings $settings, |
80
|
|
|
IURLGenerator $urlGenerator, |
81
|
|
|
IManager $activityManager, |
82
|
|
|
IFactory $l10nFactory, |
83
|
|
|
IConfig $config, |
84
|
|
|
$user) { |
85
|
5 |
|
parent::__construct($appName, $request); |
86
|
5 |
|
$this->data = $data; |
87
|
5 |
|
$this->helper = $helper; |
88
|
5 |
|
$this->settings = $settings; |
89
|
5 |
|
$this->urlGenerator = $urlGenerator; |
90
|
5 |
|
$this->activityManager = $activityManager; |
91
|
5 |
|
$this->l10nFactory = $l10nFactory; |
92
|
5 |
|
$this->config = $config; |
93
|
5 |
|
$this->user = $user; |
94
|
5 |
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @PublicPage |
98
|
|
|
* @NoCSRFRequired |
99
|
|
|
* |
100
|
|
|
* @return TemplateResponse |
101
|
|
|
*/ |
102
|
4 |
|
public function show() { |
103
|
|
|
try { |
104
|
4 |
|
$user = $this->activityManager->getCurrentUserId(); |
105
|
|
|
|
106
|
2 |
|
$userLang = $this->config->getUserValue($user, 'core', 'lang'); |
107
|
|
|
|
108
|
|
|
// Overwrite user and language in the helper |
109
|
2 |
|
$this->l = $this->l10nFactory->get('activity', $userLang); |
110
|
2 |
|
$this->helper->setL10n($this->l); |
111
|
2 |
|
$this->helper->setUser($user); |
112
|
|
|
|
113
|
2 |
|
$description = (string) $this->l->t('Personal activity feed for %s', $user); |
114
|
2 |
|
$response = $this->data->get($this->helper, $this->settings, $user, 0, self::DEFAULT_PAGE_SIZE, 'desc', 'all'); |
115
|
2 |
|
$data = $response['data']; |
116
|
|
|
|
117
|
2 |
|
$activities = []; |
118
|
2 |
|
foreach ($data as $activity) { |
119
|
|
|
$activity['subject_prepared'] = $this->parseMessage($activity['subject_prepared']); |
120
|
|
|
$activity['message_prepared'] = $this->parseMessage($activity['message_prepared']); |
121
|
|
|
$activities[] = $activity; |
122
|
2 |
|
} |
123
|
|
|
|
124
|
4 |
|
} catch (\UnexpectedValueException $e) { |
125
|
2 |
|
$this->l = $this->l10nFactory->get('activity'); |
126
|
2 |
|
$description = (string) $this->l->t('Your feed URL is invalid'); |
127
|
|
|
|
128
|
|
|
$activities = [ |
129
|
|
|
[ |
130
|
2 |
|
'activity_id' => -1, |
131
|
2 |
|
'timestamp' => time(), |
132
|
2 |
|
'subject' => true, |
133
|
2 |
|
'subject_prepared' => $description, |
134
|
|
|
] |
135
|
2 |
|
]; |
136
|
|
|
} |
137
|
|
|
|
138
|
4 |
|
$response = new TemplateResponse('activity', 'rss', [ |
139
|
4 |
|
'rssLang' => $this->l->getLanguageCode(), |
140
|
4 |
|
'rssLink' => $this->urlGenerator->linkToRouteAbsolute('activity.Feed.show'), |
141
|
4 |
|
'rssPubDate' => date('r'), |
142
|
4 |
|
'description' => $description, |
143
|
4 |
|
'activities' => $activities, |
144
|
4 |
|
], ''); |
145
|
|
|
|
146
|
4 |
|
if ($this->request->getHeader('accept') !== null && stristr($this->request->getHeader('accept'), 'application/rss+xml')) { |
147
|
2 |
|
$response->addHeader('Content-Type', 'application/rss+xml'); |
148
|
2 |
|
} else { |
149
|
2 |
|
$response->addHeader('Content-Type', 'text/xml; charset=UTF-8'); |
150
|
|
|
} |
151
|
|
|
|
152
|
4 |
|
return $response; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Parse the parameters in the subject and message |
157
|
|
|
* |
158
|
|
|
* @param string $message |
159
|
|
|
* @return string |
160
|
|
|
*/ |
161
|
|
|
protected function parseMessage($message) { |
162
|
|
|
$message = $this->parseCollections($message); |
163
|
|
|
$message = $this->parseParameters($message); |
164
|
|
|
return $message; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Parse collections |
169
|
|
|
* |
170
|
|
|
* @param string $message |
171
|
|
|
* @return string |
172
|
|
|
*/ |
173
|
|
|
protected function parseCollections($message) { |
174
|
|
|
return preg_replace_callback('/<collection>(.*?)<\/collection>/', function($match) { |
175
|
|
|
$parameterList = explode('><', $match[1]); |
176
|
|
|
$parameterListLength = sizeof($parameterList); |
177
|
|
|
|
178
|
|
|
$parameters = []; |
179
|
|
View Code Duplication |
for ($i = 0; $i < $parameterListLength; $i++) { |
|
|
|
|
180
|
|
|
$parameter = $parameterList[$i]; |
181
|
|
|
if ($i > 0) { |
182
|
|
|
$parameter = '<' . $parameter; |
183
|
|
|
} |
184
|
|
|
if ($i + 1 < $parameterListLength) { |
185
|
|
|
$parameter = $parameter . '>'; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
$parameters[] = $this->parseParameters($parameter); |
189
|
|
|
} |
190
|
|
|
if ($parameterListLength === 1) { |
191
|
|
|
return array_pop($parameters); |
192
|
|
|
} else { |
193
|
|
|
$lastParameter = array_pop($parameters); |
194
|
|
|
return $this->l->t('%s and %s', [ |
195
|
|
|
implode($this->l->t(', '), $parameters), |
196
|
|
|
$lastParameter, |
197
|
|
|
]); |
198
|
|
|
} |
199
|
|
|
}, $message); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* Parse the parameters in the subject and message |
204
|
|
|
* |
205
|
|
|
* @param string $message |
206
|
|
|
* @return string |
207
|
|
|
*/ |
208
|
|
|
protected function parseParameters($message) { |
209
|
|
|
$message = $this->parseUntypedParameters($message); |
210
|
|
|
$message = $this->parseUserParameters($message); |
211
|
|
|
$message = $this->parseFederatedCloudIDParameters($message); |
212
|
|
|
$message = $this->parseFileParameters($message); |
213
|
|
|
return $message; |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* Display the parameter value |
218
|
|
|
* |
219
|
|
|
* @param string $message |
220
|
|
|
* @return string |
221
|
|
|
*/ |
222
|
|
|
protected function parseUntypedParameters($message) { |
223
|
|
|
return preg_replace_callback('/<parameter>(.*?)<\/parameter>/', function($match) { |
224
|
|
|
return $match[1]; |
225
|
|
|
}, $message); |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* Display the users display name |
230
|
|
|
* |
231
|
|
|
* @param string $message |
232
|
|
|
* @return string |
233
|
|
|
*/ |
234
|
|
|
protected function parseUserParameters($message) { |
235
|
|
|
return preg_replace_callback('/<user\ display\-name=\"(.*?)\">(.*?)<\/user>/', function($match) { |
236
|
|
|
return $match[1]; |
237
|
|
|
}, $message); |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* Display the full cloud id |
242
|
|
|
* |
243
|
|
|
* @param string $message |
244
|
|
|
* @return string |
245
|
|
|
*/ |
246
|
|
|
protected function parseFederatedCloudIDParameters($message) { |
247
|
|
|
return preg_replace_callback('/<federated-cloud-id\ display\-name=\"(.*?)\"\ user=\"(.*?)\"\ server=\"(.*?)\">(.*?)<\/federated-cloud-id>/', function($match) { |
248
|
|
|
return $match[1]; |
249
|
|
|
}, $message); |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* Display the path for files |
254
|
|
|
* |
255
|
|
|
* @param string $message |
256
|
|
|
* @return string |
257
|
|
|
*/ |
258
|
|
|
protected function parseFileParameters($message) { |
259
|
|
|
return preg_replace_callback('/<file\ link=\"(.*?)\"\ id=\"(.*?)\">(.*?)<\/file>/', function($match) { |
260
|
|
|
return $match[3]; |
261
|
|
|
}, $message); |
262
|
|
|
} |
263
|
|
|
} |
264
|
|
|
|
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.