pixelfed/app/Http/Controllers/ProfileMigrationController.php

70 lines
2.6 KiB
PHP
Raw Normal View History

2024-03-02 11:21:04 +00:00
<?php
namespace App\Http\Controllers;
use App\Http\Requests\ProfileMigrationStoreRequest;
2024-03-05 06:16:32 +00:00
use App\Jobs\ProfilePipeline\ProfileMigrationDeliverMoveActivityPipeline;
2024-03-02 11:21:04 +00:00
use App\Jobs\ProfilePipeline\ProfileMigrationMoveFollowersPipeline;
use App\Models\ProfileAlias;
use App\Models\ProfileMigration;
use App\Services\AccountService;
use App\Services\WebfingerService;
use App\Util\ActivityPub\Helpers;
use Illuminate\Http\Request;
2024-03-05 06:16:32 +00:00
use Illuminate\Support\Facades\Bus;
2024-03-02 11:21:04 +00:00
class ProfileMigrationController extends Controller
{
public function __construct()
{
$this->middleware('auth');
}
public function index(Request $request)
{
2024-03-05 06:16:32 +00:00
abort_if((bool) config_cache('federation.activitypub.enabled') === false, 404);
2024-03-02 11:21:04 +00:00
$hasExistingMigration = ProfileMigration::whereProfileId($request->user()->profile_id)
->where('created_at', '>', now()->subDays(30))
->exists();
return view('settings.migration.index', compact('hasExistingMigration'));
}
public function store(ProfileMigrationStoreRequest $request)
{
2024-03-05 06:16:32 +00:00
abort_if((bool) config_cache('federation.activitypub.enabled') === false, 404);
2024-03-02 11:21:04 +00:00
$acct = WebfingerService::rawGet($request->safe()->acct);
if (! $acct) {
return redirect()->back()->withErrors(['acct' => 'The new account you provided is not responding to our requests.']);
}
$newAccount = Helpers::profileFetch($acct);
if (! $newAccount) {
return redirect()->back()->withErrors(['acct' => 'An error occured, please try again later. Code: res-failed-account-fetch']);
}
$user = $request->user();
ProfileAlias::updateOrCreate([
'profile_id' => $user->profile_id,
'acct' => $request->safe()->acct,
'uri' => $acct,
]);
2024-03-05 06:16:32 +00:00
$migration = ProfileMigration::create([
2024-03-02 11:21:04 +00:00
'profile_id' => $request->user()->profile_id,
'acct' => $request->safe()->acct,
'followers_count' => $request->user()->profile->followers_count,
'target_profile_id' => $newAccount['id'],
]);
$user->profile->update([
'moved_to_profile_id' => $newAccount->id,
'indexable' => false,
]);
AccountService::del($user->profile_id);
2024-03-05 06:16:32 +00:00
Bus::batch([
new ProfileMigrationDeliverMoveActivityPipeline($migration, $user->profile, $newAccount),
new ProfileMigrationMoveFollowersPipeline($user->profile_id, $newAccount->id),
])->onQueue('follow')->dispatch();
2024-03-02 11:21:04 +00:00
return redirect()->back()->with(['status' => 'Succesfully migrated account!']);
}
}