Skip to content

Commit

Permalink
Make MUC Light API to use room JID
Browse files Browse the repository at this point in the history
  • Loading branch information
Premwoik committed Feb 22, 2022
1 parent 04a5863 commit fd628de
Show file tree
Hide file tree
Showing 9 changed files with 343 additions and 407 deletions.
242 changes: 118 additions & 124 deletions big_tests/tests/graphql_muc_light_SUITE.erl

Large diffs are not rendered by default.

16 changes: 8 additions & 8 deletions priv/graphql/schemas/admin/muc_light.gql
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,27 @@ type MUCLightAdminMutation @protected{
"Create a MUC light room under the given XMPP hostname"
createRoom(domain: String!, name: String!, owner: JID!, subject: String!, id: String): Room
"Change configuration of a MUC Light room"
changeRoomConfiguration(id: String!, domain: String!, name: String!, owner: JID!, subject: String!): Room
changeRoomConfiguration(room: JID!, owner: JID!, name: String!, subject: String!): Room
"Invite a user to a MUC Light room"
inviteUser(domain: String!, name: String!, sender: JID!, recipient: JID!): String
inviteUser(room: JID!, sender: JID!, recipient: JID!): String
"Remove a MUC Light room"
deleteRoom(id: String!, domain: String!): String
deleteRoom(room: JID!): String
"Kick a user from a MUC Light room"
kickUser(domain: String!, id: String!, user: JID!): String
kickUser(room: JID!, user: JID!): String
"Send a message to a MUC Light room"
sendMessageToRoom(domain: String!, name: String!, from: JID!, body: String!): String
sendMessageToRoom(room: JID!, from: JID!, body: String!): String
}

