1 |
|
-module(mongoose_graphql_muc_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, null_to_undefined/1]). |
11 |
|
-import(mongoose_graphql_muc_light_helper, [page_size_or_max_limit/2]). |
12 |
|
|
13 |
|
execute(_Ctx, _Obj, <<"listRooms">>, Args) -> |
14 |
25 |
list_rooms(Args); |
15 |
|
execute(_Ctx, _Obj, <<"listRoomUsers">>, Args) -> |
16 |
5 |
list_room_users(Args); |
17 |
|
execute(_Ctx, _Obj, <<"listRoomAffiliations">>, Args) -> |
18 |
11 |
list_room_affiliations(Args); |
19 |
|
execute(_Ctx, _Obj, <<"getRoomMessages">>, Args) -> |
20 |
5 |
get_room_messages(Args); |
21 |
|
execute(_Ctx, _Obj, <<"getRoomConfig">>, Args) -> |
22 |
5 |
get_room_config(Args). |
23 |
|
|
24 |
|
-spec list_rooms(map()) -> {ok, map()}. |
25 |
|
list_rooms(#{<<"mucDomain">> := MUCDomain, <<"from">> := FromJID, |
26 |
|
<<"limit">> := Limit, <<"index">> := Index}) -> |
27 |
25 |
Limit2 = null_to_undefined(Limit), |
28 |
25 |
Index2 = null_to_undefined(Index), |
29 |
25 |
case mod_muc_api:get_rooms(MUCDomain, FromJID, Limit2, Index2) of |
30 |
|
{ok, {Rooms, RSM}} -> |
31 |
21 |
{ok, mongoose_graphql_muc_helper:make_rooms_payload(Rooms, RSM)}; |
32 |
|
Error -> |
33 |
4 |
make_error(Error, #{mucDomain => MUCDomain}) |
34 |
|
end. |
35 |
|
|
36 |
|
-spec list_room_users(map()) -> {ok, [{ok, map()}]} | {error, resolver_error()}. |
37 |
|
list_room_users(#{<<"room">> := RoomJID}) -> |
38 |
5 |
case mod_muc_api:get_room_users(RoomJID) of |
39 |
|
{ok, Users} -> |
40 |
3 |
{ok, mongoose_graphql_muc_helper:format_users(Users)}; |
41 |
|
Error -> |
42 |
2 |
make_error(Error, #{room => jid:to_binary(RoomJID)}) |
43 |
|
end. |
44 |
|
|
45 |
|
-spec list_room_affiliations(map()) -> {ok, [{ok, map()}]} | {error, resolver_error()}. |
46 |
|
list_room_affiliations(#{<<"room">> := RoomJID, <<"affiliation">> := Aff}) -> |
47 |
11 |
Aff2 = null_to_undefined(Aff), |
48 |
11 |
case mod_muc_api:get_room_affiliations(RoomJID, Aff2) of |
49 |
|
{ok, Affs} -> |
50 |
9 |
{ok, mongoose_graphql_muc_helper:format_affs(Affs)}; |
51 |
|
Error -> |
52 |
2 |
make_error(Error, #{room => jid:to_binary(RoomJID)}) |
53 |
|
end. |
54 |
|
|
55 |
|
-spec get_room_config(map()) -> {ok, map()} | {error, resolver_error()}. |
56 |
|
get_room_config(#{<<"room">> := RoomJID}) -> |
57 |
5 |
case mod_muc_api:get_room_config(RoomJID) of |
58 |
|
{ok, Config} -> |
59 |
3 |
{ok, mongoose_graphql_muc_helper:muc_room_config_to_map(Config)}; |
60 |
|
Error -> |
61 |
2 |
make_error(Error, #{room => jid:to_binary(RoomJID)}) |
62 |
|
end. |
63 |
|
|
64 |
|
-spec get_room_messages(map()) -> {ok, map()} | {error, resolver_error()}. |
65 |
|
get_room_messages(#{<<"room">> := RoomJID, <<"pageSize">> := PageSize, <<"before">> := Before}) -> |
66 |
5 |
Before2 = null_to_undefined(Before), |
67 |
5 |
PageSize2 = page_size_or_max_limit(PageSize, 50), |
68 |
5 |
case mod_muc_api:get_room_messages(RoomJID, PageSize2, Before2) of |
69 |
|
{ok, Rows} -> |
70 |
3 |
Maps = lists:map(fun mongoose_graphql_stanza_helper:row_to_map/1, Rows), |
71 |
3 |
{ok, #{<<"stanzas">> => Maps, <<"limit">> => PageSize2}}; |
72 |
|
Err -> |
73 |
2 |
make_error(Err, #{room => RoomJID}) |
74 |
|
end. |