Completed
Push — master ( 010658...163ba4 )
by Joas
02:49
created

Settings::feed()   B

Complexity

Conditions 3
Paths 2

Size

Total Lines 24
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 24
ccs 0
cts 16
cp 0
rs 8.9713
c 0
b 0
f 0
cc 3
eloc 13
nc 2
nop 1
crap 12
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016, ownCloud, Inc.
4
 *
5
 * @author Joas Schilling <[email protected]>
6
 * @author Lukas Reschke <[email protected]>
7
 *
8
 * @license AGPL-3.0
9
 *
10
 * This code is free software: you can redistribute it and/or modify
11
 * it under the terms of the GNU Affero General Public License, version 3,
12
 * as published by the Free Software Foundation.
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, version 3,
20
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
21
 *
22
 */
23
24
namespace OCA\Activity\Controller;
25
26
use OCA\Activity\CurrentUser;
27
use OCA\Activity\UserSettings;
28
use OCP\Activity\IExtension;
29
use OCP\Activity\IManager;
30
use OCP\Activity\ISetting;
31
use OCP\AppFramework\Controller;
32
use OCP\AppFramework\Http\DataResponse;
33
use OCP\AppFramework\Http\TemplateResponse;
34
use OCP\IConfig;
35
use OCP\IL10N;
36
use OCP\IRequest;
37
use OCP\IURLGenerator;
38
use OCP\Security\ISecureRandom;
39
40
class Settings extends Controller {
41
	/** @var \OCP\IConfig */
42
	protected $config;
43
44
	/** @var \OCP\Security\ISecureRandom */
45
	protected $random;
46
47
	/** @var \OCP\IURLGenerator */
48
	protected $urlGenerator;
49
50
	/** @var IManager */
51
	protected $manager;
52
53
	/** @var \OCA\Activity\UserSettings */
54
	protected $userSettings;
55
56
	/** @var \OCP\IL10N */
57
	protected $l10n;
58
59
	/** @var string */
60
	protected $user;
61
62
	/**
63
	 * constructor of the controller
64
	 *
65
	 * @param string $appName
66
	 * @param IRequest $request
67
	 * @param IConfig $config
68
	 * @param ISecureRandom $random
69
	 * @param IURLGenerator $urlGenerator
70
	 * @param IManager $manager
71
	 * @param UserSettings $userSettings
72
	 * @param IL10N $l10n
73
	 * @param CurrentUser $currentUser
74
	 */
75 3 View Code Duplication
	public function __construct($appName,
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
76
								IRequest $request,
77
								IConfig $config,
78
								ISecureRandom $random,
79
								IURLGenerator $urlGenerator,
80
								IManager $manager,
81
								UserSettings $userSettings,
82
								IL10N $l10n,
83
								CurrentUser $currentUser) {
84 3
		parent::__construct($appName, $request);
85 3
		$this->config = $config;
86 3
		$this->random = $random;
87 3
		$this->urlGenerator = $urlGenerator;
88 3
		$this->manager = $manager;
89 3
		$this->userSettings = $userSettings;
90 3
		$this->l10n = $l10n;
91 3
		$this->user = (string) $currentUser->getUID();
92 3
	}
93
94
	/**
95
	 * @NoAdminRequired
96
	 *
97
	 * @param int $notify_setting_batchtime
98
	 * @param bool $notify_setting_self
99
	 * @param bool $notify_setting_selfemail
100
	 * @return DataResponse
101
	 */
102
	public function personal(
103
			$notify_setting_batchtime = UserSettings::EMAIL_SEND_HOURLY,
104
			$notify_setting_self = false,
105
			$notify_setting_selfemail = false) {
106
107
		$settings = $this->manager->getSettings();
108 View Code Duplication
		foreach ($settings as $setting) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
109
			if ($setting->canChangeStream()) {
110
				$this->config->setUserValue(
111
					$this->user, 'activity',
112
					'notify_stream_' . $setting->getIdentifier(),
113
					(int) $this->request->getParam($setting->getIdentifier() . '_stream', false)
114
				);
115
			}
116
117
			if ($setting->canChangeMail()) {
118
				$this->config->setUserValue(
119
					$this->user, 'activity',
120
					'notify_email_' . $setting->getIdentifier(),
121
					(int) $this->request->getParam($setting->getIdentifier() . '_email', false)
122
				);
123
			}
124
		}
125
126
		$email_batch_time = 3600;
127 View Code Duplication
		if ($notify_setting_batchtime === UserSettings::EMAIL_SEND_DAILY) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
128
			$email_batch_time = 3600 * 24;
129
		} else if ($notify_setting_batchtime === UserSettings::EMAIL_SEND_WEEKLY) {
130
			$email_batch_time = 3600 * 24 * 7;
131
		}
