forked from mirror/pixelfed
Add AutolinkService
This commit is contained in:
parent
d02a8f005f
commit
639e9859ed
1 changed files with 54 additions and 0 deletions
54
app/Services/AutolinkService.php
Normal file
54
app/Services/AutolinkService.php
Normal file
|
@ -0,0 +1,54 @@
|
|||
<?php
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use Cache;
|
||||
use App\Profile;
|
||||
use Illuminate\Support\Str;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use App\Util\Webfinger\WebfingerUrl;
|
||||
|
||||
class AutolinkService
|
||||
{
|
||||
const CACHE_KEY = 'pf:services:autolink:';
|
||||
|
||||
public static function mentionedUsernameExists($username)
|
||||
{
|
||||
$key = 'pf:services:autolink:userexists:' . hash('sha256', $username);
|
||||
|
||||
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);
|
||||
|
||||
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