Webfinger completed. All fields there.
							parent
							
								
									774a626af6
								
							
						
					
					
						commit
						c6c547df51
					
				|  | @ -1,4 +1,13 @@ | ||||||
| %% db_safe_insert.hrl | %% db_safe_insert.hrl | ||||||
| 
 | 
 | ||||||
| -record(global_message, {id, activity, timestamp}). | -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 | ||||||
|  | }). | ||||||
|  |  | ||||||
|  | @ -1,40 +1,50 @@ | ||||||
| %% webfinger_handler.erl |  | ||||||
| %% Handler per l'endpoint /.well-known/webfinger |  | ||||||
| -module(webfinger_handler). | -module(webfinger_handler). | ||||||
| -behaviour(cowboy_handler). | -behaviour(cowboy_handler). | ||||||
| 
 |  | ||||||
| %% Cowboy vuole che esportiamo la funzione init/2 |  | ||||||
| -export([init/2]). | -export([init/2]). | ||||||
| 
 | 
 | ||||||
|  | -include("db_safe_insert.hrl"). | ||||||
|  | 
 | ||||||
| init(Req, State) -> | init(Req, State) -> | ||||||
|     Params = cowboy_req:parse_qs(Req), |     Params = cowboy_req:parse_qs(Req), | ||||||
|     Resource = proplists:get_value(<<"resource">>, Params), |     Resource = proplists:get_value(<<"resource">>, Params), | ||||||
| 
 |  | ||||||
|     case Resource of |     case Resource of | ||||||
|         undefined -> |         undefined -> | ||||||
|             {ok, Resp} = cowboy_req:reply( |             reply_json(400, <<"Missing resource parameter">>, Req, State); | ||||||
|                 400, |  | ||||||
|                 #{<<"content-type">> => <<"application/json">>}, |  | ||||||
|                 <<"{\"error\": \"Missing resource parameter\"}">>, |  | ||||||
|                 Req |  | ||||||
|             ), |  | ||||||
|             {ok, Resp, State}; |  | ||||||
|         _ -> |         _ -> | ||||||
|             case parse_acct(Resource) of |             case parse_acct(Resource) of | ||||||
|                 {ok, Username, Domain} -> |                 {ok, Username, Domain} -> | ||||||
|                     %% Qui interroghiamo Mnesia! |  | ||||||
|                     case user_db:get_user(Username) of |                     case user_db:get_user(Username) of | ||||||
|                         {ok, _User} -> |                         {ok, User} -> | ||||||
|                             ActorUrl = <<"https://", Domain/binary, "/users/", Username/binary>>, |                             %% 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 = #{ |                             WebfingerMap = #{ | ||||||
|                                 <<"subject">> => <<"acct:", Username/binary, "@", Domain/binary>>, |                                 <<"subject">> => Subject, | ||||||
|  |                                 <<"aliases">> => [ProfileUrl, ActorUrl], | ||||||
|                                 <<"links">> => [ |                                 <<"links">> => [ | ||||||
|                                     #{ |                                     #{ | ||||||
|                                         <<"rel">> => <<"self">>, |                                         <<"rel">> => <<"self">>, | ||||||
|                                         <<"type">> => <<"application/activity+json">>, |                                         <<"type">> => <<"application/activity+json">>, | ||||||
|                                         <<"href">> => ActorUrl |                                         <<"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), |                             Json = jsx:encode(WebfingerMap), | ||||||
|                             {ok, Resp} = cowboy_req:reply( |                             {ok, Resp} = cowboy_req:reply( | ||||||
|  | @ -45,27 +55,23 @@ init(Req, State) -> | ||||||
|                             ), |                             ), | ||||||
|                             {ok, Resp, State}; |                             {ok, Resp, State}; | ||||||
|                         not_found -> |                         not_found -> | ||||||
|                             {ok, Resp} = cowboy_req:reply( |                             reply_json(404, <<"User not found">>, Req, State) | ||||||
|                                 404, |  | ||||||
|                                 #{<<"content-type">> => <<"application/json">>}, |  | ||||||
|                                 <<"{\"error\": \"User not found\"}">>, |  | ||||||
|                                 Req |  | ||||||
|                             ), |  | ||||||
|                             {ok, Resp, State} |  | ||||||
|                     end; |                     end; | ||||||
|                 error -> |                 error -> | ||||||
|                     {ok, Resp} = cowboy_req:reply( |                     reply_json(400, <<"Invalid resource format">>, Req, State) | ||||||
|                         400, |  | ||||||
|                         #{<<"content-type">> => <<"application/json">>}, |  | ||||||
|                         <<"{\"error\": \"Invalid resource format\"}">>, |  | ||||||
|                         Req |  | ||||||
|                     ), |  | ||||||
|                     {ok, Resp, State} |  | ||||||
|             end |             end | ||||||
|     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>>) -> | parse_acct(<<"acct:", Rest/binary>>) -> | ||||||
|     case binary:split(Rest, <<"@">>) of |     case binary:split(Rest, <<"@">>) of | ||||||
|         [Username, Domain] when Username =/= <<>>, Domain =/= <<>> -> |         [Username, Domain] when Username =/= <<>>, Domain =/= <<>> -> | ||||||
|  | @ -75,3 +81,8 @@ parse_acct(<<"acct:", Rest/binary>>) -> | ||||||
|     end; |     end; | ||||||
| parse_acct(_) -> | parse_acct(_) -> | ||||||
|     error. |     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. | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue