Commits

Torbjorn Tornkvist  committed b449cbc

Fixing adding of comments.

  • Participants
  • Parent commits a974921

Comments (0)

Files changed (5)

File src/redhot2.app.src

          ,{external_port, 80}
 	 ,{log_dir, "/tmp"}
 	 ,{doc_root, "./www"}
+         ,{mail_from, "MAILADDRESS"}
          ,{authors, [{"http://etnt.myopenid.com/" % Claimed OpenID
                      ,"tobbe"                     % Nickname
                      ,"tobbe@tornkvist.org"}      % Email,

File src/redhot2.erl

          , twitter_passwd/0
          , log_dir/0
          , top_dir/0
+         , is_valid_author/2
          , author2email/1
          , maybe_nick/1
          , to_latin1/1
     filename:join(["/"|lists:reverse(tl(lists:reverse(string:tokens(filename:dirname(code:which(?MODULE)),"/"))))]).
 
 
+is_valid_author(Author, Who) when is_binary(Author) orelse is_binary(Who) -> 
+    is_valid_author(b2l(Author), b2l(Who));
+is_valid_author(Author, Who) when is_list(Author) orelse is_list(Who) ->
+    case lists:keysearch(Author, 2, authors()) of
+        {value,{Who, _Nick, _Email}} -> true;
+        _                            -> false
+    end.
+
 author2email(Nick) when is_binary(Nick) ->
     author2email(b2l(Nick));
 author2email(Nick) when is_list(Nick) ->

File src/redhot2_couchdb.erl

          ,entry/1
          ,new_blog_entry/4
          ,store_blog_entry/6
+         ,store_comment/5
         ]).
 
 -export([http_get_req/1
          , l2b/1
          , b2l/1]).
 
+-define(is_bool(B), (B == true orelse B == false)).
 
 -define(DB_NAME, "eblog").
 %%-define(DB_NAME, "rh2").
            {"created_tz", l2b(lists:flatten(rfc3339(Created)))}],
     store_doc(KVs).
 
+store_comment(Ref, Text, Who, Created, Author)
+  when is_binary(Ref), is_binary(Text), is_binary(Who),
+       is_integer(Created), ?is_bool(Author) ->
+    KVs = [{"type",<<"comment">>},
+           {"ref",Ref}, 
+           {"text",Text}, 
+           {"who",Who},
+           {"author",Author},
+           {"created",Created},
+           {"created_tz", l2b(lists:flatten(rfc3339(Created)))}],
+    store_doc(KVs).
+
 %%
 %% @doc Take a key-value tuple list and store it as a new CouchDB document.
 %%

File src/redhot2_sendmail.erl

+-module(redhot2_sendmail).
+-export([send/4
+         ,send/5
+        ]).
+
+-ignore_xref([{send,4}]).
+
+send(To, From, Subject, Text) ->
+    send(To, From, Subject, Text, "text/plain").
+
+send(To, From, Subject, Text, ContentType) ->
+    do_send(To, From, Subject, Text, ContentType).
+
+do_send(To0, From, Subject, Text, ContentType) ->
+    Data = list_to_binary(["To: ", To0, "\n",
+                           "From: ", From, "\n",
+                           %% Assume not too long line here!
+                           "Subject: ", Subject, "\n",  
+                           "Content-type: ", ContentType,"\n",
+                           "\n\n",
+                           Text]),
+    To = shell_quote(To0),
+    ErrFile = "redhot_mail.error",
+    P = open_port({spawn, "/usr/sbin/sendmail -f " ++ From ++
+                                  " -bm " ++ To ++ " 2> " ++ ErrFile}, 
+                  [stream, eof]),
+    P ! {self(), {command, Data}},
+    P ! {self(), close},
+    rec_data(P, To),
+    P ! {self(), close}.
+
+rec_data(P, User) ->
+    receive
+        {P, {data, _Data}} -> rec_data(P, User);
+        {P, closed}        -> ok;
+        {P, eof}           -> ok
+    after 15000            -> ok
+    end.
+
+shell_quote(String) ->
+    %% 1. Put single quotes around the string.
+    "'" ++
+        %% 2. Remove any single quote
+        [C || C <- lists:flatten(String),
+              C =/= $' % ' emacs
+                 ] 
+        ++ "'". 
+

File src/redhot2_web_entry.erl

 
 -import(redhot2,
         [author2email/1
+         , is_valid_author/2
          , maybe_nick/1
+         , mail_from/0
          , gtostr/1
          , to_latin1/1
          , b2l/1
+         , l2b/1
+         , gnow/0
         ]).
 
 -import(redhot2_common,
               #grid_12 { body=redhot2_common:footer() }
              ]}.
 
+
+event({comment, Id}) ->
+    case {wf:session(authenticated), wf:user()} of
+        {true, Who} ->
+            [Text] = wf:qs("com_text"),
+            {obj,L} = redhot2_couchdb:entry(Id),
+            Author = proplists:get_value("author",L),
+            Title = proplists:get_value("title",L),
+            redhot2_couchdb:store_comment(l2b(Id), 
+                                          l2b(Text), 
+                                          l2b(maybe_nick(Who)), 
+                                          gnow(), 
+                                          is_valid_author(Author, Who)),
+            Email = author2email(Author),
+            spawn(fun() -> redhot2_sendmail:send(Email, mail_from(), 
+                                                 "New Comment...", 
+                                                 "...on your article: "++Title) 
+                  end),
+            wf:redirect("/entry/"++Id);
+        _ ->
+            wf:redirect("/entry/"++Id)
+    end;
 event(Event) ->
     io:format("Event=~p~n",[Event]),
     ok.