1 |
|
-module(mongoose_graphql_session_helper). |
2 |
|
|
3 |
|
-export([format_session/1, format_sessions/1, format_status_user/1, format_status_users/1]). |
4 |
|
|
5 |
|
-ignore_xref([format_session/1, format_status_user/1]). |
6 |
|
|
7 |
|
-type session_data() :: map(). |
8 |
|
-type session_list() :: [{ok, session_data()}]. |
9 |
|
-type status_user_data() :: map(). |
10 |
|
-type status_user_list() :: [{ok, status_user_data()}]. |
11 |
|
|
12 |
|
-export_type([session_data/0, session_list/0, status_user_data/0, status_user_list/0]). |
13 |
|
|
14 |
|
-spec format_sessions([mongoose_session_api:session_info()]) -> session_list(). |
15 |
|
format_sessions(Sessions) -> |
16 |
:-( |
lists:map(fun(S) -> {ok, format_session(S)} end, Sessions). |
17 |
|
|
18 |
|
-spec format_session(mongoose_session_api:session_info()) -> session_data(). |
19 |
|
format_session({USR, Conn, Address, Prio, Node, Uptime}) -> |
20 |
:-( |
{IP, Port} = from_address(Address), |
21 |
:-( |
#{<<"user">> => jid:to_binary(USR), |
22 |
|
<<"connection">> => from_conn(Conn), |
23 |
|
<<"ip">> => IP, |
24 |
|
<<"port">> => Port, |
25 |
|
<<"priority">> => Prio, |
26 |
|
<<"node">> => atom_to_binary(Node), |
27 |
|
<<"uptime">> => Uptime}. |
28 |
|
|
29 |
:-( |
from_conn(undefined) -> null; |
30 |
:-( |
from_conn(Conn) -> atom_to_binary(Conn). |
31 |
|
|
32 |
|
from_address(undefined) -> |
33 |
:-( |
{null, null}; |
34 |
|
from_address({IP, Port}) -> |
35 |
:-( |
{iolist_to_binary(inet:ntoa(IP)), Port}. |
36 |
|
|
37 |
|
-spec format_status_users([mongoose_session_api:status_user_info()]) -> status_user_list(). |
38 |
|
format_status_users(Sessions) -> |
39 |
:-( |
lists:map(fun(S) -> {ok, format_status_user(S)} end, Sessions). |
40 |
|
|
41 |
|
-spec format_status_user(mongoose_session_api:status_user_info()) -> status_user_data(). |
42 |
|
format_status_user({JID, Prio, StatusText}) -> |
43 |
:-( |
#{<<"user">> => jid:to_binary(JID), |
44 |
|
<<"priority">> => Prio, |
45 |
|
<<"text">> => iolist_to_binary(StatusText)}. |