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