132
133
		$this->config->setUserValue(
134
			$this->user, 'activity',
135
			'notify_setting_batchtime',
136
			$email_batch_time
137
		);
138
		$this->config->setUserValue(
139
			$this->user, 'activity',
140
			'notify_setting_self',
141
			(int) $notify_setting_self
142
		);
143
		$this->config->setUserValue(
144
			$this->user, 'activity',
145
			'notify_setting_selfemail',
146
			(int) $notify_setting_selfemail
147
		);
148
149
		return new DataResponse(array(
150
			'data'		=> array(
151
				'message'	=> (string) $this->l10n->t('Your settings have been updated.'),
152
			),
153
		));
154
	}
155
156
	/**
157
	 * @param int $notify_setting_batchtime
158
	 * @param bool $notify_setting_self
159
	 * @param bool $notify_setting_selfemail
160
	 * @return DataResponse
161
	 */
162
	public function admin(
163
			$notify_setting_batchtime = UserSettings::EMAIL_SEND_HOURLY,
164
			$notify_setting_self = false,
165
			$notify_setting_selfemail = false) {
166
167
		$settings = $this->manager->getSettings();
168 View Code Duplication
		foreach ($settings as $setting) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
169
			if ($setting->canChangeStream()) {
170
				$this->config->setAppValue(
171
					'activity',
172
					'notify_stream_' . $setting->getIdentifier(),
173
					(int) $this->request->getParam($setting->getIdentifier() . '_stream', false)
174
				);
175
			}
176
177
			if ($setting->canChangeMail()) {
178
				$this->config->setAppValue(
179
					'activity',
180
					'notify_email_' . $setting->getIdentifier(),
181
					(int) $this->request->getParam($setting->getIdentifier() . '_email', false)
182
				);
183
			}
184
		}
185
186
		$email_batch_time = 3600;
187 View Code Duplication
		if ($notify_setting_batchtime === UserSettings::EMAIL_SEND_DAILY) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
188
			$email_batch_time = 3600 * 24;
189
		} else if ($notify_setting_batchtime === UserSettings::EMAIL_SEND_WEEKLY) {
190
			$email_batch_time = 3600 * 24 * 7;
191
		}
192
193
		$this->config->setAppValue(
194
			'activity',
195
			'notify_setting_batchtime',
196
			$email_batch_time
197
		);
198
		$this->config->setAppValue(
199
			'activity',
200
			'notify_setting_self',
201
			(int) $notify_setting_self
202
		);
203
		$this->config->setAppValue(
204
			'activity',
205
			'notify_setting_selfemail',
206
			(int) $notify_setting_selfemail
207
		);
208
209
		return new DataResponse(array(
210
			'data'		=> array(
211
				'message'	=> (string) $this->l10n->t('Settings have been updated.'),
212
			),
213
		));
214
	}
215
216
	/**
217
	 * @NoAdminRequired
218
	 * @NoCSRFRequired
219
	 *
220
	 * @return TemplateResponse
221
	 */
