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