diff --git a/app/Models/Activity.php b/app/Models/Activity.php index 5e661d36..526f202e 100644 --- a/app/Models/Activity.php +++ b/app/Models/Activity.php @@ -21,6 +21,7 @@ namespace Poniverse\Ponyfm\Models; use Illuminate\Database\Eloquent\Model; +use Illuminate\Database\Eloquent\SoftDeletes; /** * Poniverse\Ponyfm\Models\Activity @@ -39,6 +40,8 @@ use Illuminate\Database\Eloquent\Model; * @property-read mixed $text */ class Activity extends Model { + use SoftDeletes; + public $timestamps = false; protected $dates = ['created_at']; protected $fillable = ['created_at', 'user_id', 'activity_type', 'resource_type', 'resource_id']; diff --git a/app/Models/Album.php b/app/Models/Album.php index 876e9ca2..9b90b900 100644 --- a/app/Models/Album.php +++ b/app/Models/Album.php @@ -20,6 +20,7 @@ namespace Poniverse\Ponyfm\Models; +use DB; use Exception; use Helpers; use Illuminate\Database\Eloquent\Model; @@ -377,6 +378,7 @@ class Album extends Model implements Searchable, Commentable, Favouritable /** * @param string $key + * @return string */ public function getCacheKey($key) { @@ -394,6 +396,13 @@ class Album extends Model implements Searchable, Commentable, Favouritable return parent::save($options); } + public function delete() { + DB::transaction(function () { + $this->activities()->delete(); + parent::delete(); + }); + } + protected function recountTracks() { $this->track_count = $this->tracks->count(); } diff --git a/app/Models/Comment.php b/app/Models/Comment.php index 6f7fd3f1..dfbdc0ab 100644 --- a/app/Models/Comment.php +++ b/app/Models/Comment.php @@ -20,6 +20,7 @@ namespace Poniverse\Ponyfm\Models; +use DB; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\MorphMany; use Illuminate\Database\Eloquent\SoftDeletes; @@ -50,7 +51,6 @@ use Poniverse\Ponyfm\Contracts\Commentable; */ class Comment extends Model { - use SoftDeletes; protected $table = 'comments'; @@ -131,4 +131,11 @@ class Comment extends Model } } } + + public function delete() { + DB::transaction(function () { + $this->activities()->delete(); + parent::delete(); + }); + } } diff --git a/app/Models/Notification.php b/app/Models/Notification.php index 8e2e5c99..9379a827 100644 --- a/app/Models/Notification.php +++ b/app/Models/Notification.php @@ -66,10 +66,11 @@ class Notification extends Model { 'activity.resource', 'activity.resource.user', ]) - ->join('activities', 'notifications.activity_id', '=', 'activities.id') - ->where('notifications.user_id', $user->id) - ->select('*', 'notifications.id as id') - ->orderBy('activities.created_at', 'DESC'); + ->join('activities', 'notifications.activity_id', '=', 'activities.id') + ->where('notifications.user_id', $user->id) + ->whereNull('activities.deleted_at') + ->select('*', 'notifications.id as id') + ->orderBy('activities.created_at', 'DESC'); return $result; } diff --git a/app/Models/Playlist.php b/app/Models/Playlist.php index f2639a3f..c700b12a 100644 --- a/app/Models/Playlist.php +++ b/app/Models/Playlist.php @@ -20,6 +20,7 @@ namespace Poniverse\Ponyfm\Models; +use DB; use Helpers; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\HasMany; @@ -294,6 +295,13 @@ class Playlist extends Model implements Searchable, Commentable, Favouritable return 'playlist-'.$this->id.'-'.$key; } + public function delete() { + DB::transaction(function () { + $this->activities()->delete(); + parent::delete(); + }); + } + /** * Returns this model in Elasticsearch-friendly form. The array returned by * this method should match the current mapping for this model's ES type. diff --git a/app/Models/Track.php b/app/Models/Track.php index 6000a3c5..abb6b117 100644 --- a/app/Models/Track.php +++ b/app/Models/Track.php @@ -880,6 +880,12 @@ class Track extends Model implements Searchable, Commentable, Favouritable return 'track-'.$this->id.'-'.$key; } + public function delete() { + DB::transaction(function () { + $this->activities()->delete(); + parent::delete(); + }); + } /** * @inheritdoc diff --git a/database/migrations/2016_07_10_000354_add_deleted_at_column_to_activities.php b/database/migrations/2016_07_10_000354_add_deleted_at_column_to_activities.php new file mode 100644 index 00000000..ad9b5303 --- /dev/null +++ b/database/migrations/2016_07_10_000354_add_deleted_at_column_to_activities.php @@ -0,0 +1,78 @@ +. + */ + +use Illuminate\Database\Schema\Blueprint; +use Illuminate\Database\Migrations\Migration; + +class AddDeletedAtColumnToActivities extends Migration +{ + /** + * Run the migrations. + * + * @return void + */ + public function up() + { + Schema::table('activities', function(Blueprint $table) { + $table->softDeletes()->index(); + }); + + // Retroactively fix activities that should be marked as deleted. + // Tracks + DB::table('activities') + ->where('resource_type', 2) + ->join('tracks', 'activities.resource_id', '=', 'tracks.id') + ->whereNotNull('tracks.deleted_at') + ->update(['activities.deleted_at' => DB::raw('tracks.deleted_at')]); + + // Albums + DB::table('activities') + ->where('resource_type', 3) + ->join('albums', 'activities.resource_id', '=', 'albums.id') + ->whereNotNull('albums.deleted_at') + ->update(['activities.deleted_at' => DB::raw('albums.deleted_at')]); + + // Playlists + DB::table('activities') + ->where('resource_type', 4) + ->join('playlists', 'activities.resource_id', '=', 'playlists.id') + ->whereNotNull('playlists.deleted_at') + ->update(['activities.deleted_at' => DB::raw('playlists.deleted_at')]); + + // Comments + DB::table('activities') + ->where('resource_type', 5) + ->join('comments', 'activities.resource_id', '=', 'comments.id') + ->whereNotNull('comments.deleted_at') + ->update(['activities.deleted_at' => DB::raw('comments.deleted_at')]); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('activities', function (Blueprint $table) { + $table->dropSoftDeletes(); + }); + } +} diff --git a/vagrant/pony.fm.supervisor.config b/vagrant/pony.fm.supervisor.config index c014c64d..c737e5e2 100644 --- a/vagrant/pony.fm.supervisor.config +++ b/vagrant/pony.fm.supervisor.config @@ -1,9 +1,9 @@ [program: ponyfm-worker] process_name = %(program_name)s_%(process_num)02d -command = php /vagrant/artisan queue:listen --queue=default,notifications,indexing --sleep=3 --tries=3 +command = php /vagrant/artisan queue:listen --queue=default,notifications,indexing --sleep=5 --tries=3 autostart = true autorestart = true user = www-data -numprocs = 4 +numprocs = 2 redirect_stderr = true stdout_logfile = /vagrant/storage/logs/worker.log