Skip to content

Commit

Permalink
Some refactoring for integration with frontend
Browse files Browse the repository at this point in the history
  • Loading branch information
lucashsilva committed May 9, 2017
1 parent 6460f43 commit 2073a4a
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 8,263 deletions.
56 changes: 54 additions & 2 deletions app/chat/chat_socket.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
var User = require('../models/user');
var ChatRecord = require('../models/chat_record');
var config = require('../../config/main');
var jwt = require('jsonwebtoken');
var jwtAuth = require('socketio-jwt-auth');
Expand All @@ -24,11 +25,62 @@ module.exports = function(server) {
return done(null, user);
});
}));

io.on('connection', function(socket) {
ChatRecord.remove({}, function() {

})
let user = socket.request.user;

console.log('user ' + user._id + ' connected to the chat');
socket.on('subscribe', function(heroId) {
socket.join(heroId);
socket.room = heroId;

let notification = new ChatRecord({
heroId: heroId,
type: 'notification',
from: user._id,
message: 'joined the chat room'
});

socket.to(heroId).emit('new message', notification);
notification.save();
});

socket.on('get user info', function(userId, fn) {
User.findOne({_id: userId}, function(err, user){
if(user) {
fn({_id: userId, name: user.name});
}
});
})

socket.on('retrieve messages', function(heroId, fn) {
ChatRecord.find({heroId: heroId}, function(err, records){
fn(records);
console.log(records);
});
});

socket.on('new message', function(msg, fn){
let message = new ChatRecord(msg);
socket.to(message.heroId).emit('new message', message);
message.save();
fn(message);
});

socket.on('disconnect', function() {
let notification = new ChatRecord({
heroId: socket.room,
type: 'notification',
from: user._id,
message: 'left the chat room'
});

socket.to(socket.room).emit('new message', notification);
notification.save();
});

});
};

27 changes: 27 additions & 0 deletions app/models/chat_record.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
const mongoose = require('mongoose');

// Schema defines how the user's data will be stored in MongoDB
const ChatRecordSchema = new mongoose.Schema({
heroId: {
type: String,
required: true
// chat room id = hero id
},
type: {
type: String,
enum: ['message', 'notification'],
default: 'message'
},
from: {
type: String,
required: true
},
message: {
type: String,
required: true
}
}, {
timestamps: true
});

module.exports = mongoose.model('ChatRecord', ChatRecordSchema);
2 changes: 1 addition & 1 deletion app/routes/user_routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ module.exports = function(app) {
return res.status(400).json({ success: false, message: err.message });
}

return res.status(201).json({ success: true, message: 'Successfully created new user.', userInfo: generateUserInfo(newUser) });
return res.status(201).json({ success: true, message: 'Successfully created new user.' });
});
});

Expand Down
1 change: 0 additions & 1 deletion server.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ app.use(cors());

// Log requests to console
app.use(morgan('dev'));
app.use('/', express.static(__dirname + '/www'));

// Connect to database
mongoose.connect(config.database);
Expand Down
58 changes: 0 additions & 58 deletions www/index.html

This file was deleted.

Loading

0 comments on commit 2073a4a

Please sign in to comment.