Completed
Pull Request — stable9 (#58)
by Joas
02:24
created

GroupHelper   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 217
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 98.95%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 24
lcom 1
cbo 3
dl 0
loc 217
ccs 94
cts 95
cp 0.9895
rs 10
c 1
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A setUser() 0 3 1
A setL10n() 0 3 1
A getGroupKey() 0 15 3
A getGroupParameter() 0 8 2
C addActivity() 0 62 9
A closeOpenGroup() 0 9 2
B getActivities() 0 26 4
A getEventFromArray() 0 14 1
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016, ownCloud, Inc.
4
 *
5
 * @author Joas Schilling <[email protected]>
6
 *
7
 * @license AGPL-3.0
8
 *
9
 * This code is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU Affero General Public License, version 3,
11
 * as published by the Free Software Foundation.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
 * GNU Affero General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Affero General Public License, version 3,
19
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
20
 *
21
 */
22
23
namespace OCA\Activity;
24
25
use OCA\Activity\Parameter\Collection;
26
use OCA\Activity\Parameter\IParameter;
27
use OCP\Activity\IEvent;
28
use OCP\Activity\IManager;
29
use OCP\IL10N;
30
31
class GroupHelper {
32
	/** @var array */
33
	protected $activities = array();
34
35
	/** @var array */
36
	protected $openGroup = array();
37
38
	/** @var string */
39
	protected $groupKey = '';
40
41
	/** @var int */
42
	protected $groupTime = 0;
43
44
	/** @var bool */
45
	protected $allowGrouping;
46
47
	/** @var \OCP\Activity\IManager */
48
	protected $activityManager;
49
50
	/** @var \OCA\Activity\DataHelper */
51
	protected $dataHelper;
52
53
	/**
54
	 * @param \OCP\Activity\IManager $activityManager
55
	 * @param \OCA\Activity\DataHelper $dataHelper
56
	 * @param bool $allowGrouping
57
	 */
58 31
	public function __construct(IManager $activityManager, DataHelper $dataHelper, $allowGrouping) {
59 31
		$this->allowGrouping = $allowGrouping;
60
61 31
		$this->activityManager = $activityManager;
62 31
		$this->dataHelper = $dataHelper;
63 31
	}
64
65
	/**
66
	 * @param string $user
67
	 */
68 6
	public function setUser($user) {
69 6
		$this->dataHelper->setUser($user);
70 6
	}
71
72
	/**
73
	 * @param IL10N $l
74
	 */
75 1
	public function setL10n(IL10N $l) {
76 1
		$this->dataHelper->setL10n($l);
77 1
	}
78
79
	/**
80
	 * Add an activity to the internal array
81
	 *
82
	 * @param array $activity
83
	 */
84 12
	public function addActivity($activity) {
85 12
		$activity['activity_id'] = (int) $activity['activity_id'];
86 12
		$activity['timestamp'] = (int) $activity['timestamp'];
87 12
		$activity['object_id'] = (int) $activity['object_id'];
88 12
		$activity['object_name'] = (string) $activity['file'];
89 12
		unset($activity['priority']);
90 12
		unset($activity['file']);
91
92 12
		$event = $this->getEventFromArray(array_merge($activity, [
93 12
			'subjectparams' => [],
94
			'messageparams' => [],
95
		]));
96
97 12
		$activity['subjectparams_array'] = $this->dataHelper->getParameters($event, 'subject', $activity['subjectparams']);
98 12
		$activity['messageparams_array'] = $this->dataHelper->getParameters($event, 'message', $activity['messageparams']);
99
100 12
		$groupKey = $this->getGroupKey($activity);
101 12
		if ($groupKey === false) {
102 5
			$this->closeOpenGroup();
103 5
			$this->activities[] = $activity;
104 5
			return;
105
		}
106
107
		// Only group when the event has the same group key
108
		// and the time difference is not bigger than 3 days.
109 8
		if ($groupKey === $this->groupKey &&
110 8
			abs($activity['timestamp'] - $this->groupTime) < (3 * 24 * 60 * 60)
111 8
			&& (!isset($this->openGroup['activity_ids']) || sizeof($this->openGroup['activity_ids']) <= 5)
112
		) {
113 2
			$parameter = $this->getGroupParameter($activity);
114 2
			if ($parameter !== false) {
115
				/** @var IParameter $parameterInstance */
116 2
				$parameterInstance = $this->openGroup['subjectparams_array'][$parameter];
117
118 2
				if (!($parameterInstance instanceof Collection)) {
119 1
					$collection = $this->dataHelper->createCollection();
120 1
					$collection->addParameter($parameterInstance);
121 1
					$parameterInstance = $collection;
122
				}
123
124
				/** @var Collection $parameterInstance */
125 2
				$parameterInstance->addParameter($activity['subjectparams_array'][$parameter]);
126 2
				$this->openGroup['subjectparams_array'][$parameter] = $parameterInstance;
127
128 2
				if (!isset($this->openGroup['activity_ids'])) {
129 1
					$this->openGroup['activity_ids'] = [(int) $this->openGroup['activity_id']];
130 1
					$this->openGroup['files'] = [
131 1
						$this->openGroup['object_id'] => $this->openGroup['object_name']
132
					];
133
				}
134 2
				$this->openGroup['activity_ids'][] = (int) $activity['activity_id'];
135
136 2
				$this->openGroup['files'][$activity['object_id']] = $activity['object_name'];
137
			}
138
		} else {
139 6
			$this->closeOpenGroup();
140
141 6
			$this->groupKey = $groupKey;
142 6
			$this->groupTime = $activity['timestamp'];
143 6
			$this->openGroup = $activity;
144
		}
145 8
	}
146
147
	/**
148
	 * Closes the currently open group and adds it to the list of activities
149
	 */
150 13
	protected function closeOpenGroup() {
151 13
		if (!empty($this->openGroup)) {
152 7
			$this->activities[] = $this->openGroup;
153
		}
154
155 13
		$this->openGroup = [];
156 13
		$this->groupKey = '';
157 13
		$this->groupTime = 0;
158 13
	}
159
160
	/**
161
	 * Get grouping key for an activity
162
	 *
163
	 * @param array $activity
164
	 * @return false|string False, if grouping is not allowed, grouping key otherwise
165
	 */
166 7
	protected function getGroupKey($activity) {
167 7
		if ($this->getGroupParameter($activity) === false) {
168 4
			return false;
169
		}
170
171
		// FIXME
172
		// Non-local users are currently not distinguishable, so grouping them might
173
		// remove the information how many different users performed the same action.
174
		// So we do not group them anymore, until we found another solution.
175 4
		if ($activity['user'] === '') {
176 1
			return false;
177
		}
178
179 3
		return $activity['app'] . '|' . $activity['user'] . '|' . $activity['subject'] . '|' . $activity['object_type'];
180
	}
181
182
	/**
183
	 * Get the parameter which is the varying part
184
	 *
185
	 * @param array $activity
186
	 * @return bool|int False if the activity should not be grouped, parameter position otherwise
187
	 */
188 8
	protected function getGroupParameter($activity) {
189 8
		if (!$this->allowGrouping) {
190 2
			return false;
191
		}
192
193
		// Allow other apps to group their notifications
194 6
		return $this->activityManager->getGroupParameter($activity);
195
	}
196
197
	/**
198
	 * Get the prepared activities
199
	 *
200
	 * @return array translated activities ready for use
201
	 */
202 8
	public function getActivities() {
203 8
		$this->closeOpenGroup();
204
205 8
		$return = array();
206 8
		foreach ($this->activities as $activity) {
207 6
			$this->activityManager->setFormattingObject($activity['object_type'], $activity['object_id']);
208 6
			$activity = $this->dataHelper->formatStrings($activity, 'subject');
209 6
			$activity = $this->dataHelper->formatStrings($activity, 'message');
210
211 6
			foreach ($activity['subjectparams'] as $i => $param) {
0 ignored issues
show
Bug introduced by
The expression $activity['subjectparams'] of type string is not traversable.
Loading history...
212
				/** @var IParameter $param */
213 4
				$activity['subjectparams'][$i] = $param->getParameterInfo();
214
			}
215 6
			foreach ($activity['messageparams'] as $i => $param) {
0 ignored issues
show
Bug introduced by
The expression $activity['messageparams'] of type string is not traversable.
Loading history...
216
				/** @var IParameter $param */
217
				$activity['messageparams'][$i] = $param->getParameterInfo();
218
			}
219
220 6
			$activity['typeicon'] = $this->activityManager->getTypeIcon($activity['type']);
221 6
			$return[] = $activity;
222
		}
223 8
		$this->activityManager->setFormattingObject('', 0);
224 8
		$this->activities = [];
225
226 8
		return $return;
227
	}
228
229
	/**
230
	 * @param array $activity
231
	 * @return IEvent
232
	 */
233 6
	public function getEventFromArray(array $activity) {
234 6
		$event = $this->activityManager->generateEvent();
235 6
		$event->setApp($activity['app'])
236 6
			->setType($activity['type'])
237 6
			->setAffectedUser($activity['affecteduser'])
238 6
			->setAuthor($activity['user'])
239 6
			->setTimestamp($activity['timestamp'])
240 6
			->setSubject($activity['subject'], $activity['subjectparams'])
241 6
			->setMessage($activity['message'], $activity['messageparams'])
242 6
			->setObject($activity['object_type'], $activity['object_id'], $activity['object_name'])
243 6
			->setLink($activity['link']);
244
245 6
		return $event;
246
	}
247
}
248