Skip to content
This repository has been archived by the owner on May 1, 2020. It is now read-only.

Commit

Permalink
UI v2 and code fixes for school
Browse files Browse the repository at this point in the history
  • Loading branch information
maniflames committed Oct 29, 2017
1 parent ba4e01e commit c8082c8
Show file tree
Hide file tree
Showing 35 changed files with 537 additions and 253 deletions.
117 changes: 87 additions & 30 deletions api/controllers/ChatroomController.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@
* @help :: See http://sailsjs.org/#!/documentation/concepts/Controllers
*/


module.exports = {
show: function(req, res){
Chatroom.findOne({id: req.params.chatroom})
.exec(function(err, foundChat){
if(err){
sails.log.error(err);
return res.view('error');
return res.serverError();
}

return res.view('chatroom', {chat: foundChat});
Expand All @@ -20,11 +21,29 @@ module.exports = {
},

add: function(req, res){
let notContains = ['<', '>', '\'', '\\', '/', '(', ')', '"', '}', '{', '[', ']', "*"];
let errors = [];

notContains.map(function(char){
if(req.body.chatname.indexOf(char) != -1){
valid = false;
errors.push({error: 'You can\'t use ' + char + ' in your username'});
}
})

if(!valid){
return res.view('addChatroom', {"errors": errors})
}

if(req.body.chatname = ''){
return res.view('addChatroom', {"errors": [{"error": "Name of chatroom cannot be empty"}]})
}

Chatroom.create({name: req.body.chatname})
.exec(function(err, newChat){
if(err){
sails.log.error(err);
return res.view('error');
return res.serverError();
}

newChat.members.add(req.session.userId);
Expand All @@ -33,10 +52,10 @@ module.exports = {
newChat.save(function(err){
if(err){
sails.log.error(err);
return res.view('error');
return res.serverError();
}

return res.redirect('/');
return res.redirect(sails.getUrlFor('UserController.index'));

});
})
Expand All @@ -46,42 +65,57 @@ module.exports = {
Chatroom.findOne({id: req.params.chatroom})
.populate('admins')
.populate('members')
.populate('blocked')
.exec(function(err, foundChat){
if(err){
sails.log.error(err);
return res.view('error');
return res.serverError();
}

return res.view('chatroomSettings', {chat: foundChat});

})
},

//LIGO: !! functionality preferably in ONE Query, send the amount of chatroom members in request
//Smartest thing to do would be to add a policy
//Look into sum and stuff it could really slim this thing.
settingsEdit: function(req, res){
Chatroom.findOne({id: req.params.chatroom})
.populate('admins')
.populate('members')
.populate('blocked')
.exec(function(err, foundChat){
if(err){
sails.log.error(err);
return res.serverError();
}

return res.view('chatroomSettingsEdit', {chat: foundChat});

})
},


leave: function(req, res){
Chatroom.findOne({id: req.params.chatroom})
.populate('members')
.exec(function(err, foundChat){
if(err){
sails.log.error(err);
return res.view('error');
return res.serverError();
}

if(foundChat === undefined){
return res.view('error');
return res.notFound();
}

if(foundChat.members.length > 1){
foundChat.members.remove(req.session.userId);
foundChat.save(function(err){
if(err){
sails.log.error(err);
return res.view('error');
return res.serverError();
}

return res.json({"location":"/"});
return res.redirect(sails.getUrlFor('UserController.index'));

});

Expand All @@ -91,36 +125,34 @@ module.exports = {
.exec(function(err, destroyedChat){
if(err){
sails.log.error(err);
return res.view('error');
return res.serverError();
}

return res.json({"location":"/"});
return res.redirect(sails.getUrlFor('UserController.index'));
})
}
})
},

//LIGO: write chatMember service

addChatMember: function(req, res){

User.findOne({username: req.body.username})
.populate('chats')
.exec(function(err, foundUser){
if(err){
sails.log.error(err);
return res.view('error');
return res.serverError();
}

if(foundUser === undefined){
return res.redirect('/');
return res.json({"errors": [{"error": "User not found"}]});
}

foundUser.chats.add(req.params.chatroom);
foundUser.save(function(err){
if(err){
sails.log.error(err);
return res.view('error');
return res.serverError();
}
})

Expand All @@ -136,22 +168,22 @@ module.exports = {
.exec(function(err, foundUser){
if(err){
sails.log.error(err);
return res.json({"location":"/"});
return res.serverError();
}

if(foundUser === undefined){
return res.json({"location":"/"});
return res.notFound();
}

foundUser.chats.remove(req.params.chatroom);
foundUser.save(function(err){
if(err){
sails.log.error(err);
return res.json({"location":"/"});
return res.serverError();
}
});

return res.json({"location": req.body.origin});
return res.ok();
})
},

Expand All @@ -162,23 +194,48 @@ module.exports = {
.exec(function(err, foundUser){
if(err){
sails.log.error(err);
return res.json({"location":"/"});
return res.serverError();
}

if(foundUser === undefined){
return res.json({"location":"/"});
return res.notFound();
}

let unblock = true;

foundUser.chats.map(function(chat){

if(chat.id == req.params.chatroom){
unblock = false;
}

return;
});

if(unblock){

foundUser.chats.add(req.params.chatroom);
foundUser.blockedFrom.remove(req.params.chatroom);
status = 'unblocked';

} else {

foundUser.chats.remove(req.params.chatroom);
foundUser.blockedFrom.add(req.params.chatroom);
status = 'blocked';
}

foundUser.chats.remove(req.params.chatroom);
foundUser.blockedFrom.add(req.params.chatroom);
foundUser.save(function(err){
if(err){
sails.log.error(err);
return res.json({"location":"/"});
return res.serverError();
}
});

return res.json({"location": req.body.origin});
return res.ok();


});
})
},

}
75 changes: 54 additions & 21 deletions api/controllers/UserController.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,55 @@

module.exports = {
index: function(req, res){

User.findOne({id: req.session.userId})
.populate('chats')
.exec(function(err, userAndChats){
if(err){
console.log(err);
return res.view(err);
return res.serverError();
}

if(_.isEmpty(userAndChats.chats)){
return res.view('dashboard', {username: userAndChats.username});
}
return res.view('dashboard', {chats: userAndChats.chats});

let countQuery = 'SELECT chatroom_members__user_chats.chatroom_members AS chatroom, COUNT(chatroom_members__user_chats.chatroom_members) AS count FROM `user` INNER JOIN chatroom_members__user_chats ON user.id = chatroom_members__user_chats.user_chats GROUP BY chatroom_members__user_chats.chatroom_members';
User.query(countQuery, [], function(err, rawResult){

let chatsWithCount = [];

userAndChats.chats.map(function(chat){
rawResult.map(function(result){
if (result.chatroom == chat.id){
chat.count = result.count;
chatsWithCount.push(chat);
}
})
});

return res.view('dashboard', {chats: chatsWithCount, username: userAndChats.username});

})

})
},

register: function(req, res){
AuthService.register(req)
.then(function(newUser){
return res.redirect('/');
})
.catch(function(err){
sails.log.error(err);
return res.view('register', {errors: err});
})
AuthService.register(req)
.then(function(newUser){
return res.redirect(sails.getUrlFor('UserController.login'));
})
.catch(function(err){
sails.log.error(err);
return res.view('register', {errors: err});
})
},

login: function(req, res){
AuthService.login(req)
.then(function(loggedInUser){
return res.redirect('/');
return res.redirect(sails.getUrlFor('UserController.index'));
})
.catch(function(err){
sails.log.error(err);
Expand All @@ -42,7 +65,7 @@ module.exports = {

logout: function(req, res){
res.clearCookie('sid');
return res.redirect('login');
return res.redirect(sails.getUrlFor('UserController.login'));
},

settings: function(req, res){
Expand All @@ -60,7 +83,8 @@ module.exports = {
return res.view('dashboard', {chats: results});
})
.catch(function(err){
return sails.log.error(err);
sails.log.error(err);
return res.serverError();
});

},
Expand All @@ -69,13 +93,11 @@ module.exports = {
User.findOne({username: req.params.username}).exec(function(err, foundUser){
if(err){
sails.log.error(err)
return res.view('error');
return res.serverError();
}

if(foundUser == undefined || foundUser == ''){
//LIGO: Change his into a 404 once I've made it
sails.log.error('Not a user');
return res.view('error');
return res.notFound();
}

return res.view('userDetail', {user: foundUser});
Expand All @@ -86,16 +108,27 @@ module.exports = {
User.findOne({username: req.params.username}).exec(function(err, foundUser){
if(err){
sails.log.error(err)
return res.view('error');
return res.serverError();
}

if(foundUser == undefined || foundUser == ''){
//LIGO: Change his into a 404 once I've made it
sails.log.error('Not a user');
return res.view('error');
return res.notFound();
}

return res.view('userDetailEdit', {user: foundUser});
});
},

detailEditSave: function(req, res){
sails.log.debug(req.body.bio);
User.update({username: req.params.username}, {bio: req.body.bio}).exec(function(err, updatedUser){
if(err){
sails.log.error(err)
return res.serverError();
}

return res.redirect(sails.getUrlFor('UserController.detailEdit').replace(':username', req.params.username));
})
}

};
Loading

0 comments on commit c8082c8

Please sign in to comment.