diff --git a/app/Models/Concerns/NotificationContext.php b/app/Models/Concerns/NotificationContext.php new file mode 100644 index 0000000..ba93dd7 --- /dev/null +++ b/app/Models/Concerns/NotificationContext.php @@ -0,0 +1,29 @@ +notificationsAbout()->delete(); + }); + } + + /** + * Notifications having this model as a context + * + * @return MorphMany + */ + public function notificationsAbout() + { + return $this->morphMany(Notification::class, 'context'); + } +} diff --git a/app/Models/Concerns/Reportable.php b/app/Models/Concerns/Reportable.php new file mode 100644 index 0000000..cc84c2d --- /dev/null +++ b/app/Models/Concerns/Reportable.php @@ -0,0 +1,28 @@ +reportsOf()->delete(); + }); + } + + /** + * Reports of this user + * + * @return MorphMany + */ + public function reportsOf() + { + return $this->morphMany(ContentReport::class, 'object'); + } +} diff --git a/app/Models/Proposal.php b/app/Models/Proposal.php index 4f99837..bbcd675 100644 --- a/app/Models/Proposal.php +++ b/app/Models/Proposal.php @@ -2,13 +2,33 @@ namespace App\Models; +use App\Models\Concerns\NotificationContext; +use App\Models\Concerns\Reportable; +use Illuminate\Database\Eloquent\Collection; use Illuminate\Database\Eloquent\Model; /** * Change proposal + * + * @property Row[]|Collection $addedRows + * @property Row[]|Collection $removedRows */ class Proposal extends Model { + use Reportable; + use NotificationContext; + + protected static function boot() + { + parent::boot(); + + static::deleting(function(Proposal $self) { + // update refcounts + $self->addedRows()->decrement('refs'); + $self->removedRows()->decrement('refs'); + }); + } + /** Added rows */ public function addedRows() { diff --git a/app/Models/Revision.php b/app/Models/Revision.php index d1c1a8e..c3290ff 100644 --- a/app/Models/Revision.php +++ b/app/Models/Revision.php @@ -12,6 +12,16 @@ class Revision extends Model { use HasRelaquentRelationships; + protected static function boot() + { + parent::boot(); + + static::deleting(function(Revision $self) { + // update refcounts + $self->rows()->decrement('refs'); + }); + } + /** Included rows */ public function rows() { diff --git a/app/Models/Table.php b/app/Models/Table.php index 405711a..9d14e38 100644 --- a/app/Models/Table.php +++ b/app/Models/Table.php @@ -2,6 +2,8 @@ namespace App\Models; +use App\Models\Concerns\NotificationContext; +use App\Models\Concerns\Reportable; use Illuminate\Database\Eloquent\Model; /** @@ -9,6 +11,19 @@ use Illuminate\Database\Eloquent\Model; */ class Table extends Model { + use Reportable; + use NotificationContext; + + protected static function boot() + { + parent::boot(); + + static::deleting(function(Table $self) { + // update refcounts + $self->revisions()->decrement('refs'); + }); + } + /** Owning user */ public function owner() { diff --git a/app/Models/TableComment.php b/app/Models/TableComment.php index 255679d..e5c3d16 100644 --- a/app/Models/TableComment.php +++ b/app/Models/TableComment.php @@ -2,6 +2,8 @@ namespace App\Models; +use App\Models\Concerns\NotificationContext; +use App\Models\Concerns\Reportable; use Illuminate\Database\Eloquent\Model; /** @@ -9,6 +11,9 @@ use Illuminate\Database\Eloquent\Model; */ class TableComment extends Model { + use Reportable; + use NotificationContext; + /** Context data table */ public function table() { diff --git a/app/Models/User.php b/app/Models/User.php index 89b6cfe..ec73522 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -3,14 +3,21 @@ namespace App\Models; use AdamWathan\EloquentOAuth\OAuthIdentity; +use App\Models\Concerns\Reportable; +use Illuminate\Database\Eloquent\Collection; +use Illuminate\Database\Eloquent\Relations\HasMany; +use Illuminate\Database\Eloquent\Relations\MorphMany; use Illuminate\Notifications\Notifiable; use Illuminate\Foundation\Auth\User as Authenticatable; /** * A user in the application + * + * @property Proposal[]|Collection $proposals */ class User extends Authenticatable { + use Reportable; use Notifiable; /** @@ -31,6 +38,20 @@ class User extends Authenticatable 'password', 'remember_token', ]; + protected static function boot() + { + parent::boot(); + + static::deleting(function(User $self) { + // manually delete proposals to ensure row refcounts are updated + foreach ($self->proposals as $proposal) { + $proposal->delete(); + } + + $self->notificationsCaused()->delete(); + }); + } + /** Owned tables */ public function dataTables() { @@ -78,4 +99,14 @@ class User extends Authenticatable { return $this->hasMany(Notification::class); } + + /** + * Notifications caused by this user + * + * @return HasMany + */ + public function notificationsCaused() + { + return $this->hasMany(Notification::class, 'actor_id'); + } }