Skip to content

Commit

Permalink
Basic chat on computer done, with user system and README showing used…
Browse files Browse the repository at this point in the history
… libraries.
  • Loading branch information
eyeballcode committed Nov 20, 2015
1 parent 626f6c6 commit 9cff2b2
Show file tree
Hide file tree
Showing 26 changed files with 1,044 additions and 87 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@ This few things were used in this project:

1. Markdown-HTML by @bjb568. [Link here](https://github.com/bjb568/Markdown-HTML)

2. Password Strength by @bjb568 as well. [Link here](https://devdoodle.net/dev/31)
2. Password Strength by @bjb568 as well. [Link here](https://devdoodle.net/dev/31)

3. Node-login by @braitsch. [Link here](https://github.com/braitsch/node-login)
19 changes: 12 additions & 7 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,6 @@ var MongoStore = require('connect-mongo')(session);

var app = express();

var usedDBCs = [
'instagramUserID',
'instagramMediaID',
'instagramPostDetails',
'users'
];

global.dbcs = {};

function noop() {
Expand All @@ -49,6 +42,18 @@ mongo.connect('mongodb://localhost:27017/TFHWebSite', {}, function (err, db) {
if (err) throw err;
dbcs.users = collection;
});
db.createCollection('chatRooms', function (err, collection) {
if (err) throw err;
dbcs.chatRooms = collection;
});
db.createCollection('chatUsers', function (err, collection) {
if (err) throw err;
dbcs.chatUsers = collection;
});
db.createCollection('chatMessages', function (err, collection) {
if (err) throw err;
dbcs.chatMessages = collection;
});
});

app.use(function (req, res, next) {
Expand Down
12 changes: 6 additions & 6 deletions bin/www
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,18 @@ var port = normalizePort(process.env.PORT || '3000');
app.set('port', port);

/**
* Create HTTP server.
* Create HTTP global.server.
*/

var server = http.createServer(app);
global.server = http.createServer(app);

/**
* Listen on provided port, on all network interfaces.
*/

server.listen(port);
server.on('error', onError);
server.on('listening', onListening);
global.server.listen(port);
global.server.on('error', onError);
global.server.on('listening', onListening);

/**
* Normalize a port into a number, string, or false.
Expand Down Expand Up @@ -82,7 +82,7 @@ function onError(error) {
*/

function onListening() {
var addr = server.address();
var addr = global.server.address();
var bind = typeof addr === 'string'
? 'pipe ' + addr
: 'port ' + addr.port;
Expand Down
18 changes: 17 additions & 1 deletion modules/account-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ var dbName = 'TFHWebSite';
/* establish the database connection */

var db,
users;
users,
chatUsers;

mongo.connect('mongodb://localhost:27017/TFHWebSite', {}, function (err, db1) {
db = db1;

users = db1.collection('users');
chatUsers = db1.collection('chatUsers');
});


Expand Down Expand Up @@ -61,7 +63,14 @@ exports.addNewAccount = function (newData, callback) {
newData.pass = hash;
// append date stamp when record was created //
newData.date = moment().format('MMMM Do YYYY, h:mm:ss a');
newData.emailHash = require('md5')(newData.email);
users.insert(newData, {safe: true}, callback);
chatUsers.insert({
name: newData.name,
email: newData.email,
emailHash: newData.emailHash,
key: require('md5')(newData.email + newData.date + generateSalt())
}, {safe: true});
});
}
});
Expand All @@ -74,6 +83,7 @@ exports.updateAccount = function (newData, callback) {
o.name = newData.name;
o.email = newData.email;
o.country = newData.country;
newData.emailHash = require('md5')(newData.email);
if (newData.pass == '') {
users.save(o, {safe: true}, function (err) {
if (err) callback(err);
Expand Down Expand Up @@ -116,6 +126,12 @@ exports.getAccountByEmail = function (email, callback) {
});
};

exports.getAccountByEmailHash = function (hash, callback) {
users.findOne({emailHash: hash}, function (e, o) {
callback(o);
});
};

exports.validateResetLink = function (email, passHash, callback) {
users.find({$and: [{email: email, pass: passHash}]}, function (e, o) {
callback(o ? 'ok' : null);
Expand Down
22 changes: 12 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,23 @@
},
"dependencies": {
"body-parser": "~1.13.2",
"compression": "latest",
"connect-mongo": "0.8.2",
"cookie-parser": "~1.3.5",
"debug": "~2.2.0",
"emailjs": "1.0.0",
"errorhandler": "1.4.2",
"express": "~4.13.1",
"express-session": "1.11.3",
"jade": "~1.11.0",
"jsdom": "latest",
"md5": "^2.0.0",
"moment": "2.10.3",
"mongodb": "^2.0.47",
"morgan": "~1.6.1",
"request": "latest",
"serve-favicon": "~2.3.0",
"stylus": "0.42.3",
"jsdom": "latest",
"request": "latest",
"compression": "latest",
"mongodb": "^2.0.47",
"express-session": "1.11.3",
"moment" : "2.10.3",
"emailjs": "1.0.0",
"connect-mongo": "0.8.2",
"errorhandler": "1.4.2"
"ws": "latest"
}
}
}
Binary file added public/images/anon.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
25 changes: 25 additions & 0 deletions public/javascript/chat-loggedin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
function key() {
return $('#key').val();
}

$(function () {
var roomID = parseInt(location.pathname.match(/\d+/)[0]);
$('#send').click(function () {
if ($('#messageInput').val()) {
$.ajax({
type: 'POST',
url: '/chat/rooms/' + roomID + '/messages/add',
data: {
text: $('#messageInput').val(),
key: key()
}
}).done(function () {
$('#messageInput').val('')
});
}
});

$('#messageInput').keydown('ctrl+enter', function (e) {
$('#send').click();
});
});
52 changes: 52 additions & 0 deletions public/javascript/chat-master.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
function addMessage(sender, senderImg, content, messageID, isOut) {
$('.mainChat').append(
$('<div class="messageWrap">').append(
$('<div class="userCard">').append(
$('<img width="32" height="32">').attr('src', senderImg)
).append(
$('<a>').text(sender).attr('href', '/users/' + sender)
)
).append(
$('<div class="messageBubble' + (isOut ? ' messageOut' : '') + '">').attr('id', messageID).html(markdown(content))
).append($('<hr>'))
)
}

$(function () {
window.CHAT = {
room: {
id: parseInt(location.pathname.match(/\d+/)[0])
}, user: {
name: $('#usernameTitle').text().split(/Logged in as ([\w+ ]+)+/)[1]
}
};

function createChatWS() {
var ws = new WebSocket('ws://' + location.hostname + ':4000/rooms/' + CHAT.room.id);
ws.onmessage = function (msg) {
var data = JSON.parse(msg.data);
addMessage(data.senderName, data.senderImg, data.content, data.messageID, (data.senderName === CHAT.user.name))
$('.mainChat').scrollTop(99999999999999999);
};
ws.onerror = function (event) {
ws = createChatWS();
};
return ws;
}

CHAT.ws = createChatWS();
$.ajax({
type: 'POST',
url: '/chat/rooms/' + CHAT.room.id + '/messages'
}).done(function (data) {
var messages = JSON.parse(data);
for (var messageID in messages) {
if (!messages.hasOwnProperty(messageID))
continue;
var message = messages[messageID];
addMessage(message.senderName, message.senderImg, message.content, messageID, (message.senderName === CHAT.user.name));
}
$('.mainChat').scrollTop(999);
$('#blockChat, #chatLoading').remove();
});
});

0 comments on commit 9cff2b2

Please sign in to comment.