InstanceUpdateTotalLocalPosts.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. $res = [
  47. 'count' => $this->getTotalLocalPosts(),
  48. ];
  49. Storage::put('total_local_posts.json', json_encode($res, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT));
  50. ConfigCacheService::put('instance.stats.total_local_posts', $res['count']);
  51. }
  52. protected function getCached()
  53. {
  54. return Storage::json('total_local_posts.json');
  55. }
  56. protected function updateAndCache()
  57. {
  58. $res = [
  59. 'count' => $this->getTotalLocalPosts(),
  60. ];
  61. Storage::put('total_local_posts.json', json_encode($res, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT));
  62. ConfigCacheService::put('instance.stats.total_local_posts', $res['count']);
  63. }
  64. protected function getTotalLocalPosts()
  65. {
  66. return DB::table('statuses')
  67. ->whereNull('deleted_at')
  68. ->where('local', true)
  69. ->whereNot('type', 'share') # Ignore boosts for the post count
  70. ->count();
  71. }
  72. }