Altre cose buffe. Ma ora esco a prendere una birra.

main
Uriel Fanelli 2025-05-10 22:14:28 +02:00
parent e3fecc7fd5
commit 0cf55ca2ec
4 changed files with 73 additions and 21 deletions

View File

@ -12,6 +12,7 @@ start(_Type, _Args) ->
%% creiamo un utente di test, solo per vedere se mnesia va
user_db:init(),
timeline_db:init(),
users_local_check:check_ENV(), %% controlla le variabili d'ambiente
%% aggiungiamo un paio di utenti di esempio.
user_db:add_user(<<"admin">>, #{email => <<"admin@example.com">>}),

View File

@ -6,42 +6,27 @@
init(Req, State) ->
{ok, Body, Req2} = cowboy_req:read_body(Req),
Activity = jsx:decode(Body, [return_maps]),
%% Validazione dei campi obbligatori
case validate_activity(Activity) of
false ->
{ok, Resp} = cowboy_req:reply(400, #{}, <<"Invalid ActivityPub message">>, Req2),
{ok, Resp, State};
true ->
io:format("Activity: ~p~n", [Activity]),
To = maps:get(<<"to">>, Activity, []),
io:format("To: ~p~n", [To]),
case has_local_recipient(To) of
%% Qui uso la funzione del nuovo modulo!
case users_local_check:has_local_recipient(To) of
true ->
{ok, Resp} = cowboy_req:reply(200, #{}, <<"Delivered to local user">>, Req2),
{ok, Resp, State};
false ->
%% Salva nell'inbox globale
timeline_db:add_message(Activity),
{ok, Resp} = cowboy_req:reply(202, #{}, <<"Saved to global timeline">>, Req2),
{ok, Resp} = cowboy_req:reply(202, #{}, <<"Saved to global inbox">>, Req2),
{ok, Resp, State}
end
end.
%% Controlla se un destinatario è un utente locale
is_local_user(Dest) when is_binary(Dest) ->
io:format("Controllo utente locale: ~p~n", [Dest]),
user_db:get_user(Dest) =/= not_found;
is_local_user(_) ->
false.
%% Controlla se almeno uno dei destinatari è locale
has_local_recipient(To) when is_list(To) ->
lists:any(fun is_local_user/1, To);
has_local_recipient(To) when is_binary(To) ->
is_local_user(To);
has_local_recipient(_) ->
false.
%% vediamo di controllare se ci sono i campi obbligatori minimi di ActivityPub:

61
src/users_local_check.erl Normal file
View File

@ -0,0 +1,61 @@
%% users_local_check.erl
-module(users_local_check).
-export([
fqdn_is_local/1,
user_exists/1,
user_is_local/1,
has_local_recipient/1,
check_ENV/0
]).
check_ENV() ->
case os:getenv("AP_FQDN") of
false ->
io:format("Errore: la variabile di ambiente AP_FQDN non è settata!~n"),
erlang:halt(1);
_ ->
ok
end.
get_fqdn() ->
logger:notice("Chiamata get_fqdn()~n"),
case os:getenv("AP_FQDN") of
false ->
logger:notice("Errore: la variabile di ambiente AP_FQDN deve essere settata!~n"),
exit({error, ap_fqdn_not_set});
FQDN ->
logger:notice("FQDN settata correttamente: ~p", [FQDN]),
list_to_binary(FQDN)
end.
%% Controlla se il dominio è locale
fqdn_is_local(FQDN) when is_binary(FQDN) ->
FQDN =:= get_fqdn().
%% Controlla se l'utente esiste nel DB
user_exists(UserAtFqdn) when is_binary(UserAtFqdn) ->
user_db:get_user(UserAtFqdn) =/= not_found.
%% Controlla se un user@fqdn è locale
user_is_local(UserAtFqdn) when is_binary(UserAtFqdn) ->
case binary:split(UserAtFqdn, <<"@">>, [global]) of
[_, Domain] ->
fqdn_is_local(Domain) andalso user_exists(UserAtFqdn);
_ ->
false
end;
user_is_local(_) ->
false.
%% Controlla se almeno uno dei destinatari è locale
has_local_recipient(To) when is_list(To) ->
lists:any(fun user_is_local/1, To);
has_local_recipient(To) when is_binary(To) ->
user_is_local(To);
has_local_recipient(_) ->
false.

View File

@ -1,2 +1,7 @@
#!/bin/bash
export AP_FQDN="localhost"
rebar3 compile
rebar3 shell --apps activitypub_server