some work on models

pull/26/head
Ondřej Hruška 6 years ago
parent 6fd1d7eb89
commit 7493d3e176
Signed by: MightyPork
GPG Key ID: 2C5FD5035250423D
  1. 29
      app/Models/Concerns/NotificationContext.php
  2. 28
      app/Models/Concerns/Reportable.php
  3. 20
      app/Models/Proposal.php
  4. 10
      app/Models/Revision.php
  5. 15
      app/Models/Table.php
  6. 5
      app/Models/TableComment.php
  7. 31
      app/Models/User.php

@ -0,0 +1,29 @@
<?php
namespace App\Models\Concerns;
use App\Models\ContentReport;
use App\Models\Notification;
use Illuminate\Database\Eloquent\Relations\MorphMany;
trait NotificationContext
{
public function bootNotificationContext()
{
static::deleting(function(NotificationContext $self) {
$self->notificationsAbout()->delete();
});
}
/**
* Notifications having this model as a context
*
* @return MorphMany
*/
public function notificationsAbout()
{
return $this->morphMany(Notification::class, 'context');
}
}

@ -0,0 +1,28 @@
<?php
namespace App\Models\Concerns;
use App\Models\ContentReport;
use Illuminate\Database\Eloquent\Relations\MorphMany;
trait Reportable
{
public function bootReportable()
{
static::deleting(function(Reportable $self) {
$self->reportsOf()->delete();
});
}
/**
* Reports of this user
*
* @return MorphMany
*/
public function reportsOf()
{
return $this->morphMany(ContentReport::class, 'object');
}
}

@ -2,13 +2,33 @@
namespace App\Models; namespace App\Models;
use App\Models\Concerns\NotificationContext;
use App\Models\Concerns\Reportable;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
/** /**
* Change proposal * Change proposal
*
* @property Row[]|Collection $addedRows
* @property Row[]|Collection $removedRows
*/ */
class Proposal extends Model 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 */ /** Added rows */
public function addedRows() public function addedRows()
{ {

@ -12,6 +12,16 @@ class Revision extends Model
{ {
use HasRelaquentRelationships; use HasRelaquentRelationships;
protected static function boot()
{
parent::boot();
static::deleting(function(Revision $self) {
// update refcounts
$self->rows()->decrement('refs');
});
}
/** Included rows */ /** Included rows */
public function rows() public function rows()
{ {

@ -2,6 +2,8 @@
namespace App\Models; namespace App\Models;
use App\Models\Concerns\NotificationContext;
use App\Models\Concerns\Reportable;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
/** /**
@ -9,6 +11,19 @@ use Illuminate\Database\Eloquent\Model;
*/ */
class Table extends 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 */ /** Owning user */
public function owner() public function owner()
{ {

@ -2,6 +2,8 @@
namespace App\Models; namespace App\Models;
use App\Models\Concerns\NotificationContext;
use App\Models\Concerns\Reportable;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
/** /**
@ -9,6 +11,9 @@ use Illuminate\Database\Eloquent\Model;
*/ */
class TableComment extends Model class TableComment extends Model
{ {
use Reportable;
use NotificationContext;
/** Context data table */ /** Context data table */
public function table() public function table()
{ {

@ -3,14 +3,21 @@
namespace App\Models; namespace App\Models;
use AdamWathan\EloquentOAuth\OAuthIdentity; 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\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable; use Illuminate\Foundation\Auth\User as Authenticatable;
/** /**
* A user in the application * A user in the application
*
* @property Proposal[]|Collection $proposals
*/ */
class User extends Authenticatable class User extends Authenticatable
{ {
use Reportable;
use Notifiable; use Notifiable;
/** /**
@ -31,6 +38,20 @@ class User extends Authenticatable
'password', 'remember_token', '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 */ /** Owned tables */
public function dataTables() public function dataTables()
{ {
@ -78,4 +99,14 @@ class User extends Authenticatable
{ {
return $this->hasMany(Notification::class); return $this->hasMany(Notification::class);
} }
/**
* Notifications caused by this user
*
* @return HasMany
*/
public function notificationsCaused()
{
return $this->hasMany(Notification::class, 'actor_id');
}
} }

Loading…
Cancel
Save