mirror of
https://github.com/Poniverse/Pony.fm.git
synced 2025-01-31 11:16:42 +01:00
Implemented an account merging CLI tool.
This commit is contained in:
parent
a2b35ba8a3
commit
4e540de38e
4 changed files with 207 additions and 164 deletions
120
app/Commands/MergeAccountsCommand.php
Normal file
120
app/Commands/MergeAccountsCommand.php
Normal file
|
@ -0,0 +1,120 @@
|
||||||
|
<?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\Commands;
|
||||||
|
|
||||||
|
use Carbon\Carbon;
|
||||||
|
use DB;
|
||||||
|
use Poniverse\Ponyfm\Models\Album;
|
||||||
|
use Poniverse\Ponyfm\Models\Comment;
|
||||||
|
use Poniverse\Ponyfm\Models\Favourite;
|
||||||
|
use Poniverse\Ponyfm\Models\Follower;
|
||||||
|
use Poniverse\Ponyfm\Models\Image;
|
||||||
|
use Poniverse\Ponyfm\Models\PinnedPlaylist;
|
||||||
|
use Poniverse\Ponyfm\Models\Playlist;
|
||||||
|
use Poniverse\Ponyfm\Models\ResourceLogItem;
|
||||||
|
use Poniverse\Ponyfm\Models\ResourceUser;
|
||||||
|
use Poniverse\Ponyfm\Models\Track;
|
||||||
|
use Poniverse\Ponyfm\Models\User;
|
||||||
|
|
||||||
|
class MergeAccountsCommand extends CommandBase
|
||||||
|
{
|
||||||
|
private $sourceAccount;
|
||||||
|
private $destinationAccount;
|
||||||
|
|
||||||
|
function __construct(User $sourceAccount, User $destinationAccount)
|
||||||
|
{
|
||||||
|
$this->sourceAccount = $sourceAccount;
|
||||||
|
$this->destinationAccount = $destinationAccount;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @throws \Exception
|
||||||
|
* @return CommandResponse
|
||||||
|
*/
|
||||||
|
public function execute()
|
||||||
|
{
|
||||||
|
DB::transaction(function() {
|
||||||
|
$accountIds = [$this->sourceAccount->id];
|
||||||
|
|
||||||
|
foreach (Album::whereIn('user_id', $accountIds)->get() as $album) {
|
||||||
|
$album->user_id = $this->destinationAccount->id;
|
||||||
|
$album->save();
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (Comment::whereIn('user_id', $accountIds)->get() as $comment) {
|
||||||
|
$comment->user_id = $this->destinationAccount->id;
|
||||||
|
$comment->save();
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (Favourite::whereIn('user_id', $accountIds)->get() as $favourite) {
|
||||||
|
$favourite->user_id = $this->destinationAccount->id;
|
||||||
|
$favourite->save();
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (Follower::whereIn('artist_id', $accountIds)->get() as $follow) {
|
||||||
|
$follow->artist_id = $this->destinationAccount->id;
|
||||||
|
$follow->save();
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (Image::whereIn('uploaded_by', $accountIds)->get() as $image) {
|
||||||
|
$image->uploaded_by = $this->destinationAccount->id;
|
||||||
|
$image->save();
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (Image::whereIn('uploaded_by', $accountIds)->get() as $image) {
|
||||||
|
$image->uploaded_by = $this->destinationAccount->id;
|
||||||
|
$image->save();
|
||||||
|
}
|
||||||
|
|
||||||
|
DB::table('oauth2_tokens')->whereIn('user_id', $accountIds)->update(['user_id' => $this->destinationAccount->id]);
|
||||||
|
|
||||||
|
foreach (PinnedPlaylist::whereIn('user_id', $accountIds)->get() as $playlist) {
|
||||||
|
$playlist->user_id = $this->destinationAccount->id;
|
||||||
|
$playlist->save();
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (Playlist::whereIn('user_id', $accountIds)->get() as $playlist) {
|
||||||
|
$playlist->user_id = $this->destinationAccount->id;
|
||||||
|
$playlist->save();
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (ResourceLogItem::whereIn('user_id', $accountIds)->get() as $item) {
|
||||||
|
$item->user_id = $this->destinationAccount->id;
|
||||||
|
$item->save();
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (ResourceUser::whereIn('user_id', $accountIds)->get() as $item) {
|
||||||
|
$item->user_id = $this->destinationAccount->id;
|
||||||
|
$item->save();
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (Track::whereIn('user_id', $accountIds)->get() as $track) {
|
||||||
|
$track->user_id = $this->destinationAccount->id;
|
||||||
|
$track->save();
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->sourceAccount->disabled_at = Carbon::now();
|
||||||
|
$this->sourceAccount->save();
|
||||||
|
});
|
||||||
|
|
||||||
|
return CommandResponse::succeed();
|
||||||
|
}
|
||||||
|
}
|
86
app/Console/Commands/MergeAccounts.php
Normal file
86
app/Console/Commands/MergeAccounts.php
Normal file
|
@ -0,0 +1,86 @@
|
||||||
|
<?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\Console\Commands;
|
||||||
|
|
||||||
|
use Carbon\Carbon;
|
||||||
|
use DB;
|
||||||
|
use Illuminate\Console\Command;
|
||||||
|
use Illuminate\Support\Collection;
|
||||||
|
use Poniverse\Ponyfm\Commands\MergeAccountsCommand;
|
||||||
|
use Poniverse\Ponyfm\Models\Album;
|
||||||
|
use Poniverse\Ponyfm\Models\Comment;
|
||||||
|
use Poniverse\Ponyfm\Models\Favourite;
|
||||||
|
use Poniverse\Ponyfm\Models\Follower;
|
||||||
|
use Poniverse\Ponyfm\Models\Image;
|
||||||
|
use Poniverse\Ponyfm\Models\PinnedPlaylist;
|
||||||
|
use Poniverse\Ponyfm\Models\Playlist;
|
||||||
|
use Poniverse\Ponyfm\Models\ResourceLogItem;
|
||||||
|
use Poniverse\Ponyfm\Models\ResourceUser;
|
||||||
|
use Poniverse\Ponyfm\Models\Track;
|
||||||
|
use Poniverse\Ponyfm\Models\User;
|
||||||
|
|
||||||
|
class MergeAccounts extends Command
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* The name and signature of the console command.
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $signature = 'accounts:merge
|
||||||
|
{sourceAccountId : ID of the source account (the one being disabled and having content transferred out of it)}
|
||||||
|
{destinationAccountId : ID of the destination account (the one gaining content)}';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The console command description.
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $description = 'Merges two accounts';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a new command instance.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function __construct()
|
||||||
|
{
|
||||||
|
parent::__construct();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Execute the console command.
|
||||||
|
*
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
public function handle()
|
||||||
|
{
|
||||||
|
$sourceAccountId = $this->argument('sourceAccountId');
|
||||||
|
$destinationAccountId = $this->argument('destinationAccountId');
|
||||||
|
|
||||||
|
$sourceAccount = User::find($sourceAccountId);
|
||||||
|
$destinationAccount = User::find($destinationAccountId);
|
||||||
|
|
||||||
|
$this->info("Merging {$sourceAccount->display_name} ({$sourceAccountId}) into {$destinationAccount->display_name} ({$destinationAccountId})...");
|
||||||
|
|
||||||
|
$command = new MergeAccountsCommand($sourceAccount, $destinationAccount);
|
||||||
|
$command->execute();
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,163 +0,0 @@
|
||||||
<?php
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Pony.fm - A community for pony fan music.
|
|
||||||
* Copyright (C) 2015 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\Console\Commands;
|
|
||||||
|
|
||||||
use Carbon\Carbon;
|
|
||||||
use DB;
|
|
||||||
use Illuminate\Console\Command;
|
|
||||||
use Illuminate\Support\Collection;
|
|
||||||
use Poniverse\Ponyfm\Models\Album;
|
|
||||||
use Poniverse\Ponyfm\Models\Comment;
|
|
||||||
use Poniverse\Ponyfm\Models\Favourite;
|
|
||||||
use Poniverse\Ponyfm\Models\Follower;
|
|
||||||
use Poniverse\Ponyfm\Models\Image;
|
|
||||||
use Poniverse\Ponyfm\Models\PinnedPlaylist;
|
|
||||||
use Poniverse\Ponyfm\Models\Playlist;
|
|
||||||
use Poniverse\Ponyfm\Models\ResourceLogItem;
|
|
||||||
use Poniverse\Ponyfm\Models\ResourceUser;
|
|
||||||
use Poniverse\Ponyfm\Models\Track;
|
|
||||||
use Poniverse\Ponyfm\Models\User;
|
|
||||||
|
|
||||||
class MergeDuplicateAccounts extends Command
|
|
||||||
{
|
|
||||||
/**
|
|
||||||
* The name and signature of the console command.
|
|
||||||
*
|
|
||||||
* @var string
|
|
||||||
*/
|
|
||||||
protected $signature = 'auth:merge-duplicates';
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The console command description.
|
|
||||||
*
|
|
||||||
* @var string
|
|
||||||
*/
|
|
||||||
protected $description = 'Merges duplicate accounts';
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Create a new command instance.
|
|
||||||
*
|
|
||||||
* @return void
|
|
||||||
*/
|
|
||||||
public function __construct()
|
|
||||||
{
|
|
||||||
parent::__construct();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Execute the console command.
|
|
||||||
*
|
|
||||||
* @return mixed
|
|
||||||
*/
|
|
||||||
public function handle()
|
|
||||||
{
|
|
||||||
// Get list of affected users
|
|
||||||
$usernames = DB::table('users')
|
|
||||||
->select(['username', DB::raw('COUNT(*) as count')])
|
|
||||||
->whereNull('disabled_at')
|
|
||||||
->whereNotNull('username')
|
|
||||||
->groupBy(DB::raw('LOWER(username)'))
|
|
||||||
->having('count', '>=', 2)
|
|
||||||
->lists('username');
|
|
||||||
|
|
||||||
foreach($usernames as $username) {
|
|
||||||
// Find the relevant accounts
|
|
||||||
// ==========================
|
|
||||||
|
|
||||||
/** @var Collection $accounts */
|
|
||||||
$accounts = User::where('username', $username)->orderBy('created_at', 'ASC')->get();
|
|
||||||
$firstAccount = $accounts[0];
|
|
||||||
$accounts->forget(0);
|
|
||||||
$accountIds = $accounts->pluck('id');
|
|
||||||
|
|
||||||
|
|
||||||
// Reassign content
|
|
||||||
// ================
|
|
||||||
// This is done with the less-efficient-than-raw-SQL Eloquent
|
|
||||||
// methods to generate appropriate revision logs.
|
|
||||||
|
|
||||||
$this->info('Merging duplicates for: '.$firstAccount->username);
|
|
||||||
DB::transaction(function() use ($accounts, $accountIds, $firstAccount) {
|
|
||||||
foreach (Album::whereIn('user_id', $accountIds)->get() as $album) {
|
|
||||||
$album->user_id = $firstAccount->id;
|
|
||||||
$album->save();
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach (Comment::whereIn('user_id', $accountIds)->get() as $comment) {
|
|
||||||
$comment->user_id = $firstAccount->id;
|
|
||||||
$comment->save();
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach (Favourite::whereIn('user_id', $accountIds)->get() as $favourite) {
|
|
||||||
$favourite->user_id = $firstAccount->id;
|
|
||||||
$favourite->save();
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach (Follower::whereIn('artist_id', $accountIds)->get() as $follow) {
|
|
||||||
$follow->artist_id = $firstAccount->id;
|
|
||||||
$follow->save();
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach (Image::whereIn('uploaded_by', $accountIds)->get() as $image) {
|
|
||||||
$image->uploaded_by = $firstAccount->id;
|
|
||||||
$image->save();
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach (Image::whereIn('uploaded_by', $accountIds)->get() as $image) {
|
|
||||||
$image->uploaded_by = $firstAccount->id;
|
|
||||||
$image->save();
|
|
||||||
}
|
|
||||||
|
|
||||||
DB::table('oauth2_tokens')->whereIn('user_id', $accountIds)->update(['user_id' => $firstAccount->id]);
|
|
||||||
|
|
||||||
foreach (PinnedPlaylist::whereIn('user_id', $accountIds)->get() as $playlist) {
|
|
||||||
$playlist->user_id = $firstAccount->id;
|
|
||||||
$playlist->save();
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach (Playlist::whereIn('user_id', $accountIds)->get() as $playlist) {
|
|
||||||
$playlist->user_id = $firstAccount->id;
|
|
||||||
$playlist->save();
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach (ResourceLogItem::whereIn('user_id', $accountIds)->get() as $item) {
|
|
||||||
$item->user_id = $firstAccount->id;
|
|
||||||
$item->save();
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach (ResourceUser::whereIn('user_id', $accountIds)->get() as $item) {
|
|
||||||
$item->user_id = $firstAccount->id;
|
|
||||||
$item->save();
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach (Track::whereIn('user_id', $accountIds)->get() as $track) {
|
|
||||||
$track->user_id = $firstAccount->id;
|
|
||||||
$track->save();
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach($accounts as $account) {
|
|
||||||
$account->disabled_at = Carbon::now();
|
|
||||||
$account->save();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -43,7 +43,7 @@ class Kernel extends ConsoleKernel
|
||||||
\Poniverse\Ponyfm\Console\Commands\RebuildTrack::class,
|
\Poniverse\Ponyfm\Console\Commands\RebuildTrack::class,
|
||||||
\Poniverse\Ponyfm\Console\Commands\RebuildFilesizes::class,
|
\Poniverse\Ponyfm\Console\Commands\RebuildFilesizes::class,
|
||||||
\Poniverse\Ponyfm\Console\Commands\RebuildSearchIndex::class,
|
\Poniverse\Ponyfm\Console\Commands\RebuildSearchIndex::class,
|
||||||
\Poniverse\Ponyfm\Console\Commands\MergeDuplicateAccounts::class,
|
\Poniverse\Ponyfm\Console\Commands\MergeAccounts::class,
|
||||||
];
|
];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in a new issue