Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

[minor] Added autolink feature, restrictions, and more functionality …

…and performance
  • Loading branch information...
commit 1ee49d7e0306d536b829c160d1e98b75c22be89c 1 parent f6578d7
Alejandro Morales authored November 27, 2011
23  app.js
@@ -7,9 +7,11 @@ var express = require('express'),
7 7
   express.session({ secret: 'keyboard cat' })),
8 8
     nowjs = require('now'),
9 9
     groups =[],
10  
-    ID=require('./lib/ObjectID').ObjectId;
  10
+    escape = require('./lib/autolink'),
  11
+    ID=require('./lib/ObjectID').ObjectId; // for testing purpose
11 12
 
12  
-var comments = require('./lib/comments');
  13
+var comments = require('./lib/comments'),
  14
+    Comment = require('./lib/comments').Comment;
13 15
 /* A little hack for correct handle of session */
14 16
   
15 17
 app.configure(function(){
@@ -89,11 +91,16 @@ everyone.now.joinRoom = function(req){
89 91
      nowjs.getGroup(req.url).addUser(this.user.clientId);
90 92
 }
91 93
 everyone.now.sendComment = function(req) {
92  
-  /* { url:'http://numbus.co:8080/f/prueba/h', 
93  
-    comment: { authorId: "4ebef2c27f21bd298a000000", comment: "YOUR COMMENT",time: Date.now(), 
94  
-    parent: "URL parent or comment parent as reply" } }  */
95  
-    comments.newComment(req, function(e,d){
96  
-      nowjs.getGroup(req.url).now.receiveMessage(req.data.authorId,util.toStaticHTML(req.data.comment));
  94
+    var self = this;
  95
+    req = new Comment(req);
  96
+    req.save(function(e,d){
  97
+      if (d) {
  98
+        nowjs.getGroup(req.url).now.receiveMessage(req.data.authorId,escape.autoLink(util.toStaticHTML(req.data.comment)));
  99
+      } else {
  100
+        nowjs.getClient(self.user.clientId, function(){        
  101
+          this.now.receiveMessage('SERVER:', req.validateC());
  102
+        });
  103
+      }
97 104
     });
98 105
 }
99 106
 everyone.now.fetchBySite = function(req,res){
@@ -120,4 +127,4 @@ everyone.now.edit = function(req){
120 127
   comments.edit(req,function(e,d){
121 128
     nowjs.getGroup(req.url).now.onEditMessage(req.data.authorId,util.toStaticHTML(req.data.comment));
122 129
   });
123  
-}
  130
+}
784  lib/autolink.js
... ...
@@ -0,0 +1,784 @@
100  lib/comments.js
@@ -16,6 +16,13 @@ var redis = require('./redis').redis,
16 16
  * @id that is the page identifier for example: http://test.com/d/i/r/topath the id would be dirtopath
17 17
  * @parent is the hostname for of the page: i.e test.com
18 18
 */
  19
+var Comment = module.exports.Comment = function(comment){
  20
+  this.url = comment.url || 'home';
  21
+  this.data = comment.data || null;
  22
+  this.author = comment.data.author || 'Anon';
  23
+  this.date = new Date(Date.now());
  24
+  return this;
  25
+}
19 26
 var threadKey = module.exports.newComment = function(id,parent) {
20 27
     return NAMESPACE + ':' + parent + ':' + id;
21 28
 }
@@ -40,11 +47,15 @@ var convertURL = function(_url) {
40 47
 var slOff = function(_url){
41 48
   return _url.replace(/[^\w \xC0-\xFF]/g,'') ;
42 49
 }
  50
+var toJSONString = function(val){
  51
+  val.comment = util.toStaticHTML(val.comment);
  52
+  return JSON.stringify(val)
  53
+};
43 54
 
44 55
 /* Delete duplicate members */
45 56
 Array.prototype.unique = function () {
46 57
   var r = new Array();
47  
-  o:for(var i = 0, n = this.length; i < n; i++){
  58
+  o: for(var i = 0, n = this.length; i < n; i++){
48 59
     for(var x = 0, y = r.length; x < y; x++) {
49 60
       if(r[x]==this[i]) {
50 61
         continue o;
@@ -68,6 +79,92 @@ Array.prototype.unique = function () {
68 79
     comment: { authorId: "4ebef2c27f21bd298a000000", comment: "YOUR COMMENT",time: Date.now(), 
69 80
     parent: "URL parent or comment parent as reply" } } 
70 81
 */
  82
+Comment.prototype.validate = function(res){
  83
+  var log =[]
  84
+  try { 
  85
+    if (/^(http:\/\/)([\w]+\.){1,}[A-Z]{2,}\b/gi.test(this.url)){} else { log.push('Invalid URL');}
  86
+    if (this.data.comment.length<50) { log.push('The comment has to be at least 50 chars long')}
  87
+    if (!this.data.authorId) {log.push('I need a username')}
  88
+    if (log.length===0) {
  89
+      return true
  90
+    } else {
  91
+     return null;
  92
+    }
  93
+  } catch (e){
  94
+    return null;
  95
+  }
  96
+}
  97
+Comment.prototype.validateC = function(res){
  98
+  var log =[]
  99
+  try { 
  100
+    if (/^(http:\/\/)([\w]+\.){1,}[A-Z]{2,}\b/gi.test(this.url)){} else { log.push('Invalid URL');}
  101
+    if (this.data.comment.length<50) { log.push('The comment has to be at least 50 chars long')}
  102
+    if (!this.data.authorId) {log.push('I need a username')}
  103
+    if (log.length===0) {
  104
+      return true
  105
+    } else {
  106
+     return log;
  107
+    }
  108
+  } catch (e){
  109
+    return e;
  110
+  }
  111
+}
  112
+Comment.prototype.save = function(res){    
  113
+  var self = this;
  114
+    if (self.validate() != null) {
  115
+      var threadId = convertURL(this.url).slug || 'home',
  116
+          parent = convertURL(this.url).host;
  117
+          thread = threadKey(threadId,parent),
  118
+          redis.exists(thread, function(e,d){
  119
+            if (e) res('No ok: ' + e,null);
  120
+            redis.hincrby(thread,'id',1,function(e4,d4){
  121
+              redis.hincrby('u:' + self.data.authorId, 'id',1, function(error, data){
  122
+                 /* Setting up floating points */
  123
+                self.data.lid = data, self.data.id = d4;self.data.url = url(self.url).pathname;
  124
+                redis.multi()
  125
+                     .HSET('u:' + self.data.authorId, data, toJSONString(self.data))
  126
+                     .HSET(thread,d4, toJSONString(self.data))
  127
+                     .lpush(shortTKeys(parent),url(self.url).pathname)
  128
+                     .exec(function(e2,d2){
  129
+                       if (e2) res('No ok: ' + err, null)
  130
+                       res(null, d2)
  131
+                     });
  132
+              });
  133
+            });
  134
+          })
  135
+    } else {
  136
+        res(this.validate(), null)
  137
+    } 
  138
+}
  139
+Comment.prototype.delete = function(res){
  140
+  if (this.validate()){
  141
+    var threadId = convertURL(this.url).slug || 'home',
  142
+          parent = convertURL(this.url).host;
  143
+          thread = threadKey(threadId,parent),
  144
+          comment = this.data;
  145
+      redis.multi()
  146
+           .HDEL(thread, comment.id)
  147
+           .HDEL('u:'+ comment.authorId, comment.lid)
  148
+           .exec(function(e,d2){
  149
+             if (e) {
  150
+               res('No ok: ' + e, null)
  151
+             } else {
  152
+               res(null, 'ok')
  153
+             }
  154
+           });
  155
+  } else {
  156
+    res('Comment bad former')
  157
+  }
  158
+}
  159
+
  160
+/*
  161
+var p = { url:'http://numbus.co:8080/f/prueba/h', 
  162
+    data: { authorId: "4ebef2c27f21bd298a000000", comment: "YOUR COMMENT",time: Date.now(), 
  163
+    parent: "URL parent or comment parent as reply" } } 
  164
+
  165
+var c = new Comment(p)
  166
+console.log(c);
  167
+*/  
71 168
 var newComment = module.exports.newComment = function(req,res) {
72 169
     var O = req;    
73 170
     /* the RegExp was a const but everytwo presented a bug */
@@ -75,7 +172,6 @@ var newComment = module.exports.newComment = function(req,res) {
75 172
       var threadId = convertURL(O.url).slug || 'home',
76 173
           parent = convertURL(O.url).host;
77 174
           thread = threadKey(threadId,parent),
78  
-          toJSONString = function(val){val.comment = util.toStaticHTML(val.comment);return JSON.stringify(val)};
79 175
           redis.exists(thread, function(e,d){
80 176
             if (e) res('No ok: ' + e,null);
81 177
             redis.hincrby(thread,'id',1,function(e4,d4){
9  package.json
... ...
@@ -1,9 +1,10 @@
1 1
 {
2  
-    "name": "application-name"
3  
-  , "version": "0.0.1"
  2
+    "name": "ncomm"
  3
+  , "version": "0.2.1"
4 4
   , "private": true
5 5
   , "dependencies": {
6 6
       "express": "2.5.0"
7  
-    , "jade": ">= 0.0.1"
  7
+    , "jade": ">= 0.0.1",
  8
+      "now":">=0.7"
8 9
   }
9  
-}
  10
+}
2  public/index.html
@@ -78,7 +78,7 @@
78 78
       tmpl.render({ data: data })
79 79
       );
80 80
       var end = +new Date();
81  
-      console.log( end-start, "ms" );
  81
+      console.log( (end-start)+ "ms" );
82 82
     }
83 83
     window.setInterval(render, 1000);
84 84
     render();
20  views/form.jade
@@ -5,6 +5,9 @@ form(action="javascript:alert('success!');",id="form")
5 5
   label Comment:
6 6
   textarea(name="comment",id="comment")
7 7
   input(type="submit",value="submit")
  8
+
  9
+#comments
  10
+
8 11
 script
9 12
   var p = document.getElementById('url');
10 13
   p.value = window.location.href;
@@ -23,7 +26,7 @@ script
23 26
                   if ( (comment.authorName || comment.authorId) === "#{user.id}") {
24 27
                      NODE = '> <a href="#" id="edit">Edit </a> | <a href="#" id="delete">Delete</a></div>' 
25 28
                   }
26  
-                  $('body').append("<br><div id='comment" + i +"'><label>"+ (comment.authorName || comment.authorId) +  ":</label><p> " + comment.comment + "</p><input type='hidden' id='commentId"+ i+"' value='"+JSON.stringify(r[m])+"'>" + (NODE || '</div>') );
  29
+                  $('#comments').prepend("<br><div id='comment" + i +"'><label>"+ (comment.authorName || comment.authorId) +  ":</label><p> " + comment.comment + "</p><input type='hidden' id='commentId"+ i+"' value='"+ JSON.stringify(r[m])+"'>" + (NODE || '</div>') );
27 30
                 }
28 31
                 i++;
29 32
               }
@@ -31,13 +34,18 @@ script
31 34
               console.log(e)
32 35
             }
33 36
           });
  37
+          // TODO
  38
+          $('.edit').click(function(){
  39
+            console.log(this.closest('div'));
  40
+          });
34 41
           now.receiveMessage = function(name, message){
35  
-            if (name === "#{user.id}") {
36  
-              $('body').append("<br>" + name + ": " + message + ' <a href="#" id="edit">Edit </a> | <a href="#" id="delete">Delete</a>');   
37  
-            } else {
38  
-              $('body').append("<br>" + name + ": " + message);
  42
+             var NODE ='';
  43
+             var i = (parseFloat($('#comments div')[0].id.substr(7)) + 1);
  44
+             if ( name === "#{user.id}") {
  45
+               NODE = '> <a href="#" class="edit">Edit </a> | <a href="#" class="delete">Delete</a></div>' 
  46
+              }
  47
+              $('#comments').prepend("<br><div id='comment" + i +"'><label>"+ name +  ":</label><p> " + message + "</p><input type='hidden' id='commentId"+ "' value='"+message+"'" + (NODE || '</div>') );
39 48
             }
40  
-          }
41 49
       now.joinRoom({url: document.getElementById('url').value,id:document.getElementById('userId').value});
42 50
      
43 51
     })
2  views/layout.jade
@@ -3,6 +3,6 @@ html
3 3
   head
4 4
     title= title
5 5
     link(rel='stylesheet', href='/stylesheets/style.css')
6  
-    script(type="text/javascript" ,src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js")
  6
+    script(type="text/javascript" ,src="//numbus.co/assets/javascripts/jquery-1.5.1.min.js")
7 7
     script(src="/nowjs/now.js")
8 8
   body!= body

0 notes on commit 1ee49d7

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