Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
e94d890
removed fs dependency
bodqhrohro Aug 20, 2015
cdabd9c
registration: check if email or nick are already present
bodqhrohro Aug 20, 2015
60edd05
added password hashing && fxd email/nick check
bodqhrohro Aug 22, 2015
f06fc4b
added check for database connection
bodqhrohro Aug 22, 2015
2e2ac5f
fxd authorization
bodqhrohro Aug 22, 2015
a4f0d16
implemented CRUD for posts
bodqhrohro Aug 22, 2015
e5d062b
fxd user's wall
bodqhrohro Aug 23, 2015
a790636
added check if wall exists
bodqhrohro Aug 23, 2015
d3dc68a
implemented following
bodqhrohro Aug 23, 2015
4d6debc
added userinfo update
bodqhrohro Aug 23, 2015
7a30247
Merge branch 'master' of https://github.com/IntersogLABs/SocialNetwor…
bodqhrohro Aug 23, 2015
8a74c79
attached frontend
bodqhrohro Sep 6, 2015
7b232f1
rerouted backend to api/
bodqhrohro Sep 6, 2015
d4db3ba
added logging in & registration
bodqhrohro Sep 10, 2015
33c6d1c
added wall displaying && attached unadded new files
bodqhrohro Sep 15, 2015
bae9357
added posting to wall
bodqhrohro Sep 16, 2015
e5716dd
trying to implement following button
bodqhrohro Sep 21, 2015
cd34108
finished subscribe/unsubscribe button
bodqhrohro Sep 24, 2015
4f5d444
implemented feed
bodqhrohro Sep 24, 2015
4ca1d79
profile view & edit
bodqhrohro Sep 29, 2015
aa9421c
display owner in feed
bodqhrohro Sep 30, 2015
c6f9a70
session saving & logout
bodqhrohro Sep 30, 2015
f1a744b
sanitize prev
bodqhrohro Sep 30, 2015
e0a0156
fxd logout separation
bodqhrohro Sep 30, 2015
7fcd628
reset form on submit
bodqhrohro Sep 30, 2015
f620a02
added error handling
bodqhrohro Oct 1, 2015
73c9edc
fxd password leak
bodqhrohro Oct 1, 2015
89e147c
error handling for register
bodqhrohro Oct 1, 2015
93121b7
error handling for login
bodqhrohro Oct 1, 2015
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules
.idea
.idea
.*.sw*
26 changes: 16 additions & 10 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
var express = require('express');
var bodyParser = require('body-parser');
GLOBAL._ = require('underscore');
var fs= require('fs')
var app = express();
var router = express.Router();
var MongoClient = require('mongodb').MongoClient
var url = 'mongodb://localhost:27017/socialNetwork';
MongoClient.connect(url, function(err, db) {
console.log("Connected correctly to server");
console.log(err ? err : "Connected correctly to server");
GLOBAL.DB = db;
app.listen(80)
app.listen(100)
});

