ProfileMigrationController.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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. $hasExistingMigration = ProfileMigration::whereProfileId($request->user()->profile_id)
  23. ->where('created_at', '>', now()->subDays(30))
  24. ->exists();
  25. return view('settings.migration.index', compact('hasExistingMigration'));
  26. }
  27. public function store(ProfileMigrationStoreRequest $request)
  28. {
  29. abort_if((bool) config_cache('federation.activitypub.enabled') === false, 404);
  30. $acct = WebfingerService::rawGet($request->safe()->acct);
  31. if (! $acct) {
  32. return redirect()->back()->withErrors(['acct' => 'The new account you provided is not responding to our requests.']);
  33. }
  34. $newAccount = Helpers::profileFetch($acct);
  35. if (! $newAccount) {
  36. return redirect()->back()->withErrors(['acct' => 'An error occured, please try again later. Code: res-failed-account-fetch']);
  37. }
  38. $user = $request->user();
  39. ProfileAlias::updateOrCreate([
  40. 'profile_id' => $user->profile_id,
  41. 'acct' => $request->safe()->acct,
  42. 'uri' => $acct,
  43. ]);
  44. $migration = ProfileMigration::create([
  45. 'profile_id' => $request->user()->profile_id,
  46. 'acct' => $request->safe()->acct,
  47. 'followers_count' => $request->user()->profile->followers_count,
  48. 'target_profile_id' => $newAccount['id'],
  49. ]);
  50. $user->profile->update([
  51. 'moved_to_profile_id' => $newAccount->id,
  52. 'indexable' => false,
  53. ]);
  54. AccountService::del($user->profile_id);
  55. Bus::batch([
  56. new ProfileMigrationDeliverMoveActivityPipeline($migration, $user->profile, $newAccount),
  57. new ProfileMigrationMoveFollowersPipeline($user->profile_id, $newAccount->id),
  58. ])->onQueue('follow')->dispatch();
  59. return redirect()->back()->with(['status' => 'Succesfully migrated account!']);
  60. }
  61. }