2013-08-28 19:36:48 +04:00
|
|
|
<?php
|
2023-09-11 15:57:20 +03:00
|
|
|
|
|
|
|
declare(strict_types=1);
|
2013-08-28 19:36:48 +04:00
|
|
|
/**
|
2024-05-02 15:24:56 +03:00
|
|
|
* SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
|
2024-05-01 18:21:16 +03:00
|
|
|
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
2013-08-28 19:36:48 +04:00
|
|
|
*/
|
|
|
|
namespace OCA\Activity;
|
|
|
|
|
2021-01-21 10:27:06 +03:00
|
|
|
use Doctrine\DBAL\Platforms\MySQLPlatform;
|
2020-07-21 18:58:46 +03:00
|
|
|
use OCA\Activity\Filter\AllFilter;
|
2024-04-17 16:45:29 +03:00
|
|
|
use OCP\Activity\Exceptions\FilterNotFoundException;
|
2015-08-20 14:03:59 +03:00
|
|
|
use OCP\Activity\IEvent;
|
2014-12-03 14:27:07 +03:00
|
|
|
use OCP\Activity\IExtension;
|
2016-10-20 19:02:18 +03:00
|
|
|
use OCP\Activity\IFilter;
|
2015-08-14 13:32:49 +03:00
|
|
|
use OCP\Activity\IManager;
|
2015-11-04 11:26:52 +03:00
|
|
|
use OCP\DB\QueryBuilder\IQueryBuilder;
|
2015-08-14 13:32:49 +03:00
|
|
|
use OCP\IDBConnection;
|
2023-09-11 15:57:20 +03:00
|
|
|
use Psr\Log\LoggerInterface;
|
2014-07-14 15:13:23 +04:00
|
|
|
|
2013-08-28 19:36:48 +04:00
|
|
|
/**
|
2013-09-17 16:27:41 +04:00
|
|
|
* @brief Class for managing the data in the activities
|
|
|
|
*/
|
2015-02-05 14:45:40 +03:00
|
|
|
class Data {
|
2014-07-14 15:13:23 +04:00
|
|
|
|
2023-10-06 18:35:14 +03:00
|
|
|
/** @var */
|
|
|
|
protected ?IQueryBuilder $insertActivity = null;
|
|
|
|
protected ?IQueryBuilder $insertMail = null;
|
2015-08-14 13:32:49 +03:00
|
|
|
|
2023-10-06 18:35:14 +03:00
|
|
|
public function __construct(
|
|
|
|
protected IManager $activityManager,
|
|
|
|
protected IDBConnection $connection,
|
|
|
|
protected LoggerInterface $logger) {
|
2014-07-14 15:13:23 +04:00
|
|
|
}
|
|
|
|
|
2013-08-28 19:36:48 +04:00
|
|
|
/**
|
2014-06-04 19:31:54 +04:00
|
|
|
* Send an event into the activity stream
|
|
|
|
*
|
2015-08-20 14:03:59 +03:00
|
|
|
* @param IEvent $event
|
2020-06-17 15:40:20 +03:00
|
|
|
* @return int
|
2013-08-28 19:36:48 +04:00
|
|
|
*/
|
2020-06-17 15:40:20 +03:00
|
|
|
public function send(IEvent $event): int {
|
2021-01-22 10:44:14 +03:00
|
|
|
if ($event->getAffectedUser() === '') {
|
2020-07-21 19:39:15 +03:00
|
|
|
return 0;
|
2013-10-02 17:32:27 +04:00
|
|
|
}
|
2013-08-28 19:36:48 +04:00
|
|
|
|
2022-02-01 20:29:22 +03:00
|
|
|
if ($this->insertActivity === null) {
|
|
|
|
$this->insertActivity = $this->connection->getQueryBuilder();
|
|
|
|
$this->insertActivity->insert('activity')
|
|
|
|
->values([
|
|
|
|
'app' => $this->insertActivity->createParameter('app'),
|
|
|
|
'subject' => $this->insertActivity->createParameter('subject'),
|
|
|
|
'subjectparams' => $this->insertActivity->createParameter('subjectparams'),
|
|
|
|
'message' => $this->insertActivity->createParameter('message'),
|
|
|
|
'messageparams' => $this->insertActivity->createParameter('messageparams'),
|
|
|
|
'file' => $this->insertActivity->createParameter('object_name'),
|
|
|
|
'link' => $this->insertActivity->createParameter('link'),
|
|
|
|
'user' => $this->insertActivity->createParameter('user'),
|
|
|
|
'affecteduser' => $this->insertActivity->createParameter('affecteduser'),
|
|
|
|
'timestamp' => $this->insertActivity->createParameter('timestamp'),
|
|
|
|
'priority' => $this->insertActivity->createParameter('priority'),
|
|
|
|
'type' => $this->insertActivity->createParameter('type'),
|
|
|
|
'object_type' => $this->insertActivity->createParameter('object_type'),
|
|
|
|
'object_id' => $this->insertActivity->createParameter('object_id'),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2013-08-28 19:36:48 +04:00
|
|
|
// store in DB
|
2022-02-01 20:29:22 +03:00
|
|
|
$this->insertActivity->setParameters([
|
|
|
|
'app' => $event->getApp(),
|
|
|
|
'type' => $event->getType(),
|
|
|
|
'affecteduser' => $event->getAffectedUser(),
|
|
|
|
'user' => $event->getAuthor(),
|
|
|
|
'timestamp' => $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' => $event->getObjectId(),
|
|
|
|
'object_name' => $event->getObjectName(),
|
|
|
|
'link' => $event->getLink(),
|
|
|
|
]);
|
|
|
|
$this->insertActivity->executeStatement();
|
|
|
|
|
|
|
|
return $this->insertActivity->getLastInsertId();
|
2013-08-28 19:36:48 +04:00
|
|
|
}
|
|
|
|
|
2014-04-16 18:51:47 +04:00
|
|
|
/**
|
2015-08-19 18:39:38 +03:00
|
|
|
* Send an event as email
|
2014-04-16 18:51:47 +04:00
|
|
|
*
|
2015-08-20 14:03:59 +03:00
|
|
|
* @param IEvent $event
|
2020-08-14 04:29:04 +03:00
|
|
|
* @param int $latestSendTime Activity $timestamp + batch setting of $affectedUser
|
2014-04-16 18:51:47 +04:00
|
|
|
* @return bool
|
|
|
|
*/
|
2018-10-11 13:11:30 +03:00
|
|
|
public function storeMail(IEvent $event, int $latestSendTime): bool {
|
|
|
|
$affectedUser = $event->getAffectedUser();
|
2021-01-22 10:44:14 +03:00
|
|
|
if ($affectedUser === '') {
|
2015-08-20 14:03:59 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-02-01 20:29:22 +03:00
|
|
|
if ($this->insertMail === null) {
|
|
|
|
$this->insertMail = $this->connection->getQueryBuilder();
|
|
|
|
$this->insertMail->insert('activity_mq')
|
|
|
|
->values([
|
|
|
|
'amq_appid' => $this->insertMail->createParameter('amq_appid'),
|
|
|
|
'amq_subject' => $this->insertMail->createParameter('amq_subject'),
|
|
|
|
'amq_subjectparams' => $this->insertMail->createParameter('amq_subjectparams'),
|
|
|
|
'amq_affecteduser' => $this->insertMail->createParameter('amq_affecteduser'),
|
|
|
|
'amq_timestamp' => $this->insertMail->createParameter('amq_timestamp'),
|
|
|
|
'amq_type' => $this->insertMail->createParameter('amq_type'),
|
|
|
|
'amq_latest_send' => $this->insertMail->createParameter('amq_latest_send'),
|
|
|
|
'object_type' => $this->insertMail->createParameter('object_type'),
|
|
|
|
'object_id' => $this->insertMail->createParameter('object_id'),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->insertMail->setParameters([
|
|
|
|
'amq_appid' => $event->getApp(),
|
|
|
|
'amq_subject' => $event->getSubject(),
|
|
|
|
'amq_subjectparams' => json_encode($event->getSubjectParameters()),
|
|
|
|
'amq_affecteduser' => $affectedUser,
|
|
|
|
'amq_timestamp' => $event->getTimestamp(),
|
|
|
|
'amq_type' => $event->getType(),
|
|
|
|
'amq_latest_send' => $latestSendTime,
|
|
|
|
'object_type' => $event->getObjectType(),
|
|
|
|
'object_id' => $event->getObjectId(),
|
|
|
|
]);
|
|
|
|
|
|
|
|
$this->insertMail->executeStatement();
|
2014-04-16 18:51:47 +04:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-11-04 11:26:52 +03:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*
|
2020-08-14 04:29:04 +03:00
|
|
|
* @param bool $returnEvents return only the events
|
2015-11-04 11:26:52 +03:00
|
|
|
* @return array
|
|
|
|
*
|
|
|
|
*/
|
2020-08-14 04:29:04 +03:00
|
|
|
public function get(GroupHelper $groupHelper, UserSettings $userSettings, $user, $since, $limit, $sort, $filter, $objectType = '', $objectId = 0, bool $returnEvents = false) {
|
2015-11-04 11:26:52 +03:00
|
|
|
// get current user
|
|
|
|
if ($user === '') {
|
|
|
|
throw new \OutOfBoundsException('Invalid user', 1);
|
|
|
|
}
|
|
|
|
|
2022-06-02 13:29:07 +03:00
|
|
|
$limit = min(200, $limit);
|
|
|
|
|
2016-10-20 19:02:18 +03:00
|
|
|
$activeFilter = null;
|
|
|
|
try {
|
|
|
|
$activeFilter = $this->activityManager->getFilterById($filter);
|
2024-04-17 16:45:29 +03:00
|
|
|
} catch (FilterNotFoundException) {
|
2016-10-20 19:02:18 +03:00
|
|
|
// Unknown filter => ignore and show all activities
|
|
|
|
}
|
|
|
|
|
2015-11-04 11:26:52 +03:00
|
|
|
$query = $this->connection->getQueryBuilder();
|
|
|
|
$query->select('*')
|
|
|
|
->from('activity');
|
|
|
|
|
2020-07-21 18:58:46 +03:00
|
|
|
$query->where($query->expr()->eq('affecteduser', $query->createNamedParameter($user)));
|
|
|
|
|
|
|
|
if ($activeFilter instanceof IFilter && !($activeFilter instanceof AllFilter)) {
|
|
|
|
$notificationTypes = $userSettings->getNotificationTypes();
|
|
|
|
$notificationTypes = $activeFilter->filterTypes($notificationTypes);
|
|
|
|
$notificationTypes = array_unique($notificationTypes);
|
|
|
|
|
|
|
|
$query->andWhere($query->expr()->in('type', $query->createNamedParameter($notificationTypes, IQueryBuilder::PARAM_STR_ARRAY)));
|
|
|
|
}
|
|
|
|
|
2015-11-04 11:26:52 +03:00
|
|
|
if ($filter === 'self') {
|
|
|
|
$query->andWhere($query->expr()->eq('user', $query->createNamedParameter($user)));
|
2021-01-22 10:19:22 +03:00
|
|
|
} elseif ($filter === 'by') {
|
2015-11-04 11:26:52 +03:00
|
|
|
$query->andWhere($query->expr()->neq('user', $query->createNamedParameter($user)));
|
2021-01-22 10:19:22 +03:00
|
|
|
} elseif ($filter === 'filter') {
|
2015-11-04 11:26:52 +03:00
|
|
|
$query->andWhere($query->expr()->eq('object_type', $query->createNamedParameter($objectType)));
|
|
|
|
$query->andWhere($query->expr()->eq('object_id', $query->createNamedParameter($objectId)));
|
|
|
|
}
|
|
|
|
|
2016-10-20 19:02:18 +03:00
|
|
|
if ($activeFilter instanceof IFilter) {
|
|
|
|
$apps = $activeFilter->allowedApps();
|
|
|
|
if (!empty($apps)) {
|
|
|
|
$query->andWhere($query->expr()->in('app', $query->createNamedParameter($apps, IQueryBuilder::PARAM_STR_ARRAY)));
|
2015-11-04 11:26:52 +03:00
|
|
|
}
|
2016-10-20 19:02:18 +03:00
|
|
|
}
|
2015-11-04 11:26:52 +03:00
|
|
|
|
2016-10-20 19:02:18 +03:00
|
|
|
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);
|
2024-04-17 16:45:29 +03:00
|
|
|
} catch (FilterNotFoundException) {
|
2016-10-20 19:02:18 +03:00
|
|
|
}
|
2015-11-04 11:26:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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);
|
|
|
|
|
2024-01-17 14:22:34 +03:00
|
|
|
$result = $query->executeQuery();
|
2015-11-04 11:26:52 +03:00
|
|
|
$hasMore = false;
|
|
|
|
while ($row = $result->fetch()) {
|
|
|
|
if ($limit === 0) {
|
|
|
|
$hasMore = true;
|
|
|
|
break;
|
|
|
|
}
|
2020-08-14 04:29:04 +03:00
|
|
|
$headers['X-Activity-Last-Given'] = (int)$row['activity_id'];
|
2015-11-04 11:26:52 +03:00
|
|
|
$groupHelper->addActivity($row);
|
|
|
|
$limit--;
|
|
|
|
}
|
|
|
|
$result->closeCursor();
|
|
|
|
|
2020-08-14 04:29:04 +03:00
|
|
|
if ($returnEvents) {
|
|
|
|
return $groupHelper->getEvents();
|
|
|
|
} else {
|
|
|
|
return ['data' => $groupHelper->getActivities(), 'has_more' => $hasMore, 'headers' => $headers];
|
|
|
|
}
|
2015-11-04 11:26:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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();
|
2016-12-19 18:57:41 +03:00
|
|
|
$queryBuilder->select(['affecteduser', 'timestamp'])
|
2015-11-04 11:26:52 +03:00
|
|
|
->from('activity')
|
2020-08-14 04:29:04 +03:00
|
|
|
->where($queryBuilder->expr()->eq('activity_id', $queryBuilder->createNamedParameter((int)$since)));
|
2024-01-17 14:22:34 +03:00
|
|
|
$result = $queryBuilder->executeQuery();
|
2015-11-04 11:26:52 +03:00
|
|
|
$activity = $result->fetch();
|
|
|
|
$result->closeCursor();
|
|
|
|
|
|
|
|
if ($activity) {
|
|
|
|
if ($activity['affecteduser'] !== $user) {
|
|
|
|
throw new \OutOfBoundsException('Invalid since', 2);
|
|
|
|
}
|
2020-08-14 04:29:04 +03:00
|
|
|
$timestamp = (int)$activity['timestamp'];
|
2015-11-04 11:26:52 +03:00
|
|
|
|
|
|
|
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
|
|
|
|
*/
|
2015-12-14 14:19:18 +03:00
|
|
|
$fetchQuery = $this->connection->getQueryBuilder();
|
|
|
|
$fetchQuery->select('activity_id')
|
2015-11-04 11:26:52 +03:00
|
|
|
->from('activity')
|
2015-12-14 14:19:18 +03:00
|
|
|
->where($fetchQuery->expr()->eq('affecteduser', $fetchQuery->createNamedParameter($user)))
|
2015-11-04 11:26:52 +03:00
|
|
|
->orderBy('timestamp', $sort)
|
|
|
|
->setMaxResults(1);
|
2024-01-17 14:22:34 +03:00
|
|
|
$result = $fetchQuery->executeQuery();
|
2015-11-04 11:26:52 +03:00
|
|
|
$activity = $result->fetch();
|
|
|
|
$result->closeCursor();
|
|
|
|
|
|
|
|
if ($activity !== false) {
|
|
|
|
return [
|
2020-08-14 04:29:04 +03:00
|
|
|
'X-Activity-First-Known' => (int)$activity['activity_id'],
|
2015-11-04 11:26:52 +03:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2014-12-10 18:11:57 +03:00
|
|
|
/**
|
|
|
|
* Verify that the filter is valid
|
|
|
|
*
|
2015-01-16 11:05:02 +03:00
|
|
|
* @param string $filterValue
|
2014-12-10 18:11:57 +03:00
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function validateFilter($filterValue) {
|
|
|
|
if (!isset($filterValue)) {
|
|
|
|
return 'all';
|
|
|
|
}
|
|
|
|
|
2014-07-02 17:16:40 +04:00
|
|
|
switch ($filterValue) {
|
2015-08-14 17:12:08 +03:00
|
|
|
case 'filter':
|
2014-07-02 17:16:40 +04:00
|
|
|
return $filterValue;
|
2014-06-03 20:16:45 +04:00
|
|
|
default:
|
2016-10-20 19:02:18 +03:00
|
|
|
try {
|
|
|
|
$this->activityManager->getFilterById($filterValue);
|
2014-07-02 17:16:40 +04:00
|
|
|
return $filterValue;
|
2024-04-17 16:45:29 +03:00
|
|
|
} catch (FilterNotFoundException) {
|
2016-10-20 19:02:18 +03:00
|
|
|
return 'all';
|
2014-07-02 17:16:40 +04:00
|
|
|
}
|
2014-06-03 20:16:45 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-28 21:32:28 +04:00
|
|
|
/**
|
2014-06-04 16:15:23 +04:00
|
|
|
* Delete old events
|
|
|
|
*
|
2014-06-03 14:32:37 +04:00
|
|
|
* @param int $expireDays Minimum 1 day
|
2013-09-17 16:27:41 +04:00
|
|
|
*/
|
2014-07-14 15:13:23 +04:00
|
|
|
public function expire($expireDays = 365) {
|
2014-06-03 14:32:37 +04:00
|
|
|
$ttl = (60 * 60 * 24 * max(1, $expireDays));
|
2013-08-28 21:32:28 +04:00
|
|
|
|
2013-09-17 16:27:41 +04:00
|
|
|
$timelimit = time() - $ttl;
|
2020-08-14 04:29:04 +03:00
|
|
|
$this->deleteActivities([
|
|
|
|
'timestamp' => [$timelimit, '<'],
|
|
|
|
]);
|
2014-06-04 16:15:23 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete activities that match certain conditions
|
|
|
|
*
|
2014-06-04 18:52:08 +04:00
|
|
|
* @param array $conditions Array with conditions that have to be met
|
2024-08-26 13:33:46 +03:00
|
|
|
* 'field' => 'value' => `field` = 'value'
|
|
|
|
* 'field' => array('value', 'operator') => `field` operator 'value'
|
2014-06-04 16:15:23 +04:00
|
|
|
*/
|
2023-09-11 15:57:20 +03:00
|
|
|
public function deleteActivities($conditions): void {
|
|
|
|
$platform = $this->connection->getDatabasePlatform();
|
2024-09-14 21:31:28 +03:00
|
|
|
if ($platform instanceof MySQLPlatform) {
|
2023-09-11 15:57:20 +03:00
|
|
|
$this->logger->debug('Choosing chunked activity delete for MySQL/MariaDB', ['app' => 'activity']);
|
|
|
|
$this->deleteActivitiesForMySQL($conditions);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
$this->logger->debug('Choosing regular activity delete', ['app' => 'activity']);
|
|
|
|
$deleteQuery = $this->connection->getQueryBuilder();
|
|
|
|
$deleteQuery->delete('activity');
|
2022-02-01 20:20:22 +03:00
|
|
|
|
2014-06-04 16:15:23 +04:00
|
|
|
foreach ($conditions as $column => $comparison) {
|
2022-02-01 20:20:22 +03:00
|
|
|
if (is_array($comparison)) {
|
|
|
|
$operation = $comparison[1] ?? '=';
|
|
|
|
$value = $comparison[0];
|
|
|
|
} else {
|
|
|
|
$operation = '=';
|
|
|
|
$value = $comparison;
|
|
|
|
}
|
2014-06-04 16:15:23 +04:00
|
|
|
|
2023-09-11 15:57:20 +03:00
|
|
|
$deleteQuery->andWhere($deleteQuery->expr()->comparison($column, $operation, $deleteQuery->createNamedParameter($value)));
|
2014-06-04 16:15:23 +04:00
|
|
|
}
|
2023-09-11 15:57:20 +03:00
|
|
|
// Dont use chunked delete - let the DB handle the large row count natively
|
|
|
|
$deleteQuery->executeStatement();
|
2020-06-17 15:40:20 +03:00
|
|
|
}
|
2020-03-02 15:55:13 +03:00
|
|
|
|
2020-06-17 15:40:20 +03:00
|
|
|
public function getById(int $activityId): ?IEvent {
|
|
|
|
$query = $this->connection->getQueryBuilder();
|
|
|
|
$query->select('*')
|
|
|
|
->from('activity')
|
|
|
|
->where($query->expr()->eq('activity_id', $query->createNamedParameter($activityId)));
|
2020-03-02 15:55:13 +03:00
|
|
|
|
2024-01-17 14:22:34 +03:00
|
|
|
$result = $query->executeQuery();
|
2020-06-17 15:40:20 +03:00
|
|
|
if ($row = $result->fetch()) {
|
|
|
|
$event = $this->activityManager->generateEvent();
|
2020-08-14 04:29:04 +03:00
|
|
|
$event->setApp((string)$row['app'])
|
|
|
|
->setType((string)$row['type'])
|
|
|
|
->setAffectedUser((string)$row['affecteduser'])
|
|
|
|
->setAuthor((string)$row['user'])
|
|
|
|
->setTimestamp((int)$row['timestamp'])
|
|
|
|
->setSubject((string)$row['subject'], (array)json_decode($row['subjectparams'], true))
|
|
|
|
->setMessage((string)$row['message'], (array)json_decode($row['messageparams'], true))
|
|
|
|
->setObject((string)$row['object_type'], (int)$row['object_id'], (string)$row['file'])
|
|
|
|
->setLink((string)$row['link']);
|
2020-06-17 15:40:20 +03:00
|
|
|
|
|
|
|
return $event;
|
|
|
|
}
|
2022-02-01 20:20:22 +03:00
|
|
|
|
|
|
|
return null;
|
2013-08-28 21:32:28 +04:00
|
|
|
}
|
2020-08-14 04:29:04 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the id of the first activity in the stream since a specified time
|
|
|
|
*
|
|
|
|
* @param string $user
|
|
|
|
* @param int $timestamp
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public function getFirstActivitySince(string $user, int $timestamp): int {
|
|
|
|
$query = $this->connection->getQueryBuilder();
|
|
|
|
$query->select('activity_id')
|
|
|
|
->from('activity')
|
|
|
|
->where($query->expr()->eq('affecteduser', $query->createNamedParameter($user)))
|
|
|
|
->andWhere($query->expr()->gt('timestamp', $query->createNamedParameter($timestamp, IQueryBuilder::PARAM_INT)))
|
|
|
|
->orderBy('timestamp', 'ASC')
|
|
|
|
->setMaxResults(1);
|
|
|
|
|
2024-01-17 14:22:34 +03:00
|
|
|
$res = $query->executeQuery()->fetch(\PDO::FETCH_COLUMN);
|
2020-08-14 04:29:04 +03:00
|
|
|
return (int)$res;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the number of activity items and the latest activity id since the specified activity
|
|
|
|
*
|
|
|
|
* @param string $user
|
|
|
|
* @param int $since
|
2020-08-15 02:13:31 +03:00
|
|
|
* @param bool $byOthers
|
2020-08-14 04:29:04 +03:00
|
|
|
* @return array
|
|
|
|
*/
|
2020-08-15 02:13:31 +03:00
|
|
|
public function getActivitySince(string $user, int $since, bool $byOthers) {
|
2020-08-14 04:29:04 +03:00
|
|
|
$query = $this->connection->getQueryBuilder();
|
2020-08-15 02:13:31 +03:00
|
|
|
$nameParam = $query->createNamedParameter($user);
|
2020-08-18 17:42:25 +03:00
|
|
|
$query->select($query->func()->count('activity_id', 'count'))
|
|
|
|
->selectAlias($query->func()->max('activity_id'), 'max')
|
2020-08-14 04:29:04 +03:00
|
|
|
->from('activity')
|
2020-08-15 02:13:31 +03:00
|
|
|
->where($query->expr()->eq('affecteduser', $nameParam))
|
2020-08-14 04:29:04 +03:00
|
|
|
->andWhere($query->expr()->gt('activity_id', $query->createNamedParameter($since, IQueryBuilder::PARAM_INT)));
|
|
|
|
|
2020-08-15 02:13:31 +03:00
|
|
|
if ($byOthers) {
|
|
|
|
$query->andWhere($query->expr()->neq('user', $nameParam));
|
|
|
|
}
|
|
|
|
|
2024-01-17 14:22:34 +03:00
|
|
|
return $query->executeQuery()->fetch();
|
2020-08-14 04:29:04 +03:00
|
|
|
}
|
2023-09-11 15:57:20 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Add galera safe delete chunking if using mysql
|
|
|
|
* Stops us hitting wsrep_max_ws_rows when large row counts are deleted
|
|
|
|
*
|
|
|
|
* @param array $conditions
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
private function deleteActivitiesForMySQL(array $conditions): void {
|
|
|
|
$query = $this->connection->getQueryBuilder();
|
|
|
|
$query->select('activity_id')
|
|
|
|
->from('activity');
|
|
|
|
|
|
|
|
foreach ($conditions as $column => $comparison) {
|
|
|
|
if (is_array($comparison)) {
|
|
|
|
$operation = $comparison[1] ?? '=';
|
|
|
|
$value = $comparison[0];
|
|
|
|
} else {
|
|
|
|
$operation = '=';
|
|
|
|
$value = $comparison;
|
|
|
|
}
|
|
|
|
$query->where($query->expr()->comparison($column, $operation, $query->createNamedParameter($value)));
|
|
|
|
}
|
|
|
|
|
2023-10-10 14:41:33 +03:00
|
|
|
$query->setMaxResults(50000);
|
2023-09-11 15:57:20 +03:00
|
|
|
$result = $query->executeQuery();
|
|
|
|
$count = $result->rowCount();
|
2024-09-14 21:31:28 +03:00
|
|
|
if ($count === 0) {
|
2023-09-11 15:57:20 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
$ids = array_map(static function (array $id) {
|
|
|
|
return (int)$id[0];
|
|
|
|
}, $result->fetchAll(\PDO::FETCH_NUM));
|
|
|
|
$result->closeCursor();
|
|
|
|
|
2023-10-10 14:41:33 +03:00
|
|
|
$queryResult = 0;
|
2023-09-11 15:57:20 +03:00
|
|
|
$deleteQuery = $this->connection->getQueryBuilder();
|
|
|
|
$deleteQuery->delete('activity');
|
|
|
|
$deleteQuery->where($deleteQuery->expr()->in('activity_id', $deleteQuery->createParameter('ids'), IQueryBuilder::PARAM_INT_ARRAY));
|
2024-09-14 21:31:28 +03:00
|
|
|
foreach (array_chunk($ids, 1000) as $chunk) {
|
2023-10-10 14:41:33 +03:00
|
|
|
$deleteQuery->setParameter('ids', $chunk, IQueryBuilder::PARAM_INT_ARRAY);
|
|
|
|
$queryResult += $deleteQuery->executeStatement();
|
|
|
|
}
|
2024-09-14 21:31:28 +03:00
|
|
|
if ($queryResult === 50000) {
|
2023-09-11 15:57:20 +03:00
|
|
|
$this->deleteActivitiesForMySQL($conditions);
|
|
|
|
}
|
|
|
|
}
|
2013-08-28 19:36:48 +04:00
|
|
|
}
|