Completed
Push — master ( 0c06e3...0bb60e )
by
unknown
12s
created

GroupHelper::getActivities()   B

Complexity

Conditions 4
Paths 5

Size

Total Lines 26
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 4.0023

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 26
ccs 18
cts 19
cp 0.9474
rs 8.5806
cc 4
eloc 16
nc 5
nop 0
crap 4.0023
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 31
	public function __construct(IManager $activityManager, DataHelper $dataHelper, $allowGrouping) {
58 31
		$this->allowGrouping = $allowGrouping;
59
60 31
		$this->activityManager = $activityManager;
61 31
		$this->dataHelper = $dataHelper;
62 31
	}
63
64
	/**
65
	 * @param string $user
66
	 */
67 6
	public function setUser($user) {
68 6
		$this->dataHelper->setUser($user);
69 6
	}
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 12
	public function addActivity($activity) {
84 12
		$activity['activity_id'] = (int) $activity['activity_id'];
85 12
		$activity['timestamp'] = (int) $activity['timestamp'];
86 12
		$activity['object_id'] = (int) $activity['object_id'];
87 12
		$activity['object_name'] = (string) $activity['file'];
88 12
		unset($activity['priority']);
89 12
		unset($activity['file']);
90
91 12
		$event = $this->getEventFromArray(array_merge($activity, [
92 12
			'subjectparams' => [],
93 12
			'messageparams' => [],
94 12
		]));
95
96 12
		$activity['subjectparams_array'] = $this->dataHelper->getParameters($event, 'subject', $activity['subjectparams']);
97 12
		$activity['messageparams_array'] = $this->dataHelper->getParameters($event, 'message', $activity['messageparams']);
98
99 12
		$groupKey = $this->getGroupKey($activity);
100 12
		if ($groupKey === false) {
101 5
			$this->closeOpenGroup();
102 5
			$this->activities[] = $activity;
103 5
			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 8
		if ($groupKey === $this->groupKey &&
109 4
			abs($activity['timestamp'] - $this->groupTime) < (3 * 24 * 60 * 60)
110 8
			&& (!isset($this->openGroup['activity_ids']) || sizeof($this->openGroup['activity_ids']) <= 5)
111 8
		) {
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 6
			$this->closeOpenGroup();
139
140 6
			$this->groupKey = $groupKey;
141 6
			$this->groupTime = $activity['timestamp'];
142 6
			$this->openGroup = $activity;
143
		}
144 8
	}
145
146
	/**
147
	 * Closes the currently open group and adds it to the list of activities
148
	 */
149 13
	protected function closeOpenGroup() {
150 13
		if (!empty($this->openGroup)) {
151 7
			$this->activities[] = $this->openGroup;
152 7
		}
153
154 13
		$this->openGroup = [];
155 13
		$this->groupKey = '';
156 13
		$this->groupTime = 0;
157 13
	}
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 7
	protected function getGroupKey($activity) {
166 7
		if ($this->getGroupParameter($activity) === false) {
167 4
			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 4
		if ($activity['user'] === '') {
175 1
			return false;
176
		}
177
178 3
		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 8
	protected function getGroupParameter($activity) {
188 8
		if (!$this->allowGrouping) {
189 2
			return false;
190
		}
191
192
		// Allow other apps to group their notifications
193 6
		return $this->activityManager->getGroupParameter($activity);
194
	}
195
196
	/**
197
	 * Get the prepared activities
198
	 *
199
	 * @return array translated activities ready for use
200
	 */
201 8
	public function getActivities() {
202 8
		$this->closeOpenGroup();
203
204 8
		$return = array();
205 8
		foreach ($this->activities as $activity) {
206 6
			$this->activityManager->setFormattingObject($activity['object_type'], $activity['object_id']);
207 6
			$activity = $this->dataHelper->formatStrings($activity, 'subject');
208 6
			$activity = $this->dataHelper->formatStrings($activity, 'message');
209
210 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...
211
				/** @var IParameter $param */
212 4
				$activity['subjectparams'][$i] = $param->getParameterInfo();
213 6
			}
214 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...
215
				/** @var IParameter $param */
216
				$activity['messageparams'][$i] = $param->getParameterInfo();
217 6
			}
218
219 6
			$activity['typeicon'] = $this->activityManager->getTypeIcon($activity['type']);
220 6
			$return[] = $activity;
221 8
		}
222 8
		$this->activityManager->setFormattingObject('', 0);
223 8
		$this->activities = [];
224
225 8
		return $return;
226
	}
227
228
	/**
229
	 * @param array $activity
230
	 * @return IEvent
231
	 */
232 6
	public function getEventFromArray(array $activity) {
233 6
		$event = $this->activityManager->generateEvent();
234 6
		$event->setApp($activity['app'])
235 6
			->setType($activity['type'])
236 6
			->setAffectedUser($activity['affecteduser'])
237 6
			->setAuthor($activity['user'])
238 6
			->setTimestamp($activity['timestamp'])
239 6
			->setSubject($activity['subject'], $activity['subjectparams'])
240 6
			->setMessage($activity['message'], $activity['messageparams'])
241 6
			->setObject($activity['object_type'], $activity['object_id'], $activity['object_name'])
242 6
			->setLink($activity['link']);
243
244 6
		return $event;
245
	}
246
}
247