"""
Allow admin to get information about Multi-User Chat Light rooms.
"""
type MUCLightAdminQuery @protected{
"Get the MUC Light room archived messages"
getRoomMessages(domain: String!, id: String!, pageSize: Int!, before: DateTime): StanzasPayload
getRoomMessages(room: JID!, pageSize: Int!, before: DateTime): StanzasPayload
"Get configuration of the MUC Light room"
getRoomConfig(domain: String!, id: String!): Room
getRoomConfig(room: JID!): Room
"Get users list of given MUC Light room"
listRoomUsers(domain: String!, id: String!): [RoomUser!]
listRoomUsers(room: JID!): [RoomUser!]
"Get the list of MUC Light rooms that the user participates in"
listUserRooms(user: JID!): [JID!]
}
Expand Down
30 changes: 14 additions & 16 deletions src/graphql/admin/mongoose_graphql_muc_light_admin_mutation.erl
Original file line number Diff line number Diff line change
Expand Up @@ -35,34 +35,32 @@ create_room(#{<<"id">> := RoomID, <<"domain">> := Domain, <<"name">> := RoomName
end.

-spec change_room_config(map()) -> {ok, map()} | {error, resolver_error()}.
change_room_config(#{<<"id">> := RoomID, <<"domain">> := Domain, <<"name">> := RoomName,
change_room_config(#{<<"room">> := RoomJID, <<"name">> := RoomName,
<<"owner">> := OwnerJID, <<"subject">> := Subject}) ->
case mod_muc_light_api:change_room_config(Domain, RoomID, RoomName, OwnerJID, Subject) of
case mod_muc_light_api:change_room_config(RoomJID, OwnerJID, RoomName, Subject) of
{ok, Room} ->
{ok, make_room(Room)};
Err ->
make_error(Err, #{domain => Domain, id => RoomID, owner => OwnerJID})
make_error(Err, #{room => RoomJID, owner => OwnerJID})
end.

-spec delete_room(map()) -> {ok, binary()} | {error, resolver_error()}.
delete_room(#{<<"domain">> := Domain, <<"id">> := RoomID}) ->
Result = mod_muc_light_api:delete_room(Domain, RoomID),
format_result(Result, #{domain => Domain, id => RoomID}).
delete_room(#{<<"room">> := RoomJID}) ->
Result = mod_muc_light_api:delete_room(RoomJID),
format_result(Result, #{room => RoomJID}).

-spec invite_user(map()) -> {ok, binary()} | {error, resolver_error()}.
invite_user(#{<<"domain">> := Domain, <<"name">> := Name, <<"sender">> := SenderJID,
invite_user(#{<<"room">> := RoomJID, <<"sender">> := SenderJID,
<<"recipient">> := RecipientJID}) ->
Result = mod_muc_light_api:invite_to_room(Domain, Name, SenderJID, RecipientJID),
format_result(Result, #{domain => Domain, name => Name,
sender => sender, recipient => RecipientJID}).
Result = mod_muc_light_api:invite_to_room(RoomJID, SenderJID, RecipientJID),
format_result(Result, #{room => RoomJID, sender => SenderJID, recipient => RecipientJID}).

-spec kick_user(map()) -> {ok, binary()} | {error, resolver_error()}.
kick_user(#{<<"domain">> := Domain, <<"id">> := RoomID, <<"user">> := UserJID}) ->
Result = mod_muc_light_api:remove_user_from_room(Domain, RoomID, UserJID, UserJID),
kick_user(#{<<"room">> := RoomJID, <<"user">> := UserJID}) ->
Result = mod_muc_light_api:remove_user_from_room(RoomJID, UserJID, UserJID),
format_result(Result, #{user => UserJID}).

-spec send_msg_to_room(map()) -> {ok, binary()} | {error, resolver_error()}.
send_msg_to_room(#{<<"domain">> := Domain, <<"name">> := RoomName, <<"from">> := FromJID,
<<"body">> := Message}) ->
Result = mod_muc_light_api:send_message(Domain, RoomName, FromJID, Message),
format_result(Result, #{domain => Domain, name => RoomName, from => FromJID}).
send_msg_to_room(#{<<"room">> := RoomJID, <<"from">> := FromJID, <<"body">> := Message}) ->
Result = mod_muc_light_api:send_message(RoomJID, FromJID, Message),
format_result(Result, #{room => RoomJID, from => FromJID}).
20 changes: 10 additions & 10 deletions src/graphql/admin/mongoose_graphql_muc_light_admin_query.erl
Original file line number Diff line number Diff line change
Expand Up @@ -28,33 +28,33 @@ list_user_rooms(#{<<"user">> := UserJID}) ->
end.

-spec list_room_users(map()) -> {ok, [map()]} | {error, resolver_error()}.
list_room_users(#{<<"domain">> := Domain, <<"id">> := RoomID}) ->
case mod_muc_light_api:get_room_aff(Domain, RoomID) of
list_room_users(#{<<"room">> := RoomJID}) ->
case mod_muc_light_api:get_room_aff(RoomJID) of
{ok, Affs} ->
{ok, [make_ok_user(A) || A <- Affs]};
Err ->
make_error(Err, #{domain => Domain, id => RoomID})
make_error(Err, #{room => RoomJID})
end.

-spec get_room_config(map()) -> {ok, map()} | {error, resolver_error()}.
get_room_config(#{<<"domain">> := Domain, <<"id">> := RoomID}) ->
case mod_muc_light_api:get_room_info(Domain, RoomID) of
get_room_config(#{<<"room">> := RoomJID}) ->
case mod_muc_light_api:get_room_info(RoomJID) of
{ok, Room} ->
{ok, make_room(Room)};
Err ->
make_error(Err, #{domain => Domain, id => RoomID})
make_error(Err, #{room => RoomJID})
end.

-spec get_room_messages(map()) -> {ok, map()} | {error, resolver_error()}.
get_room_messages(#{<<"domain">> := Domain, <<"id">> := RoomID,
<<"pageSize">> := PageSize, <<"before">> := Before}) ->
get_room_messages(#{<<"room">> := RoomJID, <<"pageSize">> := PageSize,
<<"before">> := Before}) ->
Before2 = null_to_undefined(Before),
case mod_muc_light_api:get_room_messages(Domain, RoomID, PageSize, Before2) of
case mod_muc_light_api:get_room_messages(RoomJID, PageSize, Before2) of
{ok, Rows} ->
Maps = lists:map(fun mongoose_graphql_stanza_helper:row_to_map/1, Rows),
{ok, #{<<"stanzas">> => Maps, <<"limit">> => null}};
Err ->
make_error(Err, #{domain => Domain, id => RoomID})
make_error(Err, #{room => RoomJID})
end.

%% Helpers
Expand Down
3 changes: 2 additions & 1 deletion src/graphql/mongoose_graphql_errors.erl
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ err(_Ctx, ErrorTerm) ->

%% callback invoked when resolver crashes
-spec crash(map(), term()) -> err_msg().
crash(_Ctx, #{type := Type}) ->
crash(_Ctx, #{type := Type} = E) ->
io:format("~p", [E]),
#{message => <<"Unexpected ", Type/binary, " resolver crash">>,
extensions => #{code => resolver_crash}}.

Expand Down
4 changes: 3 additions & 1 deletion src/mongoose_client_api/mongoose_client_api_rooms_config.erl
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,6 @@ handle_request_by_method(<<"PUT">>,
mongoose_client_api_rooms:assert_room_id_set(Req, State),
#{user := User, jid := #jid{lserver = Server}, room_id := RoomID} = State,
UserJID = jid:from_binary(User),
mod_muc_light_api:change_room_config(Server, RoomID, Name, UserJID, Subject).
HostType = mod_muc_light_utils:server_host_to_host_type(Server),
MUCServer = mod_muc_light_utils:server_host_to_muc_host(HostType, Server),
mod_muc_light_api:change_room_config(jid:make_bare(RoomID, MUCServer), Name, UserJID, Subject).
3 changes: 2 additions & 1 deletion src/muc_light/mod_muc_light.erl
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,8 @@ try_to_create_room(CreatorJid, RoomJID, #create{raw_config = RawConfig} = Creati
change_room_config(UserJid, RoomID, MUCLightDomain, ConfigReq, Acc1) ->
RoomJID = jid:make(RoomID, MUCLightDomain, <<>>),
{Acc2, AffUsersRes} = get_room_affiliations_from_acc(Acc1, RoomJID),
case mod_muc_light_room:process_request(UserJid, RoomJID, {set, ConfigReq}, AffUsersRes, Acc2) of
case mod_muc_light_room:process_request(UserJid, RoomJID, {set, ConfigReq},
AffUsersRes, Acc2) of
{set, ConfigResp, _} ->
{ok, RoomJID, ConfigResp};
{error, _Reason} = E ->
Expand Down
Loading

0 comments on commit fd628de

Please sign in to comment.