diff --git a/framework/core/src/Discussion/Discussion.php b/framework/core/src/Discussion/Discussion.php index 930cf3084..e73533024 100644 --- a/framework/core/src/Discussion/Discussion.php +++ b/framework/core/src/Discussion/Discussion.php @@ -21,6 +21,7 @@ use Flarum\Discussion\Event\Restored; use Flarum\Discussion\Event\Started; use Flarum\Event\GetModelIsPrivate; use Flarum\Foundation\EventGeneratorTrait; +use Flarum\Notification\Notification; use Flarum\Post\MergeableInterface; use Flarum\Post\Post; use Flarum\User\User; @@ -97,8 +98,18 @@ class Discussion extends AbstractModel { parent::boot(); + static::deleting(function (Discussion $discussion) { + Notification::whereSubjectModel(Post::class) + ->whereIn('subject_id', function ($query) use ($discussion) { + $query->select('id')->from('posts')->where('discussion_id', $discussion->id); + }) + ->delete(); + }); + static::deleted(function (Discussion $discussion) { $discussion->raise(new Deleted($discussion)); + + Notification::whereSubject($discussion)->delete(); }); static::saving(function (Discussion $discussion) { diff --git a/framework/core/src/Notification/Notification.php b/framework/core/src/Notification/Notification.php index 4c97a93aa..a29058ab9 100644 --- a/framework/core/src/Notification/Notification.php +++ b/framework/core/src/Notification/Notification.php @@ -169,6 +169,35 @@ class Notification extends AbstractModel }); } + /** + * Scope the query to include only notifications that have the given + * subject. + * + * @param Builder $query + * @param object $model + */ + public function scopeWhereSubject(Builder $query, $model) + { + $query->whereSubjectModel(get_class($model)) + ->where('subject_id', $model->id); + } + + /** + * Scope the query to include only notification types that use the given + * subject model. + * + * @param Builder $query + * @param string $class + */ + public function scopeWhereSubjectModel(Builder $query, string $class) + { + $notificationTypes = array_filter(Notification::getSubjectModels(), function ($modelClass) use ($class) { + return $modelClass === $class or is_subclass_of($class, $modelClass); + }); + + $query->whereIn('type', array_keys($notificationTypes)); + } + /** * Get the type-to-subject-model map. * diff --git a/framework/core/src/Post/Post.php b/framework/core/src/Post/Post.php index 987ef47fa..24aeac5f2 100644 --- a/framework/core/src/Post/Post.php +++ b/framework/core/src/Post/Post.php @@ -17,6 +17,7 @@ use Flarum\Discussion\Discussion; use Flarum\Event\GetModelIsPrivate; use Flarum\Event\ScopeModelVisibility; use Flarum\Foundation\EventGeneratorTrait; +use Flarum\Notification\Notification; use Flarum\Post\Event\Deleted; use Flarum\User\User; use Illuminate\Database\Eloquent\Builder; @@ -106,6 +107,8 @@ class Post extends AbstractModel static::deleted(function (Post $post) { $post->raise(new Deleted($post)); + + Notification::whereSubject($post)->delete(); }); static::addGlobalScope(new RegisteredTypesScope); diff --git a/framework/core/src/User/User.php b/framework/core/src/User/User.php index 323ee59f5..8bf1ef1b2 100644 --- a/framework/core/src/User/User.php +++ b/framework/core/src/User/User.php @@ -125,6 +125,8 @@ class User extends AbstractModel static::deleted(function (User $user) { $user->raise(new Deleted($user)); + + Notification::whereSubject($user)->delete(); }); static::$dispatcher->dispatch(