AutolinkService.php 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <?php
  2. namespace App\Services;
  3. use Cache;
  4. use App\Profile;
  5. use Illuminate\Support\Str;
  6. use Illuminate\Support\Facades\Http;
  7. use App\Util\Webfinger\WebfingerUrl;
  8. class AutolinkService
  9. {
  10. const CACHE_KEY = 'pf:services:autolink:';
  11. public static function mentionedUsernameExists($username)
  12. {
  13. $key = 'pf:services:autolink:userexists:' . hash('sha256', $username);
  14. return Cache::remember($key, 3600, function() use($username) {
  15. $remote = Str::of($username)->contains('@');
  16. $profile = Profile::whereUsername($username)->first();
  17. if($profile) {
  18. if($profile->domain != null) {
  19. $instance = InstanceService::getByDomain($profile->domain);
  20. if($instance && $instance->banned == true) {
  21. return false;
  22. }
  23. }
  24. return true;
  25. } else {
  26. if($remote) {
  27. $parts = explode('@', $username);
  28. $domain = last($parts);
  29. $instance = InstanceService::getByDomain($domain);
  30. if($instance) {
  31. if($instance->banned == true) {
  32. return false;
  33. } else {
  34. $wf = WebfingerUrl::generateWebfingerUrl($username);
  35. $res = Http::head($wf);
  36. return $res->ok();
  37. }
  38. } else {
  39. $wf = WebfingerUrl::generateWebfingerUrl($username);
  40. $res = Http::head($wf);
  41. return $res->ok();
  42. }
  43. }
  44. }
  45. return false;
  46. });
  47. }
  48. }