3 namespace BookStack\Actions;
5 use BookStack\Auth\Permissions\PermissionService;
6 use BookStack\Auth\User;
7 use BookStack\Entities\Models\Chapter;
8 use BookStack\Entities\Models\Entity;
9 use BookStack\Entities\Models\Page;
10 use BookStack\Interfaces\Loggable;
11 use Illuminate\Database\Eloquent\Builder;
12 use Illuminate\Database\Eloquent\Relations\Relation;
13 use Illuminate\Support\Facades\Log;
18 protected $permissionService;
20 public function __construct(Activity $activity, PermissionService $permissionService)
22 $this->activity = $activity;
23 $this->permissionService = $permissionService;
27 * Add activity data to database for an entity.
29 public function addForEntity(Entity $entity, string $type)
31 $activity = $this->newActivityForUser($type);
32 $entity->activity()->save($activity);
33 $this->setNotification($type);
37 * Add a generic activity event to the database.
39 * @param string|Loggable $detail
41 public function add(string $type, $detail = '')
43 if ($detail instanceof Loggable) {
44 $detail = $detail->logDescriptor();
47 $activity = $this->newActivityForUser($type);
48 $activity->detail = $detail;
50 $this->setNotification($type);
54 * Get a new activity instance for the current user.
56 protected function newActivityForUser(string $type): Activity
58 return $this->activity->newInstance()->forceFill([
59 'type' => strtolower($type),
60 'user_id' => user()->id,
65 * Removes the entity attachment from each of its activities
66 * and instead uses the 'extra' field with the entities name.
67 * Used when an entity is deleted.
69 public function removeEntity(Entity $entity)
71 $entity->activity()->update([
72 'detail' => $entity->name,
74 'entity_type' => null,
79 * Gets the latest activity.
81 public function latest(int $count = 20, int $page = 0): array
83 $activityList = $this->permissionService
84 ->filterRestrictedEntityRelations($this->activity->newQuery(), 'activities', 'entity_id', 'entity_type')
85 ->orderBy('created_at', 'desc')
86 ->with(['user', 'entity'])
87 ->skip($count * $page)
91 return $this->filterSimilar($activityList);
95 * Gets the latest activity for an entity, Filtering out similar
96 * items to prevent a message activity list.
98 public function entityActivity(Entity $entity, int $count = 20, int $page = 1): array
100 /** @var [string => int[]] $queryIds */
101 $queryIds = [$entity->getMorphClass() => [$entity->id]];
103 if ($entity->isA('book')) {
104 $queryIds[(new Chapter())->getMorphClass()] = $entity->chapters()->visible()->pluck('id');
106 if ($entity->isA('book') || $entity->isA('chapter')) {
107 $queryIds[(new Page())->getMorphClass()] = $entity->pages()->visible()->pluck('id');
110 $query = $this->activity->newQuery();
111 $query->where(function (Builder $query) use ($queryIds) {
112 foreach ($queryIds as $morphClass => $idArr) {
113 $query->orWhere(function (Builder $innerQuery) use ($morphClass, $idArr) {
114 $innerQuery->where('entity_type', '=', $morphClass)
115 ->whereIn('entity_id', $idArr);
120 $activity = $query->orderBy('created_at', 'desc')
121 ->with(['entity' => function (Relation $query) {
122 $query->withTrashed();
124 ->skip($count * ($page - 1))
128 return $this->filterSimilar($activity);
132 * Get latest activity for a user, Filtering out similar items.
134 public function userActivity(User $user, int $count = 20, int $page = 0): array
136 $activityList = $this->permissionService
137 ->filterRestrictedEntityRelations($this->activity->newQuery(), 'activities', 'entity_id', 'entity_type')
138 ->orderBy('created_at', 'desc')
139 ->where('user_id', '=', $user->id)
140 ->skip($count * $page)
144 return $this->filterSimilar($activityList);
148 * Filters out similar activity.
150 * @param Activity[] $activities
154 protected function filterSimilar(iterable $activities): array
157 $previousItem = null;
159 foreach ($activities as $activityItem) {
160 if (!$previousItem || !$activityItem->isSimilarTo($previousItem)) {
161 $newActivity[] = $activityItem;
164 $previousItem = $activityItem;
171 * Flashes a notification message to the session if an appropriate message is available.
173 protected function setNotification(string $type)
175 $notificationTextKey = 'activities.' . $type . '_notification';
176 if (trans()->has($notificationTextKey)) {
177 $message = trans($notificationTextKey);
178 session()->flash('success', $message);
183 * Log out a failed login attempt, Providing the given username
184 * as part of the message if the '%u' string is used.
186 public function logFailedLogin(string $username)
188 $message = config('logging.failed_login.message');
193 $message = str_replace('%u', $username, $message);
194 $channel = config('logging.failed_login.channel');
195 Log::channel($channel)->warning($message);