-
Notifications
You must be signed in to change notification settings - Fork 2
/
messager.blade.php
354 lines (306 loc) · 12.1 KB
/
messager.blade.php
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
@extends('layouts.app')
@section('content')
<div class="container con-t">
<div class="row">
<nav class="menu">
<ul class="items">
<li class="item">
<i class="fa fa-home" aria-hidden="true"></i>
</li>
<li class="item">
<i class="fa fa-user" aria-hidden="true"></i>
</li>
<li class="item">
<i class="fa fa-pencil" aria-hidden="true"></i>
</li>
<li class="item item-active">
<i class="fa fa-commenting" aria-hidden="true"></i>
</li>
<li class="item">
<i class="fa fa-file" aria-hidden="true"></i>
</li>
<li class="item">
<i class="fa fa-cog" aria-hidden="true"></i>
</li>
</ul>
</nav>
<section class="discussions">
<div class="discussion search">
<div class="searchbar">
<i class="fa fa-search" aria-hidden="true"></i>
<input type="text" placeholder="Search...">
</div>
</div>
<div class="discussion message-active">
<div class="photo" style="background-image: url(https://images.unsplash.com/photo-1438761681033-6461ffad8d80?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=1050&q=80);">
<div class="online"></div>
</div>
<div class="desc-contact">
<p class="name">Megan Leib</p>
<p class="message">9 pm at the bar if possible 😳</p>
</div>
<div class="timer">12 sec</div>
</div>
<div class="discussion">
<div class="photo" style="background-image: url(https://i.pinimg.com/originals/a9/26/52/a926525d966c9479c18d3b4f8e64b434.jpg);">
<div class="online"></div>
</div>
<div class="desc-contact">
<p class="name">Dave Corlew</p>
<p class="message">Let's meet for a coffee or something today ?</p>
</div>
<div class="timer">3 min</div>
</div>
<div class="discussion">
<div class="photo" style="background-image: url(https://images.unsplash.com/photo-1497551060073-4c5ab6435f12?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=667&q=80);">
</div>
<div class="desc-contact">
<p class="name">Jerome Seiber</p>
<p class="message">I've sent you the annual report</p>
</div>
<div class="timer">42 min</div>
</div>
<div class="discussion">
<div class="photo" style="background-image: url(https://card.thomasdaubenton.com/img/photo.jpg);">
<div class="online"></div>
</div>
<div class="desc-contact">
<p class="name">Thomas Dbtn</p>
<p class="message">See you tomorrow ! 🙂</p>
</div>
<div class="timer">2 hour</div>
</div>
<div class="discussion">
<div class="photo" style="background-image: url(https://images.unsplash.com/photo-1553514029-1318c9127859?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=700&q=80);">
</div>
<div class="desc-contact">
<p class="name">Elsie Amador</p>
<p class="message">What the f**k is going on ?</p>
</div>
<div class="timer">1 day</div>
</div>
<div class="discussion">
<div class="photo" style="background-image: url(https://images.unsplash.com/photo-1541747157478-3222166cf342?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=967&q=80);">
</div>
<div class="desc-contact">
<p class="name">Billy Southard</p>
<p class="message">Ahahah 😂</p>
</div>
<div class="timer">4 days</div>
</div>
<div class="discussion">
<div class="photo" style="background-image: url(https://images.unsplash.com/photo-1435348773030-a1d74f568bc2?ixlib=rb-1.2.1&ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&auto=format&fit=crop&w=1050&q=80);">
<div class="online"></div>
</div>
<div class="desc-contact">
<p class="name">Paul Walker</p>
<p class="message">You can't see me</p>
</div>
<div class="timer">1 week</div>
</div>
</section>
<section class="chat">
<div class="header-chat">
<i class="icon fa fa-user-o" aria-hidden="true"></i>
<p class="name">{{$user->name}}
<span id="status">Offline</span>
{{-- @if ($user->is_online == 1)
<span id="status">Online</span>
@else
<span id="status">Offline</span>
@endif --}}
</p>
<i class="icon clickable fa fa-ellipsis-h right" aria-hidden="true"></i>
</div>
<div class="messages-chat" id="container-message">
@foreach ($messages as $message)
@if ($message->from->id == auth()->user()->id)
<?php
$seen = 'fa-sharp fa-solid fa-check-double unreaded';
?>
@if ($message->is_readed == 1)
<?php
$seen = 'fa-sharp fa-solid fa-check-double readed';
?>
@endif
<div class="message text-only">
<div class="response">
<p class="text">{{$message->message}}</p>
</div>
</div>
<p class="time my_time">{{$message->created_at->format('h:i:s A')}} <i class="{{$seen}}"></i></p>
@else
<div class="message">
<div class="photo" style="background-image: url(https://images.unsplash.com/photo-1438761681033-6461ffad8d80?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=1050&q=80);">
<div class="online"></div>
</div>
<p class="text">{{$message->message}} </p>
</div>
<p class="time">{{$message->created_at->format('h:i:s A')}}</p>
@endif
@endforeach
</div>
<p id="typing"></p>
<div class="footer-chat">
<i class="icon fa-regular fa-face-smile clickable" style="font-size:25pt;"></i>
<form class="w-100" id="form">
<input type="text" class="write-message" id="input-message" placeholder="Type your message here">
<input type="text" id="touserId" value="{{$user->id}}" hidden>
<input type="text" hidden id="room-id" value="{{$room_id}}">
<input type="submit" hidden>
</form>
<i class="icon send fa-solid fa-paper-plane clickable" id="send-btn"></i>
</div>
</section>
</div>
</div>
@endsection
@section('script')
@vite(['resources/js/app.js'])
<script type="module">
let token = $('meta[name="csrf-token"]').attr('content');
let form = document.getElementById('form');
let inputMessage = document.getElementById('input-message');
let container = document.getElementById('container-message');
let roomid = document.getElementById('room-id');
const roomId = roomid.value;
let touserId =document.getElementById('touserId');
const toUserId = touserId.value;
let send = document.getElementById('send-btn');
let typing = document.getElementById('typing');
let status = document.getElementById('status');
let read_message = document.querySelectorAll('.fa-check-double.unreaded');
let usersOnline = [];
const channel = Echo.join(`message.${roomId}`);
function add_status_code()
{
let status_code = 0
usersOnline.forEach(user => {
if(user.id == toUserId){
status_code = 1
}
});
return status_code
}
function check_message_status(message)
{
let status_t = 'unreaded'
if(message.status == 1){
status_t = 'readed'
}
return status_t
}
function create_message(message)
{
let status_t = check_message_status(message)
console.log(status_t)
var today = new Date();
var time = today.toLocaleTimeString();
if(message.id == '{{auth()->user()->id}}'){
container.innerHTML +=`
<div class="message text-only">
<div class="response">
<p class="text">${message.message}</p>
</div>
</div>
<p class="time my_time">${time} <i class="fa-sharp fa-solid fa-check-double ${status_t}"></i></p>
`
}else{
var today = new Date();
var time = today.toLocaleTimeString();
container.innerHTML +=`
<div class="message">
<div class="photo" style="background-image: url(https://images.unsplash.com/photo-1438761681033-6461ffad8d80?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8&ixlib=rb-1.2.1&auto=format&fit=crop&w=1050&q=80);">
<div class="online"></div>
</div>
<p class="text"> ${message.message} </p>
</div>
<p class="time">${time}</p>
`;
}
}
function read_all_message()
{
usersOnline.forEach(user => {
if(user.id == toUserId){
let readedd = document.querySelectorAll('.fa-check-double.unreaded');
readedd.forEach(el=>{
el.className = 'fa-sharp fa-solid fa-check-double readed';
})
$.ajax({
method: "POST",
url: "/read_all",
data: {
toId: toUserId,
roomId: roomId,
_token: token
},
});
}
});
}
inputMessage.addEventListener('input',function(event){
if(inputMessage.value.length == 0){
channel.whisper('stop-typing');
}else{
channel.whisper('typing',{
name: "{{$user->name}}"
})
}
})
form.addEventListener('submit',function(event){
const userInput = inputMessage.value;
event.preventDefault();
let status_code = add_status_code()
$.ajax({
method: "POST",
url: "/send",
data: {
message: userInput,
roomid:roomId,
touserId:toUserId,
status:status_code,
_token: token
},
});
channel.whisper('stop-typing');
inputMessage.value="";
})
channel.here((users)=>{
usersOnline = [...users]
console.log({usersOnline},'Here')
usersOnline.forEach(user => {
if(user.id == toUserId){
status.innerHTML = "Online";
}
});
})
.joining((user) => {
usersOnline.push(user);
console.log({usersOnline},'Join')
usersOnline.forEach(user => {
if(user.id == toUserId){
status.innerHTML = "Online";
}
});
})
.leaving((user) => {
usersOnline = usersOnline.filter((usersOnline)=> usersOnline.id !== user.id);
usersOnline.forEach(user => {
if (user.id != toUserId){
status.innerHTML = "Offline";
}
});
})
.listen('.chat-message',(event)=>{
create_message(event)
})
.listenForWhisper('typing',(event)=>{
typing.innerHTML = "typing..";
read_all_message()
})
.listenForWhisper('stop-typing',(event)=>{
typing.innerHTML = "";
})
</script>
@endsection