Переглянути джерело

Update AutolinkService, optimize lookups

Daniel Supernault 7 місяців тому
батько
коміт
eac2c19601
1 змінених файлів з 15 додано та 43 видалено
  1. 15 43
      app/Services/AutolinkService.php

+ 15 - 43
app/Services/AutolinkService.php

@@ -2,53 +2,25 @@
 
 namespace App\Services;
 
-use Cache;
 use App\Profile;
-use Illuminate\Support\Str;
-use Illuminate\Support\Facades\Http;
-use App\Util\Webfinger\WebfingerUrl;
+use Cache;
+use Purify;
 
 class AutolinkService
 {
-	const CACHE_KEY = 'pf:services:autolink:';
-
-	public static function mentionedUsernameExists($username)
-	{
-		$key = 'pf:services:autolink:userexists:' . hash('sha256', $username);
+    const CACHE_KEY = 'pf:services:autolink:mue:';
 
-		return Cache::remember($key, 3600, function() use($username) {
-			$remote = Str::of($username)->contains('@');
-			$profile = Profile::whereUsername($username)->first();
-			if($profile) {
-				if($profile->domain != null) {
-					$instance = InstanceService::getByDomain($profile->domain);
-					if($instance && $instance->banned == true) {
-						return false;
-					}
-				}
-				return true;
-			} else {
-				if($remote) {
-					$parts = explode('@', $username);
-					$domain = last($parts);
-					$instance = InstanceService::getByDomain($domain);
+    public static function mentionedUsernameExists($username)
+    {
+        if (str_starts_with($username, '@')) {
+            if (substr_count($username, '@') === 1) {
+                $username = substr($username, 1);
+            }
+        }
+        $name = Purify::clean(strtolower($username));
 
-					if($instance) {
-						if($instance->banned == true) {
-							return false;
-						} else {
-							$wf = WebfingerUrl::generateWebfingerUrl($username);
-							$res = Http::head($wf);
-							return $res->ok();
-						}
-					} else {
-						$wf = WebfingerUrl::generateWebfingerUrl($username);
-						$res = Http::head($wf);
-						return $res->ok();
-					}
-				}
-			}
-			return false;
-		});
-	}
+        return Cache::remember(self::CACHE_KEY.base64_encode($name), 7200, function () use ($name) {
+            return Profile::where('username', $name)->exists();
+        });
+    }
 }