Skip to content

Commit

Permalink
Wip
Browse files Browse the repository at this point in the history
  • Loading branch information
Premwoik committed Jun 24, 2022
1 parent d0e2e34 commit 7c70d7f
Show file tree
Hide file tree
Showing 15 changed files with 298 additions and 15 deletions.
1 change: 1 addition & 0 deletions big_tests/default.spec
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
{suites, "tests", graphql_SUITE}.
{suites, "tests", graphql_account_SUITE}.
{suites, "tests", graphql_domain_SUITE}.
{suites, "tests", graphql_inbox_SUITE}.
{suites, "tests", graphql_last_SUITE}.
{suites, "tests", graphql_muc_SUITE}.
{suites, "tests", graphql_muc_light_SUITE}.
Expand Down
1 change: 1 addition & 0 deletions big_tests/dynamic_domains.spec
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
{suites, "tests", graphql_SUITE}.
{suites, "tests", graphql_account_SUITE}.
{suites, "tests", graphql_domain_SUITE}.
{suites, "tests", graphql_inbox_SUITE}.
{suites, "tests", graphql_last_SUITE}.
{suites, "tests", graphql_muc_SUITE}.
{suites, "tests", graphql_muc_light_SUITE}.
Expand Down
140 changes: 140 additions & 0 deletions big_tests/tests/graphql_inbox_SUITE.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
-module(graphql_inbox_SUITE).

-compile([export_all, nowarn_export_all]).

-import(distributed_helper, [mim/0, require_rpc_nodes/1, rpc/4]).
-import(graphql_helper, [execute_user/3, execute_auth/2, user_to_bin/1, user_to_jid/1,
get_ok_value/2, get_err_msg/1, get_err_code/1]).

-include_lib("eunit/include/eunit.hrl").

-define(assertErrMsg(Res, ContainsPart), assert_err_msg(ContainsPart, Res)).
-define(assertErrCode(Res, Code), assert_err_code(Code, Res)).

-define(NONEXISTENT_JID, <<"[email protected]">>).
-define(DEFAULT_DT, <<"2022-04-17T12:58:30.000000Z">>).

suite() ->
require_rpc_nodes([mim]) ++ escalus:suite().

all() ->
[{group, user_inbox},
{group, admin_inbox}].

groups() ->
[{user_inbox, [parallel], user_inbox_handler()},
{admin_last, [parallel], admin_inbox_handler()}].

user_inbox_handler() ->
[user_flush_own_bin].

admin_inbox_handler() ->
[admin_flush_user_bin,
admin_try_flush_nonexistent_user_bin,
admin_flush_domain_bin,
admin_try_flush_nonexistent_domain_bin,
admin_flush_global_bin,
admin_try_flush_nonexistent_host_type_bin].

init_per_suite(Config) ->
HostType = domain_helper:host_type(),
Config1 = dynamic_modules:save_modules([HostType], Config),
InboxOptions = inbox_helper:inbox_opts(regular),
ok = dynamic_modules:ensure_modules(HostType, [{mod_inbox, InboxOptions}]),
escalus:init_per_suite(Config1).

end_per_suite(Config) ->
dynamic_modules:restore_modules(Config),
escalus:end_per_suite(Config).

init_per_group(admin_inbox, Config) ->
graphql_helper:init_admin_handler(Config);
init_per_group(user_inbox, Config) ->
[{schema_endpoint, user} | Config].

end_per_group(_, _Config) ->
escalus_fresh:clean().

init_per_testcase(CaseName, Config) ->
escalus:init_per_testcase(CaseName, Config).

end_per_testcase(CaseName, Config) ->
escalus:end_per_testcase(CaseName, Config).

%% Admin test cases

admin_flush_user_bin(Config) ->
escalus:fresh_story_with_config(Config, [{alice, 1}],
fun admin_flush_user_bin/2).

admin_flush_user_bin(Config, Alice) ->
%% TODO add test body
?assert(false).

admin_flush_domain_bin(Config) ->
escalus:fresh_story_with_config(Config, [{alice, 1}],
fun admin_flush_domain_bin/2).

admin_flush_domain_bin(Config, Alice) ->
%% TODO add test body
?assert(false).

admin_flush_global_bin(Config) ->
escalus:fresh_story_with_config(Config, [{alice, 1}],
fun admin_flush_global_bin/2).

admin_flush_global_bin(Config, Alice) ->
%% TODO add test body
?assert(false).

admin_try_flush_nonexistent_user_bin(Config) ->
%% TODO add test body
?assert(false).

