1 |
|
-module(mongoose_graphql_stanza_admin_query). |
2 |
|
|
3 |
|
-export([execute/4]). |
4 |
|
|
5 |
|
-ignore_xref([execute/4]). |
6 |
|
|
7 |
|
-include("../mongoose_graphql_types.hrl"). |
8 |
|
-include("mongoose_logger.hrl"). |
9 |
|
|
10 |
|
-type result() :: {ok, map()} | {error, term()}. |
11 |
|
|
12 |
|
-spec execute(graphql:endpoint_context(), graphql:ast(), binary(), map()) -> |
13 |
|
result(). |
14 |
|
execute(_Ctx, _Obj, <<"getLastMessages">>, Opts) -> |
15 |
:-( |
get_last_messages(Opts). |
16 |
|
|
17 |
|
get_last_messages(#{<<"caller">> := Caller, <<"limit">> := Limit, |
18 |
|
<<"with">> := With, <<"before">> := Before}) |
19 |
|
when is_integer(Limit) -> |
20 |
:-( |
case mongoose_graphql_helper:check_user(Caller) of |
21 |
|
{ok, _HostType} -> |
22 |
:-( |
get_last_messages2(Caller, Limit, With, Before); |
23 |
|
Error -> |
24 |
:-( |
Error |
25 |
|
end. |
26 |
|
|
27 |
|
get_last_messages2(Caller, Limit, With, Before) -> |
28 |
:-( |
With2 = null_as_undefined(With), |
29 |
:-( |
Before2 = null_as_undefined(Before), %% Before is in microseconds |
30 |
:-( |
Limit2 = min(500, Limit), |
31 |
:-( |
Rows = mongoose_stanza_api:lookup_recent_messages(Caller, With2, Before2, Limit2), |
32 |
:-( |
Maps = lists:map(fun row_to_map/1, Rows), |
33 |
:-( |
{ok, #{<<"stanzas">> => Maps, <<"limit">> => Limit2}}. |
34 |
|
|
35 |
:-( |
null_as_undefined(null) -> undefined; |
36 |
:-( |
null_as_undefined(Value) -> Value. |
37 |
|
|
38 |
|
-spec row_to_map(mod_mam:message_row()) -> {ok, map()}. |
39 |
|
row_to_map(#{id := Id, jid := From, packet := Msg}) -> |
40 |
:-( |
{Microseconds, _} = mod_mam_utils:decode_compact_uuid(Id), |
41 |
:-( |
StanzaID = mod_mam_utils:mess_id_to_external_binary(Id), |
42 |
:-( |
Map = #{<<"sender">> => From, <<"timestamp">> => Microseconds, |
43 |
|
<<"stanza_id">> => StanzaID, <<"stanza">> => Msg}, |
44 |
:-( |
{ok, Map}. |