Skip to content

Commit d8dd46c

Browse files
author
Daniel Ribeiro
committed
First draft of notifications
1 parent 0825ecf commit d8dd46c

3 files changed

Lines changed: 82 additions & 64 deletions

File tree

controllers/office.controller.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,29 @@
11
var HashMap = require('hashmap');
22
// Constructor
33
function OfficeController() {
4-
this.usersInRoomOffice = new HashMap();
4+
this.usersInRoomOffice = new HashMap();
55
}
66

7-
OfficeController.prototype.addUserInRoom = function(user,room) {
8-
this.removeUser(user.id)
7+
OfficeController.prototype.addUserInRoom = function (user, room) {
8+
this.removeUser(user.id)
99
var userInRoom = {
10-
user:user,
11-
room:room
12-
};
10+
user: user,
11+
room: room
12+
};
1313
this.usersInRoomOffice.set(user.id,
14-
userInRoom
14+
userInRoom
1515
);
1616

1717
return userInRoom;
1818
};
1919

20-
OfficeController.prototype.removeUser = function(userId) {
20+
OfficeController.prototype.removeUser = function (userId) {
2121
this.usersInRoomOffice.delete(userId);
2222
};
2323

24-
OfficeController.prototype.getUsersInOffice = function() {
24+
OfficeController.prototype.getUsersInOffice = function () {
2525
return this.usersInRoomOffice;
2626
};
2727

2828

29-
module.exports = OfficeController;
29+
module.exports = OfficeController;

office.server.js

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,52 @@
1-
var OfficeController = require('./controllers/office.controller');
1+
var OfficeController = require('./controllers/office.controller');
22

33
// Constructor
44
function Office(server) {
55
this.officeController = new OfficeController();
6-
6+
77
const io = require("socket.io")(server)
8-
9-
io.use(function(socket, next){
108

9+
io.use(function (socket, next) {
1110
socket.user = JSON.parse(socket.handshake.query.user)
12-
13-
return next();
11+
12+
return next();
1413
});
1514

1615
io.on('connection', (socket) => {
1716
console.log('New user connected')
1817
var that = this;
19-
var currentUser = socket.user;
18+
var currentUser = socket.user;
19+
2020

21-
2221
var room_param = socket.handshake.query.room;
2322
var room = room_param ? room_param : "room-1";
2423
addUserInRoom(socket.user, room);
2524

26-
that.officeController.getUsersInOffice().forEach(function(value, key) {
27-
addUserInRoom(value.user,value.room);
25+
that.officeController.getUsersInOffice().forEach(function (value, key) {
26+
addUserInRoom(value.user, value.room);
2827
});
2928

3029

31-
socket.on('disconnect', function(socket) {
32-
console.log("disconnect");
33-
io.sockets.emit('disconnect', currentUser.id);
34-
that.officeController.removeUser(currentUser.id)
30+
socket.on('disconnect', function (socket) {
31+
io.sockets.emit('disconnect', currentUser.id);
32+
that.officeController.removeUser(currentUser.id)
3533

3634
});
3735

3836
socket.on('enter-room', (data) => {
39-
addUserInRoom(currentUser,data.room);
40-
})
37+
addUserInRoom(currentUser, data.room);
38+
})
4139

42-
function addUserInRoom(user,room){
43-
//console.log(user,room)
44-
var userInRoom = that.officeController.addUserInRoom(user,room)
45-
46-
io.sockets.emit('enter-room', userInRoom);
47-
}
40+
function addUserInRoom(user, room) {
41+
//console.log(user,room)
42+
var userInRoom = that.officeController.addUserInRoom(user, room)
43+
44+
io.sockets.emit('enter-room', userInRoom);
45+
}
4846

4947
});
5048

51-
49+
5250
}
5351

54-
module.exports = Office;
52+
module.exports = Office;

public/office.web.js

Lines changed: 50 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,93 @@
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

Comments
 (0)