facebook.erl 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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. { proplists:get_value(<<"id">>, Props),
  22. Ori#user{ display_name = UserName,
  23. avatar = "https://graph.facebook.com/" ++ UserName ++ "/picture",
  24. email = email_prop(Props, facebook_id),
  25. name = proplists:get_value(<<"first_name">>, Props),
  26. surname = proplists:get_value(<<"last_name">>, Props),
  27. facebook_id = Id,
  28. age = {element(3, BirthDay), element(1, BirthDay), element(2, BirthDay)},
  29. register_date = erlang:now(),
  30. status = ok }}.
  31. email_prop(Props, _) -> binary_to_list(proplists:get_value(<<"email">>, Props)).
  32. login_button() -> #panel{class=["btn-group"], body=
  33. #link{id=loginfb, class=[btn, "btn-primary", "btn-large"],
  34. body=[#i{class=["icon-facebook", "icon-large"]}, <<"Facebook">>],
  35. actions= "$('#loginfb').on('click', fb_login);" }}.
  36. sdk() ->
  37. wf:wire(#api{name=setFbIframe, tag=fb}),
  38. wf:wire(#api{name=fbAutoLogin, tag=fb}),
  39. wf:wire(#api{name=fbLogin, tag=fb}),
  40. [ #panel{id="fb-root"},
  41. #dtl{bind_script=false, file="facebook_sdk", ext="dtl", folder="priv/static/js",
  42. bindings=[{appid, ?FB_APP_ID},{channelUrl, ?HTTP_ADDRESS ++ "/static/channel.html"} ] } ].