From: Dan Brown Date: Thu, 19 Sep 2019 17:03:17 +0000 (+0100) Subject: Simplified activity facade interface X-Git-Tag: v0.28.0~1^2~87 X-Git-Url: http://source.bookstackapp.com/bookstack/commitdiff_plain/615b2de43396978777a6c82b595212289ac8c8e8?ds=inline;hp=-c Simplified activity facade interface Also cleaned up any other bits along the way. --- 615b2de43396978777a6c82b595212289ac8c8e8 diff --git a/app/Actions/Activity.php b/app/Actions/Activity.php index 1ae1811e1..05f0129dd 100644 --- a/app/Actions/Activity.php +++ b/app/Actions/Activity.php @@ -3,13 +3,18 @@ namespace BookStack\Actions; use BookStack\Auth\User; +use BookStack\Entities\Entity; use BookStack\Model; /** - * @property string key - * @property \User user - * @property \Entity entity - * @property string extra + * @property string $key + * @property User $user + * @property Entity $entity + * @property string $extra + * @property string $entity_type + * @property int $entity_id + * @property int $user_id + * @property int $book_id */ class Activity extends Model { diff --git a/app/Actions/ActivityService.php b/app/Actions/ActivityService.php index f4f82a6f4..8511e4d6d 100644 --- a/app/Actions/ActivityService.php +++ b/app/Actions/ActivityService.php @@ -2,7 +2,6 @@ use BookStack\Auth\Permissions\PermissionService; use BookStack\Entities\Entity; -use Session; class ActivityService { @@ -12,7 +11,7 @@ class ActivityService /** * ActivityService constructor. - * @param \BookStack\Actions\Activity $activity + * @param Activity $activity * @param PermissionService $permissionService */ public function __construct(Activity $activity, PermissionService $permissionService) @@ -24,42 +23,46 @@ class ActivityService /** * Add activity data to database. - * @param Entity $entity - * @param $activityKey + * @param \BookStack\Entities\Entity $entity + * @param string $activityKey * @param int $bookId - * @param bool $extra */ - public function add(Entity $entity, $activityKey, $bookId = 0, $extra = false) + public function add(Entity $entity, string $activityKey, int $bookId = null) { - $activity = $this->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 @@ -90,7 +93,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); } @@ -171,7 +178,7 @@ class ActivityService $notificationTextKey = 'activities.' . $activityKey . '_notification'; if (trans()->has($notificationTextKey)) { $message = trans($notificationTextKey); - Session::flash('success', $message); + session()->flash('success', $message); } } } diff --git a/app/Actions/ViewService.php b/app/Actions/ViewService.php index 532f31c42..b576483a0 100644 --- a/app/Actions/ViewService.php +++ b/app/Actions/ViewService.php @@ -3,6 +3,7 @@ use BookStack\Auth\Permissions\PermissionService; use BookStack\Entities\Entity; use BookStack\Entities\EntityProvider; +use DB; use Illuminate\Support\Collection; class ViewService @@ -13,8 +14,8 @@ class ViewService /** * ViewService constructor. - * @param \BookStack\Actions\View $view - * @param \BookStack\Auth\Permissions\PermissionService $permissionService + * @param View $view + * @param PermissionService $permissionService * @param EntityProvider $entityProvider */ public function __construct(View $view, PermissionService $permissionService, EntityProvider $entityProvider) @@ -26,7 +27,7 @@ class ViewService /** * Add a view to the given entity. - * @param Entity $entity + * @param \BookStack\Entities\Entity $entity * @return int */ public function add(Entity $entity) @@ -64,7 +65,7 @@ class ViewService $skipCount = $count * $page; $query = $this->permissionService ->filterRestrictedEntityRelations($this->view, 'views', 'viewable_id', 'viewable_type', $action) - ->select('*', 'viewable_id', 'viewable_type', \DB::raw('SUM(views) as view_count')) + ->select('*', 'viewable_id', 'viewable_type', DB::raw('SUM(views) as view_count')) ->groupBy('viewable_id', 'viewable_type') ->orderBy('view_count', 'desc'); diff --git a/app/Entities/Book.php b/app/Entities/Book.php index 7d3d5e4ae..ce4c4b90e 100644 --- a/app/Entities/Book.php +++ b/app/Entities/Book.php @@ -2,6 +2,13 @@ use BookStack\Uploads\Image; +/** + * Class Book + * @property string $description + * @property int $image_id + * @property Image|null $cover + * @package BookStack\Entities + */ class Book extends Entity { public $searchFactor = 2; diff --git a/app/Entities/Entity.php b/app/Entities/Entity.php index 482d21766..480d7caa7 100644 --- a/app/Entities/Entity.php +++ b/app/Entities/Entity.php @@ -15,7 +15,7 @@ use Illuminate\Database\Eloquent\Relations\MorphMany; * The base class for book-like items such as pages, chapters & books. * This is not a database model in itself but extended. * - * @property integer $id + * @property int $id * @property string $name * @property string $slug * @property Carbon $created_at diff --git a/app/Http/Controllers/BookController.php b/app/Http/Controllers/BookController.php index 7a0ee0f0e..1f9caf756 100644 --- a/app/Http/Controllers/BookController.php +++ b/app/Http/Controllers/BookController.php @@ -58,11 +58,6 @@ class BookController extends Controller $view = setting()->getUser($this->currentUser, 'books_view_type', config('app.views.books')); $sort = setting()->getUser($this->currentUser, 'books_sort', 'name'); $order = setting()->getUser($this->currentUser, 'books_sort_order', 'asc'); - $sortOptions = [ - 'name' => trans('common.sort_name'), - 'created_at' => trans('common.sort_created_at'), - 'updated_at' => trans('common.sort_updated_at'), - ]; $books = $this->bookRepo->getAllPaginated('book', 18, $sort, $order); $recents = $this->signedIn ? $this->bookRepo->getRecentlyViewed('book', 4, 0) : false; @@ -80,7 +75,6 @@ class BookController extends Controller 'view' => $view, 'sort' => $sort, 'order' => $order, - 'sortOptions' => $sortOptions, ]); } @@ -114,6 +108,7 @@ class BookController extends Controller * @throws NotFoundException * @throws ImageUploadException * @throws ValidationException + * @throws Throwable */ public function store(Request $request, string $shelfSlug = null) { @@ -192,6 +187,7 @@ class BookController extends Controller * @throws ImageUploadException * @throws NotFoundException * @throws ValidationException + * @throws Throwable */ public function update(Request $request, string $slug) { @@ -340,7 +336,7 @@ class BookController extends Controller { $book = $this->bookRepo->getBySlug($bookSlug); $this->checkOwnablePermission('book-delete', $book); - Activity::addMessage('book_delete', 0, $book->name); + Activity::addMessage('book_delete', $book->name); if ($book->cover) { $this->imageRepo->destroyImage($book->cover); diff --git a/app/Http/Controllers/BookshelfController.php b/app/Http/Controllers/BookshelfController.php index c369ce655..9bb94484c 100644 --- a/app/Http/Controllers/BookshelfController.php +++ b/app/Http/Controllers/BookshelfController.php @@ -212,7 +212,7 @@ class BookshelfController extends Controller { $shelf = $this->entityRepo->getEntityBySlug('bookshelf', $slug); /** @var $shelf Bookshelf */ $this->checkOwnablePermission('bookshelf-delete', $shelf); - Activity::addMessage('bookshelf_delete', 0, $shelf->name); + Activity::addMessage('bookshelf_delete', $shelf->name); if ($shelf->cover) { $this->imageRepo->destroyImage($shelf->cover); diff --git a/app/Http/Controllers/ChapterController.php b/app/Http/Controllers/ChapterController.php index a4a1dbf24..8090556c9 100644 --- a/app/Http/Controllers/ChapterController.php +++ b/app/Http/Controllers/ChapterController.php @@ -143,7 +143,7 @@ class ChapterController extends Controller $chapter = $this->entityRepo->getEntityBySlug('chapter', $chapterSlug, $bookSlug); $book = $chapter->book; $this->checkOwnablePermission('chapter-delete', $chapter); - Activity::addMessage('chapter_delete', $book->id, $chapter->name); + Activity::addMessage('chapter_delete', $chapter->name, $book->id); $this->entityRepo->destroyChapter($chapter); return redirect($book->getUrl()); } diff --git a/app/Http/Controllers/Controller.php b/app/Http/Controllers/Controller.php index e34cb7e59..7bca77495 100644 --- a/app/Http/Controllers/Controller.php +++ b/app/Http/Controllers/Controller.php @@ -3,6 +3,8 @@ namespace BookStack\Http\Controllers; use BookStack\Auth\User; +use BookStack\Entities\Entity; +use BookStack\Facades\Activity; use BookStack\Ownable; use Illuminate\Foundation\Bus\DispatchesJobs; use Illuminate\Foundation\Validation\ValidatesRequests; diff --git a/app/Http/Controllers/PageController.php b/app/Http/Controllers/PageController.php index 30f58ab6a..37b575010 100644 --- a/app/Http/Controllers/PageController.php +++ b/app/Http/Controllers/PageController.php @@ -358,7 +358,7 @@ class PageController extends Controller $this->checkOwnablePermission('page-delete', $page); $this->pageRepo->destroyPage($page); - Activity::addMessage('page_delete', $book->id, $page->name); + Activity::addMessage('page_delete', $page->name, $book->id); $this->showSuccessNotification( trans('entities.pages_delete_success')); return redirect($book->getUrl()); } diff --git a/resources/views/books/list.blade.php b/resources/views/books/list.blade.php index 871d931f1..42a2757f9 100644 --- a/resources/views/books/list.blade.php +++ b/resources/views/books/list.blade.php @@ -4,7 +4,11 @@

{{ trans('entities.books') }}

- @include('partials.sort', ['options' => $sortOptions, 'order' => $order, 'sort' => $sort, 'type' => 'books']) + @include('partials.sort', ['options' => [ + 'name' => trans('common.sort_name'), + 'created_at' => trans('common.sort_created_at'), + 'updated_at' => trans('common.sort_updated_at'), + ], 'order' => $order, 'sort' => $sort, 'type' => 'books'])