Andrii Zadorozhnii 11 лет назад
Родитель
Сommit
a8f9f06e53
4 измененных файлов с 122 добавлено и 60 удалено
  1. 7 7
      include/feeds.hrl
  2. 34 0
      src/kvs.erl
  3. 51 51
      src/kvs_comment.erl
  4. 30 2
      src/kvs_feed.erl

+ 7 - 7
include/feeds.hrl

@@ -23,9 +23,9 @@
         deleted,
         likes,
         likes_count,
-        comments,
-        comments_rear,
-        comments_count,
+%        comments,
+%        comments_rear,
+%        comments_count,
         media = [], %% for oembed
         etc,       %% field to link additional info
         type = {user, normal}
@@ -45,7 +45,7 @@
         type :: {atom(), atom() | string()},
         thumbnail_height}).
 
--record(comment, {?ITERATOR(entry),
+-record(comment, {?ITERATOR(feed),
 %        id,          %% {comment_id, entry_id}
         comment_id,  %% generowane przez id_seq
         entry_id,    %% index
@@ -53,9 +53,9 @@
         author_id,
         creation_time,
         media = [],  %% for oembed
-        parent,
-        comments,
-        comments_rear
+        parent
+%        comments,
+%        comments_rear
 }).
 
 

+ 34 - 0
src/kvs.erl

@@ -64,6 +64,40 @@ add(Record) when is_tuple(Record) ->
             error_logger:info_msg("PUT: ~p", [R3]),
             {ok, R3} end 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;
+%    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}.
+
 remove(RecordName, RecordId) ->
     error_logger:info_msg("Remove ~p ~p", [RecordName, RecordId]),
     case kvs:get(RecordName, RecordId) of {error, not_found} -> error_logger:info_msg("not found");

+ 51 - 51
src/kvs_comment.erl

@@ -4,58 +4,58 @@
 -include_lib("kvs/include/config.hrl").
 -compile(export_all).
 
-add(FId, User, EntryId, ParentComment, CommentId, Content, Medias) ->
-     case kvs:get(entry,{EntryId, FId}) of
-         {ok, _E} -> add(FId, User, EntryId, ParentComment, CommentId, Content, Medias, dont_check);
-         _ -> 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;
-    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)];
-read_comments(C) -> kvs:traversal(comment, #comment.next, C, all).
-
-feed_comments({EId, FId}) ->
-    case kvs:get(entry,{EId, FId}) of
-        {ok, #entry{comments_rear = undefined}} -> [];
-        {ok, #entry{comments_rear = First}} -> lists:flatten(read_comments(First));
-        _ -> [] end.
+%add(FId, User, EntryId, ParentComment, CommentId, Content, Medias) ->
+%     case kvs:get(entry,{EntryId, FId}) of
+%         {ok, _E} -> add(FId, User, EntryId, ParentComment, CommentId, Content, Medias, dont_check);
+%         _ -> 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;
+%    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, C%omment}.
+%
+%read_comments(undefined) -> [];
+%read_comments([#comment{comments = C} | Rest]) -> [read_comments(C) | read_comments(Rest)];
+%read_comments(C) -> kvs:traversal(comment, C#comment.next, undefined).%
+
+%feed_comments({EId, FId}) ->
+%    case kvs:get(entry,{EId, FId}) of
+%        {ok, #entry{comments_rear = undefined}} -> [];
+%        {ok, #entry{comments_rear = First}} -> lists:flatten(read_comments(First));
+%        _ -> [] end.
 
 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].
+%remove(FId, EId) ->
+%    AllComments = feed_comments({EId, FId}),
+%    [begin kvs:delete(comment, ID) end || #comment{id = ID, media = _M} <- AllComments].
 

+ 30 - 2
src/kvs_feed.erl

@@ -116,7 +116,7 @@ handle_notice([kvs_feed, _, Owner, entry, Eid, add],
     case lists:keyfind(Fid,2,Feeds) of false -> skip;
       {_,_} ->
         EntryId = case Eid of new -> kvs:uuid(); _-> Eid end,
-        E = Entry#entry{id = {EntryId, Fid}, entry_id = EntryId },
+        E = Entry#entry{id = {EntryId, Fid}, entry_id = EntryId, feeds=[comments] },
         kvs:add(E),
 
         % todo: group entry counts should be counted for each feed
@@ -166,7 +166,35 @@ handle_notice([kvs_feed, entry, {Eid, FeedId}, comment, Cid, add],
   HasFeed = lists:keyfind(FeedId,2,Feeds) /= false,
   if HasFeed ->
     [begin error_logger:info_msg("Comment: worker ~p entry ~p cid ",[Owner, Eid]),
-      kvs_comment:add(E#entry.feed_id, From, E#entry.entry_id, Parent, Cid, Content, Medias)
+    {_, CFid} = lists:keyfind(comments, 1, E#entry.feeds),
+    error_logger:info_msg("Add comment to entry ~p~n----------------~n", [E#entry.id]),
+    error_logger:info_msg("Comments feed id: ~p", [CFid]),
+    error_logger:info_msg("Comment parent ~p", [Parent]),
+    FeedId2 = case Parent of undefined -> CFid;
+    Id ->
+        case kvs:get(comment, {Parent, {E#entry.entry_id, E#entry.feed_id}}) of {error, not_found} -> error_logger:info_msg("NO PARENT COMMENT"),CFid;
+            {ok, C} -> {_, PCFid} = lists:keyfind(comments, 1, C#comment.feeds), PCFid end
+    end,
+
+    error_logger:info_msg("Target feed id: ~p", [FeedId2]),
+
+    EntryId = E#entry.entry_id,
+    FullId = {Cid, {EntryId, E#entry.feed_id}},
+    User = From,
+
+    Comment = #comment{id = FullId,
+                     author_id = User,
+                     comment_id = Cid,
+                     entry_id = EntryId,
+                     feed_id =  FeedId2, % entry commens or parent comment comments
+                     content = Content,
+                     media = Medias,
+                     creation_time = now(),
+                     feeds = [comments]},
+    error_logger:info_msg("Comment ~p ready to put.", [Comment]),
+    kvs:add(Comment)
+%      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,