Skip to content

Commit

Permalink
#10 Add angular-ui selec2 to configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
aurimus committed Oct 7, 2014
2 parents 3469c55 + 4e5e12e commit 2b4d9bc
Show file tree
Hide file tree
Showing 60 changed files with 1,110 additions and 1,061 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ public/dist
app/tests/coverage/
.bower-*/
.idea/
scripts/deploy.sh
116 changes: 0 additions & 116 deletions app/controllers/articles.server.controller.js

This file was deleted.

36 changes: 16 additions & 20 deletions app/controllers/messages.server.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/
var mongoose = require('mongoose'),
errorHandler = require('./errors'),
config = require('../../config/config'),
sanitizeHtml = require('sanitize-html'),
Message = mongoose.model('Message'),
Thread = mongoose.model('Thread'),
Expand All @@ -27,6 +28,8 @@ var messageSanitizeOptions = {
allowedSchemes: [ 'http', 'https', 'ftp', 'mailto', 'tel' ]
};

// Populate users with these fields
var userPopulateFields = config.app.miniUserProfileFields.join(' ');

/**
* List of threads aka inbox
Expand All @@ -42,8 +45,8 @@ exports.inbox = function(req, res) {
}
)
.sort('updated')
.populate('userFrom', 'displayName username')
.populate('userTo', 'displayName username')
.populate('userFrom', userPopulateFields)
.populate('userTo', userPopulateFields)
.populate('message', 'content')
.exec(function(err, threads) {
if (err) {
Expand All @@ -56,13 +59,13 @@ exports.inbox = function(req, res) {
var threadsCleaned = [];
threads.forEach(function(thread) {

// Threads need just excerpt
thread.message.excerpt = sanitizeHtml(thread.message.content, {allowedTags: []}); // Clean message content from html
thread.message.excerpt = thread.message.excerpt.replace(/\s/g, ' '); // Remove white space. Matches a single white space character, including space, tab, form feed, line feed.
thread.message.excerpt = thread.message.excerpt.substring(0,100) + ' ...'; // Shorten
// Threads need just excerpt
thread.message.excerpt = sanitizeHtml(thread.message.content, {allowedTags: []}); // Clean message content from html
thread.message.excerpt = thread.message.excerpt.replace(/\s/g, ' '); // Remove white space. Matches a single white space character, including space, tab, form feed, line feed.
thread.message.excerpt = thread.message.excerpt.substring(0,100) + ' ...'; // Shorten

delete thread.message.content;
threadsCleaned.push(thread);
delete thread.message.content;
threadsCleaned.push(thread);
});

res.jsonp(threadsCleaned);
Expand All @@ -80,12 +83,6 @@ exports.send = function(req, res) {
// take out socket instance from the app container, we'll need it later
var socketio = req.app.get('socketio');

var userByID = function(id) {
User.findById(id).exec(function(err, user) {
return (err || !user) ? false : user;
});
};

var message = new Message(req.body);
message.userFrom = req.user;

Expand Down Expand Up @@ -145,10 +142,10 @@ exports.send = function(req, res) {

// We'll need some info about related users, populate some fields
message
.populate('userFrom', 'displayName username')
.populate('userFrom', userPopulateFields)
.populate({
path: 'userTo',
select: 'displayName username'
select: userPopulateFields
}, function(err, message) {
if (err) {
return res.status(400).send({
Expand Down Expand Up @@ -181,6 +178,7 @@ exports.thread = function(req, res) {
* Thread middleware
*/
exports.threadByUser = function(req, res, next, userId) {

Message.find(
{
$or: [
Expand All @@ -190,8 +188,8 @@ exports.threadByUser = function(req, res, next, userId) {
}
)
.sort('-created')
.populate('userFrom', 'displayName username')
.populate('userTo', 'displayName username')
.populate('userFrom', userPopulateFields)
.populate('userTo', userPopulateFields)
.exec(function(err, messages) {
if (err) return next(err);
if (!messages) return next(new Error('Failed to load messages.'));
Expand Down Expand Up @@ -277,5 +275,3 @@ exports.delete = function(req, res) {
});
};
*/


69 changes: 63 additions & 6 deletions app/controllers/references.server.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,20 @@
*/
var mongoose = require('mongoose'),
errorHandler = require('./errors'),
config = require('../../config/config'),
Reference = mongoose.model('Reference'),
_ = require('lodash');

// Populate users with these fields
var userPopulateFields = config.app.miniUserProfileFields.join(' ');

/**
* Create a Reference
*/
exports.create = function(req, res) {
var reference = new Reference(req.body);
reference.user = req.user;
reference.userFrom = req.user;
reference.updated = null;

reference.save(function(err) {
if (err) {
Expand All @@ -37,17 +42,38 @@ exports.read = function(req, res) {
* Update a Reference
*/
exports.update = function(req, res) {
var reference = req.reference ;
var reference = req.reference;

// Make sure we won't touch creation date, but do change update timestamp
if(req.body.created) delete req.body.created;

reference = _.extend(reference , req.body);

reference.updated = new Date();

reference.save(function(err) {
if (err) {
return res.status(400).send({
message: errorHandler.getErrorMessage(err)
});
} else {
res.jsonp(reference);

// We'll need some info about related users, populate some fields
reference
.populate({
path: 'userTo',
select: userPopulateFields
}, function(err, reference) {
if (err) {
return res.status(400).send({
message: errorHandler.getErrorMessage(err)
});
} else {
// Response
res.jsonp(reference);
}
});

}
});
};
Expand All @@ -72,7 +98,17 @@ exports.delete = function(req, res) {
/**
* List of References
*/
exports.list = function(req, res) { Reference.find().sort('-created').populate('user', 'displayName').exec(function(err, references) {
exports.list = function(req, res) {
console.log('->list');
res.jsonp(req.references);
/*
Reference.find({
userTo: userId
})
.sort('-created')
.populate('userTo', 'username displayName')
.populate('userFrom', 'username displayName')
.exec(function(err, references) {
if (err) {
return res.status(400).send({
message: errorHandler.getErrorMessage(err)
Expand All @@ -81,6 +117,7 @@ exports.list = function(req, res) { Reference.find().sort('-created').populate('
res.jsonp(references);
}
});
*/
};

/**
Expand All @@ -94,12 +131,32 @@ exports.referenceByID = function(req, res, next, id) { Reference.findById(id).po
});
};

exports.referencesByUser = function(req, res, next, userId) {
console.log('->referencesByUser: ' + userId);
Reference
.find({
$or: [
{ userFrom: userId },
{ userTo: userId }
]
})
.populate('userFrom', userPopulateFields)
.populate('userTo', userPopulateFields)
.exec(function(err, references) {
if (err) return next(err);
if (! references) return next(new Error('Failed to load References for user ' + userId));
req.references = references;
next();
});
};


/**
* Reference authorization middleware
*/
exports.hasAuthorization = function(req, res, next) {
if (req.reference.user.id !== req.user.id) {
if (req.reference.userFrom.id !== req.user.id) {
return res.status(403).send('User is not authorized');
}
next();
};
};
Loading

0 comments on commit 2b4d9bc

Please sign in to comment.