mirror of
https://github.com/pixelfed/pixelfed.git
synced 2024-12-23 05:23:17 +00:00
Update AutolinkService, optimize lookups
This commit is contained in:
parent
9eeb7b6741
commit
eac2c19601
1 changed files with 15 additions and 43 deletions
|
@ -2,53 +2,25 @@
|
||||||
|
|
||||||
namespace App\Services;
|
namespace App\Services;
|
||||||
|
|
||||||
use Cache;
|
|
||||||
use App\Profile;
|
use App\Profile;
|
||||||
use Illuminate\Support\Str;
|
use Cache;
|
||||||
use Illuminate\Support\Facades\Http;
|
use Purify;
|
||||||
use App\Util\Webfinger\WebfingerUrl;
|
|
||||||
|
|
||||||
class AutolinkService
|
class AutolinkService
|
||||||
{
|
{
|
||||||
const CACHE_KEY = 'pf:services:autolink:';
|
const CACHE_KEY = 'pf:services:autolink:mue:';
|
||||||
|
|
||||||
public static function mentionedUsernameExists($username)
|
public static function mentionedUsernameExists($username)
|
||||||
{
|
{
|
||||||
$key = 'pf:services:autolink:userexists:' . hash('sha256', $username);
|
if (str_starts_with($username, '@')) {
|
||||||
|
if (substr_count($username, '@') === 1) {
|
||||||
|
$username = substr($username, 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
$name = Purify::clean(strtolower($username));
|
||||||
|
|
||||||
return Cache::remember($key, 3600, function() use($username) {
|
return Cache::remember(self::CACHE_KEY.base64_encode($name), 7200, function () use ($name) {
|
||||||
$remote = Str::of($username)->contains('@');
|
return Profile::where('username', $name)->exists();
|
||||||
$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);
|
|
||||||
|
|
||||||
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;
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue