1 <?php namespace BookStack\Actions;
3 use BookStack\Auth\Permissions\PermissionService;
4 use BookStack\Entities\Entity;
11 protected $permissionService;
14 * ActivityService constructor.
15 * @param \BookStack\Actions\Activity $activity
16 * @param PermissionService $permissionService
18 public function __construct(Activity $activity, PermissionService $permissionService)
20 $this->activity = $activity;
21 $this->permissionService = $permissionService;
26 * Add activity data to database.
27 * @param Entity $entity
32 public function add(Entity $entity, $activityKey, $bookId = 0, $extra = false)
34 $activity = $this->activity->newInstance();
35 $activity->user_id = $this->user->id;
36 $activity->book_id = $bookId;
37 $activity->key = strtolower($activityKey);
38 if ($extra !== false) {
39 $activity->extra = $extra;
41 $entity->activity()->save($activity);
42 $this->setNotification($activityKey);
46 * Adds a activity history with a message & without binding to a entity.
49 * @param bool|false $extra
51 public function addMessage($activityKey, $bookId = 0, $extra = false)
53 $this->activity->user_id = $this->user->id;
54 $this->activity->book_id = $bookId;
55 $this->activity->key = strtolower($activityKey);
56 if ($extra !== false) {
57 $this->activity->extra = $extra;
59 $this->activity->save();
60 $this->setNotification($activityKey);
65 * Removes the entity attachment from each of its activities
66 * and instead uses the 'extra' field with the entities name.
67 * Used when an entity is deleted.
68 * @param Entity $entity
71 public function removeEntity(Entity $entity)
73 $activities = $entity->activity;
74 foreach ($activities as $activity) {
75 $activity->extra = $entity->name;
76 $activity->entity_id = 0;
77 $activity->entity_type = null;
84 * Gets the latest activity.
89 public function latest($count = 20, $page = 0)
91 $activityList = $this->permissionService
92 ->filterRestrictedEntityRelations($this->activity, 'activities', 'entity_id', 'entity_type')
93 ->orderBy('created_at', 'desc')->with('user', 'entity')->skip($count * $page)->take($count)->get();
95 return $this->filterSimilar($activityList);
99 * Gets the latest activity for an entity, Filtering out similar
100 * items to prevent a message activity list.
101 * @param Entity $entity
106 public function entityActivity($entity, $count = 20, $page = 1)
108 if ($entity->isA('book')) {
109 $query = $this->activity->where('book_id', '=', $entity->id);
111 $query = $this->activity->where('entity_type', '=', $entity->getMorphClass())
112 ->where('entity_id', '=', $entity->id);
115 $activity = $this->permissionService
116 ->filterRestrictedEntityRelations($query, 'activities', 'entity_id', 'entity_type')
117 ->orderBy('created_at', 'desc')
118 ->with(['entity', 'user.avatar'])
119 ->skip($count * ($page - 1))
123 return $this->filterSimilar($activity);
127 * Get latest activity for a user, Filtering out similar
134 public function userActivity($user, $count = 20, $page = 0)
136 $activityList = $this->permissionService
137 ->filterRestrictedEntityRelations($this->activity, 'activities', 'entity_id', 'entity_type')
138 ->orderBy('created_at', 'desc')->where('user_id', '=', $user->id)->skip($count * $page)->take($count)->get();
139 return $this->filterSimilar($activityList);
143 * Filters out similar activity.
144 * @param Activity[] $activities
147 protected function filterSimilar($activities)
150 $previousItem = false;
151 foreach ($activities as $activityItem) {
152 if ($previousItem === false) {
153 $previousItem = $activityItem;
154 $newActivity[] = $activityItem;
157 if (!$activityItem->isSimilarTo($previousItem)) {
158 $newActivity[] = $activityItem;
160 $previousItem = $activityItem;
166 * Flashes a notification message to the session if an appropriate message is available.
167 * @param $activityKey
169 protected function setNotification($activityKey)
171 $notificationTextKey = 'activities.' . $activityKey . '_notification';
172 if (trans()->has($notificationTextKey)) {
173 $message = trans($notificationTextKey);
174 Session::flash('success', $message);