From 11520e4d96d23915620541b7929fc659a7ccbc23 Mon Sep 17 00:00:00 2001 From: Josef Citrine Date: Fri, 19 May 2017 11:22:14 +0100 Subject: [PATCH] Progress commit on report system --- .../Controllers/Api/Web/ReportController.php | 22 +++ .../Controllers/Api/Web/TracksController.php | 4 + app/Models/Report.php | 148 ++++++++++++++++++ app/Models/ReportMessage.php | 27 ++++ ...2017_05_18_145120_create_reports_table.php | 67 ++++++++ routes/web.php | 4 + 6 files changed, 272 insertions(+) create mode 100644 app/Http/Controllers/Api/Web/ReportController.php create mode 100644 app/Models/Report.php create mode 100644 app/Models/ReportMessage.php create mode 100644 database/migrations/2017_05_18_145120_create_reports_table.php diff --git a/app/Http/Controllers/Api/Web/ReportController.php b/app/Http/Controllers/Api/Web/ReportController.php new file mode 100644 index 00000000..fd700b9e --- /dev/null +++ b/app/Http/Controllers/Api/Web/ReportController.php @@ -0,0 +1,22 @@ + $categories]); + } + + public function getReports() { + $reports = Report::all(); + $reports[0]['type_string'] = $reports[0]->resource(); + return $reports; + } +} diff --git a/app/Http/Controllers/Api/Web/TracksController.php b/app/Http/Controllers/Api/Web/TracksController.php index a743763f..2b16d31f 100644 --- a/app/Http/Controllers/Api/Web/TracksController.php +++ b/app/Http/Controllers/Api/Web/TracksController.php @@ -281,6 +281,10 @@ class TracksController extends ApiControllerBase return Response::json(Track::mapPrivateTrackShow($track), 200); } + public function postReport() { + + } + /** * To be run after aggregating the total number of tracks for a given query. * This is separated from applyFilters() because Postgres doesn't allow diff --git a/app/Models/Report.php b/app/Models/Report.php new file mode 100644 index 00000000..2bae3d57 --- /dev/null +++ b/app/Models/Report.php @@ -0,0 +1,148 @@ + "track", + Report::TYPE_USER => "user", + Report::TYPE_COMMENT => "comment" + ]; + + /** + * These values are stored in the "report_categories" table + * Make sure when adding a new category that you write a migration + * to update that table as well. + */ + const CATEGORY_COPYRIGHT = 1; + const CATEGORY_HARASSMENT = 2; + const CATEGORY_OFFENSIVE = 3; + const CATEGORY_SPAM = 4; + const CATEGORY_NONPONY = 5; + + /** + * You shouldn't be able to report comments for copyright infringement + * or being 'non-pony'. Here we outline which categories we can use + */ + const COMMENT_REPORT_CATEGORIES = [ + Report::CATEGORY_HARASSMENT, + Report::CATEGORY_OFFENSIVE, + Report::CATEGORY_SPAM + ]; + + public function reporter() + { + return $this->belongsTo(User::class, 'reporter_id', 'id'); + } + + public function resource() + { + return $this->morphTo('resource', 'resource_type', 'resource_id'); + } + + public function getResourceTypeAttribute($value) + { + switch ($value) { + case static::TYPE_TRACK: + return Track::class; + + case static::TYPE_USER: + return User::class; + + case static::TYPE_COMMENT: + return Comment::class; + + default: + // Null must be returned here for Eloquent's eager-loading + // of the polymorphic relation to work. + return null; + } + } + + public function setResourceTypeAttribute($value) + { + switch ($value) { + case Track::class: + $this->attributes['resource_type'] = static::TYPE_TRACK; + break; + + case User::class: + $this->attributes['resource_type'] = static::TYPE_USER; + break; + + case Comment::class: + $this->attributes['resource_type'] = static::TYPE_COMMENT; + break; + } + } + + public function getResourceTypeString():string + { + return $this->getResourceTypeStringFromId($this->resource_type); + } + + public static function getResourceTypeStringFromId($id):string { + if (array_key_exists($id, Report::TYPE_NAMES)) { + return Report::TYPE_NAMES[$id]; + } + + throw new \Exception("Unknown resource type id {$id}"); + } + + public static function getResourceTypeIdFromString($name):int { + $key = array_search($name, Report::TYPE_NAMES); + if ($key) { + return $key; + } + + throw new \Exception("Unknown resource type {$name}"); + } + + public static function getCategories($type = null) { + $catQuery = DB::table('report_categories')->select('*'); + + if ($type == Report::TYPE_COMMENT) { + $catQuery->whereIn('report_category', Report::COMMENT_REPORT_CATEGORIES); + } + + $categories = $catQuery->get(); + return $categories; + } +} diff --git a/app/Models/ReportMessage.php b/app/Models/ReportMessage.php new file mode 100644 index 00000000..f97ed838 --- /dev/null +++ b/app/Models/ReportMessage.php @@ -0,0 +1,27 @@ +unsignedTinyInteger('report_category')->primary(); + $table->string('name'); + $table->string('description'); + }); + + DB::table('report_categories')->insert([ + ['report_category' => 1, 'name' => 'Copyright Infringement', 'description' => 'Such as re-uploading other people\'s tracks without permission'], + ['report_category' => 2, 'name' => 'Harassment', 'description' => 'Attacks or threats on individuals through lyrics, repeated harassing comments, etc'], + ['report_category' => 3, 'name' => 'Offensive', 'description' => 'Includes hate speech, extremely vulgar language and sexual content'], + ['report_category' => 4, 'name' => 'Spam', 'description' => 'Spam and advertising are not allowed. You may advertise your social media in your descriptions but don\'t spam tracks or comments'], + ['report_category' => 5, 'name' => 'Non-Pony', 'description' => 'We only host pony music and we are removing anything that is clearly non-pony. Pony inspired tracks are fine however'], + ]); + + Schema::create('reports', function (Blueprint $table) { + $table->increments('id'); + $table->integer('reporter_id')->unsigned(); + $table->integer('resource_type')->unsigned(); + $table->integer('resource_id')->unsigned(); + $table->unsignedTinyInteger('category'); + $table->text('message', 65535); + $table->timestamps(); + $table->dateTime('resolved_at')->nullable()->index(); + + $table->foreign('reporter_id')->references('id')->on('users')->onUpdate('RESTRICT')->onDelete('RESTRICT'); + $table->foreign('category')->references('report_category')->on('report_categories'); + }); + + Schema::create('report_messages', function (Blueprint $table) { + $table->increments('id'); + $table->integer('user_id')->unsigned(); + $table->integer('report_id')->unsigned(); + $table->text('message', 65535); + $table->timestamps(); + + $table->foreign('user_id')->references('id')->on('users')->onUpdate('RESTRICT')->onDelete('RESTRICT'); + $table->foreign('report_id')->references('id')->on('reports')->onUpdate('RESTRICT')->onDelete('RESTRICT'); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('reports'); + Schema::dropIfExists('report_categories'); + Schema::dropIfExists('report_messages'); + } +} diff --git a/routes/web.php b/routes/web.php index b54073ab..cf2449a6 100644 --- a/routes/web.php +++ b/routes/web.php @@ -191,6 +191,8 @@ Route::group(['prefix' => 'api/web', 'middleware' => 'cors'], function () { Route::get('/favourites/tracks', 'Api\Web\FavouritesController@getTracks'); Route::get('/favourites/albums', 'Api\Web\FavouritesController@getAlbums'); Route::get('/favourites/playlists', 'Api\Web\FavouritesController@getPlaylists'); + + Route::get('/report/categories/{type}', 'Api\Web\ReportController@getReportCategories'); }); Route::group(['prefix' => 'admin', 'middleware' => ['auth', 'can:access-admin-area']], function () { @@ -212,6 +214,8 @@ Route::group(['prefix' => 'api/web', 'middleware' => 'cors'], function () { Route::post('/announcements', 'Api\Web\AnnouncementsController@postCreate'); Route::put('/announcements/{id}', 'Api\Web\AnnouncementsController@putUpdate')->where('id', '\d+'); Route::delete('/announcements/{id}', 'Api\Web\AnnouncementsController@deleteItem')->where('id', '\d+'); + + Route::get('/reports', 'Api\Web\ReportController@getReports'); }); Route::get('/auth/current', 'Api\Web\AccountController@getCurrentUser');