DecrementPostCount.php 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <?php
  2. namespace App\Jobs\ProfilePipeline;
  3. use Illuminate\Bus\Queueable;
  4. use Illuminate\Contracts\Queue\ShouldBeUnique;
  5. use Illuminate\Contracts\Queue\ShouldQueue;
  6. use Illuminate\Foundation\Bus\Dispatchable;
  7. use Illuminate\Queue\InteractsWithQueue;
  8. use Illuminate\Queue\SerializesModels;
  9. use App\Profile;
  10. use App\Status;
  11. use App\Services\AccountService;
  12. class DecrementPostCount implements ShouldQueue
  13. {
  14. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  15. public $id;
  16. /**
  17. * Create a new job instance.
  18. *
  19. * @return void
  20. */
  21. public function __construct($id)
  22. {
  23. $this->id = $id;
  24. }
  25. /**
  26. * Execute the job.
  27. *
  28. * @return void
  29. */
  30. public function handle()
  31. {
  32. $id = $this->id;
  33. $profile = Profile::find($id);
  34. if(!$profile) {
  35. return 1;
  36. }
  37. if($profile->updated_at && $profile->updated_at->lt(now()->subDays(30))) {
  38. $profile->status_count = Status::whereProfileId($id)->whereNull(['in_reply_to_id', 'reblog_of_id'])->count();
  39. $profile->save();
  40. AccountService::del($id);
  41. } else {
  42. $profile->status_count = $profile->status_count ? $profile->status_count - 1 : 0;
  43. $profile->save();
  44. AccountService::del($id);
  45. }
  46. return 1;
  47. }
  48. }