1 |
|
-module(mongoose_graphql_muc_user_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 |
3 |
create_instant_room(Ctx, Args); |
15 |
|
execute(Ctx, _Obj, <<"inviteUser">>, Args) -> |
16 |
1 |
invite_user(Ctx, Args); |
17 |
|
execute(Ctx, _Obj, <<"kickUser">>, Args) -> |
18 |
3 |
kick_user(Ctx, Args); |
19 |
|
execute(Ctx, _Obj, <<"sendMessageToRoom">>, Args) -> |
20 |
5 |
send_message_to_room(Ctx, Args); |
21 |
|
execute(Ctx, _Obj, <<"sendPrivateMessage">>, Args) -> |
22 |
1 |
send_private_message(Ctx, Args); |
23 |
|
execute(Ctx, _Obj, <<"changeRoomConfiguration">>, Args) -> |
24 |
3 |
change_room_config(Ctx, Args); |
25 |
|
execute(Ctx, _Obj, <<"deleteRoom">>, Args) -> |
26 |
3 |
delete_room(Ctx, Args); |
27 |
|
execute(Ctx, _Obj, <<"setUserRole">>, Args) -> |
28 |
7 |
set_user_role(Ctx, Args); |
29 |
|
execute(Ctx, _Obj, <<"setUserAffiliation">>, Args) -> |
30 |
20 |
set_user_affiliation(Ctx, Args); |
31 |
|
execute(Ctx, _Obj, <<"enterRoom">>, Args) -> |
32 |
4 |
enter_room(Ctx, Args); |
33 |
|
execute(Ctx, _Obj, <<"exitRoom">>, Args) -> |
34 |
1 |
exit_room(Ctx, Args). |
35 |
|
|
36 |
|
-spec enter_room(map(), map()) -> {ok, binary()} | {error, resolver_error()}. |
37 |
|
enter_room(#{user := UserJID}, #{<<"room">> := RoomJID, <<"nick">> := Nick, |
38 |
|
<<"resource">> := Resource, <<"password">> := Password}) -> |
39 |
4 |
UserJIDRes = jid:replace_resource(UserJID, Resource), |
40 |
4 |
RoomJIDRes = jid:replace_resource(RoomJID, Nick), |
41 |
4 |
Password2 = null_to_undefined(Password), |
42 |
4 |
Res = mod_muc_api:enter_room(RoomJIDRes, UserJIDRes, Password2), |
43 |
4 |
format_result(Res, #{room => jid:to_binary(RoomJID)}). |
44 |
|
|
45 |
|
-spec exit_room(map(), map()) -> {ok, binary()} | {error, resolver_error()}. |
46 |
|
exit_room(#{user := UserJID}, #{<<"room">> := RoomJID, <<"nick">> := Nick, |
47 |
|
<<"resource">> := Resource}) -> |
48 |
1 |
UserJIDRes = jid:replace_resource(UserJID, Resource), |
49 |
1 |
RoomJIDRes = jid:replace_resource(RoomJID, Nick), |
50 |
1 |
Res = mod_muc_api:exit_room(RoomJIDRes, UserJIDRes), |
51 |
1 |
format_result(Res, #{room => jid:to_binary(RoomJID)}). |
52 |
|
|
53 |
|
-spec create_instant_room(map(), map()) -> {ok, map()} | {error, resolver_error()}. |
54 |
|
create_instant_room(#{user := UserJID}, #{<<"room">> := RoomJID, <<"nick">> := Nick}) -> |
55 |
3 |
case mod_muc_api:create_instant_room(RoomJID, UserJID, Nick) of |
56 |
|
{ok, Room} -> |
57 |
2 |
{ok, mongoose_graphql_muc_helper:make_room_desc(Room)}; |
58 |
|
Error -> |
59 |
1 |
make_error(Error, #{room => jid:to_binary(RoomJID)}) |
60 |
|
end. |
61 |
|
|
62 |
|
-spec invite_user(map(), map()) -> {ok, binary()} | {error, resolver_error()}. |
63 |
|
invite_user(#{user := UserJID}, #{<<"room">> := RoomJID, <<"recipient">> := RecipientJID, |
64 |
|
<<"reason">> := Reason}) -> |
65 |
1 |
Reason2 = null_to_default(Reason, mongoose_graphql_muc_helper:default_invite_reason("user")), |
66 |
1 |
Res = mod_muc_api:invite_to_room(RoomJID, UserJID, RecipientJID, Reason2), |
67 |
1 |
format_result(Res, #{room => jid:to_binary(RoomJID)}). |
68 |
|
|
69 |
|
-spec kick_user(map(), map()) -> {ok, binary()} | {error, resolver_error()}. |
70 |
|
kick_user(#{user := UserJID}, #{<<"room">> := RoomJID, <<"nick">> := Nick, |
71 |
|
<<"reason">> := Reason}) -> |
72 |
3 |
Reason2 = null_to_default(Reason, mongoose_graphql_muc_helper:default_kick_reason("user")), |
73 |
3 |
Res = mod_muc_api:kick_user_from_room(RoomJID, UserJID, Nick, Reason2), |
74 |
3 |
format_result(Res, #{room => jid:to_binary(RoomJID)}). |
75 |
|
|
76 |
|
-spec send_message_to_room(map(), map()) -> {ok, binary()} | {error, resolver_error()}. |
77 |
|
send_message_to_room(#{user := UserJID}, #{<<"room">> := RoomJID, <<"body">> := Message, |
78 |
|
<<"resource">> := Res}) -> |
79 |
5 |
Result = do_send_message_to_room(RoomJID, UserJID, Message, Res), |
80 |
5 |
format_result(Result, #{room => jid:to_binary(RoomJID)}). |
81 |
|
|
82 |
|
-spec send_private_message(map(), map()) -> {ok, binary()} | {error, resolver_error()}. |
83 |
|
send_private_message(#{user := UserJID}, #{<<"room">> := RoomJID, <<"body">> := Message, |
84 |
|
<<"toNick">> := ToNick, <<"resource">> := Res}) -> |
85 |
1 |
Result = case mongoose_graphql_muc_helper:add_user_resource(UserJID, Res) of |
86 |
|
{ok, UserJIDRes} -> |
87 |
1 |
mod_muc_api:send_private_message(RoomJID, UserJIDRes, ToNick, Message); |
88 |
|
Error -> |
89 |
:-( |
Error |
90 |
|
end, |
91 |
1 |
format_result(Result, #{room => jid:to_binary(RoomJID)}). |
92 |
|
|
93 |
|
-spec change_room_config(map(), map()) -> {ok, map()} | {error, resolver_error()}. |
94 |
|
change_room_config(#{user := UserJID}, #{<<"room">> := RoomJID, <<"config">> := ConfigInput}) -> |
95 |
3 |
Fun = fun(Config) -> mongoose_graphql_muc_helper:make_muc_room_config(ConfigInput, Config) end, |
96 |
3 |
case mod_muc_api:modify_room_config(RoomJID, UserJID, Fun) of |
97 |
|
{ok, Config} -> |
98 |
1 |
{ok, mongoose_graphql_muc_helper:muc_room_config_to_map(Config)}; |
99 |
|
Error -> |
100 |
2 |
make_error(Error, #{room => jid:to_binary(RoomJID)}) |
101 |
|
end. |
102 |
|
|
103 |
|
-spec delete_room(map(), map()) -> {ok, binary()} | {error, resolver_error()}. |
104 |
|
delete_room(#{user := UserJID}, #{<<"room">> := RoomJID, <<"reason">> := Reason}) -> |
105 |
3 |
Reason2 = null_to_default(Reason, |
106 |
|
mongoose_graphql_muc_helper:default_room_removal_reason("user")), |
107 |
3 |
Res = mod_muc_api:delete_room(RoomJID, UserJID, Reason2), |
108 |
3 |
format_result(Res, #{room => jid:to_binary(RoomJID)}). |
109 |
|
|
110 |
|
-spec set_user_role(map(), map()) -> {ok, binary()} | {error, resolver_error()}. |
111 |
|
set_user_role(#{user := UserJID}, #{<<"room">> := RoomJID, <<"nick">> := Nick, |
112 |
|
<<"role">> := Role}) -> |
113 |
7 |
Result = mod_muc_api:set_role(RoomJID, UserJID, Nick, Role), |
114 |
7 |
format_result(Result, #{room => jid:to_binary(RoomJID)}). |
115 |
|
|
116 |
|
-spec set_user_affiliation(map(), map()) -> {ok, binary()} | {error, resolver_error()}. |
117 |
|
set_user_affiliation(#{user := FromJID}, #{<<"room">> := RoomJID, <<"user">> := UserJID, |
118 |
|
<<"affiliation">> := Aff}) -> |
119 |
20 |
Result = mod_muc_api:set_affiliation(RoomJID, FromJID, UserJID, Aff), |
120 |
20 |
format_result(Result, #{room => jid:to_binary(RoomJID)}). |
121 |
|
|
122 |
|
-spec do_send_message_to_room(jid:jid(), jid:jid(), binary(), binary() | null) -> |
123 |
|
{ok | no_session, iolist()}. |
124 |
|
do_send_message_to_room(RoomJID, UserJID, Message, Res) -> |
125 |
5 |
case mongoose_graphql_muc_helper:add_user_resource(UserJID, Res) of |
126 |
|
{ok, UserJIDRes} -> |
127 |
4 |
mod_muc_api:send_message_to_room(RoomJID, UserJIDRes, Message); |
128 |
|
Error -> |
129 |
1 |
Error |
130 |
|
end. |