1
0
mirror of https://github.com/BookStackApp/BookStack.git synced 2024-10-30 07:32:39 +01:00

Simplified activity facade interface

Also cleaned up any other bits along the way.
This commit is contained in:
Dan Brown 2019-09-19 18:03:17 +01:00
parent 2a2cc858f0
commit 615b2de433
No known key found for this signature in database
GPG Key ID: 46D9F943C24A2EF9
11 changed files with 68 additions and 46 deletions

View 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
{

View 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);
}
}
}

View 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');

View 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;

View 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

View 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);

View 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);

View 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());
}

View 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;

View 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());
}

View 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>