From bf51c5cae3e8a8d09ca31456c0da1c762e8aa068 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Chrz=C4=85szcz?= Date: Wed, 12 Oct 2022 14:53:51 +0200 Subject: [PATCH] Remove unused code --- .../mongoose_client_api.erl | 70 +----------------- .../mongoose_client_api_contacts.erl | 4 +- .../mongoose_client_api_rooms.erl | 73 +------------------ .../mongoose_client_api_rooms_config.erl | 2 +- .../mongoose_client_api_rooms_users.erl | 2 +- 5 files changed, 6 insertions(+), 145 deletions(-) diff --git a/src/mongoose_client_api/mongoose_client_api.erl b/src/mongoose_client_api/mongoose_client_api.erl index 8993fa1f27e..c68c11973b8 100644 --- a/src/mongoose_client_api/mongoose_client_api.erl +++ b/src/mongoose_client_api/mongoose_client_api.erl @@ -6,15 +6,7 @@ -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, @@ -22,8 +14,7 @@ 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"). @@ -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 %%-------------------------------------------------------------------- @@ -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(). diff --git a/src/mongoose_client_api/mongoose_client_api_contacts.erl b/src/mongoose_client_api/mongoose_client_api_contacts.erl index c795fc5855d..6d6d1ff3cb3 100644 --- a/src/mongoose_client_api/mongoose_client_api_contacts.erl +++ b/src/mongoose_client_api/mongoose_client_api_contacts.erl @@ -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(). diff --git a/src/mongoose_client_api/mongoose_client_api_rooms.erl b/src/mongoose_client_api/mongoose_client_api_rooms.erl index bab421845e3..6a25d2e6291 100644 --- a/src/mongoose_client_api/mongoose_client_api_rooms.erl +++ b/src/mongoose_client_api/mongoose_client_api_rooms.erl @@ -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, @@ -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(). @@ -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. diff --git a/src/mongoose_client_api/mongoose_client_api_rooms_config.erl b/src/mongoose_client_api/mongoose_client_api_rooms_config.erl index 16568913ebb..c10a7806998 100644 --- a/src/mongoose_client_api/mongoose_client_api_rooms_config.erl +++ b/src/mongoose_client_api/mongoose_client_api_rooms_config.erl @@ -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(). diff --git a/src/mongoose_client_api/mongoose_client_api_rooms_users.erl b/src/mongoose_client_api/mongoose_client_api_rooms_users.erl index 1525b638269..7893b127076 100644 --- a/src/mongoose_client_api/mongoose_client_api_rooms_users.erl +++ b/src/mongoose_client_api/mongoose_client_api_rooms_users.erl @@ -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().