#25: Hide notifications for deleted content.

This commit is contained in:
Peter Deltchev 2016-07-09 18:16:25 -07:00
parent a8e81729fb
commit 3f5c0d0f51
8 changed files with 119 additions and 7 deletions

View file

@ -21,6 +21,7 @@
namespace Poniverse\Ponyfm\Models; namespace Poniverse\Ponyfm\Models;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
/** /**
* Poniverse\Ponyfm\Models\Activity * Poniverse\Ponyfm\Models\Activity
@ -39,6 +40,8 @@ use Illuminate\Database\Eloquent\Model;
* @property-read mixed $text * @property-read mixed $text
*/ */
class Activity extends Model { class Activity extends Model {
use SoftDeletes;
public $timestamps = false; public $timestamps = false;
protected $dates = ['created_at']; protected $dates = ['created_at'];
protected $fillable = ['created_at', 'user_id', 'activity_type', 'resource_type', 'resource_id']; protected $fillable = ['created_at', 'user_id', 'activity_type', 'resource_type', 'resource_id'];

View file

@ -20,6 +20,7 @@
namespace Poniverse\Ponyfm\Models; namespace Poniverse\Ponyfm\Models;
use DB;
use Exception; use Exception;
use Helpers; use Helpers;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
@ -400,6 +401,7 @@ class Album extends Model implements Searchable, Commentable, Favouritable
/** /**
* @param string $key * @param string $key
* @return string
*/ */
public function getCacheKey($key) public function getCacheKey($key)
{ {
@ -417,6 +419,13 @@ class Album extends Model implements Searchable, Commentable, Favouritable
return parent::save($options); return parent::save($options);
} }
public function delete() {
DB::transaction(function () {
$this->activities()->delete();
parent::delete();
});
}
protected function recountTracks() { protected function recountTracks() {
$this->track_count = $this->tracks->count(); $this->track_count = $this->tracks->count();
} }

View file

@ -20,6 +20,7 @@
namespace Poniverse\Ponyfm\Models; namespace Poniverse\Ponyfm\Models;
use DB;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\MorphMany; use Illuminate\Database\Eloquent\Relations\MorphMany;
use Illuminate\Database\Eloquent\SoftDeletes; use Illuminate\Database\Eloquent\SoftDeletes;
@ -50,7 +51,6 @@ use Poniverse\Ponyfm\Contracts\Commentable;
*/ */
class Comment extends Model class Comment extends Model
{ {
use SoftDeletes; use SoftDeletes;
protected $table = 'comments'; protected $table = 'comments';
@ -131,4 +131,11 @@ class Comment extends Model
} }
} }
} }
public function delete() {
DB::transaction(function () {
$this->activities()->delete();
parent::delete();
});
}
} }

View file

@ -66,10 +66,11 @@ class Notification extends Model {
'activity.resource', 'activity.resource',
'activity.resource.user', 'activity.resource.user',
]) ])
->join('activities', 'notifications.activity_id', '=', 'activities.id') ->join('activities', 'notifications.activity_id', '=', 'activities.id')
->where('notifications.user_id', $user->id) ->where('notifications.user_id', $user->id)
->select('*', 'notifications.id as id') ->whereNull('activities.deleted_at')
->orderBy('activities.created_at', 'DESC'); ->select('*', 'notifications.id as id')
->orderBy('activities.created_at', 'DESC');
return $result; return $result;
} }

View file

@ -20,6 +20,7 @@
namespace Poniverse\Ponyfm\Models; namespace Poniverse\Ponyfm\Models;
use DB;
use Helpers; use Helpers;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany; use Illuminate\Database\Eloquent\Relations\HasMany;
@ -316,6 +317,13 @@ class Playlist extends Model implements Searchable, Commentable, Favouritable
return 'playlist-'.$this->id.'-'.$key; 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 * Returns this model in Elasticsearch-friendly form. The array returned by
* this method should match the current mapping for this model's ES type. * this method should match the current mapping for this model's ES type.

View file

@ -859,6 +859,12 @@ class Track extends Model implements Searchable, Commentable, Favouritable
return 'track-'.$this->id.'-'.$key; return 'track-'.$this->id.'-'.$key;
} }
public function delete() {
DB::transaction(function () {
$this->activities()->delete();
parent::delete();
});
}
/** /**
* @inheritdoc * @inheritdoc

View file

@ -0,0 +1,78 @@
<?php
/**
* Pony.fm - A community for pony fan music.
* Copyright (C) 2016 Peter Deltchev
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
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();
});
}
}

View file

@ -1,9 +1,9 @@
[program: ponyfm-worker] [program: ponyfm-worker]
process_name = %(program_name)s_%(process_num)02d 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 autostart = true
autorestart = true autorestart = true
user = www-data user = www-data
numprocs = 4 numprocs = 2
redirect_stderr = true redirect_stderr = true
stdout_logfile = /vagrant/storage/logs/worker.log stdout_logfile = /vagrant/storage/logs/worker.log