| 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 | View Code Duplication | 	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 | 2 |  | 		} catch (\UnexpectedValueException $e) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 | 2 |  | 			$this->l = $this->l10nFactory->get('activity'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 | 2 |  | 			$description = (string) $this->l->t('Your feed URL is invalid'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  | 			$activities = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  | 				[ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 | 2 |  | 					'activity_id'	=> -1, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 | 2 |  | 					'timestamp'		=> \time(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  | 					'subject'		=> true, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 | 2 |  | 					'subject_prepared'	=> $description, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  | 				] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  | 			]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 | 4 |  | 		$response = new TemplateResponse('activity', 'rss', [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 | 4 |  | 			'rssLang'		=> $this->l->getLanguageCode(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 | 4 |  | 			'rssLink'		=> $this->urlGenerator->linkToRouteAbsolute('activity.Feed.show'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 | 4 |  | 			'rssPubDate'	=> \date('r'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 | 4 |  | 			'description'	=> $description, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 | 4 |  | 			'activities'	=> $activities, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 | 4 |  | 		], ''); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 | 4 |  | 		if ($this->request->getHeader('accept') !== null && \stristr($this->request->getHeader('accept'), 'application/rss+xml')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 | 2 |  | 			$response->addHeader('Content-Type', 'application/rss+xml'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  | 		} else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 | 2 |  | 			$response->addHeader('Content-Type', 'text/xml; charset=UTF-8'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 | 4 |  | 		return $response; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  | 	} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 164 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 165 |  |  |  | 
            
                        
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.