1 <?php namespace BookStack\Actions;
3 use BookStack\Auth\Permissions\PermissionService;
4 use BookStack\Entities\Entity;
10 protected $permissionService;
13 * ActivityService constructor.
14 * @param Activity $activity
15 * @param PermissionService $permissionService
17 public function __construct(Activity $activity, PermissionService $permissionService)
19 $this->activity = $activity;
20 $this->permissionService = $permissionService;
25 * Add activity data to database.
26 * @param \BookStack\Entities\Entity $entity
27 * @param string $activityKey
30 public function add(Entity $entity, string $activityKey, int $bookId = null)
32 $activity = $this->newActivityForUser($activityKey, $bookId);
33 $entity->activity()->save($activity);
34 $this->setNotification($activityKey);
38 * Adds a activity history with a message, without binding to a entity.
39 * @param string $activityKey
40 * @param string $message
43 public function addMessage(string $activityKey, string $message, int $bookId = null)
45 $this->newActivityForUser($activityKey, $bookId)->forceFill([
49 $this->setNotification($activityKey);
53 * Get a new activity instance for the current user.
55 * @param int|null $bookId
58 protected function newActivityForUser(string $key, int $bookId = null)
60 return $this->activity->newInstance()->forceFill([
61 'key' => strtolower($key),
62 'user_id' => $this->user->id,
63 'book_id' => $bookId ?? 0,
68 * Removes the entity attachment from each of its activities
69 * and instead uses the 'extra' field with the entities name.
70 * Used when an entity is deleted.
71 * @param Entity $entity
74 public function removeEntity(Entity $entity)
76 $activities = $entity->activity;
77 foreach ($activities as $activity) {
78 $activity->extra = $entity->name;
79 $activity->entity_id = 0;
80 $activity->entity_type = null;
87 * Gets the latest activity.
92 public function latest($count = 20, $page = 0)
94 $activityList = $this->permissionService
95 ->filterRestrictedEntityRelations($this->activity, 'activities', 'entity_id', 'entity_type')
96 ->orderBy('created_at', 'desc')
97 ->with('user', 'entity')
98 ->skip($count * $page)
102 return $this->filterSimilar($activityList);
106 * Gets the latest activity for an entity, Filtering out similar
107 * items to prevent a message activity list.
108 * @param Entity $entity
113 public function entityActivity($entity, $count = 20, $page = 1)
115 if ($entity->isA('book')) {
116 $query = $this->activity->where('book_id', '=', $entity->id);
118 $query = $this->activity->where('entity_type', '=', $entity->getMorphClass())
119 ->where('entity_id', '=', $entity->id);
122 $activity = $this->permissionService
123 ->filterRestrictedEntityRelations($query, 'activities', 'entity_id', 'entity_type')
124 ->orderBy('created_at', 'desc')
125 ->with(['entity', 'user.avatar'])
126 ->skip($count * ($page - 1))
130 return $this->filterSimilar($activity);
134 * Get latest activity for a user, Filtering out similar
141 public function userActivity($user, $count = 20, $page = 0)
143 $activityList = $this->permissionService
144 ->filterRestrictedEntityRelations($this->activity, 'activities', 'entity_id', 'entity_type')
145 ->orderBy('created_at', 'desc')->where('user_id', '=', $user->id)->skip($count * $page)->take($count)->get();
146 return $this->filterSimilar($activityList);
150 * Filters out similar activity.
151 * @param Activity[] $activities
154 protected function filterSimilar($activities)
157 $previousItem = false;
158 foreach ($activities as $activityItem) {
159 if ($previousItem === false) {
160 $previousItem = $activityItem;
161 $newActivity[] = $activityItem;
164 if (!$activityItem->isSimilarTo($previousItem)) {
165 $newActivity[] = $activityItem;
167 $previousItem = $activityItem;
173 * Flashes a notification message to the session if an appropriate message is available.
174 * @param $activityKey
176 protected function setNotification($activityKey)
178 $notificationTextKey = 'activities.' . $activityKey . '_notification';
179 if (trans()->has($notificationTextKey)) {
180 $message = trans($notificationTextKey);
181 session()->flash('success', $message);