-
Notifications
You must be signed in to change notification settings - Fork 66
/
Data.php
executable file
·402 lines (360 loc) · 13.2 KB
/
Data.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
<?php
/**
* @copyright Copyright (c) 2016, ownCloud, Inc.
*
* @author Frank Karlitschek <frank@karlitschek.de>
* @author Joas Schilling <coding@schilljs.com>
* @author Thomas Müller <thomas.mueller@tmit.eu>
*
* @license AGPL-3.0
*
* This code is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License, version 3,
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/
namespace OCA\Activity;
use Doctrine\DBAL\Platforms\MySqlPlatform;
use OCP\Activity\IEvent;
use OCP\Activity\IExtension;
use OCP\Activity\IFilter;
use OCP\Activity\IManager;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\IDBConnection;
use OCP\IL10N;
/**
* @brief Class for managing the data in the activities
*/
class Data {
/** @var IManager */
protected $activityManager;
/** @var IDBConnection */
protected $connection;
/**
* @param IManager $activityManager
* @param IDBConnection $connection
*/
public function __construct(IManager $activityManager, IDBConnection $connection) {
$this->activityManager = $activityManager;
$this->connection = $connection;
}
/**
* Send an event into the activity stream
*
* @param IEvent $event
* @return bool
*/
public function send(IEvent $event) {
if ($event->getAffectedUser() === '' || $event->getAffectedUser() === null) {
return false;
}
// store in DB
$queryBuilder = $this->connection->getQueryBuilder();
$queryBuilder->insert('activity')
->values([
'app' => $queryBuilder->createParameter('app'),
'subject' => $queryBuilder->createParameter('subject'),
'subjectparams' => $queryBuilder->createParameter('subjectparams'),
'message' => $queryBuilder->createParameter('message'),
'messageparams' => $queryBuilder->createParameter('messageparams'),
'file' => $queryBuilder->createParameter('object_name'),
'link' => $queryBuilder->createParameter('link'),
'user' => $queryBuilder->createParameter('user'),
'affecteduser' => $queryBuilder->createParameter('affecteduser'),
'timestamp' => $queryBuilder->createParameter('timestamp'),
'priority' => $queryBuilder->createParameter('priority'),
'type' => $queryBuilder->createParameter('type'),
'object_type' => $queryBuilder->createParameter('object_type'),
'object_id' => $queryBuilder->createParameter('object_id'),
])
->setParameters([
'app' => $event->getApp(),
'type' => $event->getType(),
'affecteduser' => $event->getAffectedUser(),
'user' => $event->getAuthor(),
'timestamp' => (int) $event->getTimestamp(),
'subject' => $event->getSubject(),
'subjectparams' => json_encode($event->getSubjectParameters()),
'message' => $event->getMessage(),
'messageparams' => json_encode($event->getMessageParameters()),
'priority' => IExtension::PRIORITY_MEDIUM,
'object_type' => $event->getObjectType(),
'object_id' => (int) $event->getObjectId(),
'object_name' => $event->getObjectName(),
'link' => $event->getLink(),
])
->execute();
return true;
}
/**
* Send an event as email
*
* @param IEvent $event
* @param int $latestSendTime Activity $timestamp + batch setting of $affectedUser
* @return bool
*/
public function storeMail(IEvent $event, int $latestSendTime): bool {
$affectedUser = $event->getAffectedUser();
if ($affectedUser === '' || $affectedUser === null) {
return false;
}
$query = $this->connection->getQueryBuilder();
$query->insert('activity_mq')
->values([
'amq_appid' => $query->createNamedParameter($event->getApp()),
'amq_subject' => $query->createNamedParameter($event->getSubject()),
'amq_subjectparams' => $query->createNamedParameter(json_encode($event->getSubjectParameters())),
'amq_affecteduser' => $query->createNamedParameter($affectedUser),
'amq_timestamp' => $query->createNamedParameter((int) $event->getTimestamp()),
'amq_type' => $query->createNamedParameter($event->getType()),
'amq_latest_send' => $query->createNamedParameter($latestSendTime),
'object_type' => $query->createNamedParameter($event->getObjectType()),
'object_id' => $query->createNamedParameter($event->getObjectId()),
]);
$query->execute();
return true;
}
/**
* Read a list of events from the activity stream
*
* @param GroupHelper $groupHelper Allows activities to be grouped
* @param UserSettings $userSettings Gets the settings of the user
* @param string $user User for whom we display the stream
*
* @param int $since The integer ID of the last activity that has been seen.
* @param int $limit How many activities should be returned
* @param string $sort Should activities be given ascending or descending
*
* @param string $filter Filter the activities
* @param string $objectType Allows to filter the activities to a given object. May only appear together with $objectId
* @param int $objectId Allows to filter the activities to a given object. May only appear together with $objectType
*
* @return array
*
* @throws \OutOfBoundsException if the user (Code: 1) or the since (Code: 2) is invalid
* @throws \BadMethodCallException if the user has selected to display no types for this filter (Code: 3)
*/
public function get(GroupHelper $groupHelper, UserSettings $userSettings, $user, $since, $limit, $sort, $filter, $objectType = '', $objectId = 0) {
// get current user
if ($user === '') {
throw new \OutOfBoundsException('Invalid user', 1);
}
$activeFilter = null;
try {
$activeFilter = $this->activityManager->getFilterById($filter);
} catch (\InvalidArgumentException $e) {
// Unknown filter => ignore and show all activities
}
$enabledNotifications = $userSettings->getNotificationTypes($user, 'stream');
if ($activeFilter instanceof IFilter) {
$enabledNotifications = $activeFilter->filterTypes($enabledNotifications);
}
$enabledNotifications = array_unique($enabledNotifications);
// We don't want to display any activities
if (empty($enabledNotifications)) {
throw new \BadMethodCallException('No settings enabled', 3);
}
$query = $this->connection->getQueryBuilder();
$query->select('*')
->from('activity');
$query->where($query->expr()->eq('affecteduser', $query->createNamedParameter($user)))
->andWhere($query->expr()->in('type', $query->createNamedParameter($enabledNotifications, IQueryBuilder::PARAM_STR_ARRAY)));
if ($filter === 'self') {
$query->andWhere($query->expr()->eq('user', $query->createNamedParameter($user)));
} else if ($filter === 'by') {
$query->andWhere($query->expr()->neq('user', $query->createNamedParameter($user)));
} else if ($filter === 'all' && !$userSettings->getUserSetting($user, 'setting', 'self')) {
$query->andWhere($query->expr()->orX(
$query->expr()->neq('user', $query->createNamedParameter($user)),
$query->expr()->notIn('type', $query->createNamedParameter([
'file_created',
'file_changed',
'file_deleted',
'file_restored',
], IQueryBuilder::PARAM_STR_ARRAY))
));
} else if ($filter === 'filter') {
if (!$userSettings->getUserSetting($user, 'setting', 'self')) {
$query->andWhere($query->expr()->orX(
$query->expr()->neq('user', $query->createNamedParameter($user)),
$query->expr()->notIn('type', $query->createNamedParameter([
'file_created',
'file_changed',
'file_deleted',
'file_restored',
], IQueryBuilder::PARAM_STR_ARRAY))
));
}
$query->andWhere($query->expr()->eq('object_type', $query->createNamedParameter($objectType)));
$query->andWhere($query->expr()->eq('object_id', $query->createNamedParameter($objectId)));
}
if ($activeFilter instanceof IFilter) {
$apps = $activeFilter->allowedApps();
if (!empty($apps)) {
$query->andWhere($query->expr()->in('app', $query->createNamedParameter($apps, IQueryBuilder::PARAM_STR_ARRAY)));
}
}
if (
$filter === 'files_favorites' ||
(in_array($filter, ['all', 'by', 'self']) && $userSettings->getUserSetting($user, 'stream', 'files_favorites'))
) {
try {
$favoriteFilter = $this->activityManager->getFilterById('files_favorites');
/** @var \OCA\Files\Activity\Filter\Favorites $favoriteFilter */
$favoriteFilter->filterFavorites($query);
} catch (\InvalidArgumentException $e) {
}
}
/**
* Order and specify the offset
*/
$sqlSort = ($sort === 'asc') ? 'ASC' : 'DESC';
$headers = $this->setOffsetFromSince($query, $user, $since, $sqlSort);
$query->orderBy('timestamp', $sqlSort)
->addOrderBy('activity_id', $sqlSort);
$query->setMaxResults($limit + 1);
$result = $query->execute();
$hasMore = false;
while ($row = $result->fetch()) {
if ($limit === 0) {
$hasMore = true;
break;
}
$headers['X-Activity-Last-Given'] = (int) $row['activity_id'];
$groupHelper->addActivity($row);
$limit--;
}
$result->closeCursor();
return ['data' => $groupHelper->getActivities(), 'has_more' => $hasMore, 'headers' => $headers];
}
/**
* @param IQueryBuilder $query
* @param string $user
* @param int $since
* @param string $sort
*
* @return array Headers that should be set on the response
*
* @throws \OutOfBoundsException If $since is not owned by $user
*/
protected function setOffsetFromSince(IQueryBuilder $query, $user, $since, $sort) {
if ($since) {
$queryBuilder = $this->connection->getQueryBuilder();
$queryBuilder->select(['affecteduser', 'timestamp'])
->from('activity')
->where($queryBuilder->expr()->eq('activity_id', $queryBuilder->createNamedParameter((int) $since)));
$result = $queryBuilder->execute();
$activity = $result->fetch();
$result->closeCursor();
if ($activity) {
if ($activity['affecteduser'] !== $user) {
throw new \OutOfBoundsException('Invalid since', 2);
}
$timestamp = (int) $activity['timestamp'];
if ($sort === 'DESC') {
$query->andWhere($query->expr()->lte('timestamp', $query->createNamedParameter($timestamp)));
$query->andWhere($query->expr()->lt('activity_id', $query->createNamedParameter($since)));
} else {
$query->andWhere($query->expr()->gte('timestamp', $query->createNamedParameter($timestamp)));
$query->andWhere($query->expr()->gt('activity_id', $query->createNamedParameter($since)));
}
return [];
}
}
/**
* Couldn't find the since, so find the oldest one and set the header
*/
$fetchQuery = $this->connection->getQueryBuilder();
$fetchQuery->select('activity_id')
->from('activity')
->where($fetchQuery->expr()->eq('affecteduser', $fetchQuery->createNamedParameter($user)))
->orderBy('timestamp', $sort)
->setMaxResults(1);
$result = $fetchQuery->execute();
$activity = $result->fetch();
$result->closeCursor();
if ($activity !== false) {
return [
'X-Activity-First-Known' => (int) $activity['activity_id'],
];
}
return [];
}
/**
* Verify that the filter is valid
*
* @param string $filterValue
* @return string
*/
public function validateFilter($filterValue) {
if (!isset($filterValue)) {
return 'all';
}
switch ($filterValue) {
case 'filter':
return $filterValue;
default:
try {
$this->activityManager->getFilterById($filterValue);
return $filterValue;
} catch (\InvalidArgumentException $e) {
return 'all';
}
}
}
/**
* Delete old events
*
* @param int $expireDays Minimum 1 day
*/
public function expire($expireDays = 365) {
$ttl = (60 * 60 * 24 * max(1, $expireDays));
$timelimit = time() - $ttl;
$this->deleteActivities(array(
'timestamp' => array($timelimit, '<'),
));
}
/**
* Delete activities that match certain conditions
*
* @param array $conditions Array with conditions that have to be met
* 'field' => 'value' => `field` = 'value'
* 'field' => array('value', 'operator') => `field` operator 'value'
*/
public function deleteActivities($conditions) {
$sqlWhere = '';
$sqlParameters = $sqlWhereList = array();
foreach ($conditions as $column => $comparison) {
$sqlWhereList[] = " `$column` " . ((is_array($comparison) && isset($comparison[1])) ? $comparison[1] : '=') . ' ? ';
$sqlParameters[] = (is_array($comparison)) ? $comparison[0] : $comparison;
}
if (!empty($sqlWhereList)) {
$sqlWhere = ' WHERE ' . implode(' AND ', $sqlWhereList);
}
// Add galera safe delete chunking if using mysql
// Stops us hitting wsrep_max_ws_rows when large row counts are deleted
if($this->connection->getDatabasePlatform() instanceof MySqlPlatform) {
// Then use chunked delete
$max = 100000;
$query = $this->connection->prepare(
'DELETE FROM `*PREFIX*activity`' . $sqlWhere . " LIMIT " . $max);
do {
$query->execute($sqlParameters);
$deleted = $query->rowCount();
} while($deleted === $max);
} else {
// Dont use chunked delete - let the DB handle the large row count natively
$query = $this->connection->prepare(
'DELETE FROM `*PREFIX*activity`' . $sqlWhere);
$query->execute($sqlParameters);
}
}
}