Commit fd2519b604a8621ec60e829d26c01dc7fb63525c
Exists in
master
Merge branch 'master' of http://gitlab.nologostudio.ru/alarionov/rekamore-su
Showing 1 changed file Inline Diff
app/Models/Chat.php
1 | <?php | 1 | <?php |
2 | 2 | ||
3 | namespace App\Models; | 3 | namespace App\Models; |
4 | 4 | ||
5 | use Illuminate\Database\Eloquent\Factories\HasFactory; | 5 | use Illuminate\Database\Eloquent\Factories\HasFactory; |
6 | use Illuminate\Database\Eloquent\Model; | 6 | use Illuminate\Database\Eloquent\Model; |
7 | use Carbon\Carbon; | 7 | use Carbon\Carbon; |
8 | 8 | ||
9 | class Chat extends Model | 9 | class Chat extends Model |
10 | { | 10 | { |
11 | use HasFactory; | 11 | use HasFactory; |
12 | 12 | ||
13 | protected $fillable = [ | 13 | protected $fillable = [ |
14 | 'user_id', | 14 | 'user_id', |
15 | 'to_user_id', | 15 | 'to_user_id', |
16 | 'is_removed', | 16 | 'is_removed', |
17 | 'is_fixed', | 17 | 'is_fixed', |
18 | 'last_message_date', | 18 | 'last_message_date', |
19 | 'last_message_id', | 19 | 'last_message_id', |
20 | 'fixed_time', | 20 | 'fixed_time', |
21 | 'is_admin_chat' | 21 | 'is_admin_chat' |
22 | ]; | 22 | ]; |
23 | 23 | ||
24 | public function user() { | 24 | public function user() { |
25 | return $this->belongsTo(User::class, 'to_user_id'); | 25 | return $this->belongsTo(User::class, 'to_user_id'); |
26 | } | 26 | } |
27 | 27 | ||
28 | public function worker() { | 28 | public function worker() { |
29 | return $this->belongsTo(Worker::class, 'to_user_id', 'user_id'); | 29 | return $this->belongsTo(Worker::class, 'to_user_id', 'user_id'); |
30 | } | 30 | } |
31 | 31 | ||
32 | public function employer() { | 32 | public function employer() { |
33 | return $this->belongsTo(Employer::class, 'to_user_id', 'user_id'); | 33 | return $this->belongsTo(Employer::class, 'to_user_id', 'user_id'); |
34 | } | 34 | } |
35 | 35 | ||
36 | public function last_message() { | 36 | public function last_message() { |
37 | return $this->belongsTo(Message::class, 'last_message_id'); | 37 | return $this->belongsTo(Message::class, 'last_message_id'); |
38 | } | 38 | } |
39 | 39 | ||
40 | public function unread_messages() | 40 | public function unread_messages() |
41 | { | 41 | { |
42 | return $this->hasMany(Message::class, 'user_id', 'to_user_id'); | 42 | return $this->hasMany(Message::class, 'user_id', 'to_user_id'); |
43 | } | 43 | } |
44 | 44 | ||
45 | public function admin_chat_unread_messages() | 45 | public function admin_chat_unread_messages() |
46 | { | 46 | { |
47 | return $this->hasMany(Message::class, 'to_user_id', 'to_user_id'); | 47 | return $this->hasMany(Message::class, 'to_user_id', 'to_user_id'); |
48 | } | 48 | } |
49 | 49 | ||
50 | public static function pin_chat(int $chat_id, $fixed) | 50 | public static function pin_chat(int $chat_id, $fixed) |
51 | { | 51 | { |
52 | return self::where('id', '=', $chat_id) | 52 | return self::where('id', '=', $chat_id) |
53 | ->update([ | 53 | ->update([ |
54 | 'is_fixed' => !empty($fixed) ? 1 : 0, | 54 | 'is_fixed' => !empty($fixed) ? 1 : 0, |
55 | 'fixation_date' => !empty($fixed) ? Carbon::now() : null | 55 | 'fixation_date' => !empty($fixed) ? Carbon::now() : null |
56 | ]); | 56 | ]); |
57 | } | 57 | } |
58 | 58 | ||
59 | public static function remove_chat(int $chat_id) | 59 | public static function remove_chat(int $chat_id) |
60 | { | 60 | { |
61 | return self::where('id', '=', $chat_id) | 61 | return self::where('id', '=', $chat_id) |
62 | ->update(['is_removed' => 1]); | 62 | ->update(['is_removed' => 1]); |
63 | } | 63 | } |
64 | 64 | ||
65 | public static function get_user_chats(int $user_id){ | 65 | public static function get_user_chats(int $user_id){ |
66 | return Chat::query() | 66 | return Chat::query() |
67 | ->with('user') | 67 | ->with('user') |
68 | ->with('worker') | 68 | ->with('worker') |
69 | ->with('employer') | 69 | ->with('employer') |
70 | ->with('last_message') | 70 | ->with('last_message') |
71 | ->withCount(['unread_messages' => function ($query) use($user_id) { | 71 | ->withCount(['unread_messages' => function ($query) use($user_id) { |
72 | $query->where('to_user_id', '=', $user_id)->where('flag_new', '=', 1); | 72 | $query |
73 | ->where('to_user_id', '=', $user_id) | ||
74 | ->whereNotNull('chat_id_from') | ||
75 | ->where('flag_new', '=', 1) | ||
76 | ; | ||
73 | }]) | 77 | }]) |
74 | ->where('user_id', '=', $user_id) | 78 | ->where('user_id', '=', $user_id) |
75 | ->where('is_removed', '=', 0) | 79 | ->where('is_removed', '=', 0) |
76 | ->orderByDesc('is_fixed') | 80 | ->orderByDesc('is_fixed') |
77 | ->orderByDesc('fixation_date') | 81 | ->orderByDesc('fixation_date') |
78 | ->orderByDesc('last_message_date') | 82 | ->orderByDesc('last_message_date') |
79 | ->paginate(5) | 83 | ->paginate(5) |
80 | ; | 84 | ; |
81 | } | 85 | } |
82 | 86 | ||
83 | public static function get_user_admin_chat(int $user_id) | 87 | public static function get_user_admin_chat(int $user_id) |
84 | { | 88 | { |
85 | return Chat::query() | 89 | return Chat::query() |
86 | ->with('last_message') | 90 | ->with('last_message') |
87 | ->withCount(['admin_chat_unread_messages' => function ($query) use($user_id) { | 91 | ->withCount(['admin_chat_unread_messages' => function ($query) use($user_id) { |
88 | $query->whereNull('chat_id_from')->where('to_user_id', '=', $user_id)->where('flag_new', '=', 1); | 92 | $query->whereNull('chat_id_from')->where('to_user_id', '=', $user_id)->where('flag_new', '=', 1); |
89 | }]) | 93 | }]) |
90 | ->where('to_user_id', '=', $user_id) | 94 | ->where('to_user_id', '=', $user_id) |
91 | ->where('is_admin_chat', 1) | 95 | ->where('is_admin_chat', 1) |
92 | ->first() | 96 | ->first() |
93 | ; | 97 | ; |
94 | } | 98 | } |
95 | 99 | ||
96 | public static function get_chat_messages(Chat $chat){ | 100 | public static function get_chat_messages(Chat $chat){ |
97 | if ($chat->is_admin_chat){ | 101 | if ($chat->is_admin_chat){ |
98 | return Message::query() | 102 | return Message::query() |
99 | ->where('chat_id_to', $chat->id) | 103 | ->where('chat_id_to', $chat->id) |
100 | ->where('to_user_id', $chat->to_user_id) | 104 | ->where('to_user_id', $chat->to_user_id) |
101 | ->orderBy('created_at') | 105 | ->orderBy('created_at') |
102 | ->get() | 106 | ->get() |
103 | ; | 107 | ; |
104 | } else { | 108 | } else { |
105 | return Message::query() | 109 | return Message::query() |
106 | ->where(function ($query) use ($chat) { | 110 | ->where(function ($query) use ($chat) { |
107 | $query->where('chat_id_from', $chat->id)->orWhere('chat_id_to', $chat->id); | 111 | $query->where('chat_id_from', $chat->id)->orWhere('chat_id_to', $chat->id); |
108 | }) | 112 | }) |
109 | ->where(function($query) use ($chat) { | 113 | ->where(function($query) use ($chat) { |
110 | $query | 114 | $query |
111 | ->where(function($query) use ($chat) { | 115 | ->where(function($query) use ($chat) { |
112 | $query->where('user_id', $chat->user_id)->where('to_user_id', $chat->to_user_id); | 116 | $query->where('user_id', $chat->user_id)->where('to_user_id', $chat->to_user_id); |
113 | }) | 117 | }) |
114 | ->orWhere(function($query) use ($chat) { | 118 | ->orWhere(function($query) use ($chat) { |
115 | $query->where('user_id', $chat->to_user_id)->where('to_user_id', $chat->user_id); | 119 | $query->where('user_id', $chat->to_user_id)->where('to_user_id', $chat->user_id); |
116 | }) | 120 | }) |
117 | ; | 121 | ; |
118 | }) | 122 | }) |
119 | ->OrderBy('created_at') | 123 | ->OrderBy('created_at') |
120 | ->get() | 124 | ->get() |
121 | ; | 125 | ; |
122 | } | 126 | } |
123 | } | 127 | } |
124 | 128 | ||
125 | } | 129 | } |
126 | 130 |