Andrii Zadorozhnii 12 лет назад
Родитель
Сommit
6a0b4cda79
2 измененных файлов с 41 добавлено и 55 удалено
  1. 33 35
      src/kvs_comment.erl
  2. 8 20
      src/kvs_feed.erl

+ 33 - 35
src/kvs_comment.erl

@@ -10,40 +10,38 @@ add(FId, User, EntryId, ParentComment, CommentId, Content, Medias) ->
          _ -> ok end.
 
 add(FId, User, EntryId, ParentComment, CommentId, Content, Medias, _) ->
-    FullId = {CommentId, {EntryId, FId}},
-
-    Prev = case ParentComment of
-        undefined ->
-            {ok, Entry} = kvs:get(entry,{EntryId, FId}),
-            {PrevC, E} = case Entry#entry.comments of
-                        undefined -> {undefined, Entry#entry{comments_rear = FullId}};
-                        Id ->  case kvs:get(comment, Id) of {ok, PrevTop} -> kvs:put(PrevTop#comment{next = FullId}); {error, not_found} -> skip end,
-                          {Id, Entry} end,
-            kvs:put(E#entry{comments=FullId}),
-            PrevC;
-        _ ->
-            {ok, Parent} = kvs:get(comment, {{EntryId, FId}, ParentComment}),
-            {PrevC, CC} = case Parent#comment.comments of
-                        undefined -> {undefined, Parent#comment{comments_rear = FullId}};
-                        Id -> {ok, PrevTop} = kvs:get(comment, Id),
-                              kvs:put(PrevTop#comment{next = FullId}),
-                              {Id, Parent} end,
-            kvs:put(CC#comment{comments = FullId}),
-            PrevC end,
-
-    Comment = #comment{id = FullId,
-                       author_id = User,
-                       comment_id = CommentId,
-                       entry_id = EntryId,
-                       content = Content,
-                       media = Medias,
-                       creation_time = now(),
-                       prev = Prev,
-                       next = undefined},
-
-    kvs:put(Comment),
-    {ok, Comment}.
-
+  FullId = {CommentId, {EntryId, FId}},
+
+  Prev = case ParentComment of
+    undefined ->
+      {ok, Entry} = kvs:get(entry,{EntryId, FId}),
+      {PrevC, E} = case Entry#entry.comments of
+        undefined -> {undefined, Entry#entry{comments_rear = FullId}};
+        Id ->  case kvs:get(comment, Id) of {ok, PrevTop} -> kvs:put(PrevTop#comment{next = FullId}); {error, not_found} -> skip end, {Id, Entry} end,
+      kvs:put(E#entry{comments=FullId}),
+      PrevC;
+    P ->
+      case kvs:get(comment, {P, {EntryId, FId}}) of
+        {ok, Parent} ->
+          {PrevC, CC} = case Parent#comment.comments of
+            undefined -> {undefined, Parent#comment{comments_rear = FullId}};
+            Id -> {ok, PrevTop} = kvs:get(comment, Id), kvs:put(PrevTop#comment{next = FullId}), {Id, Parent} end,
+          kvs:put(CC#comment{comments = FullId}),
+          PrevC;
+        {error, not_found} -> undefined end end,
+
+  Comment = #comment{id = FullId,
+                     author_id = User,
+                     comment_id = CommentId,
+                     entry_id = EntryId,
+                     content = Content,
+                     media = Medias,
+                     creation_time = now(),
+                     prev = Prev,
+                     next = undefined},
+  error_logger:info_msg("PUT: ~p", [Comment]),
+  kvs:put(Comment),
+  {ok, Comment}.
 
 read_comments(undefined) -> [];
 read_comments([#comment{comments = C} | Rest]) -> [read_comments(C) | read_comments(Rest)];
@@ -59,5 +57,5 @@ author_comments(Who) -> DBA=?DBA,DBA:author_comments(Who).
 
 remove(FId, EId) ->
     AllComments = feed_comments({EId, FId}),
-    [begin kvs:delete(comment, ID) end || #comment{id = ID, media = M} <- AllComments].
+    [begin kvs:delete(comment, ID) end || #comment{id = ID, media = _M} <- AllComments].
 

+ 8 - 20
src/kvs_feed.erl

@@ -219,12 +219,14 @@ handle_notice([kvs_feed, Totype, Toid, entry, {Eid,Fid}, delete],
   end,
   {noreply, State};
 
-handle_notice([kvs_feed, product, _EntryOwner, comment, Cid, add],
-              [From, {Eid, FFid}, Parent, Content, Medias, _],
-              #state{feed=Fid} = State) ->
-  if FFid == Fid ->
-    error_logger:info_msg("Add comment: ~p ~p ~p", [Fid, Eid, Cid]),
-    kvs_comment:add(Fid, From, Eid, Parent, Cid, Content, Medias);
+handle_notice([kvs_feed, entry, {Eid, FeedId}, comment, Cid, add],
+              [From, Parent, Content, Medias, _],
+              #state{owner=Owner, feed=Fid} = State) ->
+  if FeedId == Fid ->
+    [begin error_logger:info_msg("Comment: worker ~p entry ~p cid ~p",[Owner, Eid, Cid]),
+      kvs_comment:add(E#entry.feed_id, From, E#entry.entry_id, Parent, Cid, Content, Medias)
+    end || E <- kvs:all_by_index(entry, entry_id, Eid)];
+
     true -> skip end,
   {noreply, State};
 
@@ -271,20 +273,6 @@ handle_notice(["kvs_feed", _Type, EntryOwner, "entry", EntryId, "delete"] = Rout
     self() ! {feed_refresh, State#state.feed, ?CACHED_ENTRIES},
     {noreply, State};
 
-handle_notice(["kvs_feed", _Type, _EntryOwner, "entry", EntryId, "edit"] = Route,
-              Message, #state{owner = Owner, feed=Feed} = State) ->
-    [NewDescription|_] = Message,
-    error_logger:info_msg("feed(~p): edit: Owner=~p, Route=~p, Message=~p", [self(), Owner, Route, Message]),
-    kvs_feed:edit_entry(Feed, EntryId, NewDescription),
-    {noreply, State};
-
-handle_notice(["kvs_feed", _Type, _EntryOwner, "comment", CommentId, "add"] = Route,
-              Message, #state{owner = Owner, feed=Feed} = State) ->
-    [From, EntryId, ParentComment, Content, Medias] = Message,
-    error_logger:info_msg("feed(~p): add comment: Owner=~p, Route=~p, Message=~p", [self(), Owner, Route, Message]),
-    kvs_comment:add(Feed, From, EntryId, ParentComment, CommentId, Content, Medias),
-    {noreply, State};
-
 handle_notice(["kvs_feed", "user", UId, "count_entry_in_statistics"] = Route, 
     Message, #state{owner = Owner, type =Type} = State) ->
     error_logger:info_msg("queue_action(~p): count_entry_in_statistics: Owner=~p, Route=~p, Message=~p", [self(), {Type, Owner}, Route, Message]),