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