Permalink
Browse files

Implemented comments sending/subscribing/publising.

  • Loading branch information...
1 parent 93bad6b commit 6822e8d029b81e373f83b9da0d4a3b725e161977 @pedrofranceschi pedrofranceschi committed Dec 11, 2010
Showing with 89 additions and 4 deletions.
  1. +1 −0 README.markdown
  2. +25 −0 blogode.js
  3. +12 −0 lib/comments.js
  4. +2 −0 views/layout.ejs
  5. +49 −4 views/posts/show.ejs
View
@@ -7,6 +7,7 @@ Dependencies
* npm install mysql
* npm install express
* npm install ejs
+* npm install faye
Maintainers
-----------
View
@@ -1,6 +1,7 @@
var express = require("express")
var sys = require("sys");
var app = express.createServer();
+var faye = require('faye');
app.configure(function() {
app.use(express.logger());
@@ -16,6 +17,11 @@ app.configure(function() {
});
});
+bayeux = new faye.NodeAdapter({
+ mount: '/faye',
+ timeout: 45
+});
+
var posts = require('./lib/posts');
var users = require('./lib/users');
var comments = require('./lib/comments');
@@ -165,5 +171,24 @@ app.get("/:id", function(req, res){
});
});
+app.post("/:id/comments/save", function(req, res){
+ // saves a comment (for a post)
+
+ if(!req.param('id') || !req.param('author_name') || !req.param('author_email') || !req.param('comment')) {
+ return res.send("Missing parameters.");
+ }
+
+ comments.saveComment(req.param('id'), escape(req.param('author_name')), escape(req.param('author_email')), escape(req.param('comment')), function(commentId) {
+ bayeux.getClient().publish('/' + req.param('id') + '/comments/bayeux', {
+ id: commentId,
+ author_name: req.param('author_name'),
+ comment: req.param('comment')
+ });
+
+ return res.send("OK");
+ });
+});
+
+bayeux.attach(app);
app.listen(3000);
console.log("Server on port %s", app.address().port);
View
@@ -13,4 +13,16 @@ exports.getCommentsOfPost = function(postId, callback) {
callback(results);
});
});
+}
+
+exports.saveComment = function(postId, authorName, authorEmail, comment, callback) {
+ database.getDatabaseConnection(function (mysql_client) {
+ mysql_client.query("INSERT INTO comments (post_id, author_name, author_email, comment) VALUES ('" + escape(postId) + "', '" + escape(authorName) + "', '" + escape(authorEmail) + "', '" + escape(comment) + "');", function (error, results, fields) {
+ if(error) {
+ throw "Error inserting comment: " + error;
+ }
+
+ callback(results.insertId);
+ });
+ });
}
View
@@ -1,6 +1,8 @@
<html>
<head>
<title>Blogode</title>
+ <script src="/faye.js"></script>
+ <script src="http://code.jquery.com/jquery-1.4.4.js"></script>
</head>
<body>
<h1>HEADER</h1><br/>
View
@@ -1,10 +1,55 @@
+<script>
+
+$(document).ready(function() {
+ $("#submit").click(function() {
+ $.ajax({
+ type: "POST",
+ url: "/<%= post.id %>/comments/save",
+ data: { author_name: $("input#author_name").val(), author_email: $("input#author_email").val(), comment: $("textarea#comment").val() },
+ success: function() {
+ }
+ });
+ return false;
+ });
+});
+
+var client = new Faye.Client('/faye', {
+ timeout: 120
+});
+
+client.subscribe('/<%= post.id %>/comments/bayeux', function(response) {
+ $("#comments_list").append("<div id=\"comment_" + response.id + "\"> " +
+ " <h4>" + unescape(response.author_name) + "</h4> " +
+ " <p>" + unescape(response.comment) + "</p> " +
+ "</div>");
+});
+
+</script>
+
<h1><%= unescape(post.title) %></h1>
<p>by <%= post.user_name %></p>
<p><%- unescape(post.body) %></p>
+<br/>
<h2>Comments</h2>
-<% for (var i=0; i < comments.length; i++) { %>
- <h4><%= comments[i].author_name %></h4>
- <p><%= comments[i].comment %></p>
-<% } %>
+<div id="comments_list">
+ <% for (var i=0; i < comments.length; i++) { %>
+ <div id="comment_<%= i %>">
+ <h4><%= unescape(comments[i].author_name) %></h4>
+ <p><%= unescape(comments[i].comment) %></p>
+ </div>
+ <% } %>
+</div>
+
+<br/>
+<h2>New comment</h2>
+
+<form>
+ <input type="text" id="author_name" placeholder="Your name" size="41"/><br/>
+ <input type="text" id="author_email" placeholder="Your e-mail" size="41"/><br/><br/>
+
+ <textarea id="comment" cols="30" rows="5" placeholder="Your comment"></textarea><br/><br/>
+
+ <input type="submit" id="submit"/>
+</form>

0 comments on commit 6822e8d

Please sign in to comment.