Skip to content

Commit

Permalink
Initial Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Ashley Bibizadeh authored and Ashley Bibizadeh committed Nov 6, 2018
0 parents commit a0af748
Show file tree
Hide file tree
Showing 32 changed files with 1,665 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/node_modules/
yarn.lock
build
.DS_Store
yarn-error.log
package-lock.json
/coverage/
21 changes: 21 additions & 0 deletions Backend/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2018 Ashley Bibizadeh <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
20 changes: 20 additions & 0 deletions Backend/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "react_realtime_chat_app_backend",
"author": "Ashley Bibizadeh",
"version": "1.0.0",
"license": "MIT",
"dependencies": {
"express": "^4.16.4",
"moment": "^2.22.2",
"socket.io": "^2.1.1"
},
"devDependencies": {
"jest": "^23.6.0",
"nodemon": "^1.18.5"
},
"scripts": {
"start": "nodemon server/index.js",
"serve": "node server/index.js",
"test": "jest"
}
}
75 changes: 75 additions & 0 deletions Backend/server/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
const path = require('path');
const express = require('express');
const http = require('http');
const socketIO = require('socket.io');

const { generateMessage, generateLocationMessage } = require('./utils/message.js');
const { isRealString } = require('./utils/validation');
const { Users } = require('./utils/users');
const publicPath = path.join(__dirname, '../public');
const PORT = process.env.PORT || 8080;

const app = express();
const server = http.createServer(app);
app.use(express.static(publicPath));

var io = socketIO(server);
var users = new Users();

io.on('connection', (socket) => {

socket.on('leave', (params) => {
socket.leave(params.room);
});

socket.on('join', (params, callback) => {

if (!isRealString(params.name) || !isRealString(params.room)) {
return callback('Bad request');
}

socket.join(params.room);
users.removeUser(socket.id);
users.addUser(socket.id, params.name, params.room);

io.to(params.room).emit('updateUserList', users.getUserList(params.room));
socket.emit('newMessage', generateMessage('Admin', params.room, 'Welcome to the chat app.'));
socket.broadcast.to(params.room).emit('newMessage', generateMessage('Admin', params.room, `${params.name} has joined.`));

callback();
});

socket.on('createMessage', (message, callback) => {
var user = users.getUser(socket.id);
if (user && isRealString(message.text)) {
let tempObj = generateMessage(user.name, user.room, message.text);
io.to(user.room).emit('newMessage', tempObj);
callback({
data: tempObj
});
}
callback();
});

socket.on('createLocationMsg', (coords) => {
var user = users.getUser(socket.id);
if (user) {
io.to(user.room).emit('createLocationMsg', generateLocationMessage(user.name, user.room, coords.lat, coords.lon));
}
});

socket.on('disconnect', () => {
var user = users.removeUser(socket.id);

if (user) {
io.to(user.room).emit('updateUserList', users.getUserList(user.room));
io.to(user.room).emit('newMessage', generateMessage('Admin', user.room, `${user.name} has left.`));
}
});

});

server.listen(PORT, () => {
console.log(`App running on port ${PORT}`);
});

21 changes: 21 additions & 0 deletions Backend/server/utils/message.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
var moment = require('moment');

var generateMessage = (from, room, text) => {
return {
from,
room,
text,
createdDate: moment().valueOf()
}
};

var generateLocationMessage = (from, room, lat, lon) => {
return {
from,
room,
url: `https://www.google.com/maps?q=${lat},${lon}`,
createdDate: moment().valueOf()
}
}

module.exports = {generateMessage, generateLocationMessage};
28 changes: 28 additions & 0 deletions Backend/server/utils/message.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
var {generateMessage, generateLocationMessage} = require('./message');

describe('generateMessage', () => {
it('Should generate correct message object', () => {
var from = 'Jen';
var room = 'Node JS';
var text = 'Some msg';
var message = generateMessage(from, room, text);

expect(typeof message.createdDate).toBe('number');
expect(message).toMatchObject({from, text});
})
});


