changelog shortlog tags branches files raw gz bz2 help

Mercurial > hg > werc / changeset: New version of comments module, must more reliable and polished.

changeset 373: 5f7e186f0c24
parent 372: d88136ca3cb9
child 374: fb1db1dee588
author: uriel@engel.se.cat-v.org
date: Fri, 30 Jan 2009 16:10:14 +0100
files: apps/bridge/app.rc apps/bridge/foot.tpl
description: New version of comments module, must more reliable and polished.
     1.1--- a/apps/bridge/app.rc	Fri Jan 30 15:18:07 2009 +0100
     1.2+++ b/apps/bridge/app.rc	Fri Jan 30 16:10:14 2009 +0100
     1.3@@ -1,30 +1,98 @@
     1.4 comment_file_types=(md html)
     1.5-fn bridge_init {
     1.6-    if(! ~ $#enable_comments 0 && ! ~ `{ls $local_path.$comment_file_types >[2]/dev/null|wc -l} 0) {
     1.7-        ll_add handlers_body_foot template apps/bridge/foot.tpl
     1.8-        if(get_post_args comment_text) {
     1.9-            d=`{date -n} # FIXME Obvious race
    1.10-            d=$local_path^'_werc/comments/'^$d/
    1.11+
    1.12+fn conf_enable_comments {
    1.13+    if(~ $1 -n) {
    1.14+        allow_new_user_comments=yes
    1.15+        shift
    1.16+    }
    1.17+    enable_comments=yes
    1.18+    groups_allowed_comments=$*
    1.19+}
    1.20 
    1.21-            u=$logged_user
    1.22-            if(~ $#logged_user 0) {
    1.23-                get_post_args comment_user_name comment_user_password
    1.24-                # XXX Should do this too if user not in required group
    1.25-                if(! login_user $comment_user_name $comment_user_password) {
    1.26-                    u=$comment_user_name':'$comment_user_password
    1.27-                    d=$d^'_pending'
    1.28-                }
    1.29-                if not
    1.30-                    u = $logged_user
    1.31-            }
    1.32+fn bridge_init {
    1.33+    if(~ $#enable_comments 1) {
    1.34+    
    1.35+        cdir=$sitedir$req_path'_werc/comments'
    1.36+        if(test -d $cdir)
    1.37+            ll_add handlers_body_foot display_comments $cdir
    1.38 
    1.39-            umask 002
    1.40-            if(mkdir -m 775 -p $d) { # Rudimentary perm checking
    1.41-                echo $u > $d/user
    1.42-                echo $comment_text > $d/body
    1.43-            }
    1.44+        if({ check_user $groups_allowed_comments || {~ $#logged_user 0 && ! ~ $#allow_new_user_comments 0} } && ! ~ `{ls $local_path.$comment_file_types >[2]/dev/null|wc -l} 0) {
    1.45+            ll_add handlers_body_foot template apps/bridge/foot.tpl
    1.46+
    1.47+            if(~ $REQUEST_METHOD POST && mk_new_comment $cdir)
    1.48+                post_redirect $base_url^$post_arg_document_uri
    1.49+            if not
    1.50+                saved_comment_text=$post_arg_comment_text
    1.51         }
    1.52     }
    1.53+}
    1.54 
    1.55+fn validate_new_user {
    1.56+    usr=$1; pass=$2; pass2=$3
    1.57+    _status=()
    1.58+
    1.59+    if(~ $"usr '' || ! echo $usr |sed 1q|grep -s '^'$allowed_user_chars'+$')
    1.60+        _status='Requested user name is invalid, must match: '^$allowed_user_chars^'+'
    1.61+    if not if(test -d etc/users/$usr)
    1.62+        _status='Sorry, user name '''^$usr^''' already taken, please pick a different one.'
    1.63+
    1.64+    if(~ $"pass '' || ! ~ $"pass $"pass2)
    1.65+        _status=($_status 'Provided passwords don''t match.')
    1.66+
    1.67+    status=$_status
    1.68 }
    1.69 
    1.70+
    1.71+fn mk_new_comment {
    1.72+    _status=()
    1.73+    dir=$1
    1.74+    if(~ $"post_arg_comment_text '')
    1.75+        _status='Provide a comment!'
    1.76+    if not if(~ $#logged_user 0) {
    1.77+        if(! ~ $#allow_new_user_comments 0) {
    1.78+            if(validate_new_user $"post_arg_comment_user $post_arg_comment_passwd $post_arg_comment_passwd2) {
    1.79+                u=$post_arg_comment_user':'$post_arg_comment_passwd
    1.80+                dir=$cdir^'_pending'
    1.81+                notify_notes='Saved comment and registration info, they will be enabled when approved by an admin.'
    1.82+                ll_add handlers_body_foot notices_handler
    1.83+            }
    1.84+            if not
    1.85+                _status=$status
    1.86+        }
    1.87+        if not
    1.88+            _status='You need to log in to comment.'
    1.89+    }
    1.90+    if not if(check_user $groups_allowed_comments)
    1.91+        u=$logged_user
    1.92+    if not
    1.93+        _status='You are not a memeber of a group allowed to comment.'
    1.94+
    1.95+    if(~ $#_status 0) {
    1.96+        umask 002
    1.97+
    1.98+        dir=$dir'/'`{date -n} # FIXME Obvious race
    1.99+        mkdir -m 775 -p $dir &&
   1.100+            echo $u > $dir/user &&
   1.101+            echo $post_arg_comment_text > $dir/body
   1.102+        _s=$status
   1.103+        if(! ~ $"_s '') {
   1.104+           dprint 'ERROR XXX: Could not create comment: ' $_s 
   1.105+            _status='Could not post comment due internal error, sorry.'
   1.106+        }
   1.107+    }
   1.108+    notify_errors=$_status
   1.109+    status=$_status
   1.110+}
   1.111+
   1.112+fn display_comments {
   1.113+    echo '<hr /><h2>Comments</h2>'
   1.114+
   1.115+    for(c in `{ls $*/}) {
   1.116+        if(test -s $c/body) {
   1.117+            echo '<div>'
   1.118+            sed 's!.+!<b>By: <i>&</i></b><br />!' < $c/user
   1.119+            cat $c/body | escape_html | sed 's,$,<br />,'
   1.120+            echo '<hr /></div>'
   1.121+        }
   1.122+     }
   1.123+}
     2.1--- a/apps/bridge/foot.tpl	Fri Jan 30 15:18:07 2009 +0100
     2.2+++ b/apps/bridge/foot.tpl	Fri Jan 30 16:10:14 2009 +0100
     2.3@@ -1,26 +1,27 @@
     2.4-% cdir = $local_path^'_werc/comments'
     2.5-% if(test -d $cdir) { 
     2.6-    <hr /><h2>Comments</h2>
     2.7-%    for(c in `{ls $cdir/}) {
     2.8-        <div><b>By: <i> 
     2.9-%           cat $c/user
    2.10-            </i></b>
    2.11-            <br />
    2.12-%           cat $c/body | escape_html | sed 's,$,<br />,'
    2.13-        <hr /></div>
    2.14-%    }
    2.15-% }
    2.16-
    2.17 <hr />
    2.18 
    2.19+% notices_handler
    2.20+% # XXX should post to bridge_post or similar
    2.21 <form action="" method="post">
    2.22-    <textarea name="comment_text" id="comment_text" cols="80" rows="16"></textarea>
    2.23+    <textarea name="comment_text" id="comment_text" cols="80" rows="16">%($"saved_comment_text%)</textarea>
    2.24     <br />
    2.25+    <input type="hidden" name="document_uri" value="%($req_path%)" />
    2.26     <input type="submit" name="post_comment" value="Post a comment" />
    2.27-% if(! check_user) {
    2.28-    <label>User: <input type="text" name="comment_user_name" value="" /></label>
    2.29-    <label>Password: <input type="password" name="comment_user_password" value="" /></label>
    2.30-    <div style="font-size: 70%">If you are not registered enter your desired user/password and your account will be created when your comment is approved.</div>
    2.31+
    2.32+% if(~ $#logged_user 0 && ! ~ $#allow_new_user_comments 0) {
    2.33+    <label>New user name:
    2.34+        <input type="text" name="comment_user" value="%($"post_arg_comment_user%)" />
    2.35+    </label>
    2.36+
    2.37+    <label>Password:
    2.38+        <input type="password" name="comment_passwd" value="" />
    2.39+    </label>
    2.40+
    2.41+    <label>Repeat password:
    2.42+        <input type="password" name="comment_passwd2" value="" />
    2.43+    </label>
    2.44+    <div style="font-size: 70%">
    2.45+    Enter your desired user name/password and after your comment has been reviewed by an addmin it will be posted and your account will be enabled. If you are already registered please <a href="/_users/login">login</a> before posting.
    2.46+    </div>
    2.47 % }
    2.48 </form>
    2.49-