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