Removed use of book_id in activity

This commit is contained in:
Dan Brown 2020-11-07 23:15:13 +00:00
parent c157dc3490
commit ee7e1122d3
No known key found for this signature in database
GPG Key ID: 46D9F943C24A2EF9
10 changed files with 51 additions and 40 deletions

View File

@ -2,9 +2,11 @@
use BookStack\Auth\Permissions\PermissionService; use BookStack\Auth\Permissions\PermissionService;
use BookStack\Auth\User; use BookStack\Auth\User;
use BookStack\Entities\Chapter;
use BookStack\Entities\Entity; use BookStack\Entities\Entity;
use BookStack\Entities\Page;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Relations\Relation; use Illuminate\Database\Eloquent\Relations\Relation;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Log;
class ActivityService class ActivityService
@ -21,11 +23,11 @@ class ActivityService
} }
/** /**
* Add activity data to database. * Add activity data to database for an entity.
*/ */
public function add(Entity $entity, string $type, ?int $bookId = null) public function addForEntity(Entity $entity, string $type)
{ {
$activity = $this->newActivityForUser($type, $bookId); $activity = $this->newActivityForUser($type);
$entity->activity()->save($activity); $entity->activity()->save($activity);
$this->setNotification($type); $this->setNotification($type);
} }
@ -33,12 +35,11 @@ class ActivityService
/** /**
* Get a new activity instance for the current user. * Get a new activity instance for the current user.
*/ */
protected function newActivityForUser(string $key, ?int $bookId = null): Activity protected function newActivityForUser(string $type): Activity
{ {
return $this->activity->newInstance()->forceFill([ return $this->activity->newInstance()->forceFill([
'key' => strtolower($key), 'key' => strtolower($type),
'user_id' => $this->user->id, 'user_id' => $this->user->id,
'book_id' => $bookId ?? 0,
]); ]);
} }
@ -47,15 +48,13 @@ class ActivityService
* and instead uses the 'extra' field with the entities name. * and instead uses the 'extra' field with the entities name.
* Used when an entity is deleted. * Used when an entity is deleted.
*/ */
public function removeEntity(Entity $entity): Collection public function removeEntity(Entity $entity)
{ {
$activities = $entity->activity()->get();
$entity->activity()->update([ $entity->activity()->update([
'extra' => $entity->name, 'extra' => $entity->name,
'entity_id' => 0, 'entity_id' => 0,
'entity_type' => '', 'entity_type' => '',
]); ]);
return $activities;
} }
/** /**
@ -80,16 +79,27 @@ class ActivityService
*/ */
public function entityActivity(Entity $entity, int $count = 20, int $page = 1): array public function entityActivity(Entity $entity, int $count = 20, int $page = 1): array
{ {
/** @var [string => int[]] $queryIds */
$queryIds = [$entity->getMorphClass() => [$entity->id]];
if ($entity->isA('book')) { if ($entity->isA('book')) {
$query = $this->activity->newQuery()->where('book_id', '=', $entity->id); $queryIds[(new Chapter)->getMorphClass()] = $entity->chapters()->visible()->pluck('id');
} else { }
$query = $this->activity->newQuery()->where('entity_type', '=', $entity->getMorphClass()) if ($entity->isA('book') || $entity->isA('chapter')) {
->where('entity_id', '=', $entity->id); $queryIds[(new Page)->getMorphClass()] = $entity->pages()->visible()->pluck('id');
} }
$activity = $this->permissionService $query = $this->activity->newQuery();
->filterRestrictedEntityRelations($query, 'activities', 'entity_id', 'entity_type') $query->where(function (Builder $query) use ($queryIds) {
->orderBy('created_at', 'desc') foreach ($queryIds as $morphClass => $idArr) {
$query->orWhere(function (Builder $innerQuery) use ($morphClass, $idArr) {
$innerQuery->where('entity_type', '=', $morphClass)
->whereIn('entity_id', $idArr);
});
}
});
$activity = $query->orderBy('created_at', 'desc')
->with(['entity' => function (Relation $query) { ->with(['entity' => function (Relation $query) {
$query->withTrashed(); $query->withTrashed();
}, 'user.avatar']) }, 'user.avatar'])

View File

@ -2,6 +2,7 @@
use BookStack\Entities\Entity; use BookStack\Entities\Entity;
use League\CommonMark\CommonMarkConverter; use League\CommonMark\CommonMarkConverter;
use BookStack\Facades\Activity as ActivityService;
/** /**
* Class CommentRepo * Class CommentRepo
@ -44,7 +45,7 @@ class CommentRepo
$comment->parent_id = $parent_id; $comment->parent_id = $parent_id;
$entity->comments()->save($comment); $entity->comments()->save($comment);
Activity::add($entity, ActivityType::COMMENTED_ON, $entity->book->id); ActivityService::addForEntity($entity, ActivityType::COMMENTED_ON);
return $comment; return $comment;
} }

View File

@ -93,7 +93,7 @@ class BookRepo
{ {
$book = new Book(); $book = new Book();
$this->baseRepo->create($book, $input); $this->baseRepo->create($book, $input);
Activity::add($book, ActivityType::BOOK_CREATE, $book->id); Activity::addForEntity($book, ActivityType::BOOK_CREATE);
return $book; return $book;
} }
@ -103,7 +103,7 @@ class BookRepo
public function update(Book $book, array $input): Book public function update(Book $book, array $input): Book
{ {
$this->baseRepo->update($book, $input); $this->baseRepo->update($book, $input);
Activity::add($book, ActivityType::BOOK_UPDATE, $book->id); Activity::addForEntity($book, ActivityType::BOOK_UPDATE);
return $book; return $book;
} }
@ -133,7 +133,7 @@ class BookRepo
{ {
$trashCan = new TrashCan(); $trashCan = new TrashCan();
$trashCan->softDestroyBook($book); $trashCan->softDestroyBook($book);
Activity::add($book, ActivityType::BOOK_DELETE, $book->id); Activity::addForEntity($book, ActivityType::BOOK_DELETE);
$trashCan->autoClearOld(); $trashCan->autoClearOld();
} }

View File

@ -89,7 +89,7 @@ class BookshelfRepo
$shelf = new Bookshelf(); $shelf = new Bookshelf();
$this->baseRepo->create($shelf, $input); $this->baseRepo->create($shelf, $input);
$this->updateBooks($shelf, $bookIds); $this->updateBooks($shelf, $bookIds);
Activity::add($shelf, ActivityType::BOOKSHELF_CREATE); Activity::addForEntity($shelf, ActivityType::BOOKSHELF_CREATE);
return $shelf; return $shelf;
} }
@ -104,7 +104,7 @@ class BookshelfRepo
$this->updateBooks($shelf, $bookIds); $this->updateBooks($shelf, $bookIds);
} }
Activity::add($shelf, ActivityType::BOOKSHELF_UPDATE); Activity::addForEntity($shelf, ActivityType::BOOKSHELF_UPDATE);
return $shelf; return $shelf;
} }
@ -179,7 +179,7 @@ class BookshelfRepo
{ {
$trashCan = new TrashCan(); $trashCan = new TrashCan();
$trashCan->softDestroyShelf($shelf); $trashCan->softDestroyShelf($shelf);
Activity::add($shelf, ActivityType::BOOKSHELF_DELETE); Activity::addForEntity($shelf, ActivityType::BOOKSHELF_DELETE);
$trashCan->autoClearOld(); $trashCan->autoClearOld();
} }
} }

View File

@ -48,7 +48,7 @@ class ChapterRepo
$chapter->book_id = $parentBook->id; $chapter->book_id = $parentBook->id;
$chapter->priority = (new BookContents($parentBook))->getLastPriority() + 1; $chapter->priority = (new BookContents($parentBook))->getLastPriority() + 1;
$this->baseRepo->create($chapter, $input); $this->baseRepo->create($chapter, $input);
Activity::add($chapter, ActivityType::CHAPTER_CREATE, $parentBook->id); Activity::addForEntity($chapter, ActivityType::CHAPTER_CREATE);
return $chapter; return $chapter;
} }
@ -58,7 +58,7 @@ class ChapterRepo
public function update(Chapter $chapter, array $input): Chapter public function update(Chapter $chapter, array $input): Chapter
{ {
$this->baseRepo->update($chapter, $input); $this->baseRepo->update($chapter, $input);
Activity::add($chapter, ActivityType::CHAPTER_UPDATE, $chapter->book->id); Activity::addForEntity($chapter, ActivityType::CHAPTER_UPDATE);
return $chapter; return $chapter;
} }
@ -78,7 +78,7 @@ class ChapterRepo
{ {
$trashCan = new TrashCan(); $trashCan = new TrashCan();
$trashCan->softDestroyChapter($chapter); $trashCan->softDestroyChapter($chapter);
Activity::add($chapter, ActivityType::CHAPTER_DELETE, $chapter->book->id); Activity::addForEntity($chapter, ActivityType::CHAPTER_DELETE);
$trashCan->autoClearOld(); $trashCan->autoClearOld();
} }
@ -106,7 +106,7 @@ class ChapterRepo
$chapter->changeBook($parent->id); $chapter->changeBook($parent->id);
$chapter->rebuildPermissions(); $chapter->rebuildPermissions();
Activity::add($chapter, ActivityType::CHAPTER_MOVE, $parent->id); Activity::addForEntity($chapter, ActivityType::CHAPTER_MOVE);
return $parent; return $parent;
} }

View File

@ -169,7 +169,7 @@ class PageRepo
$draft->indexForSearch(); $draft->indexForSearch();
$draft->refresh(); $draft->refresh();
Activity::add($draft, ActivityType::PAGE_CREATE, $draft->book->id); Activity::addForEntity($draft, ActivityType::PAGE_CREATE);
return $draft; return $draft;
} }
@ -208,7 +208,7 @@ class PageRepo
$this->savePageRevision($page, $summary); $this->savePageRevision($page, $summary);
} }
Activity::add($page, ActivityType::PAGE_UPDATE, $page->book->id); Activity::addForEntity($page, ActivityType::PAGE_UPDATE);
return $page; return $page;
} }
@ -272,7 +272,7 @@ class PageRepo
{ {
$trashCan = new TrashCan(); $trashCan = new TrashCan();
$trashCan->softDestroyPage($page); $trashCan->softDestroyPage($page);
Activity::add($page, ActivityType::PAGE_DELETE, $page->book_id); Activity::addForEntity($page, ActivityType::PAGE_DELETE);
$trashCan->autoClearOld(); $trashCan->autoClearOld();
} }
@ -293,7 +293,7 @@ class PageRepo
$page->save(); $page->save();
$page->indexForSearch(); $page->indexForSearch();
Activity::add($page, ActivityType::PAGE_RESTORE, $page->book->id); Activity::addForEntity($page, ActivityType::PAGE_RESTORE);
return $page; return $page;
} }
@ -319,7 +319,7 @@ class PageRepo
$page->changeBook($parent instanceof Book ? $parent->id : $parent->book->id); $page->changeBook($parent instanceof Book ? $parent->id : $parent->book->id);
$page->rebuildPermissions(); $page->rebuildPermissions();
Activity::add($page, ActivityType::PAGE_MOVE, $page->book->id); Activity::addForEntity($page, ActivityType::PAGE_MOVE);
return $parent; return $parent;
} }

View File

@ -97,7 +97,7 @@ class BookController extends Controller
if ($bookshelf) { if ($bookshelf) {
$bookshelf->appendBook($book); $bookshelf->appendBook($book);
Activity::add($bookshelf, ActivityType::BOOKSHELF_UPDATE); Activity::addForEntity($bookshelf, ActivityType::BOOKSHELF_UPDATE);
} }
return redirect($book->getUrl()); return redirect($book->getUrl());

View File

@ -75,7 +75,7 @@ class BookSortController extends Controller
// Rebuild permissions and add activity for involved books. // Rebuild permissions and add activity for involved books.
$booksInvolved->each(function (Book $book) { $booksInvolved->each(function (Book $book) {
Activity::add($book, ActivityType::BOOK_SORT, $book->id); Activity::addForEntity($book, ActivityType::BOOK_SORT);
}); });
return redirect($book->getUrl()); return redirect($book->getUrl());

View File

@ -38,7 +38,7 @@ class CommandsTest extends TestCase
{ {
$this->asEditor(); $this->asEditor();
$page = Page::first(); $page = Page::first();
\Activity::add($page, ActivityType::PAGE_UPDATE, $page->book->id); \Activity::addForEntity($page, ActivityType::PAGE_UPDATE);
$this->assertDatabaseHas('activities', [ $this->assertDatabaseHas('activities', [
'key' => 'page_update', 'key' => 'page_update',

View File

@ -61,8 +61,8 @@ class UserProfileTest extends BrowserKitTest
$newUser = $this->getNewBlankUser(); $newUser = $this->getNewBlankUser();
$this->actingAs($newUser); $this->actingAs($newUser);
$entities = $this->createEntityChainBelongingToUser($newUser, $newUser); $entities = $this->createEntityChainBelongingToUser($newUser, $newUser);
Activity::add($entities['book'], ActivityType::BOOK_UPDATE, $entities['book']->id); Activity::addForEntity($entities['book'], ActivityType::BOOK_UPDATE);
Activity::add($entities['page'], ActivityType::PAGE_CREATE, $entities['book']->id); Activity::addForEntity($entities['page'], ActivityType::PAGE_CREATE);
$this->asAdmin()->visit('/user/' . $newUser->id) $this->asAdmin()->visit('/user/' . $newUser->id)
->seeInElement('#recent-user-activity', 'updated book') ->seeInElement('#recent-user-activity', 'updated book')
@ -75,8 +75,8 @@ class UserProfileTest extends BrowserKitTest
$newUser = $this->getNewBlankUser(); $newUser = $this->getNewBlankUser();
$this->actingAs($newUser); $this->actingAs($newUser);
$entities = $this->createEntityChainBelongingToUser($newUser, $newUser); $entities = $this->createEntityChainBelongingToUser($newUser, $newUser);
Activity::add($entities['book'], ActivityType::BOOK_UPDATE, $entities['book']->id); Activity::addForEntity($entities['book'], ActivityType::BOOK_UPDATE);
Activity::add($entities['page'], ActivityType::PAGE_CREATE, $entities['book']->id); Activity::addForEntity($entities['page'], ActivityType::PAGE_CREATE);
$this->asAdmin()->visit('/')->clickInElement('#recent-activity', $newUser->name) $this->asAdmin()->visit('/')->clickInElement('#recent-activity', $newUser->name)
->seePageIs('/user/' . $newUser->id) ->seePageIs('/user/' . $newUser->id)