./ct_report/coverage/mod_inbox_api.COVER.html

1 %% @doc Provide an interface for frontends (like graphql or ctl) to manage inbox.
2 -module(mod_inbox_api).
3
4 -export([flush_user_bin/2, flush_domain_bin/2, flush_global_bin/2]).
5
6 -include("mongoose.hrl").
7 -include_lib("jid/include/jid.hrl").
8
9 -define(DOMAIN_NOT_FOUND_RESULT, {domain_not_found, <<"Domain not found">>}).
10
11 -define(USER_NOT_FOUND_RESULT(User, Server),
12 {user_does_not_exist, io_lib:format("User ~s@~s does not exist", [User, Server])}).
13
14 -spec flush_user_bin(jid:jid(), Days :: integer()) ->
15 {ok, integer()} | {domain_not_found | user_does_not_exist, iodata()}.
16 flush_user_bin(#jid{luser = LU, lserver = LS} = JID, Days) ->
17 16 case mongoose_domain_api:get_host_type(LS) of
18 {ok, HostType} ->
19 13 case ejabberd_auth:does_user_exist(JID) of
20 true ->
21 7 FromTS = days_to_timestamp(Days),
22 7 Count = mod_inbox_backend:empty_user_bin(HostType, LS, LU, FromTS),
23 7 {ok, Count};
24 false ->
25 6 ?USER_NOT_FOUND_RESULT(LU, LS)
26 end;
27 {error, not_found} ->
28 3 ?DOMAIN_NOT_FOUND_RESULT
29 end.
30
31 -spec flush_domain_bin(jid:server(), Days :: integer()) ->
32 {ok, integer()} | {domain_not_found, iodata()}.
33 flush_domain_bin(Domain, Days) ->
34 9 LDomain = jid:nodeprep(Domain),
35 9 case mongoose_domain_api:get_host_type(LDomain) of
36 {ok, HostType} ->
37 7 FromTS = days_to_timestamp(Days),
38 7 Count = mod_inbox_backend:empty_domain_bin(HostType, Domain, FromTS),
39 7 {ok, Count};
40 {error, not_found} ->
41 2 ?DOMAIN_NOT_FOUND_RESULT
42 end.
43
44 -spec flush_global_bin(mongooseim:host_type(), Days :: integer()) ->
45 {ok, integer()} | {host_type_not_found, binary()}.
46 flush_global_bin(HostType, Days) ->
47 21 case validate_host_type(HostType) of
48 ok ->
49 18 FromTS = days_to_timestamp(Days),
50 18 Count = mod_inbox_backend:empty_global_bin(HostType, FromTS),
51 18 {ok, Count};
52 {host_type_not_found, _} = Error ->
53 3 Error
54 end.
55
56 %% Internal
57
58 validate_host_type(HostType) ->
59 21 case lists:member(HostType, ?ALL_HOST_TYPES) of
60 true ->
61 18 ok;
62 false ->
63 3 {host_type_not_found, <<"Host type not found">>}
64 end.
65
66 days_to_timestamp(Days) ->
67 32 Now = erlang:system_time(microsecond),
68 32 mod_inbox_utils:calculate_ts_from(Now, Days).
Line Hits Source