ProfileMigrationController.php 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Http\Requests\ProfileMigrationStoreRequest;
  4. use App\Jobs\ProfilePipeline\ProfileMigrationDeliverMoveActivityPipeline;
  5. use App\Jobs\ProfilePipeline\ProfileMigrationMoveFollowersPipeline;
  6. use App\Models\ProfileAlias;
  7. use App\Models\ProfileMigration;
  8. use App\Services\AccountService;
  9. use App\Services\WebfingerService;
  10. use App\Util\ActivityPub\Helpers;
  11. use Illuminate\Http\Request;
  12. use Illuminate\Support\Facades\Bus;
  13. class ProfileMigrationController extends Controller
  14. {
  15. public function __construct()
  16. {
  17. $this->middleware('auth');
  18. }
  19. public function index(Request $request)
  20. {
  21. abort_if((bool) config_cache('federation.activitypub.enabled') === false, 404);
  22. if ((bool) config_cache('federation.migration') === false) {
  23. return redirect(route('help.account-migration'));
  24. }
  25. $hasExistingMigration = ProfileMigration::whereProfileId($request->user()->profile_id)
  26. ->where('created_at', '>', now()->subDays(30))
  27. ->exists();
  28. return view('settings.migration.index', compact('hasExistingMigration'));
  29. }
  30. public function store(ProfileMigrationStoreRequest $request)
  31. {
  32. abort_if((bool) config_cache('federation.activitypub.enabled') === false, 404);
  33. $acct = WebfingerService::rawGet($request->safe()->acct);
  34. if (! $acct) {
  35. return redirect()->back()->withErrors(['acct' => 'The new account you provided is not responding to our requests.']);
  36. }
  37. $newAccount = Helpers::profileFetch($acct);
  38. if (! $newAccount) {
  39. return redirect()->back()->withErrors(['acct' => 'An error occured, please try again later. Code: res-failed-account-fetch']);
  40. }
  41. $user = $request->user();
  42. ProfileAlias::updateOrCreate([
  43. 'profile_id' => $user->profile_id,
  44. 'acct' => $request->safe()->acct,
  45. 'uri' => $acct,
  46. ]);
  47. $migration = ProfileMigration::create([
  48. 'profile_id' => $request->user()->profile_id,
  49. 'acct' => $request->safe()->acct,
  50. 'followers_count' => $request->user()->profile->followers_count,
  51. 'target_profile_id' => $newAccount['id'],
  52. ]);
  53. $user->profile->update([
  54. 'moved_to_profile_id' => $newAccount->id,
  55. 'indexable' => false,
  56. ]);
  57. AccountService::del($user->profile_id);
  58. Bus::batch([
  59. [
  60. new ProfileMigrationDeliverMoveActivityPipeline($migration, $user->profile, $newAccount),
  61. ],
  62. [
  63. new ProfileMigrationMoveFollowersPipeline($user->profile_id, $newAccount->id),
  64. ]
  65. ])->onQueue('follow')->dispatch();
  66. return redirect()->back()->with(['status' => 'Succesfully migrated account!']);
  67. }
  68. }