ProfileMigrationController.php 2.1 KB

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