InstanceService.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. namespace App\Services;
  3. use App\Instance;
  4. use App\Util\Blurhash\Blurhash;
  5. use Illuminate\Support\Facades\Cache;
  6. use Illuminate\Support\Facades\DB;
  7. class InstanceService
  8. {
  9. const CACHE_KEY_BY_DOMAIN = 'pf:services:instance:by_domain:';
  10. const CACHE_KEY_BANNED_DOMAINS = 'instances:banned:domains';
  11. const CACHE_KEY_UNLISTED_DOMAINS = 'instances:unlisted:domains';
  12. const CACHE_KEY_NSFW_DOMAINS = 'instances:auto_cw:domains';
  13. const CACHE_KEY_STATS = 'pf:services:instances:stats';
  14. const CACHE_KEY_TOTAL_POSTS = 'pf:services:instances:self:total-posts';
  15. const CACHE_KEY_BANNER_BLURHASH = 'pf:services:instance:header-blurhash:v1';
  16. const CACHE_KEY_API_PEERS_LIST = 'pf:services:instance:api:peers:list:v0';
  17. public function __construct()
  18. {
  19. ini_set('memory_limit', config('pixelfed.memory_limit', '1024M'));
  20. }
  21. public static function getByDomain($domain)
  22. {
  23. return Cache::remember(self::CACHE_KEY_BY_DOMAIN.$domain, 3600, function () use ($domain) {
  24. return Instance::whereDomain($domain)->first();
  25. });
  26. }
  27. public static function getBannedDomains()
  28. {
  29. return Cache::remember(self::CACHE_KEY_BANNED_DOMAINS, 1209600, function () {
  30. return Instance::whereBanned(true)->pluck('domain')->toArray();
  31. });
  32. }
  33. public static function getUnlistedDomains()
  34. {
  35. return Cache::remember(self::CACHE_KEY_UNLISTED_DOMAINS, 1209600, function () {
  36. return Instance::whereUnlisted(true)->pluck('domain')->toArray();
  37. });
  38. }
  39. public static function getNsfwDomains()
  40. {
  41. return Cache::remember(self::CACHE_KEY_NSFW_DOMAINS, 1209600, function () {
  42. return Instance::whereAutoCw(true)->pluck('domain')->toArray();
  43. });
  44. }
  45. public static function software($domain)
  46. {
  47. $key = 'instances:software:'.strtolower($domain);
  48. return Cache::remember($key, 86400, function () use ($domain) {
  49. $instance = Instance::whereDomain($domain)->first();
  50. if (! $instance) {
  51. return;
  52. }
  53. return $instance->software;
  54. });
  55. }
  56. public static function stats()
  57. {
  58. return Cache::remember(self::CACHE_KEY_STATS, 86400, function () {
  59. return [
  60. 'total_count' => Instance::count(),
  61. 'new_count' => Instance::where('created_at', '>', now()->subDays(14))->count(),
  62. 'banned_count' => Instance::whereBanned(true)->count(),
  63. 'nsfw_count' => Instance::whereAutoCw(true)->count(),
  64. ];
  65. });
  66. }
  67. public static function refresh()
  68. {
  69. Cache::forget(self::CACHE_KEY_BANNED_DOMAINS);
  70. Cache::forget(self::CACHE_KEY_UNLISTED_DOMAINS);
  71. Cache::forget(self::CACHE_KEY_NSFW_DOMAINS);
  72. Cache::forget(self::CACHE_KEY_STATS);
  73. Cache::forget(self::CACHE_KEY_API_PEERS_LIST);
  74. self::getBannedDomains();
  75. self::getUnlistedDomains();
  76. self::getNsfwDomains();
  77. return true;
  78. }
  79. public static function totalLocalStatuses()
  80. {
  81. if (config('instance.enable_cc')) {
  82. return config_cache('instance.stats.total_local_posts');
  83. }
  84. return Cache::remember(self::CACHE_KEY_TOTAL_POSTS, now()->addHour(), function () {
  85. return DB::table('statuses')
  86. ->whereNull('deleted_at')
  87. ->where('local', true)
  88. ->whereNot('type', 'share')
  89. ->count();
  90. });
  91. }
  92. public static function headerBlurhash()
  93. {
  94. return Cache::rememberForever(self::CACHE_KEY_BANNER_BLURHASH, function () {
  95. if (str_ends_with(config_cache('app.banner_image'), 'headers/default.jpg')) {
  96. return 'UzJR]l{wHZRjM}R%XRkCH?X9xaWEjZj]kAjt';
  97. }
  98. $cached = config_cache('instance.banner.blurhash');
  99. if ($cached) {
  100. return $cached;
  101. }
  102. $file = config_cache('app.banner_image') ?? url(Storage::url('public/headers/default.jpg'));
  103. $image = imagecreatefromstring(file_get_contents($file));
  104. if (! $image) {
  105. return 'UzJR]l{wHZRjM}R%XRkCH?X9xaWEjZj]kAjt';
  106. }
  107. $width = imagesx($image);
  108. $height = imagesy($image);
  109. $pixels = [];
  110. for ($y = 0; $y < $height; $y++) {
  111. $row = [];
  112. for ($x = 0; $x < $width; $x++) {
  113. $index = imagecolorat($image, $x, $y);
  114. $colors = imagecolorsforindex($image, $index);
  115. $row[] = [$colors['red'], $colors['green'], $colors['blue']];
  116. }
  117. $pixels[] = $row;
  118. }
  119. // Free the allocated GdImage object from memory:
  120. imagedestroy($image);
  121. $components_x = 4;
  122. $components_y = 4;
  123. $blurhash = Blurhash::encode($pixels, $components_x, $components_y);
  124. if (strlen($blurhash) > 191) {
  125. return 'UzJR]l{wHZRjM}R%XRkCH?X9xaWEjZj]kAjt';
  126. }
  127. ConfigCacheService::put('instance.banner.blurhash', $blurhash);
  128. return $blurhash;
  129. });
  130. }
  131. }