#20: Genre tags in files are now updated when a genre is renamed.

This commit is contained in:
Peter Deltchev 2016-01-06 05:33:23 -08:00
parent bf831d839a
commit 0a078adab3
2 changed files with 103 additions and 0 deletions

View file

@ -21,12 +21,16 @@
namespace Poniverse\Ponyfm\Commands; namespace Poniverse\Ponyfm\Commands;
use Gate; use Gate;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Support\Str; use Illuminate\Support\Str;
use Poniverse\Ponyfm\Jobs\UpdateTagsForRenamedGenre;
use Poniverse\Ponyfm\Models\Genre; use Poniverse\Ponyfm\Models\Genre;
use Validator; use Validator;
class RenameGenreCommand extends CommandBase class RenameGenreCommand extends CommandBase
{ {
use DispatchesJobs;
/** @var Genre */ /** @var Genre */
private $_genre; private $_genre;
private $_newName; private $_newName;
@ -72,6 +76,8 @@ class RenameGenreCommand extends CommandBase
$this->_genre->slug = $slug; $this->_genre->slug = $slug;
$this->_genre->save(); $this->_genre->save();
$this->dispatch(new UpdateTagsForRenamedGenre($this->_genre));
return CommandResponse::succeed(['message' => 'Genre renamed!']); return CommandResponse::succeed(['message' => 'Genre renamed!']);
} }
} }

View file

@ -0,0 +1,97 @@
<?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/>.
*/
namespace Poniverse\Ponyfm\Jobs;
use Auth;
use Cache;
use Log;
use Poniverse\Ponyfm\Models\Genre;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Bus\SelfHandling;
use Illuminate\Contracts\Queue\ShouldQueue;
use Poniverse\Ponyfm\Models\Track;
use SerializesModels;
/**
* Class RenameGenre
*
* NOTE: It is assumed that the genre passed into this job has already been renamed!
* All this job does is update the tags in that genre's tracks.
*
* @package Poniverse\Ponyfm\Jobs
*/
class UpdateTagsForRenamedGenre extends Job implements SelfHandling, ShouldQueue
{
use InteractsWithQueue, SerializesModels;
protected $executingUser;
protected $genreThatWasRenamed;
protected $lockKey;
/**
* Create a new job instance.
*
* @param Genre $genreThatWasRenamed
*/
public function __construct(Genre $genreThatWasRenamed)
{
$this->executingUser = Auth::user();
$this->genreThatWasRenamed = $genreThatWasRenamed;
$this->lockKey = "genre-{$this->genreThatWasRenamed->id}-tag-update-lock";
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
// The user who kicked off this job is used when generating revision log entries.
Auth::login($this->executingUser);
// "Lock" this genre to prevent race conditions
if (Cache::has($this->lockKey)) {
Log::info("Tag updates for the \"{$this->genreThatWasRenamed->name}\" genre are currently in progress! Will try again in 30 seconds.");
$this->release(30);
return;
} else {
Cache::forever($this->lockKey, true);
}
$this->genreThatWasRenamed->tracks()->chunk(200, function ($tracks) {
foreach ($tracks as $track) {
/** @var Track $track */
$track->updateTags();
}
});
Cache::forget($this->lockKey);
}
public function failed()
{
Cache::forget($this->lockKey);
}
}