-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
495a1c7
commit c281135
Showing
2 changed files
with
50 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
const chatModel = require('../models/chats.js') | ||
|
||
const streamByUser = {} | ||
|
||
module.exports = { | ||
connection, | ||
addUser, | ||
addMessage | ||
} | ||
|
||
function connection(stream) { | ||
return function (username) { | ||
chatModel | ||
.getUser(username) | ||
.catch(function () { | ||
return chatModel.addUser(username) | ||
}) | ||
.then(function () { | ||
streamByUser[username] = stream | ||
stream.write('connected!' + username + '\n') | ||
}) | ||
} | ||
} | ||
|
||
function addUser() { | ||
return function (username, chat_id) { | ||
chatModel.addUserIntoChat(chat_id, username) | ||
} | ||
} | ||
|
||
function addMessage() { | ||
return function (username, chat_id, message) { | ||
chatModel.getAllUsersIntoChat(chat_id) | ||
.then(function (users) { | ||
users | ||
.map(user => ({ | ||
name: user, | ||
stream: streamByUser[user] | ||
})) | ||
.filter(user => user.stream) | ||
.forEach(user => { | ||
user.stream.write('chat_id!' + chat_id + '!<' + username + '> ' + message + '\n') | ||
}) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters