Completed
Push — master ( 0f6f84...dc2ec4 )
by Patrick
12s
created

GroupHelper::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 5
cts 5
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 3
crap 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;
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
		$parameterIndex = $this->getGroupParameter($activity);
167 7
		if ($parameterIndex === 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
		// the group key is based on all parameters outside of the one
180
		// pointed at by $parameterIndex as it's the varying part
181 3
		$subjectParams = json_decode($activity['subjectparams'], true);
182 3
		unset($subjectParams[$parameterIndex]);
183 3
		$paramsKey = md5(json_encode($subjectParams));
184
185 3
		return $activity['app'] . '|' . $activity['user'] . '|' . $activity['subject'] . '|' . $activity['object_type'] . '|' . $paramsKey;
186
	}
187
188
	/**
189
	 * Get the parameter which is the varying part
190
	 *
191
	 * @param array $activity
192
	 * @return bool|int False if the activity should not be grouped, parameter position otherwise
193
	 */
194 8
	protected function getGroupParameter($activity) {
195 8
		if (!$this->allowGrouping) {
196 2
			return false;
197
		}
198
199
		// Allow other apps to group their notifications
200 6
		return $this->activityManager->getGroupParameter($activity);
201
	}
202
203
	/**
204
	 * Get the prepared activities
205
	 *
206
	 * @return array translated activities ready for use
207
	 */
208 8
	public function getActivities() {
209 8
		$this->closeOpenGroup();
210
211 8
		$return = array();
212 8
		foreach ($this->activities as $activity) {
213 6
			$this->activityManager->setFormattingObject($activity['object_type'], $activity['object_id']);
214 6
			$activity = $this->dataHelper->formatStrings($activity, 'subject');
215 6
			$activity = $this->dataHelper->formatStrings($activity, 'message');
216
217 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...
218
				/** @var IParameter $param */
219 4
				$activity['subjectparams'][$i] = $param->getParameterInfo();
220 6
			}
221 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...
222
				/** @var IParameter $param */
223
				$activity['messageparams'][$i] = $param->getParameterInfo();
224 6
			}
225
226 6
			$activity['typeicon'] = $this->activityManager->getTypeIcon($activity['type']);
227 6
			$return[] = $activity;
228 8
		}
229 8
		$this->activityManager->setFormattingObject('', 0);
230 8
		$this->activities = [];
231
232 8
		return $return;
233
	}
234
235
	/**
236
	 * @param array $activity
237
	 * @return IEvent
238
	 */
239 6
	public function getEventFromArray(array $activity) {
240 6
		$event = $this->activityManager->generateEvent();
241 6
		$event->setApp($activity['app'])
242 6
			->setType($activity['type'])
243 6
			->setAffectedUser($activity['affecteduser'])
244 6
			->setAuthor($activity['user'])
245 6
			->setTimestamp($activity['timestamp'])
246 6
			->setSubject($activity['subject'], $activity['subjectparams'])
247 6
			->setMessage($activity['message'], $activity['messageparams'])
248 6
			->setObject($activity['object_type'], $activity['object_id'], $activity['object_name'])
249 6
			->setLink($activity['link']);
250
251 6
		return $event;
252
	}
253
}
254