facebook.erl 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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/users.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. callback() -> ok.
  11. event({facebook,_}) -> ok.
  12. api_event(fbLogin, Args, _Term)-> JSArgs = n2o_json:decode(Args), avz:login(facebook, JSArgs#struct.lst).
  13. registration_data(Props, facebook, Ori)->
  14. Id = proplists:get_value(<<"id">>, Props),
  15. UserName = binary_to_list(proplists:get_value(<<"username">>, Props)),
  16. BirthDay = case proplists:get_value(<<"birthday">>, Props) of
  17. undefined -> {1, 1, 1970};
  18. BD -> list_to_tuple([list_to_integer(X) || X <- string:tokens(binary_to_list(BD), "/")]) end,
  19. error_logger:info_msg("User Ori: ~p",[Ori]),
  20. error_logger:info_msg("Props: ~p",[Props]),
  21. Id = proplists:get_value(<<"id">>, Props),
  22. Email = email_prop(Props, facebook),
  23. Ori#user{ id = Email,
  24. display_name = UserName,
  25. avatar = "https://graph.facebook.com/" ++ UserName ++ "/picture",
  26. email = Email,
  27. name = proplists:get_value(<<"first_name">>, Props),
  28. surname = proplists:get_value(<<"last_name">>, Props),
  29. facebook_id = Id,
  30. age = {element(3, BirthDay), element(1, BirthDay), element(2, BirthDay)},
  31. register_date = erlang:now(),
  32. status = ok }.
  33. email_prop(Props, _) -> binary_to_list(proplists:get_value(<<"email">>, Props)).
  34. login_button() -> #panel{class=["btn-group"], body=
  35. #link{id=loginfb, class=[btn, "btn-primary", "btn-large"],
  36. body=[#i{class=["icon-facebook", "icon-large"]}, <<"Facebook">>],
  37. actions= "$('#loginfb').on('click', fb_login);" }}.
  38. sdk() ->
  39. wf:wire(#api{name=setFbIframe, tag=fb}),
  40. wf:wire(#api{name=fbAutoLogin, tag=fb}),
  41. wf:wire(#api{name=fbLogin, tag=fb}),
  42. [ #dtl{bind_script=false, file="facebook_sdk", ext="dtl", folder="priv/static/js",
  43. bindings=[{appid, ?FB_APP_ID},{channelUrl, ?HTTP_ADDRESS ++ "/static/channel.html"} ] } ].