X-Git-Url: http://source.bookstackapp.com/bookstack/blobdiff_plain/ba09dad1fed9cdfcc466f3ccb96321f74027e20b..refs/pull/1756/head:/app/Actions/ActivityService.php diff --git a/app/Actions/ActivityService.php b/app/Actions/ActivityService.php index f4f82a6f4..f56f1ca57 100644 --- a/app/Actions/ActivityService.php +++ b/app/Actions/ActivityService.php @@ -1,8 +1,8 @@ activity->newInstance(); - $activity->user_id = $this->user->id; - $activity->book_id = $bookId; - $activity->key = strtolower($activityKey); - if ($extra !== false) { - $activity->extra = $extra; - } + $activity = $this->newActivityForUser($activityKey, $bookId); $entity->activity()->save($activity); $this->setNotification($activityKey); } /** - * Adds a activity history with a message & without binding to a entity. - * @param $activityKey + * Adds a activity history with a message, without binding to a entity. + * @param string $activityKey + * @param string $message * @param int $bookId - * @param bool|false $extra */ - public function addMessage($activityKey, $bookId = 0, $extra = false) + public function addMessage(string $activityKey, string $message, int $bookId = null) { - $this->activity->user_id = $this->user->id; - $this->activity->book_id = $bookId; - $this->activity->key = strtolower($activityKey); - if ($extra !== false) { - $this->activity->extra = $extra; - } - $this->activity->save(); + $this->newActivityForUser($activityKey, $bookId)->forceFill([ + 'extra' => $message + ])->save(); + $this->setNotification($activityKey); } + /** + * Get a new activity instance for the current user. + * @param string $key + * @param int|null $bookId + * @return Activity + */ + protected function newActivityForUser(string $key, int $bookId = null) + { + return $this->activity->newInstance()->forceFill([ + 'key' => strtolower($key), + 'user_id' => $this->user->id, + 'book_id' => $bookId ?? 0, + ]); + } /** * Removes the entity attachment from each of its activities * and instead uses the 'extra' field with the entities name. * Used when an entity is deleted. - * @param Entity $entity + * @param \BookStack\Entities\Entity $entity * @return mixed */ public function removeEntity(Entity $entity) { + // TODO - Rewrite to db query. $activities = $entity->activity; foreach ($activities as $activity) { $activity->extra = $entity->name; @@ -90,7 +95,11 @@ class ActivityService { $activityList = $this->permissionService ->filterRestrictedEntityRelations($this->activity, 'activities', 'entity_id', 'entity_type') - ->orderBy('created_at', 'desc')->with('user', 'entity')->skip($count * $page)->take($count)->get(); + ->orderBy('created_at', 'desc') + ->with('user', 'entity') + ->skip($count * $page) + ->take($count) + ->get(); return $this->filterSimilar($activityList); } @@ -98,7 +107,7 @@ class ActivityService /** * Gets the latest activity for an entity, Filtering out similar * items to prevent a message activity list. - * @param Entity $entity + * @param \BookStack\Entities\Entity $entity * @param int $count * @param int $page * @return array @@ -171,7 +180,7 @@ class ActivityService $notificationTextKey = 'activities.' . $activityKey . '_notification'; if (trans()->has($notificationTextKey)) { $message = trans($notificationTextKey); - Session::flash('success', $message); + session()->flash('success', $message); } } }