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