222 1
	public function displayPanel() {
223 1
		$settings = $this->manager->getSettings();
224 1 View Code Duplication
		usort($settings, function(ISetting $a, ISetting $b) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
225 1
			if ($a->getPriority() === $b->getPriority()) {
226 1
				return $a->getIdentifier() > $b->getIdentifier();
227
			}
228
229 1
			return $a->getPriority() > $b->getPriority();
230 1
		});
231
232 1
		$activities = [];
233 1
		foreach ($settings as $setting) {
234 1
			if (!$setting->canChangeStream() && !$setting->canChangeMail()) {
235
				// No setting can be changed => don't display
236 1
				continue;
237
			}
238
239 1
			$methods = [];
240 1
			if ($setting->canChangeStream()) {
241 1
				$methods[] = IExtension::METHOD_STREAM;
242 1
			}
243 1
			if ($setting->canChangeMail()) {
244 1
				$methods[] = IExtension::METHOD_MAIL;
245 1
			}
246
247 1
			$activities[$setting->getIdentifier()] = array(
248 1
				'desc'		=> $setting->getName(),
249 1
				IExtension::METHOD_MAIL		=> $this->userSettings->getUserSetting($this->user, 'email', $setting->getIdentifier()),
250 1
				IExtension::METHOD_STREAM	=> $this->userSettings->getUserSetting($this->user, 'stream', $setting->getIdentifier()),
251 1
				'methods'	=> $methods,
252
			);
253 1
		}
254
255 1
		$settingBatchTime = UserSettings::EMAIL_SEND_HOURLY;
256 1
		$currentSetting = (int) $this->userSettings->getUserSetting($this->user, 'setting', 'batchtime');
257 1 View Code Duplication
		if ($currentSetting === 3600 * 24 * 7) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
258
			$settingBatchTime = UserSettings::EMAIL_SEND_WEEKLY;
259 1
		} else if ($currentSetting === 3600 * 24) {
260
			$settingBatchTime = UserSettings::EMAIL_SEND_DAILY;
261
		}
262
263 1
		return new TemplateResponse('activity', 'settings/personal', [
264 1
			'activities'		=> $activities,
265 1
			'activity_email'	=> $this->config->getUserValue($this->user, 'settings', 'email', ''),
266
267 1
			'setting_batchtime'	=> $settingBatchTime,
268
269 1
			'notify_self'		=> $this->userSettings->getUserSetting($this->user, 'setting', 'self'),
270 1
			'notify_selfemail'	=> $this->userSettings->getUserSetting($this->user, 'setting', 'selfemail'),
271
272
			'methods'			=> [
273 1
				IExtension::METHOD_MAIL => $this->l10n->t('Mail'),
274 1
				IExtension::METHOD_STREAM => $this->l10n->t('Stream'),
275 1
			],
276 1
		], '');
277
	}
278
279
	/**
280
	 * @NoAdminRequired
281
	 *
282
	 * @param string $enable	'true' if the feed is enabled
283
	 * @return DataResponse
284
	 */
285
	public function feed($enable) {
286
		$token = $tokenUrl = '';
287
288
		if ($enable === 'true') {
289
			$conflicts = true;
290
291
			// Check for collisions
292
			while (!empty($conflicts)) {
293
				$token = $this->random->generate(30, ISecureRandom::CHAR_UPPER . ISecureRandom::CHAR_LOWER . ISecureRandom::CHAR_DIGITS);
294
				$conflicts = $this->config->getUsersForUserValue('activity', 'rsstoken', $token);
295
			}
296
297
			$tokenUrl = $this->urlGenerator->linkToRouteAbsolute('activity.Feed.show', ['token' => $token]);
298
		}
299
300
		$this->config->setUserValue($this->user, 'activity', 'rsstoken', $token);
301
302
		return new DataResponse(array(
303
			'data'		=> array(
304
				'message'	=> (string) $this->l10n->t('Your settings have been updated.'),
305
				'rsslink'	=> $tokenUrl,
306
			),
307
		));
308
	}
309
}
310