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.
This commit is contained in:
Dan Brown 2023-07-18 15:07:31 +01:00
parent 7504ad32a7
commit 422e50302a
No known key found for this signature in database
GPG Key ID: 46D9F943C24A2EF9
4 changed files with 21 additions and 9 deletions

View File

@ -27,6 +27,10 @@ class ActivityType
const BOOKSHELF_DELETE = 'bookshelf_delete'; const BOOKSHELF_DELETE = 'bookshelf_delete';
const COMMENTED_ON = 'commented_on'; 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 PERMISSIONS_UPDATE = 'permissions_update';
const REVISION_RESTORE = 'revision_restore'; const REVISION_RESTORE = 'revision_restore';

View File

@ -33,6 +33,7 @@ class CommentRepo
$comment->parent_id = $parent_id; $comment->parent_id = $parent_id;
$entity->comments()->save($comment); $entity->comments()->save($comment);
ActivityService::add(ActivityType::COMMENT_CREATE, $comment);
ActivityService::add(ActivityType::COMMENTED_ON, $entity); ActivityService::add(ActivityType::COMMENTED_ON, $entity);
return $comment; return $comment;
@ -48,6 +49,8 @@ class CommentRepo
$comment->html = $this->commentToHtml($text); $comment->html = $this->commentToHtml($text);
$comment->save(); $comment->save();
ActivityService::add(ActivityType::COMMENT_UPDATE, $comment);
return $comment; return $comment;
} }
@ -57,6 +60,8 @@ class CommentRepo
public function delete(Comment $comment): void public function delete(Comment $comment): void
{ {
$comment->delete(); $comment->delete();
ActivityService::add(ActivityType::COMMENT_DELETE, $comment);
} }
/** /**

View File

@ -13,8 +13,10 @@ use Illuminate\Database\Eloquent\Relations\MorphTo;
* @property string $html * @property string $html
* @property int|null $parent_id * @property int|null $parent_id
* @property int $local_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 HasFactory;
use HasCreatorAndUpdater; use HasCreatorAndUpdater;
@ -57,4 +59,9 @@ class Comment extends Model
{ {
return $this->updated_at->diffForHumans(); 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})";
}
} }

View File

@ -6,6 +6,7 @@ use BookStack\Activity\DispatchWebhookJob;
use BookStack\Activity\Models\Activity; use BookStack\Activity\Models\Activity;
use BookStack\Activity\Models\Loggable; use BookStack\Activity\Models\Loggable;
use BookStack\Activity\Models\Webhook; use BookStack\Activity\Models\Webhook;
use BookStack\App\Model;
use BookStack\Entities\Models\Entity; use BookStack\Entities\Models\Entity;
use BookStack\Facades\Theme; use BookStack\Facades\Theme;
use BookStack\Theming\ThemeEvents; use BookStack\Theming\ThemeEvents;
@ -16,8 +17,6 @@ class ActivityLogger
{ {
/** /**
* Add a generic activity event to the database. * Add a generic activity event to the database.
*
* @param string|Loggable $detail
*/ */
public function add(string $type, $detail = '') public function add(string $type, $detail = '')
{ {
@ -55,7 +54,7 @@ class ActivityLogger
* 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) public function removeEntity(Entity $entity): void
{ {
$entity->activity()->update([ $entity->activity()->update([
'detail' => $entity->name, 'detail' => $entity->name,
@ -76,10 +75,7 @@ class ActivityLogger
} }
} }
/** protected function dispatchWebhooks(string $type, string|Loggable $detail): void
* @param string|Loggable $detail
*/
protected function dispatchWebhooks(string $type, $detail): void
{ {
$webhooks = Webhook::query() $webhooks = Webhook::query()
->whereHas('trackedEvents', function (Builder $query) use ($type) { ->whereHas('trackedEvents', function (Builder $query) use ($type) {
@ -98,7 +94,7 @@ class ActivityLogger
* Log out a failed login attempt, Providing the given username * Log out a failed login attempt, Providing the given username
* as part of the message if the '%u' string is used. * 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'); $message = config('logging.failed_login.message');
if (!$message) { if (!$message) {