1 <?php namespace BookStack\Services;
3 use Illuminate\Support\Facades\Auth;
4 use BookStack\Activity;
14 * ActivityService constructor.
17 public function __construct(Activity $activity)
19 $this->activity = $activity;
20 $this->user = auth()->user();
24 * Add activity data to database.
25 * @param Entity $entity
30 public function add(Entity $entity, $activityKey, $bookId = 0, $extra = false)
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;
38 $entity->activity()->save($this->activity);
39 $this->setNotification($activityKey);
43 * Adds a activity history with a message & without binding to a entitiy.
46 * @param bool|false $extra
48 public function addMessage($activityKey, $bookId = 0, $extra = false)
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;
56 $this->activity->save();
57 $this->setNotification($activityKey);
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
68 public function removeEntity(Entity $entity)
70 $activities = $entity->activity;
71 foreach ($activities as $activity) {
72 $activity->extra = $entity->name;
73 $activity->entity_id = 0;
74 $activity->entity_type = null;
81 * Gets the latest activity.
86 public function latest($count = 20, $page = 0)
88 $activityList = $this->activity->orderBy('created_at', 'desc')
89 ->skip($count * $page)->take($count)->get();
90 return $this->filterSimilar($activityList);
94 * Gets the latest activity for an entity, Filtering out similar
95 * items to prevent a message activity list.
96 * @param Entity $entity
101 public function entityActivity($entity, $count = 20, $page = 0)
103 $activity = $entity->hasMany('BookStack\Activity')->orderBy('created_at', 'desc')
104 ->skip($count * $page)->take($count)->get();
106 return $this->filterSimilar($activity);
110 * Get latest activity for a user, Filtering out similar
117 public function userActivity($user, $count = 20, $page = 0)
119 $activity = $this->activity->where('user_id', '=', $user->id)
120 ->orderBy('created_at', 'desc')->skip($count * $page)->take($count)->get();
121 return $this->filterSimilar($activity);
125 * Filters out similar activity.
126 * @param Activity[] $activities
129 protected function filterSimilar($activities)
132 $previousItem = false;
133 foreach ($activities as $activityItem) {
134 if ($previousItem === false) {
135 $previousItem = $activityItem;
136 $newActivity[] = $activityItem;
139 if (!$activityItem->isSimilarTo($previousItem)) {
140 $newActivity[] = $activityItem;
142 $previousItem = $activityItem;
148 * Flashes a notification message to the session if an appropriate message is available.
149 * @param $activityKey
151 protected function setNotification($activityKey)
153 $notificationTextKey = 'activities.' . $activityKey . '_notification';
154 if (trans()->has($notificationTextKey)) {
155 $message = trans($notificationTextKey);
156 Session::flash('success', $message);