Skip to content

Commit

Permalink
Add a test to create rooms with custom options
Browse files Browse the repository at this point in the history
  • Loading branch information
arcusfelis committed Aug 30, 2022
1 parent 31d7fbe commit 2d64673
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 41 deletions.
27 changes: 27 additions & 0 deletions big_tests/tests/graphql_muc_light_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ groups() ->

user_muc_light_tests() ->
[user_create_room,
user_create_room_with_custom_fields,
user_create_identified_room,
user_change_room_config,
user_change_room_config_errors,
Expand Down Expand Up @@ -148,6 +149,24 @@ user_create_room_story(Config, Alice) ->
Res2 = user_create_room(Alice, ?UNKNOWN_DOMAIN, Name, Subject, null, Config),
?assertNotEqual(nomatch, binary:match(get_err_msg(Res2), <<"not found">>)).

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

user_create_room_with_custom_fields_story(Config, Alice) ->
MucServer = ?config(muc_light_host, Config),
AliceBinLower = escalus_utils:jid_to_lower(escalus_client:short_jid(Alice)),
Name = <<"room with custom fields">>,
Subject = <<"testing_custom">>,
Opts = [#{<<"key">> => <<"background">>, <<"value">> => <<"red">>},
#{<<"key">> => <<"roomname">>, <<"value">> => Name},
#{<<"key">> => <<"subject">>, <<"value">> => Subject}],
Res = user_create_room_with_options(Alice, MucServer, Name, Subject, null, #{<<"background">> => <<"red">>}, Config),
#{<<"jid">> := JID, <<"name">> := Name, <<"subject">> := Subject,
<<"participants">> := Participants, <<"options">> := Opts}
= get_ok_value(?CREATE_ROOM_PATH, Res),
?assertMatch(#jid{lserver = MucServer}, jid:from_binary(JID)),
?assertEqual([#{<<"jid">> => AliceBinLower, <<"affiliation">> => <<"OWNER">>}], Participants).

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

Expand Down Expand Up @@ -926,10 +945,18 @@ user_create_room(User, MUCDomain, Name, Subject, Id, Config) ->
<<"id">> => Id},
execute_user_command(<<"muc_light">>, <<"createRoom">>, User, Vars, Config).

user_create_room_with_options(User, MUCDomain, Name, Subject, Id, CustomFields, Config) ->
Vars = #{<<"mucDomain">> => MUCDomain, <<"name">> => Name, <<"subject">> => Subject,
<<"id">> => Id, <<"options">> => format_options(CustomFields)},
execute_user_command(<<"muc_light">>, <<"createRoom">>, User, Vars, Config).

user_change_room_configuration(User, RoomJID, Name, Subject, Config) ->
Vars = #{<<"room">> => RoomJID, <<"name">> => Name, <<"subject">> => Subject},
execute_user_command(<<"muc_light">>, <<"changeRoomConfiguration">>, User, Vars, Config).

format_options(Map) ->
[#{<<"key">> => K, <<"value">> => V} || {K, V} <- maps:to_list(Map)].

user_invite_user(User, RoomJID, Recipient, Config) ->
Vars = #{<<"room">> => RoomJID, <<"recipient">> => Recipient},
execute_user_command(<<"muc_light">>, <<"inviteUser">>, User, Vars, Config).
Expand Down
7 changes: 2 additions & 5 deletions priv/graphql/schemas/admin/muc_light.gql
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,10 @@ Allow admin to manage Multi-User Chat Light rooms.
"""
type MUCLightAdminMutation @protected{
"Create a MUC light room under the given XMPP hostname"
createRoom(mucDomain: String!, name: String!, owner: JID!, subject: String!, id: NonEmptyString): Room
createRoom(mucDomain: String!, name: String!, owner: JID!, subject: String!, id: NonEmptyString, options: [RoomConfigDictEntryInput!]): Room
@protected(type: DOMAIN, args: ["owner"])
"Change configuration of a MUC Light room"
changeRoomConfiguration(room: JID!, owner: JID!, name: String!, subject: String!): Room
@protected(type: DOMAIN, args: ["room"])
"Update configuration of a MUC Light room"
updateRoomConfiguration(room: JID!, owner: JID!, options: [RoomConfigDictEntryInput!]!): Room
changeRoomConfiguration(room: JID!, owner: JID!, name: String!, subject: String!, options: [RoomConfigDictEntryInput!]): Room
@protected(type: DOMAIN, args: ["room"])
"Invite a user to a MUC Light room"
inviteUser(room: JID!, sender: JID!, recipient: JID!): String
Expand Down
6 changes: 2 additions & 4 deletions priv/graphql/schemas/user/muc_light.gql
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@ Allow user to manage Multi-User Chat Light rooms.
"""
type MUCLightUserMutation @protected{
"Create a MUC light room under the given XMPP hostname"
createRoom(mucDomain: String!, name: String!, subject: String!, id: NonEmptyString): Room
createRoom(mucDomain: String!, name: String!, subject: String!, id: NonEmptyString, options: [RoomConfigDictEntryInput!]): Room
"Change configuration of a MUC Light room"
changeRoomConfiguration(room: JID!, name: String!, subject: String!): Room
"Update configuration of a MUC Light room"
updateRoomConfiguration(room: JID!, options: [RoomConfigDictEntryInput!]!): Room
changeRoomConfiguration(room: JID!, name: String!, subject: String!, options: [RoomConfigDictEntryInput!]): Room
"Invite a user to a MUC Light room"
inviteUser(room: JID!, recipient: JID!): String
"Remove a MUC Light room"
Expand Down
16 changes: 5 additions & 11 deletions src/graphql/admin/mongoose_graphql_muc_light_admin_mutation.erl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@

-import(mongoose_graphql_helper, [make_error/2, format_result/2]).
-import(mongoose_graphql_muc_light_helper, [make_room/1, make_ok_user/1,
prepare_blocking_items/1]).
prepare_blocking_items/1,
null_to_default/2, options_to_map/1]).

execute(_Ctx, _Obj, <<"createRoom">>, Args) ->
create_room(Args);
Expand All @@ -27,17 +28,10 @@ execute(_Ctx, _Obj, <<"setBlockingList">>, Args) ->
set_blocking_list(Args).

-spec create_room(map()) -> {ok, map()} | {error, resolver_error()}.
create_room(#{<<"id">> := null, <<"mucDomain">> := MUCDomain, <<"name">> := RoomName,
<<"owner">> := CreatorJID, <<"subject">> := Subject}) ->
case mod_muc_light_api:create_room(MUCDomain, CreatorJID, RoomName, Subject) of
{ok, Room} ->
{ok, make_room(Room)};
Err ->
make_error(Err, #{mucDomain => MUCDomain, creator => CreatorJID})
end;
create_room(#{<<"id">> := RoomID, <<"mucDomain">> := MUCDomain, <<"name">> := RoomName,
<<"owner">> := CreatorJID, <<"subject">> := Subject}) ->
case mod_muc_light_api:create_room(MUCDomain, RoomID, CreatorJID, RoomName, Subject) of
<<"owner">> := CreatorJID, <<"subject">> := Subject, <<"options">> := Options}) ->
case mod_muc_light_api:create_room(MUCDomain, null_to_default(RoomID, <<>>), CreatorJID,
RoomName, Subject, options_to_map(Options)) of
{ok, Room} ->
{ok, make_room(Room)};
Err ->
Expand Down
13 changes: 12 additions & 1 deletion src/graphql/mongoose_graphql_muc_light_helper.erl
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
-module(mongoose_graphql_muc_light_helper).

