AutospamUpdateCachedDataPipeline.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. namespace App\Jobs\AutospamPipeline;
  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\Models\AutospamCustomTokens;
  10. use Illuminate\Support\Facades\Storage;
  11. use App\Services\AutospamService;
  12. use Cache;
  13. class AutospamUpdateCachedDataPipeline implements ShouldQueue
  14. {
  15. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  16. /**
  17. * Create a new job instance.
  18. */
  19. public function __construct()
  20. {
  21. }
  22. /**
  23. * Execute the job.
  24. */
  25. public function handle(): void
  26. {
  27. $spamExists = Storage::exists(AutospamService::MODEL_SPAM_PATH);
  28. if($spamExists) {
  29. $spam = json_decode(Storage::get(AutospamService::MODEL_SPAM_PATH), true);
  30. } else {
  31. $spam = [
  32. 'documents' => [
  33. 'spam' => 0
  34. ],
  35. 'words' => [
  36. 'spam' => []
  37. ]
  38. ];
  39. }
  40. $newSpam = AutospamCustomTokens::whereCategory('spam')->get();
  41. foreach($newSpam as $ns) {
  42. $key = strtolower($ns->token);
  43. if(isset($spam['words']['spam'][$key])) {
  44. $spam['words']['spam'][$key] = $spam['words']['spam'][$key] + $ns->weight;
  45. } else {
  46. $spam['words']['spam'][$key] = $ns->weight;
  47. }
  48. }
  49. $newSpamCount = count($spam['words']['spam']);
  50. if($newSpamCount) {
  51. $spam['documents']['spam'] = $newSpamCount;
  52. arsort($spam['words']['spam']);
  53. Storage::put(AutospamService::MODEL_SPAM_PATH, json_encode($spam, JSON_UNESCAPED_SLASHES|JSON_PRETTY_PRINT));
  54. }
  55. $hamExists = Storage::exists(AutospamService::MODEL_HAM_PATH);
  56. if($hamExists) {
  57. $ham = json_decode(Storage::get(AutospamService::MODEL_HAM_PATH), true);
  58. } else {
  59. $ham = [
  60. 'documents' => [
  61. 'ham' => 0
  62. ],
  63. 'words' => [
  64. 'ham' => []
  65. ]
  66. ];
  67. }
  68. $newHam = AutospamCustomTokens::whereCategory('ham')->get();
  69. foreach($newHam as $ns) {
  70. $key = strtolower($ns->token);
  71. if(isset($spam['words']['ham'][$key])) {
  72. $ham['words']['ham'][$key] = $ham['words']['ham'][$key] + $ns->weight;
  73. } else {
  74. $ham['words']['ham'][$key] = $ns->weight;
  75. }
  76. }
  77. $newHamCount = count($ham['words']['ham']);
  78. if($newHamCount) {
  79. $ham['documents']['ham'] = $newHamCount;
  80. arsort($ham['words']['ham']);
  81. Storage::put(AutospamService::MODEL_HAM_PATH, json_encode($ham, JSON_UNESCAPED_SLASHES|JSON_PRETTY_PRINT));
  82. }
  83. if($newSpamCount && $newHamCount) {
  84. $combined = [
  85. 'documents' => [
  86. 'spam' => $newSpamCount,
  87. 'ham' => $newHamCount,
  88. ],
  89. 'words' => [
  90. 'spam' => $spam['words']['spam'],
  91. 'ham' => $ham['words']['ham']
  92. ]
  93. ];
  94. Storage::put(AutospamService::MODEL_FILE_PATH, json_encode($combined, JSON_PRETTY_PRINT,JSON_UNESCAPED_SLASHES));
  95. }
  96. Cache::forget(AutospamService::MODEL_CACHE_KEY);
  97. Cache::forget(AutospamService::CHCKD_CACHE_KEY);
  98. }
  99. }