• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

esl / MongooseIM / 5076995463

25 May 2023 07:04AM UTC coverage: 15.93% (-66.0%) from 81.94%
5076995463

push

github

GitHub
Merge pull request #4027 from esl/docker-script

5388 of 33824 relevant lines covered (15.93%)

1117.97 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

10.53
/src/mod_cache_users.erl
1
%% @doc Caches info about non-anonymous users using a queue of ets tables
2
-module(mod_cache_users).
3

4
-include("mongoose_config_spec.hrl").
5

6
-behaviour(gen_mod).
7

8
%% gen_mod API
9
-export([start/2, stop/1, hooks/1, config_spec/0, supported_features/0]).
10

11
%% Hooks.
12
-export([does_cached_user_exist/3, maybe_put_user_into_cache/3,
13
         remove_user/3, remove_domain/3]).
14

15
%%====================================================================
16
%% gen_mod callbacks
17
%%====================================================================
18

19
-spec start(mongooseim:host_type(), gen_mod:module_opts()) -> ok.
20
start(HostType, Opts) ->
21
    mongoose_user_cache:start_new_cache(HostType, ?MODULE, Opts),
×
22
    ok.
×
23

24
-spec stop(mongooseim:host_type()) -> ok.
25
stop(HostType) ->
26
    mongoose_user_cache:stop_cache(HostType, ?MODULE),
×
27
    ok.
×
28

29
-spec config_spec() -> mongoose_config_spec:config_section().
30
config_spec() ->
31
    #section{items = Items,
12,808✔
32
             defaults = Defaults} = Sec = mongoose_user_cache:config_spec(),
33
    Sec#section{items = maps:remove(<<"module">>, Items),
12,808✔
34
                defaults = maps:remove(<<"module">>, Defaults)}.
35

36
-spec supported_features() -> [atom()].
37
supported_features() ->
38
    [dynamic_domains].
×
39

40
hooks(HostType) ->
41
    [
×
42
     %% These hooks must run before and after the ejabberd_auth does_user_exist hook
43
     {does_user_exist, HostType, fun ?MODULE:does_cached_user_exist/3, #{}, 30},
44
     {does_user_exist, HostType, fun ?MODULE:maybe_put_user_into_cache/3, #{}, 70},
45
     %% It is important that these two handlers happen _after_ ejabberd_auth
46
     %% but _before_ all other modules
47
     {remove_user, HostType, fun ?MODULE:remove_user/3, #{}, 10},
48
     {remove_domain, HostType, fun ?MODULE:remove_domain/3, #{}, 20}
49
    ].
50

51
%%====================================================================
52
%% Hooks
53
%%====================================================================
54

55
-spec remove_user(Acc, Params, Extra) -> {ok, Acc} when
56
      Acc :: mongoose_acc:t(),
57
      Params :: #{jid := jid:jid()},
58
      Extra :: #{host_type := mongooseim:host_type()}.
59
remove_user(Acc, #{jid := Jid}, #{host_type := HostType}) ->
60
    mongoose_user_cache:delete_user(HostType, ?MODULE, Jid),
×
61
    {ok, Acc}.
×
62

63
-spec remove_domain(Acc, Params, Extra) -> {ok , Acc} when
64
      Acc :: mongoose_domain_api:remove_domain_acc(),
65
      Params :: #{domain := jid:lserver()},
66
      Extra :: #{host_type := mongooseim:host_type()}.
67
remove_domain(Acc, #{domain := Domain}, #{host_type := HostType}) ->
68
    mongoose_user_cache:delete_domain(HostType, ?MODULE, Domain),
×
69
    {ok, Acc}.
×
70

71
-spec does_cached_user_exist(Acc, Params, Extra) -> {stop | ok , Acc} when
72
      Acc :: boolean(),
73
      Params :: #{jid := jid:jid(), request_type := ejabberd_auth:exist_type()},
74
      Extra :: #{host_type := mongooseim:host_type()}.
75
does_cached_user_exist(false,
76
                       #{jid := Jid, request_type := stored},
77
                       #{host_type := HostType}) ->
78
    case mongoose_user_cache:is_member(HostType, ?MODULE, Jid) of
×
79
        true -> {stop, true};
×
80
        false -> {ok, false}
×
81
    end;
82
does_cached_user_exist(Acc, _, _) ->
83
    {ok, Acc}.
×
84

85
-spec maybe_put_user_into_cache(Acc, Params, Extra) -> {ok , Acc} when
86
      Acc :: boolean(),
87
      Params :: #{jid := jid:jid(), request_type := ejabberd_auth:exist_type()},
88
      Extra :: #{host_type := mongooseim:host_type()}.
89
maybe_put_user_into_cache(true,
90
                          #{jid := Jid, request_type := stored},
91
                          #{host_type := HostType}) ->
92
    mongoose_user_cache:merge_entry(HostType, ?MODULE, Jid, #{}),
×
93
    {ok, true};
×
94
maybe_put_user_into_cache(Acc, _, _) ->
95
    {ok, Acc}.
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc