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 // TODO - Rewrite to db query.
77 $activities = $entity->activity;
78 foreach ($activities as $activity) {
79 $activity->extra = $entity->name;
80 $activity->entity_id = 0;
81 $activity->entity_type = null;
88 * Gets the latest activity.
93 public function latest($count = 20, $page = 0)
95 $activityList = $this->permissionService
96 ->filterRestrictedEntityRelations($this->activity, 'activities', 'entity_id', 'entity_type')
97 ->orderBy('created_at', 'desc')
98 ->with('user', 'entity')
99 ->skip($count * $page)
103 return $this->filterSimilar($activityList);
107 * Gets the latest activity for an entity, Filtering out similar
108 * items to prevent a message activity list.
109 * @param Entity $entity
114 public function entityActivity($entity, $count = 20, $page = 1)
116 if ($entity->isA('book')) {
117 $query = $this->activity->where('book_id', '=', $entity->id);
119 $query = $this->activity->where('entity_type', '=', $entity->getMorphClass())
120 ->where('entity_id', '=', $entity->id);
123 $activity = $this->permissionService
124 ->filterRestrictedEntityRelations($query, 'activities', 'entity_id', 'entity_type')
125 ->orderBy('created_at', 'desc')
126 ->with(['entity', 'user.avatar'])
127 ->skip($count * ($page - 1))
131 return $this->filterSimilar($activity);
135 * Get latest activity for a user, Filtering out similar
142 public function userActivity($user, $count = 20, $page = 0)
144 $activityList = $this->permissionService
145 ->filterRestrictedEntityRelations($this->activity, 'activities', 'entity_id', 'entity_type')
146 ->orderBy('created_at', 'desc')->where('user_id', '=', $user->id)->skip($count * $page)->take($count)->get();
147 return $this->filterSimilar($activityList);
151 * Filters out similar activity.
152 * @param Activity[] $activities
155 protected function filterSimilar($activities)
158 $previousItem = false;
159 foreach ($activities as $activityItem) {
160 if ($previousItem === false) {
161 $previousItem = $activityItem;
162 $newActivity[] = $activityItem;
165 if (!$activityItem->isSimilarTo($previousItem)) {
166 $newActivity[] = $activityItem;
168 $previousItem = $activityItem;
174 * Flashes a notification message to the session if an appropriate message is available.
175 * @param $activityKey
177 protected function setNotification($activityKey)
179 $notificationTextKey = 'activities.' . $activityKey . '_notification';
180 if (trans()->has($notificationTextKey)) {
181 $message = trans($notificationTextKey);
182 session()->flash('success', $message);