Completed
Pull Request — master (#610)
by Tom
124:16 queued 122:05
created

GroupHelper::getActivities()   B

Complexity

Conditions 4
Paths 5

Size

Total Lines 26
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 4.0186

Importance

Changes 0
Metric Value
dl 0
loc 26
ccs 17
cts 19
cp 0.8947
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 16
nc 5
nop 0
crap 4.0186
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;
23
24
use OCA\Activity\Parameter\Collection;
25
use OCA\Activity\Parameter\IParameter;
26
use OCP\Activity\IEvent;
27
use OCP\Activity\IManager;
28
use OCP\IL10N;
29
30
class GroupHelper {
31
	/** @var array */
32
	protected $activities = array();
33
34
	/** @var array */
35
	protected $openGroup = array();
36
37
	/** @var string */
38
	protected $groupKey = '';
39
40
	/** @var int */
41
	protected $groupTime = 0;
42
43
	/** @var bool */
44
	protected $allowGrouping;
45
46
	/** @var \OCP\Activity\IManager */
47
	protected $activityManager;
48
49
	/** @var \OCA\Activity\DataHelper */
50
	protected $dataHelper;
51
52
	/**
53
	 * @param \OCP\Activity\IManager $activityManager
54
	 * @param \OCA\Activity\DataHelper $dataHelper
55
	 * @param bool $allowGrouping
56
	 */
57 27
	public function __construct(IManager $activityManager, DataHelper $dataHelper, $allowGrouping) {
58 27
		$this->allowGrouping = $allowGrouping;
59
60 27
		$this->activityManager = $activityManager;
61 27
		$this->dataHelper = $dataHelper;
62 27
	}
63
64
	/**
65
	 * @param string $user
66
	 */
67 2
	public function setUser($user) {
68 2
		$this->dataHelper->setUser($user);
69 2
	}
70
71
	/**
72
	 * @param IL10N $l
73
	 */
74 1
	public function setL10n(IL10N $l) {
75 1
		$this->dataHelper->setL10n($l);
76 1
	}
77
78
	/**
79
	 * Add an activity to the internal array
80
	 *
81
	 * @param array $activity
82
	 */
83 8
	public function addActivity($activity) {
84 8
		$activity['activity_id'] = (int) $activity['activity_id'];
85 8
		$activity['timestamp'] = (int) $activity['timestamp'];
86 8
		$activity['object_id'] = (int) $activity['object_id'];
87 8
		$activity['object_name'] = (string) $activity['file'];
88 8
		unset($activity['priority']);
89 8
		unset($activity['file']);
90
91 8
		$event = $this->getEventFromArray(array_merge($activity, [
92 8
			'subjectparams' => [],
93 8
			'messageparams' => [],
94 8
		]));
95
96 8
		$activity['subjectparams_array'] = $this->dataHelper->getParameters($event, 'subject', $activity['subjectparams']);
97 8
		$activity['messageparams_array'] = $this->dataHelper->getParameters($event, 'message', $activity['messageparams']);
98
99 8
		$groupKey = $this->getGroupKey($activity);
100 8
		if ($groupKey === false) {
101 2
			$this->closeOpenGroup();
102 2
			$this->activities[] = $activity;
103 2
			return;
104
		}
105
106
		// Only group when the event has the same group key
107
		// and the time difference is not bigger than 3 days.
108 6
		if ($groupKey === $this->groupKey &&
109 4
			abs($activity['timestamp'] - $this->groupTime) < (3 * 24 * 60 * 60)
110 6
			&& (!isset($this->openGroup['activity_ids']) || sizeof($this->openGroup['activity_ids']) <= 5)
111 6
		) {
112 2
			$parameter = $this->getGroupParameter($activity);
113 2
			if ($parameter !== false) {
114
				/** @var IParameter $parameterInstance */
115 2
				$parameterInstance = $this->openGroup['subjectparams_array'][$parameter];
116
117 2
				if (!($parameterInstance instanceof Collection)) {
118 1
					$collection = $this->dataHelper->createCollection();
119 1
					$collection->addParameter($parameterInstance);
120 1
					$parameterInstance = $collection;
121 1
				}
122
123
				/** @var Collection $parameterInstance */
124 2
				$parameterInstance->addParameter($activity['subjectparams_array'][$parameter]);
125 2
				$this->openGroup['subjectparams_array'][$parameter] = $parameterInstance;
126
127 2
				if (!isset($this->openGroup['activity_ids'])) {
128 1
					$this->openGroup['activity_ids'] = [(int) $this->openGroup['activity_id']];
129 1
					$this->openGroup['files'] = [
130 1
						$this->openGroup['object_id'] => $this->openGroup['object_name']
131 1
					];
132 1
				}
133 2
				$this->openGroup['activity_ids'][] = (int) $activity['activity_id'];
134
135 2
				$this->openGroup['files'][$activity['object_id']] = $activity['object_name'];
136 2
			}
137 2
		} else {
138 4
			$this->closeOpenGroup();
139
140 4
			$this->groupKey = $groupKey;
141 4
			$this->groupTime = $activity['timestamp'];
142 4
			$this->openGroup = $activity;
143
		}
144 6
	}
145
146
	/**
147
	 * Closes the currently open group and adds it to the list of activities
148
	 */
149 9
	protected function closeOpenGroup() {
150 9
		if (!empty($this->openGroup)) {
151 5
			$this->activities[] = $this->openGroup;
152 5
		}
153
154 9
		$this->openGroup = [];
155 9
		$this->groupKey = '';
156 9
		$this->groupTime = 0;
157 9
	}
158
159
	/**
160
	 * Get grouping key for an activity
161
	 *
162
	 * @param array $activity
163
	 * @return false|string False, if grouping is not allowed, grouping key otherwise
164
	 */
165 3
	protected function getGroupKey($activity) {
166 3
		if ($this->getGroupParameter($activity) === false) {
167 1
			return false;
168
		}
169
170
		// FIXME
171
		// Non-local users are currently not distinguishable, so grouping them might
172
		// remove the information how many different users performed the same action.
173
		// So we do not group them anymore, until we found another solution.
174 2
		if ($activity['user'] === '') {
175 1
			return false;
176
		}
177
178 1
		return $activity['app'] . '|' . $activity['user'] . '|' . $activity['subject'] . '|' . $activity['object_type'];
179
	}
180
181
	/**
182
	 * Get the parameter which is the varying part
183
	 *
184
	 * @param array $activity
185
	 * @return bool|int False if the activity should not be grouped, parameter position otherwise
186
	 */
187 4
	protected function getGroupParameter($activity) {
188 4
		if (!$this->allowGrouping) {
189 2
			return false;
190
		}
191
192
		// Allow other apps to group their notifications
193 2
		return $this->activityManager->getGroupParameter($activity);
194
	}
195
196
	/**
197
	 * Get the prepared activities
198
	 *
199
	 * @return array translated activities ready for use
200
	 */
201 4
	public function getActivities() {
202 4
		$this->closeOpenGroup();
203
204 4
		$return = array();
205 4
		foreach ($this->activities as $activity) {
206 2
			$this->activityManager->setFormattingObject($activity['object_type'], $activity['object_id']);
207 2
			$activity = $this->dataHelper->formatStrings($activity, 'subject');
208 2
			$activity = $this->dataHelper->formatStrings($activity, 'message');
209
210 2
			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...
211
				/** @var IParameter $param */
212
				$activity['subjectparams'][$i] = $param->getParameterInfo();
213 2
			}
214 2
			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...
215
				/** @var IParameter $param */
216
				$activity['messageparams'][$i] = $param->getParameterInfo();
217 2
			}
218
219 2
			$activity['typeicon'] = $this->activityManager->getTypeIcon($activity['type']);
220 2
			$return[] = $activity;
221 4
		}
222 4
		$this->activityManager->setFormattingObject('', 0);
223 4
		$this->activities = [];
224
225 4
		return $return;
226
	}
227
228
	/**
229
	 * @param array $activity
230
	 * @return IEvent
231
	 */
232 2
	public function getEventFromArray(array $activity) {
233 2
		$event = $this->activityManager->generateEvent();
234 2
		$event->setApp($activity['app'])
235 2
			->setType($activity['type'])
236 2
			->setAffectedUser($activity['affecteduser'])
237 2
			->setAuthor($activity['user'])
238 2
			->setTimestamp($activity['timestamp'])
239 2
			->setSubject($activity['subject'], $activity['subjectparams'])
240 2
			->setMessage($activity['message'], $activity['messageparams'])
241 2
			->setObject($activity['object_type'], $activity['object_id'], $activity['object_name'])
242 2
			->setLink($activity['link']);
243
244 2
		return $event;
245
	}
246
}
247