2021-06-26 17:23:15 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace BookStack\Actions;
|
2015-08-16 19:59:23 +02:00
|
|
|
|
2022-07-12 21:15:41 +02:00
|
|
|
use BookStack\Auth\Permissions\PermissionApplicator;
|
2020-04-10 21:55:33 +02:00
|
|
|
use BookStack\Auth\User;
|
2021-11-20 15:03:56 +01:00
|
|
|
use BookStack\Entities\Models\Book;
|
2020-11-22 01:17:45 +01:00
|
|
|
use BookStack\Entities\Models\Chapter;
|
|
|
|
use BookStack\Entities\Models\Entity;
|
|
|
|
use BookStack\Entities\Models\Page;
|
2020-11-08 00:15:13 +01:00
|
|
|
use Illuminate\Database\Eloquent\Builder;
|
2020-11-07 15:28:50 +01:00
|
|
|
use Illuminate\Database\Eloquent\Relations\Relation;
|
2015-08-16 19:59:23 +02:00
|
|
|
|
2021-12-11 18:29:33 +01:00
|
|
|
class ActivityQueries
|
2015-08-16 19:59:23 +02:00
|
|
|
{
|
2022-07-12 21:15:41 +02:00
|
|
|
protected PermissionApplicator $permissions;
|
2015-08-16 19:59:23 +02:00
|
|
|
|
2022-07-12 21:15:41 +02:00
|
|
|
public function __construct(PermissionApplicator $permissions)
|
2015-08-16 19:59:23 +02:00
|
|
|
{
|
2022-07-12 21:15:41 +02:00
|
|
|
$this->permissions = $permissions;
|
2015-08-16 19:59:23 +02:00
|
|
|
}
|
|
|
|
|
2015-08-16 21:11:21 +02:00
|
|
|
/**
|
|
|
|
* Gets the latest activity.
|
|
|
|
*/
|
2020-04-10 21:55:33 +02:00
|
|
|
public function latest(int $count = 20, int $page = 0): array
|
2015-08-16 21:11:21 +02:00
|
|
|
{
|
2022-07-12 21:15:41 +02:00
|
|
|
$activityList = $this->permissions
|
2022-07-16 20:54:25 +02:00
|
|
|
->restrictEntityRelationQuery(Activity::query(), 'activities', 'entity_id', 'entity_type')
|
2019-09-19 19:03:17 +02:00
|
|
|
->orderBy('created_at', 'desc')
|
2020-04-10 21:55:33 +02:00
|
|
|
->with(['user', 'entity'])
|
2019-09-19 19:03:17 +02:00
|
|
|
->skip($count * $page)
|
|
|
|
->take($count)
|
|
|
|
->get();
|
2016-02-28 20:03:04 +01:00
|
|
|
|
2015-08-30 16:31:16 +02:00
|
|
|
return $this->filterSimilar($activityList);
|
2015-08-30 12:47:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-02-16 22:25:11 +01:00
|
|
|
* Gets the latest activity for an entity, Filtering out similar
|
2015-08-30 12:47:58 +02:00
|
|
|
* items to prevent a message activity list.
|
|
|
|
*/
|
2020-04-10 21:55:33 +02:00
|
|
|
public function entityActivity(Entity $entity, int $count = 20, int $page = 1): array
|
2015-08-30 12:47:58 +02:00
|
|
|
{
|
2021-11-20 15:03:56 +01:00
|
|
|
/** @var array<string, int[]> $queryIds */
|
2020-11-08 00:15:13 +01:00
|
|
|
$queryIds = [$entity->getMorphClass() => [$entity->id]];
|
|
|
|
|
2021-11-20 15:03:56 +01:00
|
|
|
if ($entity instanceof Book) {
|
2021-11-23 00:33:55 +01:00
|
|
|
$queryIds[(new Chapter())->getMorphClass()] = $entity->chapters()->scopes('visible')->pluck('id');
|
2020-11-08 00:15:13 +01:00
|
|
|
}
|
2021-11-20 15:03:56 +01:00
|
|
|
if ($entity instanceof Book || $entity instanceof Chapter) {
|
2021-11-23 00:33:55 +01:00
|
|
|
$queryIds[(new Page())->getMorphClass()] = $entity->pages()->scopes('visible')->pluck('id');
|
2016-04-24 17:54:20 +02:00
|
|
|
}
|
2020-07-28 13:59:43 +02:00
|
|
|
|
2021-12-11 18:29:33 +01:00
|
|
|
$query = Activity::query();
|
2020-11-08 00:15:13 +01:00
|
|
|
$query->where(function (Builder $query) use ($queryIds) {
|
|
|
|
foreach ($queryIds as $morphClass => $idArr) {
|
|
|
|
$query->orWhere(function (Builder $innerQuery) use ($morphClass, $idArr) {
|
|
|
|
$innerQuery->where('entity_type', '=', $morphClass)
|
|
|
|
->whereIn('entity_id', $idArr);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
$activity = $query->orderBy('created_at', 'desc')
|
2020-11-07 15:28:50 +01:00
|
|
|
->with(['entity' => function (Relation $query) {
|
|
|
|
$query->withTrashed();
|
|
|
|
}, 'user.avatar'])
|
2019-05-05 16:54:22 +02:00
|
|
|
->skip($count * ($page - 1))
|
|
|
|
->take($count)
|
|
|
|
->get();
|
2015-08-30 12:47:58 +02:00
|
|
|
|
|
|
|
return $this->filterSimilar($activity);
|
|
|
|
}
|
|
|
|
|
2016-02-16 22:25:11 +01:00
|
|
|
/**
|
2021-11-30 01:06:17 +01:00
|
|
|
* Get the latest activity for a user, Filtering out similar items.
|
2016-02-16 22:25:11 +01:00
|
|
|
*/
|
2020-04-10 21:55:33 +02:00
|
|
|
public function userActivity(User $user, int $count = 20, int $page = 0): array
|
2016-02-16 22:25:11 +01:00
|
|
|
{
|
2022-07-12 21:15:41 +02:00
|
|
|
$activityList = $this->permissions
|
2022-07-16 20:54:25 +02:00
|
|
|
->restrictEntityRelationQuery(Activity::query(), 'activities', 'entity_id', 'entity_type')
|
2020-04-10 21:55:33 +02:00
|
|
|
->orderBy('created_at', 'desc')
|
|
|
|
->where('user_id', '=', $user->id)
|
|
|
|
->skip($count * $page)
|
|
|
|
->take($count)
|
2020-04-10 23:49:52 +02:00
|
|
|
->get();
|
2020-04-10 21:55:33 +02:00
|
|
|
|
2016-03-06 14:17:46 +01:00
|
|
|
return $this->filterSimilar($activityList);
|
2016-02-16 22:25:11 +01:00
|
|
|
}
|
|
|
|
|
2015-08-30 12:47:58 +02:00
|
|
|
/**
|
2016-01-01 10:03:40 +01:00
|
|
|
* Filters out similar activity.
|
2021-06-26 17:23:15 +02:00
|
|
|
*
|
2016-02-16 22:25:11 +01:00
|
|
|
* @param Activity[] $activities
|
2015-08-30 12:47:58 +02:00
|
|
|
*/
|
2020-04-10 21:55:33 +02:00
|
|
|
protected function filterSimilar(iterable $activities): array
|
2015-08-30 16:31:16 +02:00
|
|
|
{
|
2015-08-30 12:47:58 +02:00
|
|
|
$newActivity = [];
|
2020-04-10 21:55:33 +02:00
|
|
|
$previousItem = null;
|
|
|
|
|
2016-02-16 22:25:11 +01:00
|
|
|
foreach ($activities as $activityItem) {
|
2020-04-10 21:55:33 +02:00
|
|
|
if (!$previousItem || !$activityItem->isSimilarTo($previousItem)) {
|
2015-08-30 12:47:58 +02:00
|
|
|
$newActivity[] = $activityItem;
|
|
|
|
}
|
2020-04-10 21:55:33 +02:00
|
|
|
|
2015-08-30 12:47:58 +02:00
|
|
|
$previousItem = $activityItem;
|
|
|
|
}
|
2020-04-10 21:55:33 +02:00
|
|
|
|
2015-08-30 12:47:58 +02:00
|
|
|
return $newActivity;
|
2015-08-16 21:11:21 +02:00
|
|
|
}
|
2021-12-18 12:43:05 +01:00
|
|
|
}
|