1- $ ( function ( ) {
1+ $ ( function ( ) {
22
33 var enterRoom = $ ( "[enter-room]" )
44
55 var matrixProfile = new MatrixProfile ( )
66
77 if ( matrixProfile . isProfileStored ( ) ) {
88 enterInOffice ( matrixProfile ) ;
9- } else {
9+ } else {
1010 redirectToHome ( ) ;
1111 }
1212
13- function removeUser ( userId ) {
14- $ ( '#' + userId ) . remove ( ) ;
13+ function removeUser ( data , userId ) {
14+ notify ( data , `${ data . user . name } saiu da sala` )
15+ $ ( '#' + userId ) . remove ( ) ;
1516 }
1617
17- function showUserInRoom ( user , room ) {
18-
19- var userView = $ ( '#' + user . id ) . length ;
20- if ( userView == 0 ) {
21- userView = $ ( '<img width="50px" id="' + user . id + '"src="' + user . imageUrl + '">' ) ;
22- } else {
23- userView = $ ( '#' + user . id ) . detach ( ) ;
24- }
18+ function showUserInRoom ( user , room ) {
2519
26- $ ( "#" + room ) . append ( userView ) ;
20+ var userView = $ ( '#' + user . id ) . length ;
21+ if ( userView == 0 ) {
22+ userView = $ ( '<img width="50px" id="' + user . id + '"src="' + user . imageUrl + '">' ) ;
23+ } else {
24+ userView = $ ( '#' + user . id ) . detach ( ) ;
25+ }
26+
27+ $ ( "#" + room ) . append ( userView ) ;
2728 }
2829
29- function redirectToHome ( ) {
30+ function redirectToHome ( ) {
3031 window . location . href = "./"
3132 }
3233
33- function goToMeet ( externalMeetUrl ) {
34+ function goToMeet ( externalMeetUrl ) {
3435 var r = confirm ( "Deseja entrar na call?" ) ;
3536 if ( r == true ) {
36- window . open ( externalMeetUrl , '_blank' ) ;
37+ window . open ( externalMeetUrl , '_blank' ) ;
3738 } else {
38- txt = "You pressed Cancel!" ;
39+ txt = "You pressed Cancel!" ;
3940 }
4041 }
4142
4243 function saveLastRoom ( data ) {
4344 localStorage . setItem ( 'last_room' + data . user . id , data . room ) ;
4445 }
4546
46- function enterInOffice ( matrixProfile ) {
47+ function notify ( data , title ) {
48+ var options = {
49+ icon : data . user . imageUrl ,
50+ }
51+
52+ if ( Notification . permission !== "granted" ) {
53+ Notification . requestPermission ( ) ;
54+ } else {
55+ var n = new Notification ( title , options ) ;
56+ }
57+ }
58+
59+ function enterInOffice ( matrixProfile ) {
4760 var lastRoom = localStorage . getItem ( 'last_room' + matrixProfile . loadStoredProfile ( ) . id ) ;
48- console . log ( window . location )
49- //make connection
50- var socket = io . connect ( window . location . protocol + "//" + window . location . host , {
61+
62+ //make connection
63+ var socket = io . connect ( window . location . protocol + "//" + window . location . host , {
5164 query : "user=" + matrixProfile . loadStoredProfileAsString ( ) + ( lastRoom ? "&room=" + lastRoom : "" )
5265 } )
5366
54- enterRoom . on ( "click" , function ( e ) {
67+ enterRoom . on ( "click" , function ( e ) {
5568 var room = $ ( e . target ) . parent ( ) . attr ( "id" ) ;
56- socket . emit ( 'enter-room' , { room : room , user :matrixProfile . loadStoredProfile ( ) } )
69+ socket . emit ( 'enter-room' , { room : room , user : matrixProfile . loadStoredProfile ( ) } )
5770 setTimeout ( function ( ) {
5871 goToMeet ( $ ( e . target ) . attr ( "external-meet-url" ) ) ;
59- } , 300 ) ;
60-
72+ } , 300 ) ;
73+
6174 } )
6275
6376 socket . on ( "enter-room" , ( data ) => {
64- saveLastRoom ( data ) ;
65- showUserInRoom ( data . user , data . room ) ;
77+ var loggedUserId = JSON . parse ( localStorage . getItem ( 'user' ) ) . id ;
78+ var loggedUserRoomId = localStorage . getItem ( 'last_room' + data . user . id ) ;
79+
80+ if ( loggedUserRoomId == data . room && loggedUserId != data . user . id ) {
81+ notify ( data , `${ data . user . name } entrou na sala` )
82+ }
83+
84+ saveLastRoom ( data )
85+ showUserInRoom ( data . user , data . room )
6686 } )
6787
68- socket . on ( "disconnect" , ( userId ) => {
69- removeUser ( userId ) ;
88+ socket . on ( "disconnect" , ( data ) => {
89+ removeUser ( data , userId ) ;
7090 } )
7191 }
7292
73- } ) ;
93+ } ) ;
0 commit comments