25
1
mirror of https://github.com/processone/ejabberd.git synced 2024-11-20 16:15:59 +01:00

Avoid using broad map() type wherever possible

This commit is contained in:
Evgeny Khramtsov 2019-06-27 15:22:27 +03:00
parent 6b684c866b
commit 39cf8d86d6
13 changed files with 75 additions and 53 deletions

View File

@ -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()]}.

View 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(

View 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(

View File

@ -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()].

View 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,

View 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) ->

View 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}} ->

View 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()}.

View File

@ -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{}.
%%--------------------------------------------------------------------

View File

@ -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{}.
%%--------------------------------------------------------------------

View File

@ -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) ->

View File

@ -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}, _) ->

View File

@ -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