-export([make_room/1, make_ok_user/1, blocking_item_to_map/1,
prepare_blocking_items/1, page_size_or_max_limit/2]).
prepare_blocking_items/1, page_size_or_max_limit/2,
null_to_default/2, options_to_map/1]).

-spec page_size_or_max_limit(null | integer(), integer()) -> integer().
page_size_or_max_limit(null, MaxLimit) ->
Expand Down Expand Up @@ -29,3 +30,13 @@ blocking_item_to_map({What, Action, Who}) ->

make_options(Options) ->
[{ok, #{<<"key">> => K, <<"value">> => V}} || {K, V} <- lists:sort(maps:to_list(Options))].

null_to_default(null, Default) ->
Default;
null_to_default(Value, _Default) ->
Value.

options_to_map(null) ->
#{};
options_to_map(Options) ->
maps:from_list([{K, V} || #{<<"key">> := K, <<"value">> := V} <- Options]).
24 changes: 6 additions & 18 deletions src/graphql/user/mongoose_graphql_muc_light_user_mutation.erl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
-include("../mongoose_graphql_types.hrl").

-import(mongoose_graphql_helper, [make_error/2, format_result/2]).
-import(mongoose_graphql_muc_light_helper, [make_room/1, make_ok_user/1, prepare_blocking_items/1]).
-import(mongoose_graphql_muc_light_helper, [make_room/1, make_ok_user/1, prepare_blocking_items/1,
null_to_default/2, options_to_map/1]).

execute(Ctx, _Obj, <<"createRoom">>, Args) ->
create_room(Ctx, Args);
Expand All @@ -26,17 +27,11 @@ execute(Ctx, _Obj, <<"setBlockingList">>, Args) ->
set_blocking_list(Ctx, Args).

-spec create_room(map(), map()) -> {ok, map()} | {error, resolver_error()}.
create_room(#{user := UserJID}, #{<<"id">> := null, <<"mucDomain">> := MUCDomain,
<<"name">> := RoomName, <<"subject">> := Subject}) ->
case mod_muc_light_api:create_room(MUCDomain, UserJID, RoomName, Subject) of
{ok, Room} ->
{ok, make_room(Room)};
Err ->
make_error(Err, #{mucDomain => MUCDomain})
end;
create_room(#{user := UserJID}, #{<<"id">> := RoomID, <<"mucDomain">> := MUCDomain,
<<"name">> := RoomName, <<"subject">> := Subject}) ->
case mod_muc_light_api:create_room(MUCDomain, RoomID, UserJID, RoomName, Subject) of
<<"name">> := RoomName, <<"subject">> := Subject,
<<"options">> := Options}) ->
case mod_muc_light_api:create_room(MUCDomain, null_to_default(RoomID, <<>>), UserJID,
RoomName, Subject, options_to_map(Options)) of
{ok, Room} ->
{ok, make_room(Room)};
Err ->
Expand Down Expand Up @@ -81,10 +76,3 @@ set_blocking_list(#{user := UserJID}, #{<<"items">> := Items}) ->
Items2 = prepare_blocking_items(Items),
Result = mod_muc_light_api:set_blocking(UserJID, Items2),
format_result(Result, #{user => jid:to_binary(UserJID)}).

%% Helpers

null_to_default(null, Default) ->
Default;
null_to_default(Value, _Default) ->
Value.
8 changes: 6 additions & 2 deletions src/muc_light/mod_muc_light_api.erl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

-export([create_room/4,
create_room/5,
create_room/6,
invite_to_room/3,
change_room_config/3,
change_affiliation/4,
Expand Down Expand Up @@ -53,9 +54,12 @@ create_room(MUCLightDomain, CreatorJID, RoomTitle, Subject) ->
-spec create_room(jid:lserver(), jid:luser(), jid:jid(), binary(), binary()) ->
create_room_result().
create_room(MUCLightDomain, RoomID, CreatorJID, RoomTitle, Subject) ->
create_room(MUCLightDomain, RoomID, CreatorJID, RoomTitle, Subject, #{}).

create_room(MUCLightDomain, RoomID, CreatorJID, RoomTitle, Subject, Options) ->
RoomJID = jid:make_bare(RoomID, MUCLightDomain),
Options = #{<<"roomname">> => RoomTitle, <<"subject">> => Subject},
create_room_raw(RoomJID, CreatorJID, Options).
Options1 = Options#{<<"roomname">> => RoomTitle, <<"subject">> => Subject},
create_room_raw(RoomJID, CreatorJID, Options1).

-spec invite_to_room(jid:jid(), jid:jid(), jid:jid()) ->
{ok | not_room_member | muc_server_not_found, iolist()}.
Expand Down

0 comments on commit 2d64673

Please sign in to comment.