1 <?php namespace BookStack\Services;
3 use Illuminate\Support\Facades\Auth;
4 use BookStack\Activity;
12 protected $restrictionService;
15 * ActivityService constructor.
16 * @param Activity $activity
17 * @param RestrictionService $restrictionService
19 public function __construct(Activity $activity, RestrictionService $restrictionService)
21 $this->activity = $activity;
22 $this->restrictionService = $restrictionService;
23 $this->user = auth()->user();
27 * Add activity data to database.
28 * @param Entity $entity
33 public function add(Entity $entity, $activityKey, $bookId = 0, $extra = false)
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;
42 $entity->activity()->save($activity);
43 $this->setNotification($activityKey);
47 * Adds a activity history with a message & without binding to a entity.
50 * @param bool|false $extra
52 public function addMessage($activityKey, $bookId = 0, $extra = false)
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;
60 $this->activity->save();
61 $this->setNotification($activityKey);
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
72 public function removeEntity(Entity $entity)
74 $activities = $entity->activity;
75 foreach ($activities as $activity) {
76 $activity->extra = $entity->name;
77 $activity->entity_id = 0;
78 $activity->entity_type = null;
85 * Gets the latest activity.
90 public function latest($count = 20, $page = 0)
92 $activityList = $this->restrictionService
93 ->filterRestrictedEntityRelations($this->activity, 'activities', 'entity_id', 'entity_type')
94 ->orderBy('created_at', 'desc')->skip($count * $page)->take($count)->get();
96 return $this->filterSimilar($activityList);
100 * Gets the latest activity for an entity, Filtering out similar
101 * items to prevent a message activity list.
102 * @param Entity $entity
107 public function entityActivity($entity, $count = 20, $page = 0)
109 $activity = $entity->hasMany('BookStack\Activity')->orderBy('created_at', 'desc')
110 ->skip($count * $page)->take($count)->get();
112 return $this->filterSimilar($activity);
116 * Get latest activity for a user, Filtering out similar
123 public function userActivity($user, $count = 20, $page = 0)
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);
131 * Filters out similar activity.
132 * @param Activity[] $activities
135 protected function filterSimilar($activities)
138 $previousItem = false;
139 foreach ($activities as $activityItem) {
140 if ($previousItem === false) {
141 $previousItem = $activityItem;
142 $newActivity[] = $activityItem;
145 if (!$activityItem->isSimilarTo($previousItem)) {
146 $newActivity[] = $activityItem;
148 $previousItem = $activityItem;
154 * Flashes a notification message to the session if an appropriate message is available.
155 * @param $activityKey
157 protected function setNotification($activityKey)
159 $notificationTextKey = 'activities.' . $activityKey . '_notification';
160 if (trans()->has($notificationTextKey)) {
161 $message = trans($notificationTextKey);
162 Session::flash('success', $message);