1 |
|
-module(mongoose_graphql_muc_light_admin_query). |
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, |
12 |
|
make_ok_user/1, |
13 |
|
null_to_undefined/1, |
14 |
|
page_size_or_max_limit/2]). |
15 |
|
|
16 |
|
execute(_Ctx, _Obj, <<"listUserRooms">>, Args) -> |
17 |
3 |
list_user_rooms(Args); |
18 |
|
execute(_Ctx, _Obj, <<"listRoomUsers">>, Args) -> |
19 |
3 |
list_room_users(Args); |
20 |
|
execute(_Ctx, _Obj, <<"getRoomConfig">>, Args) -> |
21 |
3 |
get_room_config(Args); |
22 |
|
execute(_Ctx, _Obj, <<"getRoomMessages">>, Args) -> |
23 |
4 |
get_room_messages(Args); |
24 |
|
execute(_Ctx, _Obj, <<"getBlockingList">>, Args) -> |
25 |
4 |
get_blocking_list(Args). |
26 |
|
|
27 |
|
-spec list_user_rooms(map()) -> {ok, [{ok, binary()}]} | {error, resolver_error()}. |
28 |
|
list_user_rooms(#{<<"user">> := UserJID}) -> |
29 |
3 |
case mod_muc_light_api:get_user_rooms(UserJID) of |
30 |
|
{ok, Rooms} -> |
31 |
2 |
{ok, [{ok, R} || R <- Rooms]}; |
32 |
|
Err -> |
33 |
1 |
make_error(Err, #{user => UserJID}) |
34 |
|
end. |
35 |
|
|
36 |
|
-spec list_room_users(map()) -> {ok, [{ok, map()}]} | {error, resolver_error()}. |
37 |
|
list_room_users(#{<<"room">> := RoomJID}) -> |
38 |
3 |
case mod_muc_light_api:get_room_aff(RoomJID) of |
39 |
|
{ok, Affs} -> |
40 |
1 |
{ok, [make_ok_user(A) || A <- Affs]}; |
41 |
|
Err -> |
42 |
2 |
make_error(Err, #{room => RoomJID}) |
43 |
|
end. |
44 |
|
|
45 |
|
-spec get_room_config(map()) -> {ok, map()} | {error, resolver_error()}. |
46 |
|
get_room_config(#{<<"room">> := RoomJID}) -> |
47 |
3 |
case mod_muc_light_api:get_room_info(RoomJID) of |
48 |
|
{ok, Room} -> |
49 |
1 |
{ok, make_room(Room)}; |
50 |
|
Err -> |
51 |
2 |
make_error(Err, #{room => RoomJID}) |
52 |
|
end. |
53 |
|
|
54 |
|
-spec get_room_messages(map()) -> {ok, map()} | {error, resolver_error()}. |
55 |
|
get_room_messages(#{<<"room">> := RoomJID, <<"pageSize">> := PageSize, |
56 |
|
<<"before">> := Before}) -> |
57 |
4 |
Before2 = null_to_undefined(Before), |
58 |
4 |
PageSize2 = page_size_or_max_limit(PageSize, 50), |
59 |
4 |
case mod_muc_light_api:get_room_messages(RoomJID, PageSize2, Before2) of |
60 |
|
{ok, Rows} -> |
61 |
3 |
Maps = lists:map(fun mongoose_graphql_stanza_helper:row_to_map/1, Rows), |
62 |
3 |
{ok, #{<<"stanzas">> => Maps, <<"limit">> => PageSize2}}; |
63 |
|
Err -> |
64 |
1 |
make_error(Err, #{room => RoomJID}) |
65 |
|
end. |
66 |
|
|
67 |
|
-spec get_blocking_list(map()) -> {ok, [{ok, map()}]} | {error, resolver_error()}. |
68 |
|
get_blocking_list(#{<<"user">> := UserJID}) -> |
69 |
4 |
case mod_muc_light_api:get_blocking_list(UserJID) of |
70 |
|
{ok, Items} -> |
71 |
3 |
Items2 = lists:map(fun mongoose_graphql_muc_light_helper:blocking_item_to_map/1, Items), |
72 |
3 |
{ok, Items2}; |
73 |
|
Err -> |
74 |
1 |
make_error(Err, #{user => UserJID}) |
75 |
|
end. |