-
Notifications
You must be signed in to change notification settings - Fork 0
/
chat.js
141 lines (108 loc) · 2.66 KB
/
chat.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
var _ = require("lodash");
var EventEmitter = require("node-event-emitter").EventEmitter;
module.exports = exports = new EventEmitter;
var EVENT_HANDLERS = {
join: join,
leave: leave,
chat: chat
}
var MAX_USERS = 16;
exports.setMaxListeners(16);
var eventId = 1;
var chat = {
users: [
{
id: 0,
name: 'System',
inChat: true
}
],
chat: []
}
var SYSTEM_USER_ID = 0;
exports.join = join;
exports.leave = leave;
exports.newMessage = newMessage;
exports.chat = chat;
exports.activeUsers = getActiveUsers;
exports.nameExists = checkNameExists;
exports.getUser = function(id) {
return chat.users.get(id);
}
function getActiveUsers() {
var allUsers = chat.users;
return allUsers.filter(function (o) { return o.inChat === true && o.id != 0; });
}
function checkNameExists(name) {
var activeUsers = getActiveUsers();
var usersWithName = activeUsers.filter(function (o) { return o.name === name; });
return (usersWithName.length > 0);
}
function handleEvent(event) {
var handler = EVENT_HANDLERS[event.name];
if(!handler) {
throw new Error("missing handler " + event.name);
}
return handler(event);
}
function join(data, callback) {
var numUsers = chat.users.length;
if (numUsers <= MAX_USERS) {
var user = {
id: _.uniqueId(),
name: data.name,
inChat: true
}
chat.users[user.id] = user;
var message = addMessage('User ' + data.name + ' joined the chat');
callback(null, user);
emit(event("join", {user: user}));
} else {
callback(new Error('Chat room full'), null);
}
}
function leave(data, callback) {
if (chat.users[data.userid]) {
var user = chat.users[data.userid];
chat.users[data.userid].inChat = false;
var message = addMessage('User ' + user.name + ' left the chat');
callback(null, user);
emit(event("leave", {user: user}));
} else {
callback(new Error('User does not exist'), false);
}
}
function newMessage(data, callback) {
var message = addMessage(data.message, chat.users[data.userid].name);
callback(null, message);
}
function state() {
return {
chat: {
users: chat.users.members,
chat: chat.chat
},
eventId: eventId,
}
}
function event(name, data) {
return _.extend(data, {id: eventId++, name:name});
}
function emit(event) {
exports.emit("chatEvent", event);
}
function addMessage(msg, username) {
username = username || chat.users[SYSTEM_USER_ID].name;
var message = {
id: _.uniqueId("chatMessage"),
text: msg,
username: username,
timestamp: new Date()
};
if (chat.chat.push(message)) {
emit(event("chat", {message: message}));
return message;
} else {
return false;
}
}