Skip to content

Commit 52166f6

Browse files
author
Vinit Kumar
committed
Merge pull request #68 from vinitkumar/cleanup
Cleanup
2 parents 565c6ea + f669920 commit 52166f6

File tree

10 files changed

+148
-12
lines changed

10 files changed

+148
-12
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
node_modules/*
2+
npm-debug.log

app/controllers/comments.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,18 @@
11
var mongoose = require('mongoose');
2+
var utils = require('../../lib/utils');
3+
4+
/**
5+
* Load comment
6+
*/
7+
8+
exports.load = function (req, res, next, id) {
9+
var tweet = req.tweet
10+
utils.findByParam(tweet.comments, { id: id }, function (err, comment) {
11+
if (err) return next(err);
12+
req.comment = comment;
13+
next();
14+
});
15+
};
216

317
// ### Create Comment
418
exports.create = function (req, res) {

app/controllers/tweets.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -85,21 +85,23 @@ exports.destroy = function (req, res) {
8585
};
8686

8787

88+
8889
exports.index = function (req, res) {
89-
var page = req.param('page') > 0 ? req.param('page'):0;
90-
var perPage = 15;
90+
var page = (req.param('page') > 0 ? req.param('page'):1) - 1;
91+
var perPage = 15;
9192
var options = {
9293
perPage: perPage,
9394
page: page
9495
};
95-
Tweet.list(options, function (err, tweets) {
96+
97+
Tweet.list(options, function(err, tweets) {
9698
if (err) return res.render('500');
9799
Tweet.count().exec(function (err, count) {
98100
res.render('tweets/index', {
99-
title: 'List of tweets',
101+
title: 'List of Tweets',
100102
tweets: tweets,
101-
page: page,
102-
pages: count/perPage
103+
page: page + 1,
104+
pages: Math.ceil(count / perPage)
103105
});
104106
});
105107
});

app/models/tweets.js

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
var mongoose = require('mongoose'),
22
env = process.env.NODE_ENV || 'development',
33
config = require('../../config/config')[env],
4-
Schema = mongoose.Schema;
4+
Schema = mongoose.Schema,
5+
utils = require('../../lib/utils')
6+
57

68
// Getters and Setters
79
var getTags = function (tags) {
@@ -77,6 +79,16 @@ TweetSchema.methods = {
7779
user: user._id
7880
});
7981
this.save(cb);
82+
},
83+
84+
removeComment: function (commentId, cb) {
85+
var index = utils.indexof(this.comments, { id: commentId });
86+
if (~index) {
87+
this.comments.splice(index, 1);
88+
} else {
89+
return cb('not found');
90+
}
91+
this.save(cb);
8092
}
8193
};
8294

@@ -86,7 +98,7 @@ TweetSchema.statics = {
8698
// Load tweets
8799
load: function (id, cb) {
88100
this.findOne({ _id: id })
89-
.populate('user', 'name email')
101+
.populate('user', 'name email username')
90102
.populate('comments.user')
91103
.exec(cb);
92104
},
@@ -96,7 +108,7 @@ TweetSchema.statics = {
96108
var criteria = options.criteria || {};
97109

98110
this.find(criteria)
99-
.populate('user', 'name')
111+
.populate('user', 'name username')
100112
.sort({'createdAt': -1})
101113
.limit(options.perPage)
102114
.skip(options.perPage * options.page)

app/views/comments/comment.jade

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
if (comment && comment.user)
33
.comment
4-
a(href="/users/"+ comment.user) #{comment.user}
4+
a(href="/users/"+ comment.user)= comment.user
55
| : 
66
!= comment.body
77
.date= formatDate(tweet.createdAt, "%b %d, %Y at %I:%M %p")

app/views/tweets/index.jade

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,11 @@ block content
4343
.row
4444
.col-lg-6.pull-left
4545
h5
46-
a(href="/users/"+tweet.user._id)=tweet.user.name
46+
- var name = tweet.user.name ? tweet.user.name : tweet.user.username
47+
48+
//- if (tweet.user.name)
49+
//- a(href="/users/"+tweet.user._id)=tweet.user.name
50+
a(href="/users/"+tweet.user._id)= name
4751
.col-lg-5.date
4852
.pull-right
4953
= formatDate(tweet.createdAt, "%b %d, %Y at %I:%M %p")
@@ -60,3 +64,7 @@ block content
6064
include ../comments/comment
6165
hr
6266
include ../comments/form
67+
68+
- if (pages > 1)
69+
ul.pagination
70+
!= createPagination(pages, page)

config/middlewares/authorization.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,21 @@ exports.tweet = {
3434
};
3535

3636

37+
/**
38+
* Comment authorization routing middleware
39+
*/
40+
41+
exports.comment = {
42+
hasAuthorization: function (req, res, next) {
43+
// if the current user is comment owner or article owner
44+
// give them authority to delete
45+
if (req.user.id === req.comment.user.id || req.user.id === req.article.user.id) {
46+
next();
47+
} else {
48+
req.flash('info', 'You are not authorized');
49+
res.redirect('/articles/' + req.article.id);
50+
}
51+
}
52+
};
53+
3754

config/routes.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
var async = require('async');
22

3+
4+
5+
36
module.exports = function (app, passport, auth) {
47
var users = require('../app/controllers/users');
58
app.get('/login', users.login);
@@ -34,6 +37,7 @@ module.exports = function (app, passport, auth) {
3437
//comment routes
3538
var comments = require('../app/controllers/comments');
3639
app.post('/tweets/:id/comments', auth.requiresLogin, comments.create);
40+
app.get('/tweets/:id/comments', auth.requiresLogin, comments.create);
3741
app.del('/tweets/:id/comments', auth.requiresLogin, comments.destroy);
3842

3943
/**

lib/utils.js

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/**
2+
* Formats mongoose errors into proper array
3+
*
4+
* @param {Array} errors
5+
* @return {Array}
6+
* @api public
7+
*/
8+
9+
exports.errors = function (errors) {
10+
var keys = Object.keys(errors)
11+
var errs = []
12+
13+
// if there is no validation error, just display a generic error
14+
if (!keys) {
15+
return ['Oops! There was an error']
16+
}
17+
18+
keys.forEach(function (key) {
19+
errs.push(errors[key].message)
20+
})
21+
22+
return errs
23+
}
24+
25+
/**
26+
* Index of object within an array
27+
*
28+
* @param {Array} arr
29+
* @param {Object} obj
30+
* @return {Number}
31+
* @api public
32+
*/
33+
34+
exports.indexof = function (arr, obj) {
35+
var index = -1; // not found initially
36+
var keys = Object.keys(obj);
37+
// filter the collection with the given criterias
38+
var result = arr.filter(function (doc, idx) {
39+
// keep a counter of matched key/value pairs
40+
var matched = 0;
41+
42+
// loop over criteria
43+
for (var i = keys.length - 1; i >= 0; i--) {
44+
if (doc[keys[i]] === obj[keys[i]]) {
45+
matched++;
46+
47+
// check if all the criterias are matched
48+
if (matched === keys.length) {
49+
index = idx;
50+
return idx;
51+
}
52+
}
53+
};
54+
});
55+
return index;
56+
}
57+
58+
/**
59+
* Find object in an array of objects that matches a condition
60+
*
61+
* @param {Array} arr
62+
* @param {Object} obj
63+
* @param {Function} cb - optional
64+
* @return {Object}
65+
* @api public
66+
*/
67+
68+
exports.findByParam = function (arr, obj, cb) {
69+
var index = exports.indexof(arr, obj)
70+
if (~index && typeof cb === 'function') {
71+
return cb(undefined, arr[index])
72+
} else if (~index && !cb) {
73+
return arr[index]
74+
} else if (!~index && typeof cb === 'function') {
75+
return cb('not found')
76+
}
77+
// else undefined is returned
78+
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "nwitter",
33
"description": "A twitter demo app in nodejs",
4-
"version": "0.0.1-49",
4+
"version": "0.0.1-54",
55
"repository": "https://github.com/vinitcool76/node-twitter",
66
"private": false,
77
"author": "Vinit Kumar <[email protected]> (http://vinitkumar.me)",

0 commit comments

Comments
 (0)