2021-04-21 05:11:43 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Services;
|
|
|
|
|
|
|
|
use Cache;
|
|
|
|
use App\Instance;
|
2023-05-11 10:06:27 +00:00
|
|
|
use App\Util\Blurhash\Blurhash;
|
2023-05-13 05:56:55 +00:00
|
|
|
use App\Services\ConfigCacheService;
|
2021-04-21 05:11:43 +00:00
|
|
|
|
|
|
|
class InstanceService
|
|
|
|
{
|
2023-01-15 09:35:10 +00:00
|
|
|
const CACHE_KEY_BY_DOMAIN = 'pf:services:instance:by_domain:';
|
2022-03-13 06:32:31 +00:00
|
|
|
const CACHE_KEY_BANNED_DOMAINS = 'instances:banned:domains';
|
|
|
|
const CACHE_KEY_UNLISTED_DOMAINS = 'instances:unlisted:domains';
|
|
|
|
const CACHE_KEY_NSFW_DOMAINS = 'instances:auto_cw:domains';
|
2023-03-19 11:29:54 +00:00
|
|
|
const CACHE_KEY_STATS = 'pf:services:instances:stats';
|
2023-05-13 05:56:55 +00:00
|
|
|
const CACHE_KEY_BANNER_BLURHASH = 'pf:services:instance:header-blurhash:v1';
|
2022-03-13 06:32:31 +00:00
|
|
|
|
2023-05-13 06:16:50 +00:00
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
ini_set('memory_limit', config('pixelfed.memory_limit', '1024M'));
|
|
|
|
}
|
|
|
|
|
2021-10-20 01:51:14 +00:00
|
|
|
public static function getByDomain($domain)
|
|
|
|
{
|
2023-01-15 09:35:10 +00:00
|
|
|
return Cache::remember(self::CACHE_KEY_BY_DOMAIN.$domain, 3600, function() use($domain) {
|
2021-10-20 01:51:14 +00:00
|
|
|
return Instance::whereDomain($domain)->first();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-04-21 05:11:43 +00:00
|
|
|
public static function getBannedDomains()
|
|
|
|
{
|
2022-12-31 13:25:43 +00:00
|
|
|
return Cache::remember(self::CACHE_KEY_BANNED_DOMAINS, 1209600, function() {
|
2021-04-21 05:11:43 +00:00
|
|
|
return Instance::whereBanned(true)->pluck('domain')->toArray();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function getUnlistedDomains()
|
|
|
|
{
|
2022-12-31 13:25:43 +00:00
|
|
|
return Cache::remember(self::CACHE_KEY_UNLISTED_DOMAINS, 1209600, function() {
|
2021-04-21 05:11:43 +00:00
|
|
|
return Instance::whereUnlisted(true)->pluck('domain')->toArray();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function getNsfwDomains()
|
|
|
|
{
|
2022-12-31 13:25:43 +00:00
|
|
|
return Cache::remember(self::CACHE_KEY_NSFW_DOMAINS, 1209600, function() {
|
2021-04-21 05:11:43 +00:00
|
|
|
return Instance::whereAutoCw(true)->pluck('domain')->toArray();
|
|
|
|
});
|
|
|
|
}
|
2021-08-31 06:37:28 +00:00
|
|
|
|
|
|
|
public static function software($domain)
|
|
|
|
{
|
|
|
|
$key = 'instances:software:' . strtolower($domain);
|
|
|
|
return Cache::remember($key, 86400, function() use($domain) {
|
|
|
|
$instance = Instance::whereDomain($domain)->first();
|
|
|
|
if(!$instance) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
return $instance->software;
|
|
|
|
});
|
|
|
|
}
|
2023-01-15 09:35:10 +00:00
|
|
|
|
2023-03-19 11:29:54 +00:00
|
|
|
public static function stats()
|
|
|
|
{
|
|
|
|
return Cache::remember(self::CACHE_KEY_STATS, 86400, function() {
|
|
|
|
return [
|
|
|
|
'total_count' => Instance::count(),
|
|
|
|
'new_count' => Instance::where('created_at', '>', now()->subDays(14))->count(),
|
|
|
|
'banned_count' => Instance::whereBanned(true)->count(),
|
|
|
|
'nsfw_count' => Instance::whereAutoCw(true)->count()
|
|
|
|
];
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-01-15 09:35:10 +00:00
|
|
|
public static function refresh()
|
|
|
|
{
|
|
|
|
Cache::forget(self::CACHE_KEY_BANNED_DOMAINS);
|
|
|
|
Cache::forget(self::CACHE_KEY_UNLISTED_DOMAINS);
|
|
|
|
Cache::forget(self::CACHE_KEY_NSFW_DOMAINS);
|
2023-03-19 11:29:54 +00:00
|
|
|
Cache::forget(self::CACHE_KEY_STATS);
|
2023-01-15 09:35:10 +00:00
|
|
|
|
|
|
|
self::getBannedDomains();
|
|
|
|
self::getUnlistedDomains();
|
|
|
|
self::getNsfwDomains();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2023-05-11 10:06:27 +00:00
|
|
|
|
|
|
|
public static function headerBlurhash()
|
|
|
|
{
|
|
|
|
return Cache::rememberForever(self::CACHE_KEY_BANNER_BLURHASH, function() {
|
|
|
|
if(str_ends_with(config_cache('app.banner_image'), 'headers/default.jpg')) {
|
|
|
|
return 'UzJR]l{wHZRjM}R%XRkCH?X9xaWEjZj]kAjt';
|
|
|
|
}
|
2023-05-13 05:56:55 +00:00
|
|
|
$cached = config_cache('instance.banner.blurhash');
|
|
|
|
|
|
|
|
if($cached && $cached !== 'UzJR]l{wHZRjM}R%XRkCH?X9xaWEjZj]kAjt') {
|
|
|
|
return $cached;
|
|
|
|
}
|
|
|
|
|
2023-05-11 10:06:27 +00:00
|
|
|
$file = config_cache('app.banner_image') ?? url(Storage::url('public/headers/default.jpg'));
|
|
|
|
|
|
|
|
$image = imagecreatefromstring(file_get_contents($file));
|
|
|
|
if(!$image) {
|
|
|
|
return 'UzJR]l{wHZRjM}R%XRkCH?X9xaWEjZj]kAjt';
|
|
|
|
}
|
|
|
|
$width = imagesx($image);
|
|
|
|
$height = imagesy($image);
|
|
|
|
|
|
|
|
$pixels = [];
|
|
|
|
for ($y = 0; $y < $height; ++$y) {
|
|
|
|
$row = [];
|
|
|
|
for ($x = 0; $x < $width; ++$x) {
|
|
|
|
$index = imagecolorat($image, $x, $y);
|
|
|
|
$colors = imagecolorsforindex($image, $index);
|
|
|
|
|
|
|
|
$row[] = [$colors['red'], $colors['green'], $colors['blue']];
|
|
|
|
}
|
|
|
|
$pixels[] = $row;
|
|
|
|
}
|
|
|
|
|
|
|
|
$components_x = 4;
|
|
|
|
$components_y = 4;
|
|
|
|
$blurhash = Blurhash::encode($pixels, $components_x, $components_y);
|
|
|
|
if(strlen($blurhash) > 191) {
|
|
|
|
return 'UzJR]l{wHZRjM}R%XRkCH?X9xaWEjZj]kAjt';
|
|
|
|
}
|
|
|
|
|
2023-05-13 05:56:55 +00:00
|
|
|
ConfigCacheService::put('instance.banner.blurhash', $blurhash);
|
|
|
|
|
2023-05-11 10:06:27 +00:00
|
|
|
return $blurhash;
|
|
|
|
});
|
|
|
|
}
|
2021-04-21 05:11:43 +00:00
|
|
|
}
|