From 422e50302a05ac6cc0d0a02fa9155b3379ea4ddd Mon Sep 17 00:00:00 2001 From: Dan Brown Date: Tue, 18 Jul 2023 15:07:31 +0100 Subject: [PATCH] Comments: Added extra comment-specific activities Kept existing "COMMENTED_ON" activity for upgrade compatibility, specifically for existing webhook usage and for showing comment activities in activity lists. Precursor to content notifications. Currently untested. Also applied some type updates. --- app/Activity/ActivityType.php | 4 ++++ app/Activity/CommentRepo.php | 5 +++++ app/Activity/Models/Comment.php | 9 ++++++++- app/Activity/Tools/ActivityLogger.php | 12 ++++-------- 4 files changed, 21 insertions(+), 9 deletions(-) diff --git a/app/Activity/ActivityType.php b/app/Activity/ActivityType.php index 3018df1d3..09b2ae73c 100644 --- a/app/Activity/ActivityType.php +++ b/app/Activity/ActivityType.php @@ -27,6 +27,10 @@ class ActivityType const BOOKSHELF_DELETE = 'bookshelf_delete'; const COMMENTED_ON = 'commented_on'; + const COMMENT_CREATE = 'comment_create'; + const COMMENT_UPDATE = 'comment_update'; + const COMMENT_DELETE = 'comment_delete'; + const PERMISSIONS_UPDATE = 'permissions_update'; const REVISION_RESTORE = 'revision_restore'; diff --git a/app/Activity/CommentRepo.php b/app/Activity/CommentRepo.php index 2aabab79d..ce2950e4d 100644 --- a/app/Activity/CommentRepo.php +++ b/app/Activity/CommentRepo.php @@ -33,6 +33,7 @@ class CommentRepo $comment->parent_id = $parent_id; $entity->comments()->save($comment); + ActivityService::add(ActivityType::COMMENT_CREATE, $comment); ActivityService::add(ActivityType::COMMENTED_ON, $entity); return $comment; @@ -48,6 +49,8 @@ class CommentRepo $comment->html = $this->commentToHtml($text); $comment->save(); + ActivityService::add(ActivityType::COMMENT_UPDATE, $comment); + return $comment; } @@ -57,6 +60,8 @@ class CommentRepo public function delete(Comment $comment): void { $comment->delete(); + + ActivityService::add(ActivityType::COMMENT_DELETE, $comment); } /** diff --git a/app/Activity/Models/Comment.php b/app/Activity/Models/Comment.php index e513bdf3d..7aea6124a 100644 --- a/app/Activity/Models/Comment.php +++ b/app/Activity/Models/Comment.php @@ -13,8 +13,10 @@ use Illuminate\Database\Eloquent\Relations\MorphTo; * @property string $html * @property int|null $parent_id * @property int $local_id + * @property string $entity_type + * @property int $entity_id */ -class Comment extends Model +class Comment extends Model implements Loggable { use HasFactory; use HasCreatorAndUpdater; @@ -57,4 +59,9 @@ class Comment extends Model { return $this->updated_at->diffForHumans(); } + + public function logDescriptor(): string + { + return "Comment #{$this->local_id} (ID: {$this->id}) for {$this->entity_type} (ID: {$this->entity_id})"; + } } diff --git a/app/Activity/Tools/ActivityLogger.php b/app/Activity/Tools/ActivityLogger.php index 105f9c408..315e8bfdf 100644 --- a/app/Activity/Tools/ActivityLogger.php +++ b/app/Activity/Tools/ActivityLogger.php @@ -6,6 +6,7 @@ use BookStack\Activity\DispatchWebhookJob; use BookStack\Activity\Models\Activity; use BookStack\Activity\Models\Loggable; use BookStack\Activity\Models\Webhook; +use BookStack\App\Model; use BookStack\Entities\Models\Entity; use BookStack\Facades\Theme; use BookStack\Theming\ThemeEvents; @@ -16,8 +17,6 @@ class ActivityLogger { /** * Add a generic activity event to the database. - * - * @param string|Loggable $detail */ public function add(string $type, $detail = '') { @@ -55,7 +54,7 @@ class ActivityLogger * and instead uses the 'extra' field with the entities name. * Used when an entity is deleted. */ - public function removeEntity(Entity $entity) + public function removeEntity(Entity $entity): void { $entity->activity()->update([ 'detail' => $entity->name, @@ -76,10 +75,7 @@ class ActivityLogger } } - /** - * @param string|Loggable $detail - */ - protected function dispatchWebhooks(string $type, $detail): void + protected function dispatchWebhooks(string $type, string|Loggable $detail): void { $webhooks = Webhook::query() ->whereHas('trackedEvents', function (Builder $query) use ($type) { @@ -98,7 +94,7 @@ class ActivityLogger * Log out a failed login attempt, Providing the given username * as part of the message if the '%u' string is used. */ - public function logFailedLogin(string $username) + public function logFailedLogin(string $username): void { $message = config('logging.failed_login.message'); if (!$message) {