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