Skip to content

Commit

Permalink
Merge pull request #68 from vinitkumar/cleanup
Browse files Browse the repository at this point in the history
Cleanup
  • Loading branch information
Vinit Kumar committed Jun 13, 2014
2 parents 565c6ea + f669920 commit 52166f6
Show file tree
Hide file tree
Showing 10 changed files with 148 additions and 12 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules/*
npm-debug.log
14 changes: 14 additions & 0 deletions app/controllers/comments.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,18 @@
var mongoose = require('mongoose');
var utils = require('../../lib/utils');

/**
* Load comment
*/

exports.load = function (req, res, next, id) {
var tweet = req.tweet
utils.findByParam(tweet.comments, { id: id }, function (err, comment) {
if (err) return next(err);
req.comment = comment;
next();
});
};

// ### Create Comment
exports.create = function (req, res) {
Expand Down
14 changes: 8 additions & 6 deletions app/controllers/tweets.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,21 +85,23 @@ exports.destroy = function (req, res) {
};



exports.index = function (req, res) {
var page = req.param('page') > 0 ? req.param('page'):0;
var perPage = 15;
var page = (req.param('page') > 0 ? req.param('page'):1) - 1;
var perPage = 15;
var options = {
perPage: perPage,
page: page
};
Tweet.list(options, function (err, tweets) {

Tweet.list(options, function(err, tweets) {
if (err) return res.render('500');
Tweet.count().exec(function (err, count) {
res.render('tweets/index', {
title: 'List of tweets',
title: 'List of Tweets',
tweets: tweets,
page: page,
pages: count/perPage
page: page + 1,
pages: Math.ceil(count / perPage)
});
});
});
Expand Down
18 changes: 15 additions & 3 deletions app/models/tweets.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
var mongoose = require('mongoose'),
env = process.env.NODE_ENV || 'development',
config = require('../../config/config')[env],
Schema = mongoose.Schema;
Schema = mongoose.Schema,
utils = require('../../lib/utils')


// Getters and Setters
var getTags = function (tags) {
Expand Down Expand Up @@ -77,6 +79,16 @@ TweetSchema.methods = {
user: user._id
});
this.save(cb);
},

removeComment: function (commentId, cb) {
var index = utils.indexof(this.comments, { id: commentId });
if (~index) {
this.comments.splice(index, 1);
} else {
return cb('not found');
}
this.save(cb);
}
};

Expand All @@ -86,7 +98,7 @@ TweetSchema.statics = {
// Load tweets
load: function (id, cb) {
this.findOne({ _id: id })
.populate('user', 'name email')
.populate('user', 'name email username')
.populate('comments.user')
.exec(cb);
},
Expand All @@ -96,7 +108,7 @@ TweetSchema.statics = {
var criteria = options.criteria || {};

this.find(criteria)
.populate('user', 'name')
.populate('user', 'name username')
.sort({'createdAt': -1})
.limit(options.perPage)
.skip(options.perPage * options.page)
Expand Down
2 changes: 1 addition & 1 deletion app/views/comments/comment.jade
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

if (comment && comment.user)
.comment
a(href="/users/"+ comment.user) #{comment.user}
a(href="/users/"+ comment.user)= comment.user
| : 
!= comment.body
.date= formatDate(tweet.createdAt, "%b %d, %Y at %I:%M %p")
10 changes: 9 additions & 1 deletion app/views/tweets/index.jade
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,11 @@ block content
.row
.col-lg-6.pull-left
h5
a(href="/users/"+tweet.user._id)=tweet.user.name
- var name = tweet.user.name ? tweet.user.name : tweet.user.username

//- if (tweet.user.name)
//- a(href="/users/"+tweet.user._id)=tweet.user.name
a(href="/users/"+tweet.user._id)= name
.col-lg-5.date
.pull-right
= formatDate(tweet.createdAt, "%b %d, %Y at %I:%M %p")
Expand All @@ -60,3 +64,7 @@ block content
include ../comments/comment
hr
include ../comments/form

- if (pages > 1)
ul.pagination
!= createPagination(pages, page)
17 changes: 17 additions & 0 deletions config/middlewares/authorization.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,21 @@ exports.tweet = {
};


/**
* Comment authorization routing middleware
*/

exports.comment = {
hasAuthorization: function (req, res, next) {
// if the current user is comment owner or article owner
// give them authority to delete
if (req.user.id === req.comment.user.id || req.user.id === req.article.user.id) {
next();
} else {
req.flash('info', 'You are not authorized');
res.redirect('/articles/' + req.article.id);
}
}
};


4 changes: 4 additions & 0 deletions config/routes.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
var async = require('async');




module.exports = function (app, passport, auth) {
var users = require('../app/controllers/users');
app.get('/login', users.login);
Expand Down Expand Up @@ -34,6 +37,7 @@ module.exports = function (app, passport, auth) {
//comment routes
var comments = require('../app/controllers/comments');
app.post('/tweets/:id/comments', auth.requiresLogin, comments.create);
app.get('/tweets/:id/comments', auth.requiresLogin, comments.create);
app.del('/tweets/:id/comments', auth.requiresLogin, comments.destroy);

/**
Expand Down
78 changes: 78 additions & 0 deletions lib/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/**
* Formats mongoose errors into proper array
*
* @param {Array} errors
* @return {Array}
* @api public
*/

exports.errors = function (errors) {
var keys = Object.keys(errors)
var errs = []

// if there is no validation error, just display a generic error
if (!keys) {
return ['Oops! There was an error']
}

keys.forEach(function (key) {
errs.push(errors[key].message)
})

return errs
}

/**
* Index of object within an array
*
* @param {Array} arr
* @param {Object} obj
* @return {Number}
* @api public
*/

exports.indexof = function (arr, obj) {
var index = -1; // not found initially
var keys = Object.keys(obj);
// filter the collection with the given criterias
var result = arr.filter(function (doc, idx) {
// keep a counter of matched key/value pairs
var matched = 0;

// loop over criteria
for (var i = keys.length - 1; i >= 0; i--) {
if (doc[keys[i]] === obj[keys[i]]) {
matched++;

// check if all the criterias are matched
if (matched === keys.length) {
index = idx;
return idx;
}
}
};
});
return index;
}

/**
* Find object in an array of objects that matches a condition
*
* @param {Array} arr
* @param {Object} obj
* @param {Function} cb - optional
* @return {Object}
* @api public
*/

exports.findByParam = function (arr, obj, cb) {
var index = exports.indexof(arr, obj)
if (~index && typeof cb === 'function') {
return cb(undefined, arr[index])
} else if (~index && !cb) {
return arr[index]
} else if (!~index && typeof cb === 'function') {
return cb('not found')
}
// else undefined is returned
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "nwitter",
"description": "A twitter demo app in nodejs",
"version": "0.0.1-49",
"version": "0.0.1-54",
"repository": "https://github.com/vinitcool76/node-twitter",
"private": false,
"author": "Vinit Kumar <vinit.kumar@changer.nl> (http://vinitkumar.me)",
Expand Down

0 comments on commit 52166f6

Please sign in to comment.