1 |
|
-module(mongoose_graphql_session_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_session_helper, [format_sessions/1, format_status_users/1]). |
11 |
|
-import(mongoose_graphql_helper, [format_result/2, make_error/2]). |
12 |
|
|
13 |
|
execute(_Ctx, _Obj, <<"listSessions">>, Args) -> |
14 |
8 |
list_sessions(Args); |
15 |
|
execute(_Ctx, _Obj, <<"countSessions">>, Args) -> |
16 |
8 |
count_sessions(Args); |
17 |
|
execute(_Ctx, _Obj, <<"listUserSessions">>, Args) -> |
18 |
4 |
list_user_sessions(Args); |
19 |
|
execute(_Ctx, _Obj, <<"countUserResources">>, Args) -> |
20 |
4 |
count_user_resources(Args); |
21 |
|
execute(_Ctx, _Obj, <<"getUserResource">>, Args) -> |
22 |
6 |
get_user_resource(Args); |
23 |
|
execute(_Ctx, _Obj, <<"listUsersWithStatus">>, Args) -> |
24 |
8 |
list_users_with_status(Args); |
25 |
|
execute(_Ctx, _Obj, <<"countUsersWithStatus">>, Args) -> |
26 |
12 |
count_users_with_status(Args). |
27 |
|
|
28 |
|
-spec list_sessions(map()) -> {ok, mongoose_graphql_session_helper:session_list()} | {error, resolver_error()}. |
29 |
|
list_sessions(#{<<"domain">> := null}) -> |
30 |
2 |
{ok, Sessions} = mongoose_session_api:list_sessions(), |
31 |
2 |
{ok, format_sessions(Sessions)}; |
32 |
|
list_sessions(#{<<"domain">> := Domain}) -> |
33 |
6 |
case mongoose_session_api:list_sessions(Domain) of |
34 |
|
{ok, Sessions} -> |
35 |
4 |
{ok, format_sessions(Sessions)}; |
36 |
|
Error -> |
37 |
2 |
make_error(Error, #{domain => Domain}) |
38 |
|
end. |
39 |
|
|
40 |
|
-spec count_sessions(map()) -> {ok, non_neg_integer()} | {error, resolver_error()}. |
41 |
|
count_sessions(#{<<"domain">> := null}) -> |
42 |
2 |
mongoose_session_api:count_sessions(); |
43 |
|
count_sessions(#{<<"domain">> := Domain}) -> |
44 |
6 |
format_result(mongoose_session_api:count_sessions(Domain), #{domain => Domain}). |
45 |
|
|
46 |
|
-spec list_user_sessions(map()) -> {ok, mongoose_graphql_session_helper:session_list()}. |
47 |
|
list_user_sessions(#{<<"user">> := JID}) -> |
48 |
4 |
case mongoose_session_api:list_user_sessions(JID) of |
49 |
|
{ok, Sessions} -> |
50 |
2 |
{ok, format_sessions(Sessions)}; |
51 |
|
Error -> |
52 |
2 |
make_error(Error, #{user => jid:to_binary(JID)}) |
53 |
|
end. |
54 |
|
|
55 |
|
-spec count_user_resources(map()) -> {ok, non_neg_integer()} | {error, resolver_error()}. |
56 |
|
count_user_resources(#{<<"user">> := JID}) -> |
57 |
4 |
format_result(mongoose_session_api:num_resources(JID), #{jid => jid:to_binary(JID)}). |
58 |
|
|
59 |
|
-spec get_user_resource(map()) -> {ok, jid:lresource()} | {error, resolver_error()}. |
60 |
|
get_user_resource(#{<<"user">> := JID, <<"number">> := ResNumber}) -> |
61 |
6 |
Result = mongoose_session_api:get_user_resource(JID, ResNumber), |
62 |
6 |
format_result(Result, #{user => jid:to_binary(JID), number => ResNumber}). |
63 |
|
|
64 |
|
-spec list_users_with_status(map()) -> {ok, mongoose_graphql_session_helper:status_user_list()} |
65 |
|
| {error, resolver_error()}. |
66 |
|
list_users_with_status(#{<<"domain">> := null, <<"status">> := Status}) -> |
67 |
4 |
{ok, StatusUsers} = mongoose_session_api:list_status_users(Status), |
68 |
4 |
{ok, format_status_users(StatusUsers)}; |
69 |
|
list_users_with_status(#{<<"domain">> := Domain, <<"status">> := Status}) -> |
70 |
4 |
case mongoose_session_api:list_status_users(Domain, Status) of |
71 |
|
{ok, StatusUsers} -> |
72 |
4 |
{ok, format_status_users(StatusUsers)}; |
73 |
|
Error -> |
74 |
:-( |
make_error(Error, #{domain => Domain, status => Status}) |
75 |
|
end. |
76 |
|
|
77 |
|
-spec count_users_with_status(map()) -> {ok, non_neg_integer()} | {error, resolver_error()}. |
78 |
|
count_users_with_status(#{<<"domain">> := null, <<"status">> := Status}) -> |
79 |
4 |
format_result(mongoose_session_api:num_status_users(Status), #{status => Status}); |
80 |
|
count_users_with_status(#{<<"domain">> := Domain, <<"status">> := Status}) -> |
81 |
8 |
format_result(mongoose_session_api:num_status_users(Domain, Status), #{domain => Domain, status => Status}). |