Completed
Pull Request — master (#499)
by Joas
02:45
created

Feed   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 128
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 6
Bugs 1 Features 0
Metric Value
wmc 6
c 6
b 1
f 0
lcom 1
cbo 3
dl 0
loc 128
ccs 41
cts 41
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B show() 0 53 5
A __construct() 0 20 1
1
<?php
2
/**
3
 * @author Joas Schilling <[email protected]>
4
 *
5
 * @copyright Copyright (c) 2016, ownCloud, Inc.
6
 * @license AGPL-3.0
7
 *
8
 * This code is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU Affero General Public License, version 3,
10
 * as published by the Free Software Foundation.
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, version 3,
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
19
 *
20
 */
21
22
namespace OCA\Activity\Controller;
23
24
use OCA\Activity\Data;
25
use OCA\Activity\GroupHelper;
26
use OCA\Activity\PlainTextParser;
27
use OCA\Activity\UserSettings;
28
use OCP\Activity\IManager;
29
use OCP\AppFramework\Controller;
30
use OCP\AppFramework\Http\TemplateResponse;
31
use OCP\IConfig;
32
use OCP\IL10N;
33
use OCP\IRequest;
34
use OCP\IURLGenerator;
35
use OCP\L10N\IFactory;
36
use OCP\Util;
37
38
class Feed extends Controller {
39
	const DEFAULT_PAGE_SIZE = 30;
40
41
	/** @var \OCA\Activity\Data */
42
	protected $data;
43
44
	/** @var \OCA\Activity\GroupHelper */
45
	protected $helper;
46
47
	/** @var \OCA\Activity\UserSettings */
48
	protected $settings;
49
50
	/** @var IURLGenerator */
51
	protected $urlGenerator;
52
53
	/** @var IManager */
54
	protected $activityManager;
55
56
	/** @var IConfig */
57
	protected $config;
58
59
	/** @var IFactory */
60
	protected $l10nFactory;
61
62
	/** @var IL10N */
63
	protected $l;
64
65
	/** @var string */
66
	protected $user;
67
68
	/** @var string */
69
	protected $tokenUser;
70
71
	/**
72
	 * constructor of the controller
73
	 *
74
	 * @param string $appName
75
	 * @param IRequest $request
76
	 * @param Data $data
77
	 * @param GroupHelper $helper
78
	 * @param UserSettings $settings
79
	 * @param IURLGenerator $urlGenerator
80
	 * @param IManager $activityManager
81
	 * @param IFactory $l10nFactory
82
	 * @param IConfig $config
83
	 * @param string $user
84
	 */
85 5
	public function __construct($appName,
86
								IRequest $request,
87
								Data $data,
88
								GroupHelper $helper,
89
								UserSettings $settings,
90
								IURLGenerator $urlGenerator,
91
								IManager $activityManager,
92
								IFactory $l10nFactory,
93
								IConfig $config,
94
								$user) {
95 5
		parent::__construct($appName, $request);
96 5
		$this->data = $data;
97 5
		$this->helper = $helper;
98 5
		$this->settings = $settings;
99 5
		$this->urlGenerator = $urlGenerator;
100 5
		$this->activityManager = $activityManager;
101 5
		$this->l10nFactory = $l10nFactory;
102 5
		$this->config = $config;
103 5
		$this->user = $user;
104 5
	}
105
106
	/**
107
	 * @PublicPage
108
	 * @NoCSRFRequired
109
	 *
110
	 * @return TemplateResponse
111
	 */
112 4
	public function show() {
113
		try {
114 4
			$user = $this->activityManager->getCurrentUserId();
115
116 2
			$userLang = $this->config->getUserValue($user, 'core', 'lang');
117
118
			// Overwrite user and language in the helper
119 2
			$this->l = $this->l10nFactory->get('activity', $userLang);
120 2
			$parser = new PlainTextParser($this->l);
121 2
			$this->helper->setL10n($this->l);
122 2
			$this->helper->setUser($user);
123
124 2
			$description = (string) $this->l->t('Personal activity feed for %s', $user);
125 2
			$response = $this->data->get($this->helper, $this->settings, $user, 0, self::DEFAULT_PAGE_SIZE, 'desc', 'all');
126 2
			$data = $response['data'];
127
128 2
			$activities = [];
129 2
			foreach ($data as $activity) {
130
				$activity['subject_prepared'] = $parser->parseMessage($activity['subject_prepared']);
131
				$activity['message_prepared'] = $parser->parseMessage($activity['message_prepared']);
132 2
				$activities[] = $activity;
133
			}
134
135 2
		} catch (\UnexpectedValueException $e) {
136 2
			$this->l = $this->l10nFactory->get('activity');
137 2
			$description = (string) $this->l->t('Your feed URL is invalid');
138
139
			$activities = [
140
				[
141
					'activity_id'	=> -1,
142 2
					'timestamp'		=> time(),
143
					'subject'		=> true,
144 2
					'subject_prepared'	=> $description,
145
				]
146 2
			];
147
		}
148
149 4
		$response = new TemplateResponse('activity', 'rss', [
150 4
			'rssLang'		=> $this->l->getLanguageCode(),
151 4
			'rssLink'		=> $this->urlGenerator->linkToRouteAbsolute('activity.Feed.show'),
152 4
			'rssPubDate'	=> date('r'),
153 4
			'description'	=> $description,
154 4
			'activities'	=> $activities,
155 4
		], '');
156
157 4
		if ($this->request->getHeader('accept') !== null && stristr($this->request->getHeader('accept'), 'application/rss+xml')) {
158 2
			$response->addHeader('Content-Type', 'application/rss+xml');
159
		} else {
160 2
			$response->addHeader('Content-Type', 'text/xml; charset=UTF-8');
161
		}
162
163 4
		return $response;
164
	}
165
}
166