]> granicus.if.org Git - ejabberd/commitdiff
Avoid using broad map() type wherever possible
authorEvgeny Khramtsov <ekhramtsov@process-one.net>
Thu, 27 Jun 2019 12:22:27 +0000 (15:22 +0300)
committerEvgeny Khramtsov <ekhramtsov@process-one.net>
Thu, 27 Jun 2019 12:22:27 +0000 (15:22 +0300)
13 files changed:
include/mod_muc_room.hrl
src/ejabberd_auth.erl
src/ejabberd_config.erl
src/ejabberd_redis.erl
src/ejabberd_system_monitor.erl
src/misc.erl
src/mod_avatar.erl
src/mod_client_state.erl
src/mod_http_upload.erl
src/mod_http_upload_quota.erl
src/mod_mqtt_session.erl
src/mod_muc_room.erl
src/mod_ping.erl

index ac58c1f51da2d55cb35b9ca15630cb87e7506acb..46eb149bb25cb027ac7b3a5669d1ecab27d0c61f 100644 (file)
     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(),
     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()]}.
index 33e0d118ff617fcc2a09ddbf14a3b4cc5c0d3069..fff63e81191c11ebc59afc80a7bcbdd2d8b14bb9 100644 (file)
@@ -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(
index 87bf35b08b0778122a399a7b13586579820de849..519a16e421096123bdf0db9b897bbb7398c7362d 100644 (file)
@@ -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(
index 29a8947ec75efd701a329d7c415df4fb182125ea..18a73414b82577c7d0cf083290605173cbbfaf3d 100644 (file)
 
 -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()].
index 1a4e3c765a13937c862684be5bc319988a5c6c7a..ca2e26b4e48f808ad31a45a1f8b9c69eee3f84a3 100644 (file)
@@ -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,
index 16e4122b556198fa19bd4e6d07903d6461bfde20..3aacd3a6cddaa1511cd9e077aa5b107fbad2bd8b 100644 (file)
@@ -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) ->
index 67f0aef200be5c47d521fe6ec216814743c3180a..b56d575f50dca49a89007e8fd953b83449919e19 100644 (file)
@@ -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}} ->
index 0b9bab34d06f6dd302cb0a4001793c300bf4eec4..855e4a5d89eabcf5acefc935f1d3a5a167344a13 100644 (file)
@@ -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()}.
index ef571274fd4b54ad061c4a236b0075b4c167bcc7..9cd828ebfd97d8d891974e692342d1e13d7f925f 100644 (file)
         service_url            :: binary() | undefined,
         thumbnail              :: boolean(),
         custom_headers         :: [{binary(), binary()}],
-        slots = #{}            :: map(),
+        slots = #{}            :: slots(),
         external_secret        :: binary()}).
 
 -record(media_info,
 
 -type state() :: #state{}.
 -type slot() :: [binary(), ...].
+-type slots() :: #{slot() => {pos_integer(), reference()}}.
 -type media_info() :: #media_info{}.
 
 %%--------------------------------------------------------------------
index 56d46b02ee6495ce7efab19bda4978db6c601019..4df799207d1e1c55e0880241557cef0dcfbe7e9a 100644 (file)
         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{}.
 
 %%--------------------------------------------------------------------
index d0abf85d4c0ef8b334b6fee8567a0fb2d6007110..9d90ab0a8ed74576d1aa75aeb358b5e2cbb16b03 100644 (file)
                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) ->
index f3c8fdbb31b5e10870042cf96a07609a7bbaa1e0..415a7f5de74a50efecbeeee40ac769afaac230e8 100644 (file)
 -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}, _) ->
index 9c4940c6edc1e8a6dcc93e461f4fbd21aee582e5..e0e2aec6da64406699957c9c8ea051eabc71f51b 100644 (file)
         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