Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
chrzaszcz committed Oct 20, 2022
1 parent 689ad91 commit 163bde2
Show file tree
Hide file tree
Showing 11 changed files with 153 additions and 166 deletions.
9 changes: 5 additions & 4 deletions src/graphql/admin/mongoose_graphql_stanza_admin_mutation.erl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

-include("../mongoose_graphql_types.hrl").

-import(mongoose_graphql_helper, [format_result/2]).
-import(mongoose_graphql_helper, [null_to_undefined/1, format_result/2]).

execute(_Ctx, _Obj, <<"sendMessage">>, Args) ->
send_message(Args);
Expand All @@ -17,14 +17,15 @@ execute(_Ctx, _Obj, <<"sendStanza">>, Args) ->
send_stanza(Args).

send_message(#{<<"from">> := From, <<"to">> := To, <<"body">> := Body}) ->
Res = mongoose_stanza_api:send_chat_message(null, From, To, Body),
Res = mongoose_stanza_api:send_chat_message(undefined, From, To, Body),
format_result(Res, #{from => jid:to_binary(From)}).

send_message_headline(#{<<"from">> := From, <<"to">> := To,
<<"body">> := Body, <<"subject">> := Subject}) ->
Res = mongoose_stanza_api:send_headline_message(null, From, To, Body, Subject),
Res = mongoose_stanza_api:send_headline_message(
undefined, From, To, null_to_undefined(Body), null_to_undefined(Subject)),
format_result(Res, #{from => jid:to_binary(From)}).

send_stanza(#{<<"stanza">> := Packet}) ->
Res = mongoose_stanza_api:send_stanza(null, Packet),
Res = mongoose_stanza_api:send_stanza(undefined, Packet),
format_result(Res, #{}).
11 changes: 4 additions & 7 deletions src/graphql/admin/mongoose_graphql_stanza_admin_query.erl
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
-module(mongoose_graphql_stanza_admin_query).
-behaviour(mongoose_graphql).

-import(mongoose_graphql_helper, [format_result/2]).

-export([execute/4]).

-ignore_xref([execute/4]).

-include("../mongoose_graphql_types.hrl").
-include("mongoose_logger.hrl").

execute(_Ctx, _Obj, <<"getLastMessages">>, Args) ->
get_last_messages(Args).

get_last_messages(#{<<"caller">> := Caller, <<"limit">> := Limit,
<<"with">> := With, <<"before">> := Before})
when is_integer(Limit) ->
case mongoose_graphql_helper:check_user(Caller, true) of
{ok, _HostType} ->
mongoose_stanza_helper:get_last_messages(Caller, Limit, With, Before);
Error ->
Error
end.
Res = mongoose_graphql_stanza_helper:get_last_messages(Caller, Limit, With, Before, true),
format_result(Res, #{user => Caller}).
22 changes: 0 additions & 22 deletions src/graphql/mongoose_graphql_helper.erl
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
-module(mongoose_graphql_helper).

-export([check_user/2]).

-export([null_to_default/2, null_to_undefined/1]).

-export([format_result/2, make_error/2, make_error/3]).

-include("jlib.hrl").
-include("mongoose_graphql_types.hrl").

-spec format_result(InResult, Context) -> OutResult when
Expand All @@ -28,25 +25,6 @@ make_error({Reason, Msg}, Context) ->
make_error(Reason, Msg, Context) ->
{error, #resolver_error{reason = Reason, msg = iolist_to_binary(Msg), context = Context}}.

-spec check_user(jid:jid(), boolean()) -> {ok, mongooseim:host_type()} | {error, term()}.
check_user(#jid{lserver = LServer} = Jid, CheckAuth) ->
case mongoose_domain_api:get_domain_host_type(LServer) of
{ok, HostType} ->
check_auth(HostType, Jid, CheckAuth);
_ ->
{error, #{what => unknown_domain, domain => LServer}}
end.

check_auth(HostType, _Jid, false) ->
{ok, HostType};
check_auth(HostType, Jid, true) ->
case ejabberd_auth:does_user_exist(HostType, Jid, stored) of
true ->
{ok, HostType};
false ->
{error, #{what => unknown_user, jid => jid:to_binary(Jid)}}
end.

null_to_default(null, Default) ->
Default;
null_to_default(Value, _Default) ->
Expand Down
20 changes: 19 additions & 1 deletion src/graphql/mongoose_graphql_stanza_helper.erl
Original file line number Diff line number Diff line change
@@ -1,6 +1,24 @@
-module(mongoose_graphql_stanza_helper).

-export([row_to_map/1]).
-import(mongoose_graphql_helper, [null_to_undefined/1, make_error/2]).

-export([get_last_messages/5, row_to_map/1]).

-spec get_last_messages(Caller :: jid:jid(),
Limit :: non_neg_integer(),
With :: null | jid:jid(),
Before :: null | mod_mam:unix_timestamp(), boolean()) ->
{ok, map()} | {unknown_user, iodata()}.
get_last_messages(Caller, Limit, With, Before, CheckUser) ->
Limit2 = min(500, Limit),
case mongoose_stanza_api:lookup_recent_messages(
Caller, null_to_undefined(With), null_to_undefined(Before), Limit2, CheckUser) of
{ok, Rows} ->
Maps = lists:map(fun row_to_map/1, Rows),
{ok, #{<<"stanzas">> => Maps, <<"limit">> => Limit2}};
Error ->
Error
end.

-spec row_to_map(mod_mam:message_row()) -> {ok, map()}.
row_to_map(#{id := Id, jid := From, packet := Msg}) ->
Expand Down
7 changes: 4 additions & 3 deletions src/graphql/user/mongoose_graphql_stanza_user_mutation.erl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

-include("../mongoose_graphql_types.hrl").

-import(mongoose_graphql_helper, [format_result/2]).
-import(mongoose_graphql_helper, [null_to_undefined/1, format_result/2]).

execute(Ctx, _Obj, <<"sendMessage">>, Args) ->
send_message(Ctx, Args);
Expand All @@ -17,12 +17,13 @@ execute(Ctx, _Obj, <<"sendStanza">>, Args) ->
send_stanza(Ctx, Args).

send_message(#{user := User}, #{<<"from">> := From, <<"to">> := To, <<"body">> := Body}) ->
Res = mongoose_stanza_api:send_chat_message(User, From, To, Body),
Res = mongoose_stanza_api:send_chat_message(User, null_to_undefined(From), To, Body),
format_result(Res, #{}).

send_message_headline(#{user := User}, #{<<"from">> := From, <<"to">> := To, <<"body">> := Body,
<<"subject">> := Subject}) ->
Res = mongoose_stanza_api:send_headline_message(User, From, To, Body, Subject),
Res = mongoose_stanza_api:send_headline_message(
User, null_to_undefined(From), To, null_to_undefined(Body), null_to_undefined(Subject)),
format_result(Res, #{}).

send_stanza(#{user := User}, #{<<"stanza">> := Packet}) ->
Expand Down
6 changes: 4 additions & 2 deletions src/graphql/user/mongoose_graphql_stanza_user_query.erl
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
-module(mongoose_graphql_stanza_user_query).
-behaviour(mongoose_graphql).

-import(mongoose_graphql_helper, [format_result/2]).

-export([execute/4]).

-ignore_xref([execute/4]).

-include("../mongoose_graphql_types.hrl").
-include("mongoose_logger.hrl").

execute(#{user := User}, _Obj, <<"getLastMessages">>, Args) ->
get_last_messages(Args, User).

get_last_messages(#{<<"limit">> := Limit,
<<"with">> := With, <<"before">> := Before}, Caller)
when is_integer(Limit) ->
mongoose_stanza_helper:get_last_messages(Caller, Limit, With, Before).
Res = mongoose_graphql_stanza_helper:get_last_messages(Caller, Limit, With, Before, false),
format_result(Res, #{user => Caller}).
12 changes: 8 additions & 4 deletions src/mongoose_admin_api/mongoose_admin_api_messages.erl
Original file line number Diff line number Diff line change
Expand Up @@ -69,16 +69,20 @@ handle_get(Req, State) ->
Args = parse_qs(Req),
Limit = get_limit(Args),
Before = get_before(Args),
Rows = mongoose_stanza_api:lookup_recent_messages(OwnerJid, WithJid, Before, Limit),
Messages = lists:map(fun row_to_map/1, Rows),
{jiffy:encode(Messages), Req, State}.
case mongoose_stanza_api:lookup_recent_messages(OwnerJid, WithJid, Before, Limit, true) of
{ok, Rows} ->
Messages = lists:map(fun row_to_map/1, Rows),
{jiffy:encode(Messages), Req, State};
{unknown_user, Msg} ->
throw_error(bad_request, Msg)
end.

handle_post(Req, State) ->
Args = parse_body(Req),
From = get_caller(Args),
To = get_to(Args),
Body = get_body(Args),
case mongoose_stanza_api:send_chat_message(null, From, To, Body) of
case mongoose_stanza_api:send_chat_message(undefined, From, To, Body) of
{ok, _} ->
{true, Req, State};
{_Error, Msg} ->
Expand Down
2 changes: 1 addition & 1 deletion src/mongoose_admin_api/mongoose_admin_api_stanzas.erl
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ from_json(Req, State) ->
handle_post(Req, State) ->
Args = parse_body(Req),
Stanza = get_stanza(Args),
case mongoose_stanza_api:send_stanza(null, Stanza) of
case mongoose_stanza_api:send_stanza(undefined, Stanza) of
{ok, _} ->
{true, Req, State};
{_Error, Msg} ->
Expand Down
4 changes: 2 additions & 2 deletions src/mongoose_client_api/mongoose_client_api_messages.erl
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,15 @@ handle_get(Req, State = #{jid := OwnerJid}) ->
Args = parse_qs(Req),
Limit = get_limit(Args),
Before = get_before(Args),
Rows = mongoose_stanza_api:lookup_recent_messages(OwnerJid, WithJid, Before, Limit),
{ok, Rows} = mongoose_stanza_api:lookup_recent_messages(OwnerJid, WithJid, Before, Limit, false),
Resp = [make_json_msg(Msg, MAMId) || #{id := MAMId, packet := Msg} <- Rows],
{jiffy:encode(Resp), Req, State}.

handle_post(Req, State = #{jid := UserJid}) ->
Args = parse_body(Req),
To = get_to(Args),
Body = get_body(Args),
{ok, Resp} = mongoose_stanza_api:send_chat_message(UserJid, null, To, Body),
{ok, Resp} = mongoose_stanza_api:send_chat_message(UserJid, undefined, To, Body),
Req2 = cowboy_req:set_resp_body(jiffy:encode(Resp), Req),
{true, Req2, State}.

Expand Down
Loading

0 comments on commit 163bde2

Please sign in to comment.