Blame view

app/Observers/MessageObserver.php 1.96 KB
f86fd56d9   Fedor   task-132985 autor...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
  <?php
  
  namespace App\Observers;
  
  use App\Models\Chat;
  use App\Models\Employer;
  use App\Models\Message;
  use Illuminate\Http\Request;
  
  class MessageObserver
  {
      public function created(Message $message): void
      {
          /** @var Employer $employer */
          $employer = Employer::query()
              ->where('user_id',  $message->to_user_id)
              ->first();
  
          if ($employer === null || !$employer->autoresponder) {
              return;
          }
  
          $recentAutoresponderMessage = Message::query()
              ->where('user_id', $message->to_user_id)
              ->where('to_user_id', $message->user_id)
              ->where('text', $employer->autoresponder_message)
              ->where('created_at', '>', now()->subDays(4))
              ->orderBy('id', 'desc')
              ->first();
  
          if ($recentAutoresponderMessage !== null) {
              return;
          }
  
          Message::add_message(
              request: new Request(),
              user_id: $message->to_user_id,
              to_user_id: $message->user_id,
              message_params: [
                  'text' => $employer->autoresponder_message,
                  'flag_new' => 1
              ]
          );
      }
  
      /**
       * Handle the Message "updated" event.
       *
       * @param  \App\Models\Message  $message
       * @return void
       */
      public function updated(Message $message)
      {
          //
      }
  
      /**
       * Handle the Message "deleted" event.
       *
       * @param  \App\Models\Message  $message
       * @return void
       */
      public function deleted(Message $message)
      {
          //
      }
  
      /**
       * Handle the Message "restored" event.
       *
       * @param  \App\Models\Message  $message
       * @return void
       */
      public function restored(Message $message)
      {
          //
      }
  
      /**
       * Handle the Message "force deleted" event.
       *
       * @param  \App\Models\Message  $message
       * @return void
       */
      public function forceDeleted(Message $message)
      {
          //
      }
  }