diff --git a/framework/core/src/Api/Resource/DiscussionResource.php b/framework/core/src/Api/Resource/DiscussionResource.php index f61780fad4..2899d9b050 100644 --- a/framework/core/src/Api/Resource/DiscussionResource.php +++ b/framework/core/src/Api/Resource/DiscussionResource.php @@ -242,16 +242,7 @@ public function sorts(): array /** @param Discussion $model */ public function creating(object $model, \Tobyz\JsonApiServer\Context $context): ?object { - $actor = $context->getActor(); - - $model->created_at = Carbon::now(); - $model->user_id = $actor->id; - - $model->setRelation('user', $actor); - - $model->raise(new Started($model)); - - return $model; + return Discussion::start(null, $context->getActor()); } /** @param Discussion $model */ diff --git a/framework/core/src/Discussion/Discussion.php b/framework/core/src/Discussion/Discussion.php index a74fdd8c89..b1fba1786f 100644 --- a/framework/core/src/Discussion/Discussion.php +++ b/framework/core/src/Discussion/Discussion.php @@ -114,6 +114,24 @@ public static function boot() }); } + /** + * Start a new discussion. Raises the DiscussionWasStarted event. + */ + public static function start(?string $title, User $user): static + { + $discussion = new static; + + $discussion->title = $title; + $discussion->created_at = Carbon::now(); + $discussion->user_id = $user->id; + + $discussion->setRelation('user', $user); + + $discussion->raise(new Event\Started($discussion)); + + return $discussion; + } + public function rename(string $title): static { if ($this->title !== $title) {