diff --git a/include/db_safe_insert.hrl b/include/db_safe_insert.hrl index 9356429..dde57a6 100644 --- a/include/db_safe_insert.hrl +++ b/include/db_safe_insert.hrl @@ -1,4 +1,13 @@ %% db_safe_insert.hrl -record(global_message, {id, activity, timestamp}). --record(ap_users, {id, name, email, created_at}). +-record(ap_users, { + id, + username, + domain, + profile_url, + actor_url, + email, + avatar_url, + created_at +}). diff --git a/src/webfinger_handler.erl b/src/webfinger_handler.erl index 8bb200d..238e3a5 100644 --- a/src/webfinger_handler.erl +++ b/src/webfinger_handler.erl @@ -1,40 +1,50 @@ -%% webfinger_handler.erl -%% Handler per l'endpoint /.well-known/webfinger -module(webfinger_handler). -behaviour(cowboy_handler). - -%% Cowboy vuole che esportiamo la funzione init/2 -export([init/2]). +-include("db_safe_insert.hrl"). + init(Req, State) -> Params = cowboy_req:parse_qs(Req), Resource = proplists:get_value(<<"resource">>, Params), - case Resource of undefined -> - {ok, Resp} = cowboy_req:reply( - 400, - #{<<"content-type">> => <<"application/json">>}, - <<"{\"error\": \"Missing resource parameter\"}">>, - Req - ), - {ok, Resp, State}; + reply_json(400, <<"Missing resource parameter">>, Req, State); _ -> case parse_acct(Resource) of {ok, Username, Domain} -> - %% Qui interroghiamo Mnesia! case user_db:get_user(Username) of - {ok, _User} -> - ActorUrl = <<"https://", Domain/binary, "/users/", Username/binary>>, + {ok, User} -> + %% Estrai i campi dal record + ProfileUrl = to_binary(User#ap_users.profile_url), + ActorUrl = to_binary(User#ap_users.actor_url), + AvatarUrl = to_binary(User#ap_users.avatar_url), + Subject = <<"acct:", Username/binary, "@", Domain/binary>>, WebfingerMap = #{ - <<"subject">> => <<"acct:", Username/binary, "@", Domain/binary>>, + <<"subject">> => Subject, + <<"aliases">> => [ProfileUrl, ActorUrl], <<"links">> => [ #{ <<"rel">> => <<"self">>, <<"type">> => <<"application/activity+json">>, <<"href">> => ActorUrl + }, + #{ + <<"rel">> => <<"http://webfinger.net/rel/profile-page">>, + <<"type">> => <<"text/html">>, + <<"href">> => ProfileUrl } - ] + ] ++ + (case AvatarUrl of + <<>> -> []; + _ -> [ + #{ + <<"rel">> => <<"http://webfinger.net/rel/avatar">>, + <<"type">> => <<"image/jpeg">>, + <<"href">> => AvatarUrl + } + ] + end) }, Json = jsx:encode(WebfingerMap), {ok, Resp} = cowboy_req:reply( @@ -45,27 +55,23 @@ init(Req, State) -> ), {ok, Resp, State}; not_found -> - {ok, Resp} = cowboy_req:reply( - 404, - #{<<"content-type">> => <<"application/json">>}, - <<"{\"error\": \"User not found\"}">>, - Req - ), - {ok, Resp, State} + reply_json(404, <<"User not found">>, Req, State) end; error -> - {ok, Resp} = cowboy_req:reply( - 400, - #{<<"content-type">> => <<"application/json">>}, - <<"{\"error\": \"Invalid resource format\"}">>, - Req - ), - {ok, Resp, State} + reply_json(400, <<"Invalid resource format">>, Req, State) end end. +reply_json(Status, Msg, Req, State) -> + {ok, Resp} = cowboy_req:reply( + Status, + #{<<"content-type">> => <<"application/json">>}, + jsx:encode(#{<<"error">> => Msg}), + Req + ), + {ok, Resp, State}. -%% Funzione di utilità per estrarre username e dominio da "acct:username@domain" +%% Utility per estrarre username e dominio da "acct:username@domain" parse_acct(<<"acct:", Rest/binary>>) -> case binary:split(Rest, <<"@">>) of [Username, Domain] when Username =/= <<>>, Domain =/= <<>> -> @@ -75,3 +81,8 @@ parse_acct(<<"acct:", Rest/binary>>) -> end; parse_acct(_) -> error. + +%% Utility per gestire stringhe/binary +to_binary(undefined) -> <<>>; +to_binary(Str) when is_list(Str) -> unicode:characters_to_binary(Str); +to_binary(Bin) when is_binary(Bin) -> Bin.