admin_try_flush_nonexistent_domain_bin(Config) ->
%% TODO add test body
?assert(false).

admin_try_flush_nonexistent_host_type_bin(Config) ->
%% TODO add test body
?assert(false).


%% User test cases

user_flush_own_bin(Config) ->
escalus:fresh_story_with_config(Config, [{alice, 1}],
fun user_flush_own_bin/2).

user_flush_own_bin(Config, Alice) ->
%% TODO add test body
?assert(false).

%% Request bodies

admin_flush_user_bin_body(User, Days) ->
Query = <<"mutation M1($user: JID!, $days: Int!)
{ inbox { flushUserBin(user: $user, days: $days) } }">>,
OpName = <<"M1">>,
Vars = #{user => user_to_bin(User), days => Days},
#{query => Query, operationName => OpName, variables => Vars}.

admin_flush_domain_bin_body(Domain, Days) ->
Query = <<"mutation M1($domain: String!, $days: Int!)
{ inbox { flushDomainBin(domain: $domain, days: $days) } }">>,
OpName = <<"M1">>,
Vars = #{domain => Domain, days => Days},
#{query => Query, operationName => OpName, variables => Vars}.

admin_flush_global_bin_body(HostType, Days) ->
Query = <<"mutation M1($hostType: String!, $days: Int!)
{ inbox { flushDomainBin(hostType: $hostType, days: $days) } }">>,
OpName = <<"M1">>,
Vars = #{hostType => HostType, days => Days},
#{query => Query, operationName => OpName, variables => Vars}.