describe('generateLocationMessage', () => {
it('Should generate correct location object', () => {
var from = 'John';
var room = 'Node JS';
var lat = 1;
var long = 2;
var url = 'https://www.google.com/maps?q=1,2';
var res = generateLocationMessage(from, room, lat, long);

expect(typeof res.createdDate).toBe('number');
expect(res).toMatchObject({from, url});
})
});
34 changes: 34 additions & 0 deletions Backend/server/utils/users.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
class Users {
constructor() {
this.users = [];
}

addUser(id, name, room) {
var user = { id, name, room };
this.users.push(user);
return user;
}

removeUser(id) {
var user = this.getUser(id);

if(user){
this.users = this.users.filter(user => user.id !== id);
}

return user;
}

getUser(id) {
return this.users.filter((user) => user.id === id)[0];
}

getUserList(room) {
var users = this.users.filter((user) => user.room === room);
var namesArr = users.map(user => user.name);
return namesArr
}

}

module.exports = { Users }
59 changes: 59 additions & 0 deletions Backend/server/utils/users.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
var {Users} = require('./users');

describe('Users', () => {
var users;

beforeEach(() => {
users = new Users();
users.users = [{
id: '1',
name: 'Ash',
room: 'Baseball'
},
{
id: '2',
name: 'Bill',
room: 'Baseball'
},
{
id: '3',
name: 'Bob',
room: 'Football'
}]
});

it('Should add new user', () => {
var users = new Users();
var user = {
id: '123',
name: 'Joe',
room: 'Football'
}
var resUser = users.addUser(user.id, user.name, user.room);
expect(users.users).toEqual([user]);
});

it('Should return names for baseball', () => {
var userList = users.getUserList('Baseball');
expect(userList).toEqual(['Ash', 'Bill']);
});

it('Should return names for Football', () => {
var userList = users.getUserList('Football');
expect(userList).toEqual(['Bob']);
});

it('Should find user', () => {
var userID = '2';
var user = users.getUser(userID);
expect(user.id).toBe(userID);
});

it('Should remove user', () => {
var userID = '1';
var user = users.removeUser(userID);
expect(user.id).toBe(userID);
expect(users.users.length).toBe(2);
});

});
5 changes: 5 additions & 0 deletions Backend/server/utils/validation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
var isRealString = (str) => {
return typeof str === 'string' && str.trim().length > 0;
};

module.exports = {isRealString}
14 changes: 14 additions & 0 deletions Backend/server/utils/validation.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
var {isRealString} = require('./validation');

describe('isRealString', () => {
it('Should return true', () => {
var string = 'Joe';
var result = isRealString(string);
expect(result).toBe(true);
})
it('Should return false', () => {
var string = ' ';
var result = isRealString(string);
expect(result).toBe(false);
})
});
23 changes: 23 additions & 0 deletions Frontend/app/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules
/.pnp
.pnp.js

# testing
/coverage

# production
/build

# misc
.DS_Store
.env.local
.env.development.local
.env.test.local
.env.production.local

npm-debug.log*
yarn-debug.log*
yarn-error.log*
21 changes: 21 additions & 0 deletions Frontend/app/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2018 Ashley Bibizadeh <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
35 changes: 35 additions & 0 deletions Frontend/app/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"name": "react_realtime_chat_app",
"author": "Ashley Bibizadeh",
"version": "0.1.0",
"license": "MIT",
"private": true,
"dependencies": {
"classnames": "^2.2.6",
"moment": "^2.22.2",
"node-sass": "^4.10.0",
"react": "^16.6.0",
"react-dom": "^16.6.0",
"react-loading-screen": "^0.0.17",
"react-router-dom": "^4.3.1",
"socket.io-client": "^2.1.1"
},
"devDependencies": {
"react-scripts": "2.1.1"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": "react-app"
},
"browserslist": [
">0.2%",
"not dead",
"not ie <= 11",
"not op_mini all"
]
}
Binary file added Frontend/app/public/favicon.ico
Binary file not shown.
Loading

0 comments on commit a0af748

Please sign in to comment.