ProfileAliasController.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Models\ProfileAlias;
  4. use App\Models\ProfileMigration;
  5. use App\Services\AccountService;
  6. use App\Services\WebfingerService;
  7. use App\Util\Lexer\Nickname;
  8. use Cache;
  9. use Illuminate\Http\Request;
  10. class ProfileAliasController extends Controller
  11. {
  12. public function __construct()
  13. {
  14. $this->middleware('auth');
  15. }
  16. public function index(Request $request)
  17. {
  18. $aliases = $request->user()->profile->aliases;
  19. return view('settings.aliases.index', compact('aliases'));
  20. }
  21. public function store(Request $request)
  22. {
  23. $this->validate($request, [
  24. 'acct' => 'required',
  25. ]);
  26. $acct = $request->input('acct');
  27. $nn = Nickname::normalizeProfileUrl($acct);
  28. if (! $nn) {
  29. return back()->with('error', 'Invalid account alias.');
  30. }
  31. if ($nn['domain'] === config('pixelfed.domain.app')) {
  32. if (strtolower($nn['username']) == ($request->user()->username)) {
  33. return back()->with('error', 'You cannot add an alias to your own account.');
  34. }
  35. }
  36. if ($request->user()->profile->aliases->count() >= 3) {
  37. return back()->with('error', 'You can only add 3 account aliases.');
  38. }
  39. $webfingerService = WebfingerService::lookup($acct);
  40. $webfingerUrl = WebfingerService::rawGet($acct);
  41. if (! $webfingerService || ! isset($webfingerService['url']) || ! $webfingerUrl || empty($webfingerUrl)) {
  42. return back()->with('error', 'Invalid account, cannot add alias at this time.');
  43. }
  44. $alias = new ProfileAlias;
  45. $alias->profile_id = $request->user()->profile_id;
  46. $alias->acct = $acct;
  47. $alias->uri = $webfingerUrl;
  48. $alias->save();
  49. Cache::forget('pf:activitypub:user-object:by-id:'.$request->user()->profile_id);
  50. return back()->with('status', 'Successfully added alias!');
  51. }
  52. public function delete(Request $request)
  53. {
  54. $this->validate($request, [
  55. 'acct' => 'required',
  56. 'id' => 'required|exists:profile_aliases',
  57. ]);
  58. $pid = $request->user()->profile_id;
  59. $acct = $request->input('acct');
  60. $alias = ProfileAlias::where('profile_id', $pid)
  61. ->where('acct', $acct)
  62. ->findOrFail($request->input('id'));
  63. $migration = ProfileMigration::whereProfileId($pid)
  64. ->whereAcct($acct)
  65. ->first();
  66. if ($migration) {
  67. $request->user()->profile->update([
  68. 'moved_to_profile_id' => null,
  69. ]);
  70. }
  71. $alias->delete();
  72. Cache::forget('pf:activitypub:user-object:by-id:'.$pid);
  73. AccountService::del($pid);
  74. return back()->with('status', 'Successfully deleted alias!');
  75. }
  76. }