1 |
|
-module(mongoose_graphql_muc_light_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]). |
11 |
|
-import(mongoose_graphql_muc_light_helper, [make_room/1, make_ok_user/1, prepare_blocking_items/1, |
12 |
|
null_to_default/2, config_to_map/3, get_not_loaded/1]). |
13 |
|
|
14 |
|
execute(_Ctx, _Obj, <<"createRoom">>, Args) -> |
15 |
37 |
create_room(Args); |
16 |
|
execute(_Ctx, _Obj, <<"changeRoomConfiguration">>, Args) -> |
17 |
20 |
change_room_config(Args); |
18 |
|
execute(_Ctx, _Obj, <<"inviteUser">>, Args) -> |
19 |
12 |
invite_user(Args); |
20 |
|
execute(_Ctx, _Obj, <<"deleteRoom">>, Args) -> |
21 |
8 |
delete_room(Args); |
22 |
|
execute(_Ctx, _Obj, <<"kickUser">>, Args) -> |
23 |
3 |
kick_user(Args); |
24 |
|
execute(_Ctx, _Obj, <<"sendMessageToRoom">>, Args) -> |
25 |
12 |
send_msg_to_room(Args); |
26 |
|
execute(_Ctx, _Obj, <<"setBlockingList">>, Args) -> |
27 |
14 |
set_blocking_list(Args). |
28 |
|
|
29 |
|
-spec create_room(map()) -> {ok, map()} | {error, resolver_error()}. |
30 |
|
create_room(#{<<"id">> := RoomID, <<"mucDomain">> := MUCDomain, <<"name">> := RoomName, |
31 |
|
<<"owner">> := CreatorJID, <<"subject">> := Subject, <<"options">> := Options}) -> |
32 |
37 |
case mod_muc_light_api:create_room(MUCDomain, null_to_default(RoomID, <<>>), CreatorJID, |
33 |
|
config_to_map(RoomName, Subject, Options)) of |
34 |
|
{ok, Room} -> |
35 |
18 |
{ok, make_room(Room)}; |
36 |
|
Err -> |
37 |
19 |
make_error(Err, #{mucDomain => MUCDomain, id => RoomID, |
38 |
|
creator => jid:to_binary(CreatorJID)}) |
39 |
|
end. |
40 |
|
|
41 |
|
-spec change_room_config(map()) -> {ok, map()} | {error, resolver_error()}. |
42 |
|
change_room_config(#{<<"room">> := RoomJID, <<"name">> := RoomName, |
43 |
|
<<"owner">> := OwnerJID, <<"subject">> := Subject, |
44 |
|
<<"options">> := Options}) -> |
45 |
20 |
Config = config_to_map(RoomName, Subject, Options), |
46 |
20 |
case mod_muc_light_api:change_room_config(RoomJID, OwnerJID, Config) of |
47 |
|
{ok, Room} -> |
48 |
6 |
{ok, make_room(Room)}; |
49 |
|
Err -> |
50 |
14 |
make_error(Err, #{room => jid:to_binary(RoomJID), owner => jid:to_binary(OwnerJID)}) |
51 |
|
end. |
52 |
|
|
53 |
|
-spec delete_room(map()) -> {ok, binary()} | {error, resolver_error()}. |
54 |
|
delete_room(#{<<"room">> := RoomJID}) -> |
55 |
8 |
Result = mod_muc_light_api:delete_room(RoomJID), |
56 |
8 |
format_result(Result, #{room => jid:to_binary(RoomJID)}). |
57 |
|
|
58 |
|
-spec invite_user(map()) -> {ok, binary()} | {error, resolver_error()}. |
59 |
|
invite_user(#{<<"room">> := RoomJID, <<"sender">> := SenderJID, |
60 |
|
<<"recipient">> := RecipientJID}) -> |
61 |
12 |
Result = mod_muc_light_api:invite_to_room(RoomJID, SenderJID, RecipientJID), |
62 |
12 |
format_result(Result, #{room => jid:to_binary(RoomJID), sender => jid:to_binary(SenderJID), |
63 |
|
recipient => jid:to_binary(RecipientJID)}). |
64 |
|
|
65 |
|
-spec kick_user(map()) -> {ok, binary()} | {error, resolver_error()}. |
66 |
|
kick_user(#{<<"room">> := RoomJID, <<"user">> := UserJID}) -> |
67 |
3 |
Result = mod_muc_light_api:change_affiliation(RoomJID, UserJID, UserJID, remove), |
68 |
3 |
format_result(Result, #{user => jid:to_binary(UserJID)}). |
69 |
|
|
70 |
|
-spec send_msg_to_room(map()) -> {ok, binary()} | {error, resolver_error()}. |
71 |
|
send_msg_to_room(#{<<"room">> := RoomJID, <<"from">> := FromJID, <<"body">> := Message}) -> |
72 |
12 |
Result = mod_muc_light_api:send_message(RoomJID, FromJID, Message), |
73 |
12 |
format_result(Result, #{room => jid:to_binary(RoomJID), from => jid:to_binary(FromJID)}). |
74 |
|
|
75 |
|
-spec set_blocking_list(map()) -> {ok, binary()} | {error, resolver_error()}. |
76 |
|
set_blocking_list(#{<<"user">> := UserJID, <<"items">> := Items}) -> |
77 |
14 |
Items2 = prepare_blocking_items(Items), |
78 |
14 |
Result = mod_muc_light_api:set_blocking(UserJID, Items2), |
79 |
14 |
format_result(Result, #{user => jid:to_binary(UserJID)}). |