user_flush_own_bin_body(Days) ->
Query = <<"mutation M1($days: Days) { inbox { flushBin(days: Int!) } }">>,
OpName = <<"M1">>,
Vars = #{days => Days},
#{query => Query, operationName => OpName, variables => Vars}.
5 changes: 2 additions & 3 deletions big_tests/tests/inbox_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -159,16 +159,15 @@ end_per_suite(Config) ->
init_per_group(GroupName, Config) when GroupName =:= regular; GroupName =:= async_pools ->
HostType = domain_helper:host_type(),
SecHostType = domain_helper:secondary_host_type(),
Config1 = dynamic_modules:save_modules(HostType, Config),
Config2 = dynamic_modules:save_modules(SecHostType, Config1),
Config1 = dynamic_modules:save_modules([HostType, SecHostType], Config),
InboxOptions = inbox_helper:inbox_opts(GroupName),
ok = dynamic_modules:ensure_modules(HostType,
inbox_helper:inbox_modules(GroupName)
++ inbox_helper:muclight_modules()
++ [{mod_offline, config_parser_helper:default_mod_config(mod_offline)}]),
ok = dynamic_modules:ensure_modules(SecHostType,
[{mod_inbox, InboxOptions#{aff_changes := false}}]),
[{inbox_opts, InboxOptions} | Config2];
[{inbox_opts, InboxOptions} | Config1];

init_per_group(muclight, Config) ->
ok = dynamic_modules:ensure_modules(domain_helper:host_type(), muclight_modules()),
Expand Down
14 changes: 14 additions & 0 deletions priv/graphql/schemas/admin/inbox.gql
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
"""
Allow admin to flush the inbox bin".
"""
type InboxAdminMutation @protected{
"Flush the user's bin and return deleted rows number"
flushUserBin(user: JID!, days: Int!): Int
@protected(type: DOMAIN, args: ["user"])
"Flush the whole domain bin and return deleted rows number"
flushDomainBin(domain: String!, days: Int!): Int
@protected(type: Domain, args: ["domain"])
"Flush the whole host type bin and return deleted rows number"
flushGlobalBin(hostType: String!, days: Int!): Int
@protected(type: GLOBAL)
}
7 changes: 7 additions & 0 deletions priv/graphql/schemas/user/inbox.gql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
"""
Allow user to flush own inbox bin".
"""
type InboxUserMutation @protected{
"Flush the user's bin and return deleted rows number"
flushBin(Days: Int!): Int
}
6 changes: 4 additions & 2 deletions src/graphql/admin/mongoose_graphql_admin_mutation.erl
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ execute(_Ctx, _Obj, <<"account">>, _Args) ->
{ok, account};
execute(_Ctx, _Obj, <<"domains">>, _Args) ->
{ok, admin};
execute(_Ctx, _Obj, <<"httpUpload">>, _Args) ->
{ok, httpUpload};
execute(_Ctx, _Obj, <<"inbox">>, _Args) ->
{ok, inbox};
execute(_Ctx, _Obj, <<"last">>, _Args) ->
{ok, last};
execute(_Ctx, _Obj, <<"muc">>, _Args) ->
{ok, muc};
execute(_Ctx, _Obj, <<"httpUpload">>, _Args) ->
{ok, httpUpload};
execute(_Ctx, _Obj, <<"muc_light">>, _Args) ->
{ok, muc_light};
execute(_Ctx, _Obj, <<"offline">>, _Args) ->
Expand Down
19 changes: 19 additions & 0 deletions src/graphql/admin/mongoose_graphql_inbox_admin_mutation.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
-module(mongoose_graphql_inbox_admin_mutation).

-behaviour(mongoose_graphql).

-export([execute/4]).

-import(mongoose_graphql_helper, [format_result/2]).

-ignore_xref([execute/4]).

execute(_Ctx, inbox, <<"flushUserBin">>, #{<<"user">> := UserJID, <<"days">> := Days}) ->
Res = mod_inbox_api:flush_user_bin(UserJID, Days),
format_result(Res, #{user => jid:to_binary(UserJID)});
execute(_Ctx, inbox, <<"flushDomainBin">>, #{<<"domain">> := Domain, <<"days">> := Days}) ->
Res = mod_offline_api:delete_expired_messages(Domain, Days),
format_result(Res, #{domain => Domain});
execute(_Ctx, inbox, <<"flushGlobalBin">>, #{<<"hostType">> := HostType, <<"days">> := Days}) ->
Res = mod_inbox_api:flush_global_bin(HostType, Days),
format_result(Res, #{host_type => HostType}).
2 changes: 2 additions & 0 deletions src/graphql/mongoose_graphql.erl
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ admin_mapping_rules() ->
'DomainAdminQuery' => mongoose_graphql_domain_admin_query,
'AdminMutation' => mongoose_graphql_admin_mutation,
'DomainAdminMutation' => mongoose_graphql_domain_admin_mutation,
'InboxAdminMutation' => mongooseim_graphql_inbox_admin_mutation,
'SessionAdminMutation' => mongoose_graphql_session_admin_mutation,
'SessionAdminQuery' => mongoose_graphql_session_admin_query,
'StanzaAdminMutation' => mongoose_graphql_stanza_admin_mutation,
Expand Down Expand Up @@ -166,6 +167,7 @@ user_mapping_rules() ->
'UserMutation' => mongoose_graphql_user_mutation,
'AccountUserQuery' => mongoose_graphql_account_user_query,
'AccountUserMutation' => mongoose_graphql_account_user_mutation,
'InboxUserMutation' => mongooseim_graphql_inbox_user_mutation,
'MUCUserMutation' => mongoose_graphql_muc_user_mutation,
'MUCUserQuery' => mongoose_graphql_muc_user_query,
'MUCLightUserMutation' => mongoose_graphql_muc_light_user_mutation,
Expand Down
13 changes: 13 additions & 0 deletions src/graphql/user/mongoose_graphql_inbox_user_mutation.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
-module(mongoose_graphql_inbox_user_mutation).

-behaviour(mongoose_graphql).

-export([execute/4]).

-import(mongoose_graphql_helper, [format_result/2]).

-ignore_xref([execute/4]).

execute(#{user := UserJID}, inbox, <<"flushBin">>, #{<<"days">> := Days}) ->
Res = mod_inbox_api:flush_user_bin(UserJID, Days),
format_result(Res, #{user => jid:to_binary(UserJID)}).
6 changes: 4 additions & 2 deletions src/graphql/user/mongoose_graphql_user_mutation.erl
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@

execute(_Ctx, _Obj, <<"account">>, _Args) ->
{ok, account};
execute(_Ctx, _Obj, <<"httpUpload">>, _Args) ->
{ok, httpUpload};
execute(_Ctx, _Obj, <<"inbox">>, _Args) ->
{ok, inbox};
execute(_Ctx, _Obj, <<"last">>, _Args) ->
{ok, last};
execute(_Ctx, _Obj, <<"muc">>, _Args) ->
{ok, muc};
execute(_Ctx, _Obj, <<"httpUpload">>, _Args) ->
{ok, httpUpload};
execute(_Ctx, _Obj, <<"muc_light">>, _Args) ->
{ok, muc_light};
execute(_Ctx, _Obj, <<"private">>, _Args) ->
Expand Down
62 changes: 62 additions & 0 deletions src/inbox/mod_inbox_api.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
-module(mod_inbox_api).

-export([flush_user_bin/2, flush_domain_bin/2, flush_global_bin/2]).

-include_lib("jid/include/jid.hrl").

-spec flush_user_bin(jid:jid(), Days :: integer()) ->
{ok, integer()} | {domain_not_found, binary()}.
flush_user_bin(#jid{luser = LU, lserver = LS} = JID, Days) ->
case mongoose_domain_api:get_host_type(LS) of
{ok, HostType} ->
case ejabberd_auth:does_user_exist(JID) of
true ->
FromTS = days_to_timestamp(Days),
Count = mod_inbox_backend:empty_user_bin(HostType, LS, LU, FromTS),
{ok, Count};
false ->
{user_not_found, <<"User does not exists">>}
end;
{error, not_found} ->
{domain_not_found, <<"Domain not found">>}
end.

-spec flush_domain_bin(jid:server(), Days :: integer()) ->
{ok, integer()} | {domain_not_found, iodata()}.
flush_domain_bin(Domain, Days) ->
LDomain = jid:nodeprep(Domain),
case mongoose_domain_api:get_host_type(LDomain) of
{ok, HostType} ->
FromTS = days_to_timestamp(Days),
Count = mod_inbox_backend:empty_domain_bin(HostType, Domain, FromTS),
{ok, Count};
{host_type_not_found, _} = Error ->
Error
end.

-spec flush_global_bin(mongooseim:host_type(), Days :: integer()) ->
{ok, integer()} | {host_type_not_found, binary()}.
flush_global_bin(HostType, Days) ->
case validate_host_type(HostType) of
ok ->
FromTS = days_to_timestamp(Days),
Count = mod_inbox_backend:empty_global_bin(HostType, FromTS),
{ok, Count};
{host_type_not_found, _} = Error ->
Error
end.

%% Internal

validate_host_type(HostType) ->
HostTypes = mongoose_config:get_opt(host_types),
case lists:member(HostType, HostTypes) of
true ->
ok;
false ->
{host_type_not_found, <<"Host type not found">>}
end.

days_to_timestamp(Days) ->
Now = erlang:system_time(microsecond),
mod_inbox_utils:calculate_ts_from(Now, Days).
9 changes: 9 additions & 0 deletions src/inbox/mod_inbox_backend.erl
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
set_inbox/6,
remove_inbox_row/2,
empty_user_bin/4,
empty_domain_bin/3,
empty_global_bin/2,
set_inbox_incr_unread/5,
get_inbox_unread/2,
Expand Down Expand Up @@ -157,6 +158,14 @@ empty_user_bin(HostType, LServer, LUser, TS) ->
Args = [HostType, LServer, LUser, TS],
mongoose_backend:call(HostType, ?MAIN_MODULE, ?FUNCTION_NAME, Args).

-spec empty_domain_bin(HostType, LServer, TS) -> non_neg_integer() when
HostType :: mongooseim:host_type(),
LServer :: jid:lserver(),
TS :: integer().
empty_domain_bin(HostType, LServer, TS) ->
Args = [HostType, LServer, TS],
mongoose_backend:call(HostType, ?MAIN_MODULE, ?FUNCTION_NAME, Args).

-spec empty_global_bin(HostType, TS) -> non_neg_integer() when
HostType :: mongooseim:host_type(),
TS :: integer().
Expand Down
16 changes: 8 additions & 8 deletions src/inbox/mod_inbox_commands.erl
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,13 @@ commands() ->
].

flush_user_bin(Domain, Name, Days) ->
{LU, LS} = jid:to_lus(jid:make_bare(Name, Domain)),
{ok, HostType} = mongoose_domain_api:get_host_type(LS),
Now = erlang:system_time(microsecond),
FromTS = mod_inbox_utils:calculate_ts_from(Now, Days),
mod_inbox_backend:empty_user_bin(HostType, LS, LU, FromTS).
JID = jid:make_bare(Name, Domain),
Res = mod_inbox_api:flush_user_bin(JID, Days),
format_result(Res).

flush_global_bin(HostType, Days) ->
Now = erlang:system_time(microsecond),
FromTS = mod_inbox_utils:calculate_ts_from(Now, Days),
mod_inbox_backend:empty_global_bin(HostType, FromTS).
Res = mod_inbox_api:flush_global_bin(HostType, Days),
format_result(Res).

format_result({ok, Count}) -> Count;
format_result({_, ErrMsg}) -> {error, bad_request, ErrMsg}.
Loading

0 comments on commit 7c70d7f

Please sign in to comment.