1 |
|
-module(mongoose_graphql_session_admin_mutation). |
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]). |
11 |
|
|
12 |
|
execute(_Ctx, _Obj, <<"kickUserSession">>, Args) -> |
13 |
8 |
kick_user_session(Args); |
14 |
|
execute(_Ctx, _Obj, <<"kickUser">>, Args) -> |
15 |
4 |
kick_user(Args); |
16 |
|
execute(_Ctx, _Obj, <<"setPresence">>, Args) -> |
17 |
10 |
set_presence(Args). |
18 |
|
|
19 |
|
-spec kick_user_session(map()) -> {ok, mongoose_session_api:kick_user_result()} | {error, resolver_error()}. |
20 |
|
kick_user_session(#{<<"user">> := JID, <<"reason">> := KickReason}) -> |
21 |
8 |
case mongoose_session_api:kick_session(JID, KickReason) of |
22 |
4 |
{ok, Result} -> {ok, Result}; |
23 |
4 |
Error -> make_error(Error, #{jid => jid:to_binary(JID), reason => KickReason}) |
24 |
|
end. |
25 |
|
|
26 |
|
-spec kick_user(map()) -> {ok, [mongoose_session_api:kick_user_result()]} | {error, resolver_error()}. |
27 |
|
kick_user(#{<<"user">> := JID, <<"reason">> := KickReason}) -> |
28 |
4 |
case mongoose_session_api:kick_sessions(JID, KickReason) of |
29 |
2 |
{ok, Result} -> {ok, Result}; |
30 |
2 |
Error -> make_error(Error, #{jid => jid:to_binary(JID), reason => KickReason}) |
31 |
|
end. |
32 |
|
|
33 |
|
-spec set_presence(map()) -> {ok, map()} | {error, resolver_error()}. |
34 |
|
set_presence(#{<<"user">> := JID, <<"type">> := Type, |
35 |
|
<<"show">> := Show, <<"status">> := Status, |
36 |
|
<<"priority">> := Priority}) -> |
37 |
10 |
Show2 = null_to_empty(Show), |
38 |
10 |
Status2 = null_to_empty(Status), |
39 |
10 |
Priority2 = null_to_empty(Priority), |
40 |
10 |
Result = mongoose_session_api:set_presence(JID, Type, Show2, Status2, Priority2), |
41 |
10 |
format_session_payload(Result, JID). |
42 |
|
|
43 |
|
%% Internal |
44 |
|
|
45 |
|
-spec null_to_empty(null | integer() | binary()) -> binary(). |
46 |
8 |
null_to_empty(null) -> <<>>; |
47 |
6 |
null_to_empty(Int) when is_integer(Int) -> integer_to_binary(Int); |
48 |
16 |
null_to_empty(Bin) -> Bin. |
49 |
|
|
50 |
|
-spec format_session_payload({atom(), binary()}, jid:jid()) -> |
51 |
|
{ok, map()} | {error, resolver_error()}. |
52 |
|
format_session_payload(InResult, JID) -> |
53 |
10 |
case InResult of |
54 |
|
{ok, Msg} -> |
55 |
6 |
{ok, make_session_payload(Msg, JID)}; |
56 |
|
Result -> |
57 |
4 |
make_error(Result, #{jid => jid:to_binary(JID)}) |
58 |
|
end. |
59 |
|
|
60 |
|
-spec make_session_payload(binary(), jid:jid()) -> map(). |
61 |
|
make_session_payload(Msg, JID) -> |
62 |
6 |
#{<<"message">> => Msg, <<"jid">> => JID}. |