Skip to content
Browse files

Commenting

  • Loading branch information...
1 parent f35b74f commit 5c2310d749ad9a0ea8cff556a79aa131811d61f3 Anatoliy Chakkaev committed May 14, 2011
View
6 app/controllers/comments_controller.js
@@ -3,9 +3,13 @@ action('create', function () {
if (!Page.pathCommentable(path)) {
return error('/');
}
+ var author = req.session.twitter;
req.comment = new Comment({
+ author: author.name || author.screen_name,
text: req.body.text,
path: path,
+ twid: author.id,
+ pic: author.profile_image_url,
date: new Date()
});
req.comment.save(function (errors) {
@@ -14,7 +18,7 @@ action('create', function () {
} else {
flash('info', 'Comment created');
Page.index[path].loadComments(function () {
- redirect(path);
+ redirect(path + '#discussion');
});
}
});
View
24 app/models/comment.js
@@ -0,0 +1,24 @@
+Comment.prototype.timeAgo = function () {
+ var diffSec = (new Date - this.date) / 1000;
+ if (diffSec < 60) {
+ return 'few seconds ago';
+ } else if (diffSec < 3600) {
+ return p(Math.round(diffSec / 60), 'minute') + ' ago';
+ } else if (diffSec < 86400) {
+ return p(Math.round(diffSec / 3600), 'hour') + ' ago';
+ } else if (diffSec < 86400 * 7) {
+ return p(Math.round(diffSec / 86400), 'day') + ' ago';
+ } else if (diffSec < 86400 * 30.4) {
+ return p(Math.round(diffSec / (7 * 86400)), 'week') + ' ago';
+ } else {
+ return p(Math.round(diffSec / (30.4 * 86400)), 'month') + ' ago';
+ }
+
+ function p (n, word) {
+ if (n > 1) {
+ word += 's';
+ }
+ return n + ' ' + word;
+ }
+};
+
View
3 app/models/page.coffee
@@ -39,5 +39,6 @@ Page.pathCommentable = (path) ->
Page.prototype.loadComments = (done) ->
Comment.find {path: @path}, (err, comments) =>
- @comments = comments || []
+ @comments = (comments || []).sort (y, x) ->
+ x.date.getTime() - y.date.getTime()
done()
View
21 app/views/cms/page.ejs
@@ -4,8 +4,13 @@
<h2 id="discussion">Discussion</h2>
<% if (request.session.twitter) { %>
- <% form_for('', {action: path_to.comments}, function (form) { %>
- Leave comment as <%- request.session.twitter.screen_name %><br />
+ <% form_for('', {action: path_to.comments, class: 'new-comment'}, function (form) { %>
+ <div class="comment-header">
+ <% if (request.session.twitter && request.session.twitter.profile_image_url) { %>
+ <img src="<%= request.session.twitter.profile_image_url %>" class="avatar" />
+ <% } %>
+ Leave comment as <%- request.session.twitter.screen_name %><br />
+ </div>
<%- form.input('path', {type: 'hidden', value: page.path}) %>
<%- form.textarea('text') %><br />
<%- form.submit('Post comment') %>
@@ -16,8 +21,16 @@
<% page.comments.forEach(function (comment) { %>
<div class="comment">
- <%- comment.author %><br />
- <%- sanitize(comment.text) %>
+ <div class="comment-header">
+ <% if (comment.pic) { %>
+ <img src="<%= comment.pic %>" class="avatar" />
+ <% } %>
+ <%- comment.author %>
+ <%- comment.timeAgo() %>
+ </div>
+ <div class="comment-body">
+ <%- sanitize(comment.text) %>
+ </div>
</div>
<% }); %>
<% } %>
View
2 config/deploy.rb
@@ -30,7 +30,7 @@
desc "Symlink config files"
task :symlink_configs, :roles => :app do
- %w[app_config.yml].each do |f|
+ %w[app_config.yml twitter.yml].each do |f|
run "ln -sf #{shared_path}/config/#{f} #{release_path}/config/#{f}"
end
end
View
2 db/schema.js
@@ -55,6 +55,8 @@ var CommentSchema = new Schema;
CommentSchema.add({
path: { type: String, index: true },
author: { type: String },
+ twid: { type: Number },
+ pic: { type: String },
text: { type: String },
date: { type: Date }
});
View
31 public/stylesheets/style.css
@@ -146,3 +146,34 @@ code {
.content ul {
list-style-type: circle;
}
+
+img.avatar {
+ padding: 5px;
+ background: #fff;
+ border: 1px solid #aaa;
+}
+
+.comment {
+ border: 1px solid #e0e0e0;
+ padding: 5px;
+ background: #eee;
+ margin-bottom: 10px;
+}
+
+.comment-header {
+ text-align: middle;
+ margin-bottom: 10px;
+}
+.comment-header img.avatar {
+ vertical-align: middle;
+}
+
+form.new-comment textarea {
+ border: 1px solid #aaa;
+ width: 100%;
+ height: 100px;
+}
+
+form.new-comment {
+ margin-bottom: 10px;
+}

0 comments on commit 5c2310d

Please sign in to comment.
Something went wrong with that request. Please try again.