Completed
Pull Request — master (#655)
by Thomas
132:41 queued 123:52
created

GroupHelper::closeOpenGroup()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 8
cts 8
cp 1
rs 9.9666
c 0
b 0
f 0
cc 2
nc 2
nop 0
crap 2
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 = [];
33
34
	/** @var array */
35
	protected $openGroup = [];
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'], $activity['file']);
89
		
90 12
		$event = $this->getEventFromArray(\array_merge($activity, [
91 12
			'subjectparams' => [],
92 12
			'messageparams' => [],
93 12
		]));
94
95 12
		$activity['subjectparams_array'] = $this->dataHelper->getParameters($event, 'subject', $activity['subjectparams']);
96 12
		$activity['messageparams_array'] = $this->dataHelper->getParameters($event, 'message', $activity['messageparams']);
97
98 12
		$groupKey = $this->getGroupKey($activity);
99 12
		if ($groupKey === false) {
100 5
			$this->closeOpenGroup();
101 5
			$this->activities[] = $activity;
102 5
			return;
103
		}
104
105
		// Only group when the event has the same group key
106
		// and the time difference is not bigger than 3 days.
107 8
		if ($groupKey === $this->groupKey &&
108 4
			\abs($activity['timestamp'] - $this->groupTime) < (3 * 24 * 60 * 60)
109 8
			&& (!isset($this->openGroup['activity_ids']) || \sizeof($this->openGroup['activity_ids']) <= 5)
110 8
		) {
111 2
			$parameter = $this->getGroupParameter($activity);
112 2
			if ($parameter !== false) {
113
				/** @var IParameter $parameterInstance */
114 2
				$parameterInstance = $this->openGroup['subjectparams_array'][$parameter];
115
116 2
				if (!($parameterInstance instanceof Collection)) {
117 1
					$collection = $this->dataHelper->createCollection();
118 1
					$collection->addParameter($parameterInstance);
119 1
					$parameterInstance = $collection;
120 1
				}
121
122
				/** @var Collection $parameterInstance */
123 2
				$parameterInstance->addParameter($activity['subjectparams_array'][$parameter]);
124 2
				$this->openGroup['subjectparams_array'][$parameter] = $parameterInstance;
125
126 2
				if (!isset($this->openGroup['activity_ids'])) {
127 1
					$this->openGroup['activity_ids'] = [(int) $this->openGroup['activity_id']];
128 1
					$this->openGroup['files'] = [
129 1
						$this->openGroup['object_id'] => $this->openGroup['object_name']
130 1
					];
131 1
				}
132 2
				$this->openGroup['activity_ids'][] = (int) $activity['activity_id'];
133
134 2
				$this->openGroup['files'][$activity['object_id']] = $activity['object_name'];
135 2
			}
136 2
		} else {
137 6
			$this->closeOpenGroup();
138
139 6
			$this->groupKey = $groupKey;
140 6
			$this->groupTime = $activity['timestamp'];
141 6
			$this->openGroup = $activity;
142
		}
143 8
	}
144
145
	/**
146
	 * Closes the currently open group and adds it to the list of activities
147
	 */
148 13
	protected function closeOpenGroup() {
149 13
		if (!empty($this->openGroup)) {
150 7
			$this->activities[] = $this->openGroup;
151 7
		}
152
153 13
		$this->openGroup = [];
154 13
		$this->groupKey = '';
155 13
		$this->groupTime = 0;
156 13
	}
157
158
	/**
159
	 * Get grouping key for an activity
160
	 *
161
	 * @param array $activity
162
	 * @return false|string False, if grouping is not allowed, grouping key otherwise
163
	 */
164 7
	protected function getGroupKey($activity) {
165 7
		$parameterIndex = $this->getGroupParameter($activity);
166 7
		if ($parameterIndex === 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
		// the group key is based on all parameters outside of the one
179
		// pointed at by $parameterIndex as it's the varying part
180 3
		$subjectParams = \json_decode($activity['subjectparams'], true);
181 3
		unset($subjectParams[$parameterIndex]);
182 3
		$paramsKey = \md5(\json_encode($subjectParams));
183
184 3
		return $activity['app'] . '|' . $activity['user'] . '|' . $activity['subject'] . '|' . $activity['object_type'] . '|' . $paramsKey;
185
	}
186
187
	/**
188
	 * Get the parameter which is the varying part
189
	 *
190
	 * @param array $activity
191
	 * @return bool|int False if the activity should not be grouped, parameter position otherwise
192
	 */
193 8
	protected function getGroupParameter($activity) {
194 8
		if (!$this->allowGrouping) {
195 2
			return false;
196
		}
197
198
		// Allow other apps to group their notifications
199 6
		return $this->activityManager->getGroupParameter($activity);
200
	}
201
202
	/**
203
	 * Get the prepared activities
204
	 *
205
	 * @return array translated activities ready for use
206
	 */
207 8
	public function getActivities() {
208 8
		$this->closeOpenGroup();
209
210 8
		$return = [];
211 8
		foreach ($this->activities as $activity) {
212 6
			$this->activityManager->setFormattingObject($activity['object_type'], $activity['object_id']);
213 6
			$activity = $this->dataHelper->formatStrings($activity, 'subject');
214 6
			$activity = $this->dataHelper->formatStrings($activity, 'message');
215
216 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...
217
				/** @var IParameter $param */
218 4
				$activity['subjectparams'][$i] = $param->getParameterInfo();
219 6
			}
220 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...
221
				/** @var IParameter $param */
222
				$activity['messageparams'][$i] = $param->getParameterInfo();
223 6
			}
224
225 6
			$activity['typeicon'] = $this->activityManager->getTypeIcon($activity['type']);
226 6
			$return[] = $activity;
227 8
		}
228 8
		$this->activityManager->setFormattingObject('', 0);
229 8
		$this->activities = [];
230
231 8
		return $return;
232
	}
233
234
	/**
235
	 * @param array $activity
236
	 * @return IEvent
237
	 */
238 6
	public function getEventFromArray(array $activity) {
239 6
		$event = $this->activityManager->generateEvent();
240 6
		$event->setApp($activity['app'])
241 6
			->setType($activity['type'])
242 6
			->setAffectedUser($activity['affecteduser'])
243 6
			->setAuthor($activity['user'])
244 6
			->setTimestamp($activity['timestamp'])
245 6
			->setSubject($activity['subject'], $activity['subjectparams'])
246 6
			->setMessage($activity['message'], $activity['messageparams'])
247 6
			->setObject($activity['object_type'], $activity['object_id'], $activity['object_name'])
248 6
			->setLink($activity['link']);
249
250 6
		return $event;
251
	}
252
}
253