Skip to content

Commit

Permalink
Make MUC Light API to use room JID
Browse files Browse the repository at this point in the history
Also use MUCLightDomain in commands instead of Domain. Apply review comments.
  • Loading branch information
Premwoik committed Feb 23, 2022
1 parent 04a5863 commit e260108
Show file tree
Hide file tree
Showing 9 changed files with 263 additions and 326 deletions.
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
24 changes: 10 additions & 14 deletions src/mongoose_client_api/mongoose_client_api_rooms.erl
Original file line number Diff line number Diff line change
Expand Up @@ -123,13 +123,12 @@ from_json(Req, State) ->

handle_request(Method, JSONData, Req, State) ->
case handle_request_by_method(Method, JSONData, Req, State) of
{error, Short, Desc} ->
mongoose_client_api:bad_request(Req, format_error(Short, Desc), State);
Room ->
RoomJID = jid:from_binary(Room),
{ok, #{jid := RoomJID}} ->
RespBody = #{<<"id">> => RoomJID#jid.luser},
RespReq = cowboy_req:set_resp_body(jiffy:encode(RespBody), Req),
{true, RespReq, State}
{true, RespReq, State};
{Short, Desc} ->
mongoose_client_api:bad_request(Req, format_error(Short, Desc), State)
end.

format_error(Short, Desc) ->
Expand All @@ -142,19 +141,16 @@ term_to_bin(Term) ->
iolist_to_binary(io_lib:format("~p", [Term])).

handle_request_by_method(<<"POST">>, JSONData, _Req,
#{user := User, jid := #jid{lserver = Server}}) ->
#{jid := #jid{lserver = LServer} = UserJID}) ->
#{<<"name">> := RoomName, <<"subject">> := Subject} = JSONData,
mod_muc_light_commands:create_unique_room(Server, RoomName, User, Subject);

MUCServer = muc_light_domain(LServer),
mod_muc_light_api:create_room(MUCServer, <<>>, RoomName, UserJID, Subject);
handle_request_by_method(<<"PUT">>, JSONData, Req, State) ->
assert_room_id_set(Req, State),
#{user := User, jid := #jid{lserver = Server}, room_id := RoomID} = State,
#{jid := #jid{lserver = LServer} = UserJID, room_id := RoomID} = State,
#{<<"name">> := RoomName, <<"subject">> := Subject} = JSONData,
mod_muc_light_commands:create_identifiable_room(Server,
RoomID,
RoomName,
User,
Subject).
MUCServer = muc_light_domain(LServer),
mod_muc_light_api:create_room(MUCServer, RoomID, RoomName, UserJID, Subject).

assert_room_id_set(_Req, #{room_id := _} = _State) ->
ok.
Expand Down
7 changes: 3 additions & 4 deletions src/mongoose_client_api/mongoose_client_api_rooms_config.erl
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,8 @@ handle_request(Method, JSONData, Req, State) ->
end.

handle_request_by_method(<<"PUT">>,
#{<<"name">> := Name, <<"subject">> := Subject},
#{<<"name">> := RoomName, <<"subject">> := Subject},
Req, State) ->
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).
#{jid := UserJID, room := #{jid := RoomJID}} = State,
mod_muc_light_api:change_room_config(RoomJID, UserJID, RoomName, Subject).
23 changes: 11 additions & 12 deletions src/mongoose_client_api/mongoose_client_api_rooms_users.erl
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,14 @@ resource_exists(Req, State) ->
allow_missing_post(Req, State) ->
{false, Req, State}.

from_json(Req, #{user := User,
role_in_room := owner,
jid := #jid{lserver = Server},
room_id := RoomID} = State) ->
from_json(Req, #{role_in_room := owner,
jid := UserJID,
room := #{jid := RoomJID}} = State) ->
{ok, Body, Req2} = cowboy_req:read_body(Req),
case mongoose_client_api:json_to_map(Body) of
{ok, #{<<"user">> := UserToInvite}} when is_binary(UserToInvite) ->
mod_muc_light_commands:change_affiliation(Server, RoomID, User, UserToInvite, <<"member">>),
mod_muc_light_api:change_affiliation(RoomJID, UserJID,
jid:from_binary(UserToInvite), <<"member">>),
{true, Req2, State};
_ ->
{false, Req, State}
Expand All @@ -61,20 +61,19 @@ from_json(Req, State) ->

delete_resource(Req, #{role_in_room := none} = State) ->
mongoose_client_api:forbidden_request(Req, State);
delete_resource(Req, #{role_in_room := owner,
user := User} = State) ->
delete_resource(Req, #{role_in_room := owner} = State) ->
UserToRemove = cowboy_req:binding(user, Req),
remove_user_from_room(User, UserToRemove, Req, State);
remove_user_from_room(UserToRemove, Req, State);
delete_resource(Req, #{user := User} = State) ->
UserToRemove = cowboy_req:binding(user, Req),
case UserToRemove of
User ->
remove_user_from_room(User, User, Req, State);
remove_user_from_room(User, Req, State);
_ ->
mongoose_client_api:forbidden_request(Req, State)
end.

remove_user_from_room(Remover, Target, Req,
#{jid := #jid{lserver = Server}, room_id := RoomID} = State) ->
mod_muc_light_commands:change_affiliation(Server, RoomID, Remover, Target, <<"none">>),
remove_user_from_room(Target, Req,
#{jid := UserJID, room := #{jid := RoomJID}} = State) ->
mod_muc_light_api:change_affiliation(RoomJID, UserJID, jid:from_binary(Target), <<"none">>),
{true, Req, State}.
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 e260108

Please sign in to comment.