facebook.erl 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. -module(facebook).
  2. -author('Andrii Zadorozhnii').
  3. -include_lib("avz/include/avz.hrl").
  4. -include_lib("n2o/include/wf.hrl").
  5. -include_lib("kvs/include/user.hrl").
  6. -compile(export_all).
  7. -export(?API).
  8. -define(HTTP_ADDRESS, case application:get_env(web, http_address) of {ok, A} -> A; _ -> "" end).
  9. -define(FB_APP_ID, case application:get_env(web, fb_id) of {ok, Id} -> Id; _-> "" end).
  10. -define(FB_BTN_CLASS, case application:get_env(web, fb_btn_class) of {ok, C} -> C; _ -> "btn-primary btn-large btn-lg" end).
  11. -define(FB_BTN_BODY, case application:get_env(wen,fb_btn_body) of {ok, _FBBTNBODY} -> _FBBTNBODY; _ -> [#i{class=[fa,"fa-facebook","fa-lg","icon-facebook","icon-large"]}, <<"Facebook">>] end).
  12. callback() -> ok.
  13. event({facebook,_Event}) -> wf:wire("fb_login();"), ok.
  14. api_event(fbLogin, Args, _Term)-> JSArgs = n2o_json:decode(Args), avz:login(facebook, JSArgs#struct.lst).
  15. registration_data(Props, facebook, Ori)->
  16. Id = proplists:get_value(<<"id">>, Props),
  17. BirthDay = case proplists:get_value(<<"birthday">>, Props) of
  18. undefined -> {1, 1, 1970};
  19. BD -> list_to_tuple([list_to_integer(X) || X <- string:tokens(binary_to_list(BD), "/")]) end,
  20. % error_logger:info_msg("User Ori: ~p",[Ori]),
  21. % error_logger:info_msg("Props: ~p",[Props]),
  22. Email = email_prop(Props, facebook),
  23. [UserName|_] = string:tokens(binary_to_list(Email),"@"), Cover = case proplists:get_value(<<"cover">>,Props) of undefined -> ""; P -> case proplists:get_value(<<"source">>,P#struct.lst) of undefined -> ""; C -> binary_to_list(C) end end,
  24. OldImages = case Ori#user.images of undefined -> []; OldI -> OldI end,
  25. Ori#user{ id = Email,
  26. display_name = UserName,
  27. images = [{fb_avatar,"https://graph.facebook.com/" ++ binary_to_list(Id) ++ "/picture?type=large"},{fb_cover,Cover}|OldImages],
  28. email = Email,
  29. names = proplists:get_value(<<"first_name">>, Props),
  30. surnames = proplists:get_value(<<"last_name">>, Props),
  31. tokens = [{facebook,Id}|Ori#user.tokens],
  32. birth = {element(3, BirthDay), element(1, BirthDay), element(2, BirthDay)},
  33. register_date = erlang:now(),
  34. status = ok }.
  35. email_prop(Props, _) -> proplists:get_value(<<"email">>, Props).
  36. login_button() -> #panel{class=["btn-group"], body=
  37. #link{id=loginfb, class=[btn, ?FB_BTN_CLASS],
  38. body=?FB_BTN_BODY, postback={facebook,loginClick} }}.
  39. sdk() ->
  40. wf:wire(#api{name=setFbIframe, tag=fb}),
  41. wf:wire(#api{name=fbAutoLogin, tag=fb}),
  42. wf:wire(#api{name=fbLogin, tag=fb}),
  43. [ #dtl{bind_script=false, file="facebook_sdk", ext="dtl", folder="priv/static/js",
  44. bindings=[{appid, ?FB_APP_ID},{channelUrl, ?HTTP_ADDRESS ++ "/static/channel.html"} ] } ].