Skip to content

Commit

Permalink
Remove unused code
Browse files Browse the repository at this point in the history
  • Loading branch information
chrzaszcz committed Oct 12, 2022
1 parent cca03aa commit bf51c5c
Show file tree
Hide file tree
Showing 5 changed files with 6 additions and 145 deletions.
70 changes: 1 addition & 69 deletions src/mongoose_client_api/mongoose_client_api.erl
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,15 @@
-export([config_spec/0, routes/1]).

-export([init/2]).
-export([content_types_provided/2]).
-export([is_authorized/2]).
-export([options/2]).
-export([allowed_methods/2]).
-export([to_json/2]).
-export([bad_request/3]).
-export([forbidden_request/2]).
-export([forbidden_request/3]).
-export([json_to_map/1]).

%% Utilities for the handler modules
-export([parse_body/1,
parse_qs/1,
try_handle_request/3,
throw_error/2]).

-ignore_xref([allowed_methods/2, content_types_provided/2, forbidden_request/3,
options/2, to_json/2]).
-ignore_xref([allowed_methods/2, options/2, to_json/2]).

-include("mongoose.hrl").
-include("mongoose_config_spec.hrl").
Expand Down Expand Up @@ -93,50 +84,6 @@ set_cors_headers(Origin, Req) ->
set_cors_header({Header, Value}, Req) ->
cowboy_req:set_resp_header(Header, Value, Req).

allowed_methods(Req, State) ->
{[<<"OPTIONS">>, <<"GET">>], Req, State}.

content_types_provided(Req, State) ->
{[
{{<<"application">>, <<"json">>, '*'}, to_json}
], Req, State}.

options(Req, State) ->
{ok, Req, State}.

to_json(Req, User) ->
{<<"{}">>, Req, User}.

bad_request(Req, Reason, State) ->
reply(400, Req, Reason, State).

forbidden_request(Req, State) ->
forbidden_request(Req, <<>>, State).

forbidden_request(Req, Reason, State) ->
reply(403, Req, Reason, State).

reply(StatusCode, Req, Body, State) ->
maybe_report_error(StatusCode, Req, Body),
Req1 = set_resp_body_if_missing(Body, Req),
Req2 = cowboy_req:reply(StatusCode, Req1),
{stop, Req2, State#{was_replied => true}}.

set_resp_body_if_missing(Body, Req) ->
case cowboy_req:has_resp_body(Req) of
true ->
Req;
false ->
cowboy_req:set_resp_body(Body, Req)
end.

maybe_report_error(StatusCode, Req, Body) when StatusCode >= 400 ->
?LOG_WARNING(#{what => reply_error,
stacktrace => element(2, erlang:process_info(self(), current_stacktrace)),
code => StatusCode, req => Req, reply_body => Body});
maybe_report_error(_StatusCode, _Req, _Body) ->
ok.

%%--------------------------------------------------------------------
%% Authorization
%%--------------------------------------------------------------------
Expand Down Expand Up @@ -176,21 +123,6 @@ do_authorize(AuthMethod, MaybeJID, Password, HTTPMethod) ->
is_noauth_http_method(<<"OPTIONS">>) -> true;
is_noauth_http_method(_) -> false.

%% -------------------------------------------------------------------
%% @doc
%% Decode JSON binary into map
%% @end
%% -------------------------------------------------------------------
-spec json_to_map(JsonBin :: binary()) -> {ok, Map :: maps:map()} | {error, invalid_json}.

json_to_map(JsonBin) ->
case catch jiffy:decode(JsonBin, [return_maps]) of
Map when is_map(Map) ->
{ok, Map};
_ ->
{error, invalid_json}
end.

%% Utilities for the handler modules

-type req() :: cowboy_req:req().
Expand Down
4 changes: 2 additions & 2 deletions src/mongoose_client_api/mongoose_client_api_contacts.erl
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
from_json/2,
delete_resource/2]).

-ignore_xref([from_json/2, to_json/2, trails/0, forbidden_request/2]).
-ignore_xref([from_json/2, to_json/2, trails/0]).

-import(mongoose_client_api, [parse_body/1, parse_qs/1, try_handle_request/3, throw_error/2]).
-import(mongoose_client_api, [parse_body/1, try_handle_request/3, throw_error/2]).

-type req() :: cowboy_req:req().
-type state() :: map().
Expand Down
73 changes: 1 addition & 72 deletions src/mongoose_client_api/mongoose_client_api_rooms.erl
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@
to_json/2,
from_json/2]).