app.use(function(req, res, next) {
app.use(express.static('public'))

var auth = require('./auth')

router.use(function(req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization, Content-Length, X-Requested-With');
Expand All @@ -24,10 +28,10 @@ app.use(function(req, res, next) {
next();
}
});
app.use(bodyParser.json())
app.use(function (req, res, next) {
router.use(bodyParser.json())
router.use(function (req, res, next) {
console.log(req.originalUrl)
if(req.originalUrl =='/register'){
if(req.originalUrl =='/api/register'){
next(null);
return;
}
Expand All @@ -37,7 +41,7 @@ app.use(function (req, res, next) {
}
var parts = req.headers['authorization'].split(":")
var nick = parts[0];
var pwd = parts[1];
var pwd = auth.encodePassword(parts[1]);
DB.collection('users').find({nick:nick,pwd:pwd}).toArray(function(err,data){

if (data.length>0) {
Expand All @@ -51,5 +55,7 @@ app.use(function (req, res, next) {


})
require('./controllers/user')(app)
require('./controllers/post')(app)
require('./controllers/user')(router)
require('./controllers/post')(router)
require('./controllers/following')(router)
app.use('/api',router);
12 changes: 12 additions & 0 deletions auth.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
var crypto = require('crypto')
module.exports = {
encodePassword: function(password) {
return crypto.createHash('md5').update(
crypto.createHash('sha1').update(
crypto.createHash('md5').update(
password
).digest('hex')
).digest('hex')
).digest('hex')
}
}
56 changes: 56 additions & 0 deletions controllers/following.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
var ObjectId = require('mongodb').ObjectID
var async = require('async')
module.exports=function(app){
app.get('/user/:id/following',function(req,res){
var UsersCollection = DB.collection('users')
DB.collection('follow')
.find({"fanId._id": new ObjectId(req.params.id)})
.toArray(function (err, conns) {
async.mapLimit(conns, 5, function (conn, next) {
UsersCollection.findOne({_id: new ObjectId(conn.idolId._id)},
function (err, data) {
conn.idol = data;
next(null,conn);
})
}, function (err,data) {
res.send(data);
})
})
})

app.get('/user/:id/folowers',function(req,res){
var UsersCollection = DB.collection('users')
DB.collection('follow')
.find({"idolId._id": new ObjectId(req.params.id)})
.toArray(function (err, conns) {
async.mapLimit(conns, 5, function (conn, next) {
UsersCollection.findOne({_id: new ObjectId(conn.fanId._id)},
function (err, data) {
conn.fan = data;
next(null,conn);
})
}, function (err,data) {
res.send(data);
})
})
})

app.post('/user/:id/follow',function(req,res){
var conn = {
fanId:{$ref:"users",_id:req.currentUser._id},
idolId:{$ref:"users",_id:new ObjectId(req.params.id)}
};
DB.collection('follow').insert(conn,function(err,data){
res.send(data);
})
})

app.delete('/user/:id/follow',function(req,res){
DB.collection('follow').deleteOne({
'fanId._id':req.currentUser._id,
'idolId._id':new ObjectId(req.params.id)
}, function (err,result){
res.send(result);
})
})
}
86 changes: 77 additions & 9 deletions controllers/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,84 @@ module.exports=function(app){
res.status(400).send({message:'content required'})
return;
}
var post = {
content:req.body.content,
authorId:{$ref:"users",_id:req.currentUser._id},
ownerId:{$ref:"users",_id:req.params.id}
};
DB.collection('posts').insert(post,function(err,data){
res.send(data);
})

DB.collection('users').findOne({_id: new ObjectId(req.params.id)},
function (err, user) {
if (!user) {
res.status(404).send({message: "not found"})
return;
}
var post = {
content:req.body.content,
authorId:{$ref:"users",_id:req.currentUser._id},
ownerId:{$ref:"users",_id:new ObjectId(req.params.id)}
};
DB.collection('posts').insert(post,function(err,data){
res.send(data);
})
})

})

}
app.get('/post',function(req,res){
DB.collection('posts').find({}).toArray(function (err, data) {
res.send(data);
})
})

app.get('/posts/:id', function (req, res) {
DB.collection('posts').findOne({_id: new ObjectId(req.params.id)},
function (err, post) {
if (!post) {
res.status(404).send({message: "not found"})
return;
}
res.send(post);
})
})

app.put('/posts/:id', function (req, res) {
var PostsCollection = DB.collection('posts');
if(!req.body.content){
res.status(400).send({message:'content required'})
return;
}

PostsCollection.findOne({_id: new ObjectId(req.params.id)},
function (err, post) {
if (!post) {
res.status(404).send({message: "not found"})
return;
} else if (!post.authorId._id.equals(req.currentUser._id)) {
res.status(403).send({message: "not allowed"})
return;
}

PostsCollection.updateOne({_id: post._id},
{$set: {content: req.body.content}},
function(err, result) {
res.send(result);
})
})
})

app.delete('/posts/:id', function (req, res) {
var PostsCollection = DB.collection('posts');
PostsCollection.findOne({_id: new ObjectId(req.params.id)},
function (err, post) {
var _id = req.currentUser._id;
if (!post) {
res.status(404).send({message: "not found"})
return;
} else if (!post.authorId._id.equals(_id) && !post.ownerId._id.equals(_id)) {
res.status(403).send({message: "not allowed"})
return;
}

PostsCollection.deleteOne({'_id': post._id},
function(err, result) {
res.send(result);
})
})
})
}
101 changes: 87 additions & 14 deletions controllers/user.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
var ObjectId = require('mongodb').ObjectID
var async = require('async')
var auth = require('../auth')
module.exports = function (app) {
app.get('/me', function (req, res) {
res.send(req.currentUser);
var me = _.clone(req.currentUser);

delete me.pwd;
res.send(me);
})
app.get('/user', function (req, res) {
DB.collection('users').find({}).toArray(function (err, data) {
Expand Down Expand Up @@ -30,7 +34,7 @@ module.exports = function (app) {
app.get('/user/:id/wall', function (req, res) {
var UsersCollection = DB.collection('users')
DB.collection('posts')
.find({"ownerId._id": req.params.id})
.find({"ownerId._id": new ObjectId(req.params.id)})
.toArray(function (err, posts) {
async.mapLimit(posts, 5, function (post, next) {
UsersCollection.findOne({_id: new ObjectId(post.authorId._id)},
Expand All @@ -54,7 +58,7 @@ module.exports = function (app) {


app.post('/register', function (req, res) {
//проверить свободен ли ник и имейл
var UsersCollection = DB.collection('users')
if (!req.body.email) {
res.status(400).send({message: "Email is required"})
return;
Expand All @@ -65,16 +69,85 @@ module.exports = function (app) {
res.status(400).send({message: "Passwords do not match"})
return;
}
var user = {
email: req.body.email,
nick: req.body.nick,
pwd: req.body.pwd
};

DB.collection('users').insert(user, function (err, data) {
delete user.pwd;
res.send(user)
})

UsersCollection
.find({"email": req.body.email})
.hasNext(function(err, data){
if (!data) {
UsersCollection
.find({"nick": req.body.nick})
.hasNext(function(err, data){
if (!data) {
var user = {
email: req.body.email,
nick: req.body.nick,
pwd: auth.encodePassword(req.body.pwd)
};

UsersCollection.insert(user, function (err, data) {
delete user.pwd;
res.send(user)
})
} else {
res.status(400).send({message: "Nick is already registered"})
}
})
} else {
res.status(400).send({message: "Email is already registered"})
}
})
})

app.put('/me', function (req, res) {
var UsersCollection = DB.collection('users')
var userInfo = {}
if (req.body.email) {userInfo.email = req.body.email}
if (req.body.nick) {userInfo.nick = req.body.nick}
if (req.body.pwd) {userInfo.pwd = auth.encodePassword(req.body.pwd)}

var checkEmail = function() {
UsersCollection
.find({"email": req.body.email})
.hasNext(function(err, data){
if (!data) {
if (userInfo.nick) {
checkNick()
} else {
updateUser()
}
} else {
res.status(400).send({message: "Email is already registered"})
}
})
}

var checkNick = function() {
UsersCollection
.find({"nick": req.body.nick})
.hasNext(function(err, data){
if (!data) {
updateUser()
} else {
res.status(400).send({message: "Nick is already registered"})
}
})
}

var updateUser = function() {
UsersCollection.updateOne({_id: req.currentUser._id},
{$set: userInfo},
function(err, result) {
res.send(result)
})
}

if (userInfo.email) {
checkEmail()
} else if (userInfo.nick) {
checkNick()
} else {
updateUser()
}

})
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"async": "^1.4.2",
"body-parser": "^1.13.3",
"express": "^4.13.3",
"mongodb": "^2.0.41",
"mongodb": "^2.0.42",
"underscore": "^1.8.3"
}
}
1 change: 1 addition & 0 deletions public/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bower_components
3 changes: 3 additions & 0 deletions public/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# SocialChaplin
Social network using Chaplin.js
Don't forget to make `bower install` firstly
20 changes: 20 additions & 0 deletions public/bower.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "chaplin-boilerplate",
"version": "0.1.0",
"main": "index.js",
"ignore": [
"**/.*",
"node_modules",
"bower_components",
"test",
"tests"
],
"dependencies": {
"chaplin": "~1.0.0",
"handlebars": "~1.0.0",
"jquery": "~2.0.3",
"lodash": "~2.4.1",
"requirejs": "~2.1.9",
"requirejs-text": "~2.0.9"
}
}
Loading