]> BookStack Code Mirror - bookstack/commitdiff
Simplified activity facade interface
authorDan Brown <redacted>
Thu, 19 Sep 2019 17:03:17 +0000 (18:03 +0100)
committerDan Brown <redacted>
Thu, 19 Sep 2019 17:03:17 +0000 (18:03 +0100)
Also cleaned up any other bits along the way.

app/Actions/Activity.php
app/Actions/ActivityService.php
app/Actions/ViewService.php
app/Entities/Book.php
app/Entities/Entity.php
app/Http/Controllers/BookController.php
app/Http/Controllers/BookshelfController.php
app/Http/Controllers/ChapterController.php
app/Http/Controllers/Controller.php
app/Http/Controllers/PageController.php
resources/views/books/list.blade.php

index 1ae1811e11461dbd94674c791b0c75143596d8b2..05f0129ddff7c322f8f900a3f5bdf4e6dba65fda 100644 (file)
@@ -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
 {
index f4f82a6f4dfbbac6b63c2fb42a15fad0631f77e9..8511e4d6d730769caf3ef5654acaf567b9d9ea7f 100644 (file)
@@ -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);
         }
     }
 }
index 532f31c423ca06ae026bb931f03deb84e16bfc87..b576483a0293de3f726382608fc647baad40905e 100644 (file)
@@ -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');
 
index 7d3d5e4ae98bf0e670c436dba1df93fb7c86d92b..ce4c4b90e08229a9b2e9efc690a14b7c68887f40 100644 (file)
@@ -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;
index 482d217662ae20b54f62578d88367b8daacb1098..480d7caa730a5392fbe4f3a94b2d06e69f64a7c5 100644 (file)
@@ -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
index 7a0ee0f0ebc3fdeb47ad0c269712252e80b2a2dd..1f9caf75641aa6c885b975946f1dcd51ec3d91f2 100644 (file)
@@ -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);
index c369ce6557cd06f48546761e79b59a031d62a473..9bb94484c76b3eb1d96685d3a62049c182b7710f 100644 (file)
@@ -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);
index a4a1dbf24cf4c26a27aa0f6c958738460d599f34..8090556c97b6e27182c108784dc138a0d22134ab 100644 (file)
@@ -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());
     }
index e34cb7e5941a137a11bbc6d20d73f97ef1af4a75..7bca77495f4c11e92449b9e63d5c19db39cce0dd 100644 (file)
@@ -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;
index 30f58ab6aee09621845440cf557d78fa1a5e8ee8..37b5750100f812d0a907f7edd3ccfd91e8072517 100644 (file)
@@ -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());
     }
index 871d931f17953496478a7f456e910c5ec3d6ee36..42a2757f94e0e949d536791f6dd79f4daa5ea38a 100644 (file)
@@ -4,7 +4,11 @@
         <h1 class="list-heading">{{ trans('entities.books') }}</h1>
         <div class="text-m-right my-m">
 
-            @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'])
 
         </div>
     </div>