-
Notifications
You must be signed in to change notification settings - Fork 0
/
chat-scripts.js
684 lines (554 loc) · 23.4 KB
/
chat-scripts.js
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
/******************************************************************
GENERAL SCRIPTS
*******************************************************************/
$(".show-login").click(function() {
$(".user-signup").hide();
$(".user-login").show();
})
$(".show-signup").click(function() {
$(".user-login").hide();
$(".user-signup").show();
})
/******************************************************************
MESSAGING SCRIPTS
*******************************************************************/
// Initialize Firebase
var config = {
apiKey: "AIzaSyDmkUcUO2a0iygoef_-G_se6zr9ytHON-o",
authDomain: "my-work-book.firebaseapp.com",
databaseURL: "https://my-work-book.firebaseio.com",
projectId: "my-work-book",
storageBucket: "my-work-book.appspot.com",
messagingSenderId: "661474412916"
};
firebase.initializeApp(config);
//********************** USER AUTHENTICATION **********************
var _firbaseDB = firebase.database(),
_storageDB = firebase.storage(),
_storageRootRef = _storageDB.ref(),
_firebaseRootRef = _firbaseDB.ref(),
_firebaseAuthRef = _firbaseDB.ref("Auth/"),
_firebaseChatRef = _firbaseDB.ref("Chats/"),
_formData,
_event,
_CURRENT_USER,
_REF_USER,
_MESSAGES_LOCATION,
_TOTAL_MESSAGES = [],
_MESSAGES_KEYS = [],
_LAST_MESSAGES = [],
_LAST_MESSAGES_KEYS = [],
_LAST_TIME = [],
_CHATTED_WITH = [];
$("body").on("submit", ".auth-form", function(ev) {
ev.preventDefault();
var _event = ev.target;
if ($(this).hasClass("auth-login")) {
_formData = $(_event).serializeArray();
loginAuth(_formData[0].value, _formData[1].value);
}
if ($(this).hasClass("auth-signup")) {
_formData = $(_event).serializeArray();
signupAuth(_formData[0].value, _formData[1].value, _formData[2].value);
}
})
function signupAuth(name, email, pass) {
firebase.auth().createUserWithEmailAndPassword(email, pass)
.then(function() {
_CURRENT_USER = firebase.auth().currentUser.uid;
$(".su-error").hide();
$(".su-error").text("");
$(".auth-signup").trigger("reset");
$(".user-signup").hide();
$(".su-welcome").show();
$.cookie("_cu", _CURRENT_USER);
setTimeout(function() {
window.location.href = "chat.html"
}, 2000);
writeUserData(_CURRENT_USER, name, email);
})
.catch(function(error) {
switch (error.code) {
case "auth/invalid-email":
{
$(".su-error").show();
$(".su-error").text("Your Email is invalid !");
break;
}
case "auth/email-already-in-use":
{
$(".su-error").show();
$(".su-error").text("Email already in use");
break;
}
}
});
function writeUserData(id, name) {
_firebaseAuthRef.child(id).set({
n: name,
e: email
});
}
}
function loginAuth(email, pass) {
firebase.auth().signInWithEmailAndPassword(email, pass)
.then(function(res) {
_CURRENT_USER = firebase.auth().currentUser.uid;
if (_CURRENT_USER) {
$(".lg-error").hide();
$(".lg-error").text("");
$(".auth-login").trigger("reset");
$(".user-login").hide();
$(".lg-welcome").show();
$.cookie("_cu", _CURRENT_USER);
setTimeout(function() {
window.location.href = "chat.html"
}, 2000);
}
})
.catch(function(error) {
var errorCode = error.code;
var errorMessage = error.message;
$(".lg-error").show();
$(".lg-error").text(errorCode);
});
}
$(".logout").on("click", function(ev) {
firebase.auth().signOut().then(function() {
window.location.href = "auth.html";
$.removeCookie("_cu");
$.removeCookie("_to");
$.removeCookie("_lo");
}).catch(function(error) {
});
})
//-------------------------------- LOAD THE CHAT LIST OF THE CURRENT USER
firebase.auth().onAuthStateChanged(function(user) {
if (user && window.location.pathname == "/Fiverr/Firebase%20Chat%20App/chat.html") {
_CURRENT_USER = $.cookie("_cu");
let p = new Promise(function(res, rej) {
_firebaseAuthRef.child(_CURRENT_USER + "/c").once("value").then(function(r) {
let obj = r.val();
for (x in obj) {
_CHATTED_WITH.push(x);
res(_CHATTED_WITH);
}
})
})
p.then(function(res) { getChatListMeta(res); });
}
else {
// No user is signed in.
}
});
function getChatListMeta(cw) {
let len = cw.length,
_el = document.getElementsByClassName("chat-ind-body-wrap")[0];
let pr = new Promise(function(resolve, reject) {
_firebaseAuthRef.child(_CURRENT_USER + "/c").on("value", function(res) {
let obj = res.val();
for (x in obj) {
_firebaseAuthRef.child(_CURRENT_USER + "/c/" + x + "/l/").on("value", function(rr) {
let ob = rr.val();
for (j in ob) {
_LAST_MESSAGES_KEYS.push(j);
resolve(_LAST_MESSAGES_KEYS);
}
})
}
})
})
pr.then(function(result) {
for (var i = 0; i < len; i++) {
_firebaseChatRef.child(result[i]).limitToLast(1).orderByChild("mess_time").on("child_added", function(re) {
_LAST_MESSAGES.push(re.val().mess);
_LAST_TIME.push(re.val().mess_time);
})
}
populateUsers();
})
function populateUsers() {
for (let i = 0; i < len; i++) {
_firebaseAuthRef.child(cw[i]).on("value", function(res) {
if(_LAST_MESSAGES[i] && typeof _LAST_MESSAGES[i] === 'object'){
console.log("Fucked off");
if(_LAST_MESSAGES[i].type == 'image/jpeg'){
_el.insertAdjacentHTML('afterbegin', '<li class="chat-ind-user" data-id="' + cw[i] + '">' +
'<div class="chat-ind-user-img">' +
'<img src="assets/1.jpg">' +
'</div>' +
'<div class="chat-ind-user-data">' +
'<div class="ciu-name"> ' + res.val().n + ' </div>' +
'<div class="ciu-text"> <i class="fa fa-camera"></i> Image </div>' +
'<span class="ciu-time">' + _LAST_TIME[i] + '</span>' +
'</div>' +
'<span class="user-availble">' +
'<i class="fa fa-circle"></i>' +
'</span>' +
'</li>');
} else{
_el.insertAdjacentHTML('afterbegin', '<li class="chat-ind-user" data-id="' + cw[i] + '">' +
'<div class="chat-ind-user-img">' +
'<img src="assets/1.jpg">' +
'</div>' +
'<div class="chat-ind-user-data">' +
'<div class="ciu-name"> ' + res.val().n + ' </div>' +
'<div class="ciu-text"> <i class="fa fa-paperclip"></i> File </div>' +
'<span class="ciu-time">' + _LAST_TIME[i] + '</span>' +
'</div>' +
'<span class="user-availble">' +
'<i class="fa fa-circle"></i>' +
'</span>' +
'</li>');
}
}
else{
console.log("Fucked off");
_el.insertAdjacentHTML('afterbegin', '<li class="chat-ind-user" data-id="' + cw[i] + '">' +
'<div class="chat-ind-user-img">' +
'<img src="assets/1.jpg">' +
'</div>' +
'<div class="chat-ind-user-data">' +
'<div class="ciu-name"> ' + res.val().n + ' </div>' +
'<div class="ciu-text">' + _LAST_MESSAGES[i] + '</div>' +
'<span class="ciu-time">' + _LAST_TIME[i] + '</span>' +
'</div>' +
'<span class="user-availble">' +
'<i class="fa fa-circle"></i>' +
'</span>' +
'</li>');
}
});
}
}
}
$(document).on("click", ".chat-ind-user", function() {
$.cookie("_to", $(this).attr("data-id"));
_REF_USER = $.cookie("_to");
if ($(this).siblings().hasClass("active-user")) {
$(this).siblings().removeClass("active-user");
$(this).addClass("active-user");
$(".messages-with-name span").text($(this).find(".ciu-name").text());
} else if (!$(this).siblings().hasClass("active-user")) {
$(this).addClass("active-user");
$(".messages-with-name span").text($(this).find(".ciu-name").text());
}
let p = new Promise(function(res, rej) {
_firebaseAuthRef.child(_CURRENT_USER + "/c/" + _REF_USER + "/l").once("value").then(function(r) {
let obj = r.val();
for (x in obj) {
res(x);
}
})
})
p.then(function(res) {
$.cookie("_lo", res);
_MESSAGES_LOCATION = $.cookie("_lo");
_TOTAL_MESSAGES = [];
_firebaseChatRef.child(_MESSAGES_LOCATION).orderByChild("mess_time").on("value", function(rr) {
let obj = rr.val();
for(x in obj){
_TOTAL_MESSAGES.push(obj[x]);
}
populateUserMessages(_TOTAL_MESSAGES);
})
})
});
//-------------------------------- FILES SHARING WITH USERS
var _uploadBtn = document.getElementById("share-file"),
_el = document.getElementsByClassName("files-list")[0],
URLS = [], FILE_TYPE=[];
//Listen for file selection
_uploadBtn.addEventListener('change', function(e){
//Get files
for (var i = 0; i < e.target.files.length; i++) {
var eFile = e.target.files[i];
$(".files-list").show();
_el.insertAdjacentHTML('beforeend','<li class="file-selected row">'+
'<div class="col-3">'+eFile.name+'</div>'+
'<div class="col-3">'+formatBytes(eFile.size,2)+'</div>'+
'<div class="col-3 progress"><div class="progress-fill"></div></div>'+
'<div class="col-3 status"> Uploading...</div>'+
'</li>');
uploadImageAsPromise(eFile,i);
}
function formatBytes(a,b){
if(0==a) return"0 Bytes";
var c=1024,
d=b||2,
e=["Bytes","KB","MB","GB","TB","PB","EB","ZB","YB"],
f=Math.floor(Math.log(a)/Math.log(c));
return parseFloat((a/Math.pow(c,f)).toFixed(d))+" "+e[f];
}
$(".close-files-list .fa").click(function(){
while (_el.firstChild) {
_el.removeChild(_el.firstChild);
}
$(".files-list").hide();
URLS = [];
})
});
//Handle waiting to upload each file using promise
function uploadImageAsPromise (eFile,i) {
return new Promise(function (resolve, reject) {
function makeid() {return Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15);}
var storageRef = firebase.storage().ref($.cookie("_lo")+"/"+ (eFile.name+makeid()) );
//Upload file
var task = storageRef.put(eFile);
//Update progress bar
task.on('state_changed',
function progress(snapshot){
var percentage = snapshot.bytesTransferred / snapshot.totalBytes * 100;
$(".progress .progress-fill").eq(i).css({"width":percentage+"%"});
},
function error(err){
},
function complete(){
var downloadURL = task.snapshot.downloadURL,
metaData = task.snapshot.metadata.contentType;
console.log(metaData);
$(".status").eq(i).text("Completed");
URLS.push(downloadURL);
FILE_TYPE.push(metaData);
}
);
});
}
$("body").on("submit", "#type-text-form", function(e) {
e.preventDefault();
_CURRENT_USER = $.cookie("_cu"),
_REF_USER = $.cookie("_to"),
_MESSAGES_LOCATION = $.cookie("_lo");
let ev = e.target,
tt = $(ev).serializeArray(),
timestamp = Math.floor(Date.now() / 1000),
_TOTAL_MESSAGES = [];
if(URLS.length>0){
_firebaseChatRef.child(_MESSAGES_LOCATION).push().set({
mess_by: _CURRENT_USER,
mess_to: _REF_USER,
mess: {file:URLS,type:FILE_TYPE},
mess_time: timestamp
}).then(function() {
$("#type-text-form").trigger("reset");
populateUserMessages();
});
}
else{
_firebaseChatRef.child(_MESSAGES_LOCATION).push().set({
mess_by: _CURRENT_USER,
mess_to: _REF_USER,
mess: tt[0].value,
mess_time: timestamp
}).then(function() {
$("#type-text-form").trigger("reset");
populateUserMessages();
});
}
})
function populateUserMessages() {
let _el = document.getElementsByClassName("messages-body")[0];
_TOTAL_MESSAGES = [];
let p = new Promise(function(resolve,reject){
_firebaseChatRef.child(_MESSAGES_LOCATION).orderByChild("mess_time").on("child_added",function(resa){
_TOTAL_MESSAGES.push(resa.val());
resolve(_TOTAL_MESSAGES);
});
})
p.then(function(res){
updateUI(res);
URLS = [];
})
function updateUI(r){
while (_el.firstChild) {
_el.removeChild(_el.firstChild);
}
for(var i = 0;i<r.length;i++){
if (r[i].mess_to == _CURRENT_USER) {
if(r[i].mess.file && typeof r[i].mess.file === 'object' && r[i].mess.file.constructor === Array){
if(r[i].mess.type=="image/jpeg"){
for(var j = 0; j<r[i].mess.file.length;j++){
_el.insertAdjacentHTML("beforeend", '<div class="message message-from">' +
'<div class=" mess-img ">' +
'<div class="mess-img-wrap">' +
'<img src="assets/1.jpg" alt="img" >' +
'</div>' +
'</div>' +
'<div class=" mess-body">' +
' <div class="mess-txt">'+
'<img src="' + r[i].mess.file[j] + '">'+
'<a href="' + r[i].mess.file[j] + '" class="download-from-mess dfm-img dfm-frm" download> <i class="fa fa-cloud-download"></i> Download Image</a>' +
'</div>' +
'</div>' +
'</div>' +
'<div class="mess-time">' +
' ' + r[i].mess_time + '' +
' </div>' +
'</div>');
}
} else{
for(var j = 0; j<r[i].mess.file.length;j++){
_el.insertAdjacentHTML("beforeend", '<div class="message message-from">' +
'<div class=" mess-img ">' +
'<div class="mess-img-wrap">' +
'<img src="assets/1.jpg" alt="img" >' +
'</div>' +
'</div>' +
'<div class=" mess-body">' +
' <div class="mess-txt">'+
'<a href="' + r[i].mess.file[j] + '" class="download-from-mess" download> <i class="fa fa-cloud-download"></i> Download File</a>' +
'</div>' +
'</div>' +
'<div class="mess-time">' +
' ' + r[i].mess_time + '' +
' </div>' +
'</div>');
}
}
}
else{
_el.insertAdjacentHTML("beforeend", '<div class="message message-from">' +
'<div class=" mess-img ">' +
'<div class="mess-img-wrap">' +
'<img src="assets/1.jpg" alt="img" >' +
'</div>' +
'</div>' +
'<div class=" mess-body">' +
'<div class="mess-txt" id="cus-mess">' + r[i].mess + '</div>' +
'</div>' +
'<div class="mess-time">' +
' ' + r[i].mess_time + '' +
' </div>' +
'</div>');
}
} else {
if(r[i].mess.file && typeof r[i].mess.file === 'object' && r[i].mess.file.constructor === Array){
if(r[i].mess.type=="image/jpeg"){
for(var j = 0; j<r[i].mess.file.length;j++){
_el.insertAdjacentHTML("beforeend", '<div class="message message-to">' +
' <div class="mess-time">' +
' ' + r[i].mess_time + '' +
' </div>' +
'<div class=" mess-body">' +
'<div class="mess-txt">'+
'<img src="' + r[i].mess.file[j] + '">'+
'<a href="' + r[i].mess.file[j] + '" class="download-from-mess dfm-img" download> <i class="fa fa-cloud-download"></i> Download Image</a>' +
'</div>' +
'</div>' +
' </div>' +
'</div>');
}
} else{
for(var j = 0; j<r[i].mess.file.length;j++){
_el.insertAdjacentHTML("beforeend", '<div class="message message-to">' +
'<div class="mess-time">' +
'' + r[i].mess_time + '' +
' </div>' +
'<div class=" mess-body">' +
'<div class="mess-txt">'+
'<a href="' + r[i].mess.file[j] + '" class="download-from-mess" download> <i class="fa fa-cloud-download"></i> Download File</a>' +
'</div>' +
'</div>' +
' </div>' +
'</div>');
}
}
}
else{
_el.insertAdjacentHTML("beforeend", '<div class="message message-to">' +
'<div class="mess-time">' +
'<div>' + r[i].mess_time + '</div>' +
' </div>' +
'<div class=" mess-body">' +
' <div class="mess-txt">' + r[i].mess + '</div>' +
' </div>' +
'</div>');
}
}
}
_el.scrollTo(0, document.body.scrollHeight);
}
}
$(".new-mess button").on("click", function() {
$(".chat-wrap-row .left-search").css({
"margin-left": "0px"
});
$(".close-search").on("click", function() {
$(".chat-wrap-row .left-search").css({
"margin-left": "-1000px"
});
})
})
$("body").on("submit", ".search-from", function(e) {
e.preventDefault();
_event = e.target;
let _searchFormData = $(_event).serializeArray(),
_user = _searchFormData[0].value,
_el = document.getElementsByClassName('searched-list')[0];
while (_el.firstChild) {
_el.removeChild(_el.firstChild);
}
_firebaseAuthRef.orderByChild("n").equalTo(_user).on("child_added", function(res) {
let arr = [],
key = [],
obj = res.val();
arr.push(res.val());
key.push(res.key);
for (let i = 0; i < arr.length; i++) {
_el.insertAdjacentHTML('afterbegin', '<li class="searched-item" data-id="' + key[i] + '">' +
'<span class="si-name"> ' + arr[i].n + ' </span>' +
'<span class="si-email">' + arr[i].e + ' </span>' +
'<span class="si-add"> <i class="fa fa-plus-circle"></i> </span>' +
'</li>');
}
})
})
$("body").on("click", ".si-add", function(e) {
let _REF_USER = $(this).parent().attr("data-id"),
_chattedWith = [],
_child_key = [];
$(".chat-wrap-row .left-search").css({
"margin-left": "-1000px"
});
_firebaseAuthRef.child(_CURRENT_USER).child("c").child(_REF_USER).child("l").push().set(true);
let p = new Promise(function(res, rej) {
_firebaseAuthRef.child(_REF_USER).once("value").then(function(r) {
res({
id: r.key,
name: r.val().n
});
})
})
p.then(function(res) {
populateUsers(res);
})
let p2 = new Promise(function(res, rej) {
_firebaseAuthRef.child(_CURRENT_USER + "/c/" + _REF_USER + "/l").once("value").then(function(r) {
let obj = r.val();
for (x in obj) {
res(x);
}
})
})
p2.then(function(res) {
$.cookie("_lo", res);
_firebaseAuthRef.child(_REF_USER).child("c").child(_CURRENT_USER).child("l").child(res).set(true);
})
function populateUsers(chattedWith) {
let len = chattedWith.length,
_el = document.getElementsByClassName("chat-ind-body-wrap")[0];
console.log(chattedWith);
_el.insertAdjacentHTML('afterbegin', '<li class="chat-ind-user" data-id="' + chattedWith.id + '">' +
'<div class="chat-ind-user-img">' +
'<img src="assets/1.jpg">' +
'</div>' +
'<div class="chat-ind-user-data">' +
'<div class="ciu-name"> ' + chattedWith.name + ' </div>' +
'<span class="ciu-time"> Now </span>' +
'</div>' +
'<span class="user-availble">' +
'<i class="fa fa-circle"></i>' +
'</span>' +
'</li>');
}
});