41 lines
1.4 KiB
Erlang
41 lines
1.4 KiB
Erlang
%% index_handler.erl
|
|
-module(index_handler).
|
|
-behaviour(cowboy_handler).
|
|
-export([init/2]).
|
|
|
|
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 ->
|
|
To = maps:get(<<"to">>, Activity, []),
|
|
%% 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 inbox">>, Req2),
|
|
{ok, Resp, State}
|
|
end
|
|
end.
|
|
|
|
|
|
%% vediamo di controllare se ci sono i campi obbligatori minimi di ActivityPub:
|
|
|
|
validate_activity(Activity) when is_map(Activity) ->
|
|
maps:is_key(<<"type">>, Activity) andalso
|
|
maps:is_key(<<"to">>, Activity) andalso
|
|
maps:is_key(<<"actor">>, Activity) andalso
|
|
maps:is_key(<<"object">>, Activity) andalso
|
|
maps:is_key(<<"content">>, Activity).
|
|
|
|
|