InstanceUpdateTotalLocalPosts.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Services\ConfigCacheService;
  4. use Cache;
  5. use DB;
  6. use Illuminate\Console\Command;
  7. use Storage;
  8. class InstanceUpdateTotalLocalPosts extends Command
  9. {
  10. /**
  11. * The name and signature of the console command.
  12. *
  13. * @var string
  14. */
  15. protected $signature = 'app:instance-update-total-local-posts';
  16. /**
  17. * The console command description.
  18. *
  19. * @var string
  20. */
  21. protected $description = 'Update the total number of local statuses/post count';
  22. /**
  23. * Execute the console command.
  24. */
  25. public function handle()
  26. {
  27. $cached = $this->checkForCache();
  28. if (! $cached) {
  29. $this->initCache();
  30. return;
  31. }
  32. $cache = $this->getCached();
  33. if (! $cache || ! isset($cache['count'])) {
  34. $this->error('Problem fetching cache');
  35. return;
  36. }
  37. $this->updateAndCache();
  38. Cache::forget('api:nodeinfo');
  39. }
  40. protected function checkForCache()
  41. {
  42. return Storage::exists('total_local_posts.json');
  43. }
  44. protected function initCache()
  45. {
  46. $count = DB::table('statuses')->whereNull(['url', 'deleted_at'])->count();
  47. $res = [
  48. 'count' => $count,
  49. ];
  50. Storage::put('total_local_posts.json', json_encode($res, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT));
  51. ConfigCacheService::put('instance.stats.total_local_posts', $res['count']);
  52. }
  53. protected function getCached()
  54. {
  55. return Storage::json('total_local_posts.json');
  56. }
  57. protected function updateAndCache()
  58. {
  59. $count = DB::table('statuses')->whereNull(['url', 'deleted_at'])->count();
  60. $res = [
  61. 'count' => $count,
  62. ];
  63. Storage::put('total_local_posts.json', json_encode($res, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT));
  64. ConfigCacheService::put('instance.stats.total_local_posts', $res['count']);
  65. }
  66. }