Skip to main content

Black Friday 2025! Only until December 1st: coupon FRIDAY25 for 40% off Yearly/Lifetime membership!

Read more here

pinkary-project/pinkary.com

1505 stars
1 code files
View pinkary-project/pinkary.com on GitHub

app/Observers/QuestionObserver.php

Open in GitHub
use App\EventActions\UpdateQuestionHashtags;
use App\Models\Question;
use App\Models\User;
use App\Notifications\QuestionAnswered;
use App\Notifications\QuestionCreated;
use App\Notifications\UserMentioned;
 
final readonly class QuestionObserver
{
public function created(Question $question): void
{
if ($question->isSharedUpdate()) {
if ($question->parent_id !== null) {
$question->loadMissing('parent.to');
if ($question->parent?->to_id !== $question->to_id) {
$question->parent?->to->notify(new QuestionCreated($question));
}
}
 
$question->mentions()->each->notify(new UserMentioned($question));
} else {
$question->loadMissing('to');
$question->to->notify(new QuestionCreated($question));
}
 
(new UpdateQuestionHashtags($question))->handle();
}
 
public function updated(Question $question): void
{
$question->loadMissing('from', 'to');
 
if ($question->is_ignored || $question->is_reported) {
$this->deleted($question);
 
return;
}
 
if ($question->answer !== null) {
$question->to->notifications()->whereJsonContains('data->question_id', $question->id)->delete();
}
 
if ($question->isDirty(['answer', 'content'])) {
(new UpdateQuestionHashtags($question))->handle();
}
 
if ($question->isDirty('answer') === false) {
return;
}
 
if ($question->from->id === $question->to->id) {
return;
}
 
$question->from->notify(new QuestionAnswered($question));
$question->mentions()->each->notify(new UserMentioned($question));
}
 
public function deleted(Question $question): void
{
$question->to->notifications()->whereJsonContains('data->question_id', $question->id)->delete();
$question->from->notifications()->whereJsonContains('data->question_id', $question->id)->delete();
 
$question->mentions()->each(function (User $user) use ($question): void {
$user->notifications()->whereJsonContains('data->question_id', $question->id)->delete();
});
 
$question->loadMissing('children');
 
$question->children->each->delete();
 
$question->hashtags()->detach();
}
}

We'd Love Your Feedback

Tell us what you like or what we can improve

Feel free to share anything you like or dislike about this page or the platform in general.