diff --git a/app/Models/Changeset.php b/app/Models/Changeset.php index 26d9335..770256f 100644 --- a/app/Models/Changeset.php +++ b/app/Models/Changeset.php @@ -4,6 +4,9 @@ namespace App\Models; use Illuminate\Database\Eloquent\Model; +/** + * A patch describing changes to apply or applied to a data table. + */ class Changeset extends Model { /** Parent data table */ @@ -17,4 +20,32 @@ class Changeset extends Model { return $this->belongsTo(User::class, 'author_id'); } + + /** Reports of this changeset */ + public function reportsOf() + { + return $this->morphMany(ContentReport::class, 'object'); + } + + /** + * Scope for pending changesets + * + * @param \Illuminate\Database\Eloquent\Builder $query + * @return \Illuminate\Database\Eloquent\Builder + */ + public function scopePending($query) + { + return $query->where('applied', false); + } + + /** + * Scope for applied changesets + * + * @param \Illuminate\Database\Eloquent\Builder $query + * @return \Illuminate\Database\Eloquent\Builder + */ + public function scopeApplied($query) + { + return $query->where('applied', true); + } } diff --git a/app/Models/ContentReport.php b/app/Models/ContentReport.php new file mode 100644 index 0000000..d9fce75 --- /dev/null +++ b/app/Models/ContentReport.php @@ -0,0 +1,23 @@ +belongsTo(User::class, 'author_id'); + } + + /** Authoring user */ + public function object() + { + return $this->morphTo(); + } +} diff --git a/app/Models/DataRow.php b/app/Models/DataRow.php index 8b47458..1b01f77 100644 --- a/app/Models/DataRow.php +++ b/app/Models/DataRow.php @@ -4,6 +4,9 @@ namespace App\Models; use Illuminate\Database\Eloquent\Model; +/** + * Row in a data table + */ class DataRow extends Model { /** Parent data table */ diff --git a/app/Models/DataTable.php b/app/Models/DataTable.php index 34acb71..9b73f37 100644 --- a/app/Models/DataTable.php +++ b/app/Models/DataTable.php @@ -4,8 +4,18 @@ namespace App\Models; use Illuminate\Database\Eloquent\Model; +/** + * A data table object (referencing changesets and rows) + */ class DataTable extends Model { + /** + * The attributes that should be mutated to dates. + * + * @var array + */ + protected $dates = ['deleted_at']; + /** Owning user */ public function owner() { @@ -13,15 +23,15 @@ class DataTable extends Model } /** Fork precursor */ - public function precursor() + public function parentTable() { - return $this->belongsTo(DataTable::class, 'precursor_id'); + return $this->belongsTo(DataTable::class, 'parent_table_id'); } /** Fork descendants */ - public function descendants() + public function forks() { - return $this->hasMany(DataTable::class, 'precursor_id'); + return $this->hasMany(DataTable::class, 'parent_table_id'); } /** Changesets */ @@ -36,15 +46,27 @@ class DataTable extends Model return $this->hasMany(DataRow::class); } - /** Changesets already merged */ - public function appliedChangesets() + /** User-submitted comments */ + public function comments() + { + return $this->hasMany(TableComment::class); + } + + /** Users favouriting this table */ + public function favouritingUsers() + { + return $this->belongsToMany(User::class, 'table_favourites'); + } + + /** Users to notify about comments */ + public function discussionFollowers() { - return $this->hasMany(Changeset::class)->where('applied', true); + return $this->belongsToMany(User::class, 'discussion_follows'); } - /** Changesets already merged */ - public function pendingChangesets() + /** Reports of this table */ + public function reportsOf() { - return $this->hasMany(Changeset::class)->where('applied', false); + return $this->morphMany(ContentReport::class, 'object'); } } diff --git a/app/Models/Notification.php b/app/Models/Notification.php new file mode 100644 index 0000000..43efdfc --- /dev/null +++ b/app/Models/Notification.php @@ -0,0 +1,51 @@ +belongsTo(User::class, 'user_id'); + } + + /** User who triggered this notification */ + public function actor() + { + return $this->belongsTo(User::class, 'actor_id'); + } + + /** Notification context (what was affected) */ + public function context() + { + return $this->morphTo(); + } + + /** + * Unseen notifications + * + * @param \Illuminate\Database\Eloquent\Builder $query + * @return \Illuminate\Database\Eloquent\Builder + */ + public function scopeUnseen($query) + { + return $query->where('seen', false); + } + + /** + * Seen notifications + * + * @param \Illuminate\Database\Eloquent\Builder $query + * @return \Illuminate\Database\Eloquent\Builder + */ + public function scopeSeen($query) + { + return $query->where('seen', true); + } +} diff --git a/app/Models/TableComment.php b/app/Models/TableComment.php new file mode 100644 index 0000000..0827132 --- /dev/null +++ b/app/Models/TableComment.php @@ -0,0 +1,41 @@ +belongsTo(DataTable::class); + } + + /** Parent comment (that we reply to; can be null) */ + public function ancestor() + { + return $this->belongsTo(TableComment::class, 'ancestor_id'); + } + + /** Replies to this comment */ + public function replies() + { + return $this->hasMany(TableComment::class, 'ancestor_id'); + } + + /** Authoring user */ + public function author() + { + return $this->belongsTo(User::class, 'author_id'); + } + + /** Reports of this comment */ + public function reportsOf() + { + return $this->morphMany(ContentReport::class, 'object'); + } +} diff --git a/app/Models/User.php b/app/Models/User.php index 174c9e1..fdfb40e 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -6,6 +6,9 @@ use AdamWathan\EloquentOAuth\OAuthIdentity; use Illuminate\Notifications\Notifiable; use Illuminate\Foundation\Auth\User as Authenticatable; +/** + * A user in the application + */ class User extends Authenticatable { use Notifiable; @@ -31,7 +34,7 @@ class User extends Authenticatable /** Owned tables */ public function dataTables() { - return $this->hasMany(DataTable::class); + return $this->hasMany(DataTable::class, 'owner_id'); } /** Assigned OAuth identities */ @@ -43,6 +46,42 @@ class User extends Authenticatable /** Assigned OAuth identities */ public function changesets() { - return $this->hasMany(Changeset::class); + return $this->hasMany(Changeset::class, 'author_id'); + } + + /** Authored comments */ + public function tableComments() + { + return $this->hasMany(TableComment::class, 'author_id'); + } + + /** User's favourite tables (personal collection) */ + public function favouriteTables() + { + return $this->belongsToMany(DataTable::class, 'table_favourites'); + } + + /** Tables whose discussions user follows (this is similar to favourites) */ + public function followedDiscussions() + { + return $this->belongsToMany(DataTable::class, 'discussion_follows'); + } + + /** Reports sent by this user */ + public function authoredReports() + { + return $this->hasMany(ContentReport::class, 'author_id'); + } + + /** Reports of this user */ + public function reportsOf() + { + return $this->morphMany(ContentReport::class, 'object'); + } + + /** Notifications for this user */ + public function notifications() + { + return $this->hasMany(Notification::class); } } diff --git a/database/migrations/2018_07_08_193638_create_data_tables_table.php b/database/migrations/2018_07_08_193638_create_data_tables_table.php index 729b41c..ef28a94 100644 --- a/database/migrations/2018_07_08_193638_create_data_tables_table.php +++ b/database/migrations/2018_07_08_193638_create_data_tables_table.php @@ -27,7 +27,7 @@ class CreateDataTablesTable extends Migration $table->foreign('owner_id')->references('id')->on('users') ->onDelete('cascade'); - $table->foreign('precursor_id')->references('id')->on('data_tables') + $table->foreign('parent_table_id')->references('id')->on('data_tables') ->onDelete('set null'); }); } diff --git a/database/migrations/2018_07_12_185431_create_comments_table.php b/database/migrations/2018_07_12_185431_create_table_comments_table.php similarity index 85% rename from database/migrations/2018_07_12_185431_create_comments_table.php rename to database/migrations/2018_07_12_185431_create_table_comments_table.php index 5b347b0..99fc956 100644 --- a/database/migrations/2018_07_12_185431_create_comments_table.php +++ b/database/migrations/2018_07_12_185431_create_table_comments_table.php @@ -4,7 +4,7 @@ use Illuminate\Support\Facades\Schema; use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration; -class CreateCommentsTable extends Migration +class CreateTableCommentsTable extends Migration { /** * Run the migrations. @@ -13,7 +13,7 @@ class CreateCommentsTable extends Migration */ public function up() { - Schema::create('comments', function (Blueprint $table) { + Schema::create('table_comments', function (Blueprint $table) { $table->increments('id'); $table->timestamps(); $table->unsignedInteger('ancestor_id')->index()->nullable(); @@ -39,6 +39,6 @@ class CreateCommentsTable extends Migration */ public function down() { - Schema::dropIfExists('comments'); + Schema::dropIfExists('table_comments'); } } diff --git a/database/migrations/2018_07_12_185840_create_favourites_table.php b/database/migrations/2018_07_12_185840_create_table_favourites_table.php similarity index 81% rename from database/migrations/2018_07_12_185840_create_favourites_table.php rename to database/migrations/2018_07_12_185840_create_table_favourites_table.php index 9eb7431..0fb7131 100644 --- a/database/migrations/2018_07_12_185840_create_favourites_table.php +++ b/database/migrations/2018_07_12_185840_create_table_favourites_table.php @@ -4,7 +4,7 @@ use Illuminate\Support\Facades\Schema; use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration; -class CreateFavouritesTable extends Migration +class CreateTableFavouritesTable extends Migration { /** * Run the migrations. @@ -13,7 +13,7 @@ class CreateFavouritesTable extends Migration */ public function up() { - Schema::create('favourites', function (Blueprint $table) { + Schema::create('table_favourites', function (Blueprint $table) { $table->increments('id'); $table->timestamps(); @@ -35,6 +35,6 @@ class CreateFavouritesTable extends Migration */ public function down() { - Schema::dropIfExists('favourites'); + Schema::dropIfExists('table_favourites'); } } diff --git a/database/migrations/2018_07_12_190830_create_notifications_table.php b/database/migrations/2018_07_12_190830_create_notifications_table.php index 312ab73..920e4fd 100644 --- a/database/migrations/2018_07_12_190830_create_notifications_table.php +++ b/database/migrations/2018_07_12_190830_create_notifications_table.php @@ -16,6 +16,7 @@ class CreateNotificationsTable extends Migration Schema::create('notifications', function (Blueprint $table) { $table->increments('id'); $table->timestamps(); + $table->boolean('seen'); $table->unsignedInteger('user_id')->index(); $table->unsignedInteger('actor_id')->index()->nullable(); // who did it $table->string('action'); // what happened, e.g.: favourited, mentioned, forked, commented