-export([resource_exists/2]).
-export([assert_room_id_set/2]).

%% Used by mongoose_client_api_rooms_*
-export([get_room_jid/3,
get_user_aff/2,
Expand All @@ -24,12 +21,11 @@

-ignore_xref([from_json/2, to_json/2, trails/0]).

-import(mongoose_client_api, [parse_body/1, parse_qs/1, try_handle_request/3, throw_error/2]).
-import(mongoose_client_api, [parse_body/1, try_handle_request/3, throw_error/2]).

-type req() :: cowboy_req:req().
-type state() :: map().

-include("mongoose.hrl").
-include("jlib.hrl").

-spec routes() -> mongoose_http_handler:routes().
Expand Down Expand Up @@ -186,70 +182,3 @@ get_room_name(#{}) -> throw_error(bad_request, <<"Missing room name">>).

get_room_subject(#{subject := Subject}) -> Subject;
get_room_subject(#{}) -> throw_error(bad_request, <<"Missing room subject">>).

assert_room_id_set(_Req, #{room_id := _} = _State) ->
ok.

resource_exists(Req, #{jid := #jid{lserver = Server}} = State) ->
RoomIDOrJID = cowboy_req:binding(id, Req),
MUCLightDomain = muc_light_domain(State),
case RoomIDOrJID of
undefined ->
Method = cowboy_req:method(Req),
case Method of
<<"GET">> ->
{true, Req, State};
_ ->
{false, Req, State}
end;
_ ->
case validate_room_id(RoomIDOrJID, Server, Req, State) of
{ok, RoomID} ->
State2 = set_room_id(RoomID, State),
does_room_exist(MUCLightDomain, Req, State2);
_ ->
mongoose_client_api:bad_request(Req, <<"invalid_room_id">>, State)
end
end.

set_room_id(RoomID, State = #{}) ->
State#{room_id => RoomID}.

does_room_exist(RoomS, Req, #{room_id := RoomU, jid := JID} = State) ->
HostType = mod_muc_light_utils:muc_host_to_host_type(RoomS),
case mod_muc_light_db_backend:get_info(HostType, {RoomU, RoomS}) of
{ok, Config, Users, Version} ->
Room = #{config => Config,
users => Users,
version => Version,
jid => jid:make_noprep(RoomU, RoomS, <<>>)},
CallerRole = determine_role(jid:to_lus(JID), Users),
{true, Req, State#{room => Room, role_in_room => CallerRole}};
_ ->
{false, Req, State}
end.

determine_role(US, Users) ->
case lists:keyfind(US, 1, Users) of
false -> none;
{_, Role} ->
Role
end.

-spec validate_room_id(RoomIDOrJID :: binary(), Server :: binary(), req(), state()) ->
{ok, RoomID :: binary()} | error.
validate_room_id(RoomIDOrJID, Server, Req, State) ->
MUCLightDomain = muc_light_domain(State),
case jid:from_binary(RoomIDOrJID) of
#jid{luser = <<>>, lserver = RoomID, lresource = <<>>} ->
{ok, RoomID};
#jid{luser = RoomID, lserver = MUCLightDomain, lresource = <<>>} ->
{ok, RoomID};
Other ->
?LOG_WARNING(#{what => muc_invalid_room_id,
text => <<"REST received room_id field is invalid "
"or of unknown format">>,
server => Server, room => RoomIDOrJID, reason => Other,
req => Req}),
error
end.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

-ignore_xref([from_json/2, trails/0]).

-import(mongoose_client_api, [parse_body/1, parse_qs/1, try_handle_request/3, throw_error/2]).
-import(mongoose_client_api, [parse_body/1, try_handle_request/3, throw_error/2]).
-import(mongoose_client_api_rooms, [get_room_jid/3, get_room_name/1, get_room_subject/1]).

-type req() :: cowboy_req:req().
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

-ignore_xref([from_json/2, trails/0]).

-import(mongoose_client_api, [parse_body/1, parse_qs/1, try_handle_request/3, throw_error/2]).
-import(mongoose_client_api, [parse_body/1, try_handle_request/3, throw_error/2]).
-import(mongoose_client_api_rooms, [get_room_jid/3, get_user_aff/2]).

-type req() :: cowboy_req:req().
Expand Down

0 comments on commit bf51c5c

Please sign in to comment.