]> BookStack Code Mirror - bookstack/blob - app/Actions/ActivityService.php
Checked over recycle bin parent/child flows
[bookstack] / app / Actions / ActivityService.php
1 <?php namespace BookStack\Actions;
2
3 use BookStack\Auth\Permissions\PermissionService;
4 use BookStack\Auth\User;
5 use BookStack\Entities\Entity;
6 use Illuminate\Database\Eloquent\Relations\Relation;
7 use Illuminate\Support\Collection;
8 use Illuminate\Support\Facades\Log;
9
10 class ActivityService
11 {
12     protected $activity;
13     protected $user;
14     protected $permissionService;
15
16     /**
17      * ActivityService constructor.
18      */
19     public function __construct(Activity $activity, PermissionService $permissionService)
20     {
21         $this->activity = $activity;
22         $this->permissionService = $permissionService;
23         $this->user = user();
24     }
25
26     /**
27      * Add activity data to database.
28      */
29     public function add(Entity $entity, string $activityKey, ?int $bookId = null)
30     {
31         $activity = $this->newActivityForUser($activityKey, $bookId);
32         $entity->activity()->save($activity);
33         $this->setNotification($activityKey);
34     }
35
36     /**
37      * Adds a activity history with a message, without binding to a entity.
38      */
39     public function addMessage(string $activityKey, string $message, ?int $bookId = null)
40     {
41         $this->newActivityForUser($activityKey, $bookId)->forceFill([
42             'extra' => $message
43         ])->save();
44
45         $this->setNotification($activityKey);
46     }
47
48     /**
49      * Get a new activity instance for the current user.
50      */
51     protected function newActivityForUser(string $key, ?int $bookId = null): Activity
52     {
53         return $this->activity->newInstance()->forceFill([
54             'key'     => strtolower($key),
55             'user_id' => $this->user->id,
56             'book_id' => $bookId ?? 0,
57         ]);
58     }
59
60     /**
61      * Removes the entity attachment from each of its activities
62      * and instead uses the 'extra' field with the entities name.
63      * Used when an entity is deleted.
64      */
65     public function removeEntity(Entity $entity): Collection
66     {
67         $activities = $entity->activity()->get();
68         $entity->activity()->update([
69             'extra'       => $entity->name,
70             'entity_id'   => 0,
71             'entity_type' => '',
72         ]);
73         return $activities;
74     }
75
76     /**
77      * Gets the latest activity.
78      */
79     public function latest(int $count = 20, int $page = 0): array
80     {
81         $activityList = $this->permissionService
82             ->filterRestrictedEntityRelations($this->activity, 'activities', 'entity_id', 'entity_type')
83             ->orderBy('created_at', 'desc')
84             ->with(['user', 'entity'])
85             ->skip($count * $page)
86             ->take($count)
87             ->get();
88
89         return $this->filterSimilar($activityList);
90     }
91
92     /**
93      * Gets the latest activity for an entity, Filtering out similar
94      * items to prevent a message activity list.
95      */
96     public function entityActivity(Entity $entity, int $count = 20, int $page = 1): array
97     {
98         if ($entity->isA('book')) {
99             $query = $this->activity->newQuery()->where('book_id', '=', $entity->id);
100         } else {
101             $query = $this->activity->newQuery()->where('entity_type', '=', $entity->getMorphClass())
102                 ->where('entity_id', '=', $entity->id);
103         }
104
105         $activity = $this->permissionService
106             ->filterRestrictedEntityRelations($query, 'activities', 'entity_id', 'entity_type')
107             ->orderBy('created_at', 'desc')
108             ->with(['entity' => function (Relation $query) {
109                 $query->withTrashed();
110             }, 'user.avatar'])
111             ->skip($count * ($page - 1))
112             ->take($count)
113             ->get();
114
115         return $this->filterSimilar($activity);
116     }
117
118     /**
119      * Get latest activity for a user, Filtering out similar items.
120      */
121     public function userActivity(User $user, int $count = 20, int $page = 0): array
122     {
123         $activityList = $this->permissionService
124             ->filterRestrictedEntityRelations($this->activity, 'activities', 'entity_id', 'entity_type')
125             ->orderBy('created_at', 'desc')
126             ->where('user_id', '=', $user->id)
127             ->skip($count * $page)
128             ->take($count)
129             ->get();
130
131         return $this->filterSimilar($activityList);
132     }
133
134     /**
135      * Filters out similar activity.
136      * @param Activity[] $activities
137      * @return array
138      */
139     protected function filterSimilar(iterable $activities): array
140     {
141         $newActivity = [];
142         $previousItem = null;
143
144         foreach ($activities as $activityItem) {
145             if (!$previousItem || !$activityItem->isSimilarTo($previousItem)) {
146                 $newActivity[] = $activityItem;
147             }
148
149             $previousItem = $activityItem;
150         }
151
152         return $newActivity;
153     }
154
155     /**
156      * Flashes a notification message to the session if an appropriate message is available.
157      */
158     protected function setNotification(string $activityKey)
159     {
160         $notificationTextKey = 'activities.' . $activityKey . '_notification';
161         if (trans()->has($notificationTextKey)) {
162             $message = trans($notificationTextKey);
163             session()->flash('success', $message);
164         }
165     }
166
167     /**
168      * Log out a failed login attempt, Providing the given username
169      * as part of the message if the '%u' string is used.
170      */
171     public function logFailedLogin(string $username)
172     {
173         $message = config('logging.failed_login.message');
174         if (!$message) {
175             return;
176         }
177
178         $message = str_replace("%u", $username, $message);
179         $channel = config('logging.failed_login.channel');
180         Log::channel($channel)->warning($message);
181     }
182 }