-
Notifications
You must be signed in to change notification settings - Fork 314
/
post.js
127 lines (104 loc) · 2.93 KB
/
post.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
var keystone = require('keystone');
var Post = keystone.list('Post');
var PostComment = keystone.list('PostComment');
exports = module.exports = function (req, res) {
var view = new keystone.View(req, res);
var locals = res.locals;
// Init locals
locals.section = 'blog';
locals.filters = {
post: req.params.post,
};
// Load the current post
view.on('init', function (next) {
var q = Post.model.findOne({
state: 'published',
key: locals.filters.post,
}).populate('author categories');
q.exec(function (err, result) {
locals.post = result;
next(err);
});
});
// Load other posts
view.on('init', function (next) {
var q = Post.model.find().where('state', 'published').sort('-publishedDate').populate('author').limit(4);
q.exec(function (err, results) {
locals.posts = results;
next(err);
});
});
// Load comments on the Post
view.on('init', function (next) {
PostComment.model.find()
.where('post', locals.post)
.where('commentState', 'published')
.where('author').ne(null)
.populate('author', 'name photo')
.sort('-publishedOn')
.exec(function (err, comments) {
if (err) return res.err(err);
if (!comments) return res.notfound('Post comments not found');
locals.comments = comments;
next();
});
});
// Create a Comment
view.on('post', { action: 'comment.create' }, function (next) {
var newComment = new PostComment.model({
state: 'published',
post: locals.post.id,
author: locals.user.id,
});
var updater = newComment.getUpdateHandler(req);
updater.process(req.body, {
fields: 'content',
flashErrors: true,
logErrors: true,
}, function (err) {
if (err) {
validationErrors = err.errors;
} else {
req.flash('success', 'Your comment was added.');
return res.redirect('/blog/post/' + locals.post.key + '#comment-id-' + newComment.id);
}
next();
});
});
// Delete a Comment
view.on('get', { remove: 'comment' }, function (next) {
if (!req.user) {
req.flash('error', 'You must be signed in to delete a comment.');
return next();
}
PostComment.model.findOne({
_id: req.query.comment,
post: locals.post.id,
})
.exec(function (err, comment) {
if (err) {
if (err.name === 'CastError') {
req.flash('error', 'The comment ' + req.query.comment + ' could not be found.');
return next();
}
return res.err(err);
}
if (!comment) {
req.flash('error', 'The comment ' + req.query.comment + ' could not be found.');
return next();
}
if (comment.author != req.user.id) {
req.flash('error', 'Sorry, you must be the author of a comment to delete it.');
return next();
}
comment.commentState = 'archived';
comment.save(function (err) {
if (err) return res.err(err);
req.flash('success', 'Your comment has been deleted.');
return res.redirect('/blog/post/' + locals.post.key);
});
});
});
// Render the view
view.render('post');
}