Browse Source

copy/paste user module for products

Andrii Zadorozhnii 11 years ago
parent
commit
99896289fd
6 changed files with 165 additions and 28 deletions
  1. 1 0
      include/feeds.hrl
  2. 8 3
      include/products.hrl
  3. 1 0
      src/kvs.erl
  4. 24 25
      src/kvs_feed.erl
  5. 128 0
      src/kvs_products.erl
  6. 3 0
      src/store_mnesia.erl

+ 1 - 0
include/feeds.hrl

@@ -9,6 +9,7 @@
         feed_id,  % are for secondary indexes
         from, % author
         to,
+        title,
         description,
         created, % time
         hidden,

+ 8 - 3
include/products.hrl

@@ -14,20 +14,25 @@
         user_price }).
 
 -record(product, {
-        id                     :: integer(), % auto
+        id,
         ext_id                 :: term(),    % ext
-        name                   :: binary(),  % admin (based on ext)
+        name                   :: string(),  % name
+        display_name           :: binary(),  % admin (based on ext)
         ext_name               :: binary(),  % ext
         vendor_id              :: integer(), % auto
         categories             :: list(integer()), % admin
+        creator,
+        owner,
         feed,
+        title,
+        description,
         description_short      :: binary(),  % admin (based on ext)
         description_long       :: binary(),  % admin (based on ext)
         image_small_url        :: binary(),  % admin (based on ext)
         image_big_url          :: binary(),  % admin (based on ext)
         publish_start_date     :: calendar:date_time(), % admin
         publish_end_date       :: calendar:date_time(), % admin
-        price                  :: integer(), % ext
+        price,
         retailer_price         :: integer(), % ext
         our_price              :: integer(), % auto
         enabled_on_site        :: boolean(), % admin

+ 1 - 0
src/kvs.erl

@@ -12,6 +12,7 @@
 -include_lib("kvs/include/log.hrl").
 -include_lib("kvs/include/membership.hrl").
 -include_lib("kvs/include/payments.hrl").
+-include_lib("kvs/include/products.hrl").
 -include_lib("stdlib/include/qlc.hrl").
 -include_lib("kvs/include/feed_state.hrl").
 -compile(export_all).

+ 24 - 25
src/kvs_feed.erl

@@ -17,36 +17,35 @@ create() ->
     ok = kvs:put(#feed{id = FId} ),
     FId.
 
-add_entry(FId, User, To, EntryId,Desc,Medias,Type,SharedBy) ->
+add_entry(FId, User, To, EntryId, Title, Desc,Medias,Type,SharedBy) ->
     case kvs:get(entry,{EntryId, FId}) of
         {ok, _} -> ok;
-        _ -> add_entry(FId, User, To, EntryId, Desc, Medias, Type, SharedBy, dont_check) end.
-
-add_entry(FId, User, To, EntryId, Desc, Medias, Type, SharedBy, _) ->
-    {ok,Feed} = kvs:get(feed, erlang:integer_to_list(FId)),
-
-    Id = {EntryId, FId},
-    Next = undefined,
-    Prev = case Feed#feed.top of
-               undefined -> undefined;
-               X -> case kvs:get(entry, X) of
-                       {ok, TopEntry} -> EditedEntry = TopEntry#entry{next = Id}, kvs:put(EditedEntry), TopEntry#entry.id;
-                       {error, _} -> undefined end end,
-
-    kvs:put(#feed{id = FId, top = {EntryId, FId}}), % update feed top with current
-
-    Entry  = #entry{id = {EntryId, FId}, entry_id = EntryId, feed_id = FId, from = User,
+        _ -> add_entry(FId, User, To, EntryId, Title, Desc, Medias, Type, SharedBy, dont_check) end.
+
+add_entry(FId, User, To, EntryId, Title, Desc, Medias, Type, SharedBy, _) ->
+  case kvs:get(feed, FId) of
+    {ok,Feed} ->
+      Id = {EntryId, FId},
+      Next = undefined,
+      Prev = case Feed#feed.top of
+        undefined -> undefined;
+        X -> case kvs:get(entry, X) of
+          {ok, TopEntry} -> EditedEntry = TopEntry#entry{next = Id}, kvs:put(EditedEntry), TopEntry#entry.id;
+          {error, _} -> undefined end end,
+
+      kvs:put(#feed{id = FId, top = {EntryId, FId}}), % update feed top with current
+
+      Entry  = #entry{id = {EntryId, FId}, entry_id = EntryId, feed_id = FId, from = User,
                     to = To, type = Type, media = Medias, created = now(),
-                    description = Desc, shared = SharedBy,
+                    title=Title, description = Desc, shared = SharedBy,
                     next = Next, prev = Prev},
 
-    ModEntry = case catch feedformat:format(Entry) of
-                   {_, Reason} -> ?ERROR("feedformat error: ~p", [Reason]), Entry;
-                   #entry{} = ME -> ME end,
-
-    kvs:put(ModEntry),
-
-    {ok, ModEntry}.
+      error_logger:info_msg("PUT ENTRY: ~p", [Entry]),
+      kvs:put(Entry),
+      {ok, Entry};
+    {error, not_found} ->
+      error_logger:info_msg("Add entry failed. No feed ~p", [FId])
+  end.
 
 entry_traversal(undefined, _) -> [];
 entry_traversal(_, 0) -> [];

+ 128 - 0
src/kvs_products.erl

@@ -0,0 +1,128 @@
+-module(kvs_products).
+-copyright('Synrc Research Center s.r.o.').
+-include_lib("kvs/include/products.hrl").
+-include_lib("kvs/include/users.hrl").
+-include_lib("kvs/include/groups.hrl").
+-include_lib("kvs/include/accounts.hrl").
+-include_lib("kvs/include/log.hrl").
+-include_lib("kvs/include/config.hrl").
+-include_lib("kvs/include/feed_state.hrl").
+-include_lib("mqs/include/mqs.hrl").
+-compile(export_all).
+
+register(#product{} = Registration) ->
+    Id = kvs:next_id("product", 1),
+    Product = Registration#product{id = Id, feed = kvs_feed:create()},
+    error_logger:info_msg("PUT PRODUCT ~p", [Product#product.id]),
+    kvs:put(Product),
+%    init_mq(Product),
+%    mqs:notify([product, init], {E, Product#product.feed}),
+    {ok, Product}.
+
+delete(Name) ->
+    case kvs:get(product, Name) of
+        {ok, Product} ->
+            GIds = kvs_group:participate(Name),
+            [ mqs:notify(["subscription", "product", Name, "remove_from_user"], {GId}) || GId <- GIds ],
+            F2U = [ {MeId, FrId} || #subscription{who = MeId, whom = FrId} <- subscriptions(Product) ],
+            [ unsubscribe(MeId, FrId) || {MeId, FrId} <- F2U ],
+            [ unsubscribe(FrId, MeId) || {MeId, FrId} <- F2U ],
+            kvs:delete(user_status, Name),
+            kvs:delete(user, Name),
+            {ok, Product};
+        E -> E end.
+
+subscribe(Who, Whom) ->
+    Record = #subscription{key={Who,Whom}, who = Who, whom = Whom},
+    kvs:put(Record).
+
+unsubscribe(Who, Whom) ->
+    case subscribed(Who, Whom) of
+        true  -> kvs:delete(subscription, {Who, Whom});
+        false -> skip end.
+
+subscriptions(undefined)-> [];
+subscriptions(#product{name = UId}) -> subscriptions(UId);
+
+subscriptions(UId) -> DBA=?DBA, DBA:subscriptions(UId).
+subscribed(Who) -> DBA=?DBA, DBA:subscribed(Who).
+
+subscribed(Who, Whom) ->
+    case kvs:get(subscription, {Who, Whom}) of
+        {ok, _} -> true;
+        _ -> false end.
+
+subscription_mq(Type, Action, Who, Whom) ->
+    case mqs:open([]) of
+        {ok,Channel} ->
+            case {Type,Action} of 
+                {user,add}     -> mqs_channel:bind_exchange(Channel, ?USER_EXCHANGE(Who), ?NOTIFICATIONS_EX, rk_user_feed(Whom));
+                {user,remove}  -> mqs_channel:unbind_exchange(Channel, ?USER_EXCHANGE(Who), ?NOTIFICATIONS_EX, rk_user_feed(Whom)) end,
+            mqs_channel:close(Channel);
+        {error,Reason} -> ?ERROR("subscription_mq error: ~p",[Reason]) end.
+
+init_mq(Product=#product{}) ->
+    Groups = kvs_group:participate(Product),
+    ProductExchange = ?USER_EXCHANGE(Product#product.name),
+    ExchangeOptions = [{type, <<"fanout">>}, durable, {auto_delete, false}],
+    case mqs:open([]) of
+        {ok, Channel} ->
+            ?INFO("Cration Exchange: ~p,",[{Channel,ProductExchange,ExchangeOptions}]),
+            mqs_channel:create_exchange(Channel, ProductExchange, ExchangeOptions),
+            Relations = build_user_relations(Product, Groups),
+            [ mqs_channel:bind_exchange(Channel, ?USER_EXCHANGE(Product#product.name), ?NOTIFICATIONS_EX, Route) || Route <- Relations],
+            mqs_channel:close(Channel);
+        {error,Reason} -> ?ERROR("init_mq error: ~p",[Reason]) end.
+
+build_user_relations(Product, Groups) -> [
+    mqs:key( [kvs_product, '*', Product]),
+    mqs:key( [kvs_feed, user, Product, '*', '*', '*']),
+    mqs:key( [kvs_feed, user, Product, '*'] ),
+    mqs:key( [kvs_payment, user, Product, '*']),
+    mqs:key( [kvs_account, user, Product, '*']),
+    mqs:key( [kvs_meeting, user, Product, '*']),
+    mqs:key( [kvs_purchase, user, Product, '*']) |
+  [ mqs:key( [kvs_feed, group, G, '*', '*', '*']) || G <- Groups ]
+    ].
+
+rk_user_feed(Product) -> mqs:key([kvs_feed, user, Product, '*', '*', '*']).
+
+retrieve_connections(Id,Type) ->
+    Friends = case Type of 
+        user -> kvs_product:list_subscr_usernames(Id);
+        _ -> kvs_group:list_group_members(Id) end,
+    case Friends of
+        [] -> [];
+        Full -> Sub = lists:sublist(Full, 10),
+            case Sub of
+                [] -> [];
+                _ ->
+                    Data = [begin
+                        case kvs:get(user,Who) of
+                            {ok,Product} -> RealName = kvs_product:user_realname_user(Product),
+                            Paid = kvs_payment:user_paid(Who),
+                            {Who,Paid,RealName};
+                        _ -> undefined end end || Who <- Sub],
+                    [ X || X <- Data, X/=undefined ] end end.
+
+handle_notice(["kvs_product", "subscribe", Who],
+    Message, #state{owner = _Owner, type =_Type} = State) ->
+    {Whom} = Message,
+    kvs_product:subscribe(Who, Whom),
+    subscription_mq(user, add, Who, Whom),
+    {noreply, State};
+
+handle_notice(["kvs_product", "unsubscribe", Who],
+    Message, #state{owner = _Owner, type =_Type} = State) ->
+    {Whom} = Message,
+    kvs_product:unsubscribe(Who, Whom),
+    subscription_mq(user, remove, Who, Whom),
+    {noreply, State};
+
+handle_notice(["kvs_product", "update", _Who],
+    Message, #state{owner = _Owner, type =_Type} = State) ->
+    {NewProduct} = Message,
+    kvs:put(NewProduct),
+    {noreply, State};
+
+handle_notice(_Route, _Message, State) -> error_logger:info_msg("Unknown USERS notice").

+ 3 - 0
src/store_mnesia.erl

@@ -11,6 +11,7 @@
 -include_lib("kvs/include/membership.hrl").
 -include_lib("kvs/include/payments.hrl").
 -include_lib("kvs/include/purchases.hrl").
+-include_lib("kvs/include/products.hrl").
 -include_lib("kvs/include/accounts.hrl").
 -include_lib("kvs/include/log.hrl").
 -include_lib("kvs/include/translations.hrl").
@@ -69,6 +70,8 @@ initialize() ->
     ?CREATE_TAB(id_seq),
     ?CREATE_TAB(transaction),
     ?CREATE_TAB(translation),
+    ?CREATE_TAB(product),
+    ?CREATE_TAB(product_category),
     mnesia:wait_for_tables([comment,subscription,group,group_subscription,user,entry],5000),
     add_indexes(),
     ok.