From 39cf8d86d62defcbeba5e6935c8b19ee3df1dd76 Mon Sep 17 00:00:00 2001 From: Evgeny Khramtsov Date: Thu, 27 Jun 2019 15:22:27 +0300 Subject: [PATCH] Avoid using broad map() type wherever possible --- include/mod_muc_room.hrl | 19 +++++++++++++------ src/ejabberd_auth.erl | 7 ++++--- src/ejabberd_config.erl | 8 +++++--- src/ejabberd_redis.erl | 3 ++- src/ejabberd_system_monitor.erl | 7 ++++--- src/misc.erl | 4 +++- src/mod_avatar.erl | 3 ++- src/mod_client_state.erl | 2 +- src/mod_http_upload.erl | 3 ++- src/mod_http_upload_quota.erl | 3 ++- src/mod_mqtt_session.erl | 28 ++++++++++++++++------------ src/mod_muc_room.erl | 27 +++++++++++++-------------- src/mod_ping.erl | 14 ++++++++------ 13 files changed, 75 insertions(+), 53 deletions(-) diff --git a/include/mod_muc_room.hrl b/include/mod_muc_room.hrl index ac58c1f51..46eb149bb 100644 --- a/include/mod_muc_room.hrl +++ b/include/mod_muc_room.hrl @@ -105,13 +105,13 @@ access = {none,none,none,none,none} :: {atom(), atom(), atom(), atom(), atom()}, jid = #jid{} :: jid(), config = #config{} :: config(), - users = #{} :: map(), - subscribers = #{} :: map(), - subscriber_nicks = #{} :: map(), + users = #{} :: users(), + subscribers = #{} :: subscribers(), + subscriber_nicks = #{} :: subscriber_nicks(), last_voice_request_time = treap:empty() :: treap:treap(), - robots = #{} :: map(), - nicks = #{} :: map(), - affiliations = #{} :: map(), + robots = #{} :: robots(), + nicks = #{} :: nicks(), + affiliations = #{} :: affiliations(), history = #lqueue{} :: lqueue(), subject = [] :: [text()], subject_author = <<"">> :: binary(), @@ -120,3 +120,10 @@ room_shaper = none :: ejabberd_shaper:shaper(), room_queue :: p1_queue:queue() | undefined }). + +-type users() :: #{ljid() => #user{}}. +-type robots() :: #{jid() => {binary(), stanza()}}. +-type nicks() :: #{binary() => [ljid()]}. +-type affiliations() :: #{ljid() => affiliation() | {affiliation(), binary()}}. +-type subscribers() :: #{ljid() => #subscriber{}}. +-type subscriber_nicks() :: #{binary() => [ljid()]}. diff --git a/src/ejabberd_auth.erl b/src/ejabberd_auth.erl index 33e0d118f..fff63e811 100644 --- a/src/ejabberd_auth.erl +++ b/src/ejabberd_auth.erl @@ -53,8 +53,9 @@ -define(SALT_LENGTH, 16). --record(state, {host_modules = #{} :: map()}). +-record(state, {host_modules = #{} :: host_modules()}). +-type host_modules() :: #{binary => [module()]}. -type password() :: binary() | #scram{}. -type digest_fun() :: fun((binary()) -> binary()). -export_type([password/0]). @@ -751,7 +752,7 @@ password_to_scram(Password, IterationCount) -> %%%---------------------------------------------------------------------- %%% Cache stuff %%%---------------------------------------------------------------------- --spec init_cache(map()) -> ok. +-spec init_cache(host_modules()) -> ok. init_cache(HostModules) -> CacheOpts = cache_opts(), {True, False} = use_cache(HostModules), @@ -771,7 +772,7 @@ cache_opts() -> LifeTime = ejabberd_option:auth_cache_life_time(), [{max_size, MaxSize}, {cache_missed, CacheMissed}, {life_time, LifeTime}]. --spec use_cache(map()) -> {True :: [module()], False :: [module()]}. +-spec use_cache(host_modules()) -> {True :: [module()], False :: [module()]}. use_cache(HostModules) -> {Enabled, Disabled} = maps:fold( diff --git a/src/ejabberd_config.erl b/src/ejabberd_config.erl index 87bf35b08..519a16e42 100644 --- a/src/ejabberd_config.erl +++ b/src/ejabberd_config.erl @@ -58,6 +58,7 @@ {exception, term(), term(), term()}. -type error_return() :: {error, econf:error_reason(), term()} | {error, error_reason()}. +-type host_config() :: #{{atom(), binary() | global} => term()}. -callback opt_type(atom()) -> econf:validator(). -callback options() -> [atom() | {atom(), term()}]. @@ -594,7 +595,7 @@ abort(Err) -> end, Err. --spec set_host_config([{atom(), term()}]) -> {ok, map()} | error_return(). +-spec set_host_config([{atom(), term()}]) -> {ok, host_config()} | error_return(). set_host_config(Opts) -> Map1 = lists:foldl( fun({Opt, Val}, M) when Opt /= host_config, @@ -635,7 +636,7 @@ set_host_config(Opts) -> _ -> {ok, Map3} end. --spec apply_defaults(ets:tid(), [binary()], map()) -> ok. +-spec apply_defaults(ets:tid(), [binary()], host_config()) -> ok. apply_defaults(Tab, Hosts, Map) -> Defaults1 = defaults(), apply_defaults(Tab, global, Map, Defaults1), @@ -646,7 +647,8 @@ apply_defaults(Tab, Hosts, Map) -> apply_defaults(Tab, Host, Map, Defaults2) end, Hosts). --spec apply_defaults(ets:tid(), global | binary(), map(), +-spec apply_defaults(ets:tid(), global | binary(), + host_config(), [atom() | {atom(), term()}]) -> ok. apply_defaults(Tab, Host, Map, Defaults) -> lists:foreach( diff --git a/src/ejabberd_redis.erl b/src/ejabberd_redis.erl index 29a8947ec..18a73414b 100644 --- a/src/ejabberd_redis.erl +++ b/src/ejabberd_redis.erl @@ -54,9 +54,10 @@ -record(state, {connection :: pid() | undefined, num :: pos_integer(), - subscriptions = #{} :: map(), + subscriptions = #{} :: subscriptions(), pending_q :: p1_queue:queue()}). +-type subscriptions() :: #{binary() => [pid()]}. -type error_reason() :: binary() | timeout | disconnected | overloaded. -type redis_error() :: {error, error_reason()}. -type redis_reply() :: undefined | binary() | [binary()]. diff --git a/src/ejabberd_system_monitor.erl b/src/ejabberd_system_monitor.erl index 1a4e3c765..ca2e26b4e 100644 --- a/src/ejabberd_system_monitor.erl +++ b/src/ejabberd_system_monitor.erl @@ -53,6 +53,7 @@ name :: pid() | atom()}). -type state() :: #state{}. -type proc_stat() :: #proc_stat{}. +-type app_pids() :: #{pid() => atom()}. %%%=================================================================== %%% API @@ -151,7 +152,7 @@ handle_overload(_State, Procs) -> end, lists:foreach(fun erlang:garbage_collect/1, Procs). --spec get_app_pids() -> map(). +-spec get_app_pids() -> app_pids(). get_app_pids() -> try application:info() of Info -> @@ -170,7 +171,7 @@ get_app_pids() -> #{} end. --spec overloaded_procs(map(), [pid()]) +-spec overloaded_procs(app_pids(), [pid()]) -> {non_neg_integer(), non_neg_integer(), dict:dict(), [proc_stat()]}. overloaded_procs(AppPids, AllProcs) -> lists:foldl( @@ -186,7 +187,7 @@ overloaded_procs(AppPids, AllProcs) -> end end, {0, 0, dict:new(), []}, AllProcs). --spec proc_stat(pid(), map()) -> proc_stat() | undefined. +-spec proc_stat(pid(), app_pids()) -> proc_stat() | undefined. proc_stat(Pid, AppPids) -> case process_info(Pid, [message_queue_len, memory, diff --git a/src/misc.erl b/src/misc.erl index 16e4122b5..3aacd3a6c 100644 --- a/src/misc.erl +++ b/src/misc.erl @@ -51,6 +51,8 @@ -include("xmpp.hrl"). -include_lib("kernel/include/file.hrl"). +-type distance_cache() :: #{{string(), string()} => non_neg_integer()}. + %%%=================================================================== %%% API %%%=================================================================== @@ -598,7 +600,7 @@ unique_timestamp() -> {MS, S, erlang:unique_integer([positive, monotonic]) rem 1000000}. %% Levenshtein distance --spec ld(string(), string(), map()) -> {non_neg_integer(), map()}. +-spec ld(string(), string(), distance_cache()) -> {non_neg_integer(), distance_cache()}. ld([] = S, T, Cache) -> {length(T), maps:put({S, T}, length(T), Cache)}; ld(S, [] = T, Cache) -> diff --git a/src/mod_avatar.erl b/src/mod_avatar.erl index 67f0aef20..b56d575f5 100644 --- a/src/mod_avatar.erl +++ b/src/mod_avatar.erl @@ -35,6 +35,7 @@ -include("logger.hrl"). -include("pubsub.hrl"). +-type avatar_id_meta() :: #{avatar_meta => {binary(), avatar_meta()}}. -opaque convert_rule() :: {default | eimp:img_type(), eimp:img_type()}. -export_type([convert_rule/0]). @@ -362,7 +363,7 @@ convert_avatar(LUser, LServer, Data, Rules) -> end end. --spec set_vcard_avatar(jid(), vcard_photo() | undefined, map()) -> ok. +-spec set_vcard_avatar(jid(), vcard_photo() | undefined, avatar_id_meta()) -> ok. set_vcard_avatar(JID, VCardPhoto, Meta) -> case get_vcard(JID) of {ok, #vcard_temp{photo = VCardPhoto}} -> diff --git a/src/mod_client_state.erl b/src/mod_client_state.erl index 0b9bab34d..855e4a5d8 100644 --- a/src/mod_client_state.erl +++ b/src/mod_client_state.erl @@ -46,7 +46,7 @@ -define(CSI_QUEUE_MAX, 100). -type csi_type() :: presence | chatstate | {pep, binary()}. --type csi_queue() :: {non_neg_integer(), map()}. +-type csi_queue() :: {non_neg_integer(), #{csi_key() => csi_element()}}. -type csi_timestamp() :: {non_neg_integer(), erlang:timestamp()}. -type csi_key() :: {ljid(), csi_type()}. -type csi_element() :: {csi_timestamp(), stanza()}. diff --git a/src/mod_http_upload.erl b/src/mod_http_upload.erl index ef571274f..9cd828ebf 100644 --- a/src/mod_http_upload.erl +++ b/src/mod_http_upload.erl @@ -105,7 +105,7 @@ service_url :: binary() | undefined, thumbnail :: boolean(), custom_headers :: [{binary(), binary()}], - slots = #{} :: map(), + slots = #{} :: slots(), external_secret :: binary()}). -record(media_info, @@ -116,6 +116,7 @@ -type state() :: #state{}. -type slot() :: [binary(), ...]. +-type slots() :: #{slot() => {pos_integer(), reference()}}. -type media_info() :: #media_info{}. %%-------------------------------------------------------------------- diff --git a/src/mod_http_upload_quota.erl b/src/mod_http_upload_quota.erl index 56d46b02e..4df799207 100644 --- a/src/mod_http_upload_quota.erl +++ b/src/mod_http_upload_quota.erl @@ -61,9 +61,10 @@ access_hard_quota :: atom(), max_days :: pos_integer() | infinity, docroot :: binary(), - disk_usage = #{} :: map(), + disk_usage = #{} :: disk_usage(), timers :: [timer:tref()]}). +-type disk_usage() :: #{{binary(), binary()} => non_neg_integer()}. -type state() :: #state{}. %%-------------------------------------------------------------------- diff --git a/src/mod_mqtt_session.erl b/src/mod_mqtt_session.erl index d0abf85d4..9d90ab0a8 100644 --- a/src/mod_mqtt_session.erl +++ b/src/mod_mqtt_session.erl @@ -40,15 +40,19 @@ will :: undefined | publish(), will_delay = 0 :: seconds(), stop_reason :: undefined | error_reason(), - acks = #{} :: map(), - subscriptions = #{} :: map(), - topic_aliases = #{} :: map(), + acks = #{} :: acks(), + subscriptions = #{} :: subscriptions(), + topic_aliases = #{} :: topic_aliases(), id = 0 :: non_neg_integer(), in_flight :: undefined | publish() | pubrel(), codec :: mqtt_codec:state(), queue :: undefined | p1_queue:queue(), tls :: boolean()}). +-type acks() :: #{non_neg_integer() => pubrec()}. +-type subscriptions() :: #{binary() => {sub_opts(), non_neg_integer()}}. +-type topic_aliases() :: #{non_neg_integer() => binary()}. + -type error_reason() :: {auth, reason_code()} | {code, reason_code()} | {peer_disconnected, reason_code(), binary()} | @@ -685,13 +689,13 @@ get_connack_properties(#state{session_expiry = SessExp, jid = JID}, server_keep_alive => KeepAlive}. -spec subscribe([{binary(), sub_opts()}], jid:ljid(), non_neg_integer()) -> - {[reason_code()], map(), properties()}. + {[reason_code()], subscriptions(), properties()}. subscribe(TopicFilters, USR, SubID) -> subscribe(TopicFilters, USR, SubID, [], #{}, ok). -spec subscribe([{binary(), sub_opts()}], jid:ljid(), non_neg_integer(), - [reason_code()], map(), ok | {error, error_reason()}) -> - {[reason_code()], map(), properties()}. + [reason_code()], subscriptions(), ok | {error, error_reason()}) -> + {[reason_code()], subscriptions(), properties()}. subscribe([{TopicFilter, SubOpts}|TopicFilters], USR, SubID, Codes, Subs, Err) -> case mod_mqtt:subscribe(USR, TopicFilter, SubOpts, SubID) of ok -> @@ -710,15 +714,15 @@ subscribe([], _USR, _SubID, Codes, Subs, Err) -> end, {lists:reverse(Codes), Subs, Props}. --spec unsubscribe([binary()], jid:ljid(), map()) -> - {[reason_code()], map(), properties()}. +-spec unsubscribe([binary()], jid:ljid(), subscriptions()) -> + {[reason_code()], subscriptions(), properties()}. unsubscribe(TopicFilters, USR, Subs) -> unsubscribe(TopicFilters, USR, [], Subs, ok). -spec unsubscribe([binary()], jid:ljid(), - [reason_code()], map(), + [reason_code()], subscriptions(), ok | {error, error_reason()}) -> - {[reason_code()], map(), properties()}. + {[reason_code()], subscriptions(), properties()}. unsubscribe([TopicFilter|TopicFilters], USR, Codes, Subs, Err) -> case mod_mqtt:unsubscribe(USR, TopicFilter) of ok -> @@ -740,7 +744,7 @@ unsubscribe([], _USR, Codes, Subs, Err) -> end, {lists:reverse(Codes), Subs, Props}. --spec select_retained(jid:ljid(), map(), map()) -> [{publish(), seconds()}]. +-spec select_retained(jid:ljid(), subscriptions(), subscriptions()) -> [{publish(), seconds()}]. select_retained(USR, NewSubs, OldSubs) -> lists:flatten( maps:fold( @@ -1254,7 +1258,7 @@ validate_payload(_, _, _) -> %%%=================================================================== %%% Misc %%%=================================================================== --spec resubscribe(jid:ljid(), map()) -> ok | {error, error_reason()}. +-spec resubscribe(jid:ljid(), subscriptions()) -> ok | {error, error_reason()}. resubscribe(USR, Subs) -> case maps:fold( fun(TopicFilter, {SubOpts, ID}, ok) -> diff --git a/src/mod_muc_room.erl b/src/mod_muc_room.erl index f3c8fdbb3..415a7f5de 100644 --- a/src/mod_muc_room.erl +++ b/src/mod_muc_room.erl @@ -83,10 +83,10 @@ -callback set_affiliation(binary(), binary(), binary(), jid(), affiliation(), binary()) -> ok | {error, any()}. -callback set_affiliations(binary(), binary(), binary(), - map()) -> ok | {error, any()}. + affiliations()) -> ok | {error, any()}. -callback get_affiliation(binary(), binary(), binary(), binary(), binary()) -> {ok, affiliation()} | {error, any()}. --callback get_affiliations(binary(), binary(), binary()) -> {ok, map()} | {error, any()}. +-callback get_affiliations(binary(), binary(), binary()) -> {ok, affiliations()} | {error, any()}. -callback search_affiliation(binary(), binary(), binary(), affiliation()) -> {ok, [{ljid(), {affiliation(), binary()}}]} | {error, any()}. @@ -725,17 +725,16 @@ terminate(Reason, _StateName, role = none}], status_codes = [332,110]}]}, maps:fold( - fun(LJID, Info, _) -> - Nick = Info#user.nick, + fun(_, #user{nick = Nick, jid = JID}, _) -> case Reason of shutdown -> send_wrapped(jid:replace_resource(StateData#state.jid, Nick), - Info#user.jid, Packet, + JID, Packet, ?NS_MUCSUB_NODES_PARTICIPANTS, StateData); _ -> ok end, - tab_remove_online_user(LJID, StateData) + tab_remove_online_user(JID, StateData) end, [], get_users_and_subscribers(StateData)), add_to_log(room_existence, stopped, StateData), case (StateData#state.config)#config.persistent of @@ -1159,7 +1158,7 @@ close_room_if_temporary_and_empty(StateData1) -> _ -> {next_state, normal_state, StateData1} end. --spec get_users_and_subscribers(state()) -> map(). +-spec get_users_and_subscribers(state()) -> users(). get_users_and_subscribers(StateData) -> OnlineSubscribers = maps:fold( fun(LJID, _, Acc) -> @@ -1344,7 +1343,7 @@ set_affiliation_fallback(JID, Affiliation, StateData, Reason) -> end, StateData#state{affiliations = Affiliations}. --spec set_affiliations(map(), state()) -> state(). +-spec set_affiliations(affiliations(), state()) -> state(). set_affiliations(Affiliations, #state{config = #config{persistent = false}} = StateData) -> set_affiliations_fallback(Affiliations, StateData); @@ -1360,7 +1359,7 @@ set_affiliations(Affiliations, StateData) -> set_affiliations_fallback(Affiliations, StateData) end. --spec set_affiliations_fallback(map(), state()) -> state(). +-spec set_affiliations_fallback(affiliations(), state()) -> state(). set_affiliations_fallback(Affiliations, StateData) -> StateData#state{affiliations = Affiliations}. @@ -1414,7 +1413,7 @@ do_get_affiliation_fallback(JID, StateData) -> end end. --spec get_affiliations(state()) -> map(). +-spec get_affiliations(state()) -> affiliations(). get_affiliations(#state{config = #config{persistent = false}} = StateData) -> get_affiliations_callback(StateData); get_affiliations(StateData) -> @@ -1429,7 +1428,7 @@ get_affiliations(StateData) -> Affiliations end. --spec get_affiliations_callback(state()) -> map(). +-spec get_affiliations_callback(state()) -> affiliations(). get_affiliations_callback(StateData) -> StateData#state.affiliations. @@ -3074,8 +3073,8 @@ send_kickban_presence(UJID, JID, Reason, Code, NewAffiliation, _ -> [] end end, - lists:foreach(fun (J) -> - #user{nick = Nick} = maps:get(J, StateData#state.users), + lists:foreach(fun (LJ) -> + #user{nick = Nick, jid = J} = maps:get(LJ, StateData#state.users), add_to_log(kickban, {Nick, Reason, Code}, StateData), tab_remove_online_user(J, StateData), send_kickban_presence1(UJID, J, Reason, Code, @@ -4506,7 +4505,7 @@ wrap(From, To, Packet, Node, Id) -> id = Id, sub_els = [El]}]}}]}. --spec send_wrapped_multiple(jid(), map(), stanza(), binary(), state()) -> ok. +-spec send_wrapped_multiple(jid(), users(), stanza(), binary(), state()) -> ok. send_wrapped_multiple(From, Users, Packet, Node, State) -> maps:fold( fun(_, #user{jid = To}, _) -> diff --git a/src/mod_ping.erl b/src/mod_ping.erl index 9c4940c6e..e0e2aec6d 100644 --- a/src/mod_ping.erl +++ b/src/mod_ping.erl @@ -53,12 +53,14 @@ user_send/1, mod_opt_type/1, mod_options/1, depends/2]). -record(state, - {host = <<"">> :: binary(), + {host :: binary(), send_pings :: boolean(), ping_interval :: non_neg_integer(), ping_ack_timeout :: undefined | non_neg_integer(), - timeout_action ::none | kill, - timers = maps:new() :: map()}). + timeout_action :: none | kill, + timers :: timers()}). + +-type timers() :: #{ljid() => reference()}. %%==================================================================== %% API @@ -200,7 +202,7 @@ init_state(Host, Opts) -> ping_interval = PingInterval, timeout_action = TimeoutAction, ping_ack_timeout = PingAckTimeout, - timers = maps:new()}. + timers = #{}}. register_hooks(Host) -> ejabberd_hooks:add(sm_register_connection_hook, Host, @@ -228,7 +230,7 @@ unregister_iq_handlers(Host) -> gen_iq_handler:remove_iq_handler(ejabberd_local, Host, ?NS_PING), gen_iq_handler:remove_iq_handler(ejabberd_sm, Host, ?NS_PING). --spec add_timer(jid(), non_neg_integer(), map()) -> map(). +-spec add_timer(jid(), non_neg_integer(), timers()) -> timers(). add_timer(JID, Interval, Timers) -> LJID = jid:tolower(JID), NewTimers = case maps:find(LJID, Timers) of @@ -241,7 +243,7 @@ add_timer(JID, Interval, Timers) -> {ping, JID}), maps:put(LJID, TRef, NewTimers). --spec del_timer(jid(), map()) -> map(). +-spec del_timer(jid(), timers()) -> timers(). del_timer(JID, Timers) -> LJID = jid:tolower(JID), case maps:find(LJID, Timers) of