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