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