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 |
:-( |
case mongoose_domain_api:get_host_type(LS) of |
18 |
|
{ok, HostType} -> |
19 |
:-( |
case ejabberd_auth:does_user_exist(JID) of |
20 |
|
true -> |
21 |
:-( |
FromTS = days_to_timestamp(Days), |
22 |
:-( |
Count = mod_inbox_backend:empty_user_bin(HostType, LS, LU, FromTS), |
23 |
:-( |
{ok, Count}; |
24 |
|
false -> |
25 |
:-( |
?USER_NOT_FOUND_RESULT(LU, LS) |
26 |
|
end; |
27 |
|
{error, not_found} -> |
28 |
:-( |
?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 |
:-( |
LDomain = jid:nodeprep(Domain), |
35 |
:-( |
case mongoose_domain_api:get_host_type(LDomain) of |
36 |
|
{ok, HostType} -> |
37 |
:-( |
FromTS = days_to_timestamp(Days), |
38 |
:-( |
Count = mod_inbox_backend:empty_domain_bin(HostType, Domain, FromTS), |
39 |
:-( |
{ok, Count}; |
40 |
|
{error, not_found} -> |
41 |
:-( |
?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 |
:-( |
case validate_host_type(HostType) of |
48 |
|
ok -> |
49 |
:-( |
FromTS = days_to_timestamp(Days), |
50 |
:-( |
Count = mod_inbox_backend:empty_global_bin(HostType, FromTS), |
51 |
:-( |
{ok, Count}; |
52 |
|
{host_type_not_found, _} = Error -> |
53 |
:-( |
Error |
54 |
|
end. |
55 |
|
|
56 |
|
%% Internal |
57 |
|
|
58 |
|
validate_host_type(HostType) -> |
59 |
:-( |
case lists:member(HostType, ?ALL_HOST_TYPES) of |
60 |
|
true -> |
61 |
:-( |
ok; |
62 |
|
false -> |
63 |
:-( |
{host_type_not_found, <<"Host type not found">>} |
64 |
|
end. |
65 |
|
|
66 |
|
days_to_timestamp(Days) -> |
67 |
:-( |
Now = erlang:system_time(microsecond), |
68 |
:-( |
mod_inbox_utils:calculate_ts_from(Now, Days). |