Blame view

app/Observers/MessageObserver.php 1.9 KB
f86fd56d9   Fedor   task-132985 autor...
1
2
3
4
5
6
7
  <?php
  
  namespace App\Observers;
  
  use App\Models\Chat;
  use App\Models\Employer;
  use App\Models\Message;
8ec6b4403   Fedor   task-132985 autor...
8
  use App\Models\User;
f86fd56d9   Fedor   task-132985 autor...
9
10
11
12
13
14
  use Illuminate\Http\Request;
  
  class MessageObserver
  {
      public function created(Message $message): void
      {
8ec6b4403   Fedor   task-132985 autor...
15
16
          /** @var User $user */
          $user = User::find($message->to_user_id);
f86fd56d9   Fedor   task-132985 autor...
17

8ec6b4403   Fedor   task-132985 autor...
18
          if ($user === null || !$user->autoresponder) {
f86fd56d9   Fedor   task-132985 autor...
19
20
21
22
23
24
              return;
          }
  
          $recentAutoresponderMessage = Message::query()
              ->where('user_id', $message->to_user_id)
              ->where('to_user_id', $message->user_id)
8ec6b4403   Fedor   task-132985 autor...
25
              ->where('text', $user->autoresponder_message)
f86fd56d9   Fedor   task-132985 autor...
26
27
28
29
30
31
32
33
34
35
36
37
38
              ->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: [
8ec6b4403   Fedor   task-132985 autor...
39
                  'text' => $user->autoresponder_message,
f86fd56d9   Fedor   task-132985 autor...
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
                  '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)
      {
          //
      }
  }