./ct_report/coverage/mongoose_graphql_muc_admin_mutation.COVER.html

1 -module(mongoose_graphql_muc_admin_mutation).
2 -behaviour(mongoose_graphql).
3
4 -export([execute/4]).
5
6 -ignore_xref([execute/4]).
7
8 -include("../mongoose_graphql_types.hrl").
9
10 -import(mongoose_graphql_helper, [make_error/2, format_result/2, null_to_default/2,
11 null_to_undefined/1]).
12
13 execute(_Ctx, _Obj, <<"createInstantRoom">>, Args) ->
14 11 create_instant_room(Args);
15 execute(_Ctx, _Obj, <<"inviteUser">>, Args) ->
16 9 invite_user(Args);
17 execute(_Ctx, _Obj, <<"kickUser">>, Args) ->
18 8 kick_user(Args);
19 execute(_Ctx, _Obj, <<"sendMessageToRoom">>, Args) ->
20 6 send_message_to_room(Args);
21 execute(_Ctx, _Obj, <<"sendPrivateMessage">>, Args) ->
22 3 send_private_message(Args);
23 execute(_Ctx, _Obj, <<"changeRoomConfiguration">>, Args) ->
24 5 change_room_config(Args);
25 execute(_Ctx, _Obj, <<"deleteRoom">>, Args) ->
26 8 delete_room(Args);
27 execute(_Ctx, _Obj, <<"setUserRole">>, Args) ->
28 17 set_user_role(Args);
29 execute(_Ctx, _Obj, <<"setUserAffiliation">>, Args) ->
30 20 set_user_affiliation(Args);
31 execute(_Ctx, _Obj, <<"enterRoom">>, Args) ->
32 12 enter_room(Args);
33 execute(_Ctx, _Obj, <<"exitRoom">>, Args) ->
34 3 exit_room(Args).
35
36 -spec create_instant_room(map()) -> {ok, map()} | {error, resolver_error()}.
37 create_instant_room(#{<<"room">> := RoomJID, <<"owner">> := OwnerJID, <<"nick">> := Nick}) ->
38 11 case mod_muc_api:create_instant_room(RoomJID, OwnerJID, Nick) of
39 {ok, Room} ->
40 6 {ok, mongoose_graphql_muc_helper:make_room_desc(Room)};
41 Error ->
42 5 make_error(Error, #{room => jid:to_binary(RoomJID), owner => jid:to_binary(OwnerJID)})
43 end.
44
45 -spec invite_user(map()) -> {ok, binary()} | {error, resolver_error()}.
46 invite_user(#{<<"room">> := RoomJID, <<"sender">> := SenderJID,
47 <<"recipient">> := RecipientJID, <<"reason">> := Reason}) ->
48 9 Reason2 = null_to_default(Reason, mongoose_graphql_muc_helper:default_invite_reason("admin")),
49 9 Res = mod_muc_api:invite_to_room(RoomJID, SenderJID, RecipientJID, Reason2),
50 9 format_result(Res, #{room => jid:to_binary(RoomJID),
51 sender => jid:to_binary(SenderJID)}).
52
53 -spec kick_user(map()) -> {ok, binary()} | {error, resolver_error()}.
54 kick_user(#{<<"room">> := RoomJID, <<"nick">> := Nick, <<"reason">> := Reason}) ->
55 8 Reason2 = null_to_default(Reason, mongoose_graphql_muc_helper:default_kick_reason("admin")),
56 8 Res = mod_muc_api:kick_user_from_room(RoomJID, Nick, Reason2),
57 8 format_result(Res, #{room => jid:to_binary(RoomJID)}).
58
59 -spec send_message_to_room(map()) -> {ok, binary()} | {error, resolver_error()}.
60 send_message_to_room(#{<<"room">> := RoomJID, <<"from">> := SenderJID, <<"body">> := Message}) ->
61 6 Res = mod_muc_api:send_message_to_room(RoomJID, SenderJID, Message),
62 6 format_result(Res, #{room => jid:to_binary(RoomJID),
63 from => jid:to_binary(SenderJID)}).
64
65 -spec send_private_message(map()) -> {ok, binary()} | {error, resolver_error()}.
66 send_private_message(#{<<"room">> := RoomJID, <<"from">> := SenderJID,
67 <<"toNick">> := ToNick, <<"body">> := Message}) ->
68 3 Res = mod_muc_api:send_private_message(RoomJID, SenderJID, ToNick, Message),
69 3 format_result(Res, #{room => jid:to_binary(RoomJID),
70 from => jid:to_binary(SenderJID)}).
71
72 -spec change_room_config(map()) -> {ok, map()} | {error, resolver_error()}.
73 change_room_config(#{<<"room">> := RoomJID, <<"config">> := ConfigInput}) ->
74 5 Fun = fun(Config) -> mongoose_graphql_muc_helper:make_muc_room_config(ConfigInput, Config) end,
75 5 case mod_muc_api:modify_room_config(RoomJID, Fun) of
76 {ok, Config} ->
77 3 {ok, mongoose_graphql_muc_helper:muc_room_config_to_map(Config)};
78 Error ->
79 2 make_error(Error, #{room => jid:to_binary(RoomJID)})
80 end.
81
82 -spec delete_room(map()) -> {ok, binary()} | {error, resolver_error()}.
83 delete_room(#{<<"room">> := RoomJID, <<"reason">> := Reason}) ->
84 8 Reason2 = null_to_default(Reason,
85 mongoose_graphql_muc_helper:default_room_removal_reason("admin")),
86 8 Res = mod_muc_api:delete_room(RoomJID, Reason2),
87 8 format_result(Res, #{room => jid:to_binary(RoomJID)}).
88
89 -spec set_user_role(map()) -> {ok, binary()} | {error, resolver_error()}.
90 set_user_role(#{<<"room">> := RoomJID, <<"nick">> := Nick, <<"role">> := Role}) ->
91 17 Result = mod_muc_api:set_role(RoomJID, Nick, Role),
92 17 format_result(Result, #{room => jid:to_binary(RoomJID)}).
93
94 -spec set_user_affiliation(map()) -> {ok, binary()} | {error, resolver_error()}.
95 set_user_affiliation(#{<<"room">> := RoomJID, <<"user">> := UserJID, <<"affiliation">> := Aff}) ->
96 20 Result = mod_muc_api:set_affiliation(RoomJID, UserJID, Aff),
97 20 format_result(Result, #{room => jid:to_binary(RoomJID)}).
98
99 -spec enter_room(map()) -> {ok, binary()} | {error, resolver_error()}.
100 enter_room(#{<<"room">> := RoomJID, <<"user">> := UserJID, <<"nick">> := Nick,
101 <<"password">> := Password}) ->
102 12 RoomJIDRes = jid:replace_resource(RoomJID, Nick),
103 12 Password2 = null_to_undefined(Password),
104 12 Result = mod_muc_api:enter_room(RoomJIDRes, UserJID, Password2),
105 12 format_result(Result, #{room => jid:to_binary(RoomJID)}).
106
107 -spec exit_room(map()) -> {ok, binary()} | {error, resolver_error()}.
108 exit_room(#{<<"room">> := RoomJID, <<"user">> := UserJID, <<"nick">> := Nick}) ->
109 3 RoomJIDRes = jid:replace_resource(RoomJID, Nick),
110 3 Result = mod_muc_api:exit_room(RoomJIDRes, UserJID),
111 3 format_result(Result, #{room => jid:to_binary(RoomJID)}).
Line Hits Source