1 |
|
-module(mongoose_client_api_messages). |
2 |
|
|
3 |
|
-behaviour(mongoose_client_api). |
4 |
|
-export([routes/0]). |
5 |
|
|
6 |
|
-behaviour(cowboy_rest). |
7 |
|
-export([trails/0, |
8 |
|
init/2, |
9 |
|
is_authorized/2, |
10 |
|
content_types_provided/2, |
11 |
|
content_types_accepted/2, |
12 |
|
allowed_methods/2, |
13 |
|
to_json/2, |
14 |
|
from_json/2]). |
15 |
|
|
16 |
|
%% Used by mongoose_client_api_sse |
17 |
|
-export([encode/2]). |
18 |
|
|
19 |
|
-ignore_xref([to_json/2, from_json/2, trails/0]). |
20 |
|
|
21 |
|
-import(mongoose_client_api, [parse_body/1, parse_qs/1, try_handle_request/3, throw_error/2]). |
22 |
|
|
23 |
|
-type req() :: cowboy_req:req(). |
24 |
|
-type state() :: mongoose_admin_api:state(). |
25 |
|
|
26 |
|
-include_lib("exml/include/exml.hrl"). |
27 |
|
|
28 |
|
-spec routes() -> mongoose_http_handler:routes(). |
29 |
|
routes() -> |
30 |
105 |
[{"/messages/[:with]", ?MODULE, #{}}]. |
31 |
|
|
32 |
|
trails() -> |
33 |
105 |
mongoose_client_api_messages_doc:trails(). |
34 |
|
|
35 |
|
-spec init(req(), state()) -> {cowboy_rest, req(), state()}. |
36 |
|
init(Req, State) -> |
37 |
9 |
mongoose_client_api:init(Req, State). |
38 |
|
|
39 |
|
-spec is_authorized(req(), state()) -> {true | {false, iodata()}, req(), state()}. |
40 |
|
is_authorized(Req, State) -> |
41 |
9 |
mongoose_client_api:is_authorized(Req, State). |
42 |
|
|
43 |
|
-spec content_types_provided(req(), state()) -> |
44 |
|
{[{{binary(), binary(), '*'}, atom()}], req(), state()}. |
45 |
|
content_types_provided(Req, State) -> |
46 |
9 |
{[ |
47 |
|
{{<<"application">>, <<"json">>, '*'}, to_json} |
48 |
|
], Req, State}. |
49 |
|
|
50 |
|
-spec content_types_accepted(req(), state()) -> |
51 |
|
{[{{binary(), binary(), '*'}, atom()}], req(), state()}. |
52 |
|
content_types_accepted(Req, State) -> |
53 |
5 |
{[ |
54 |
|
{{<<"application">>, <<"json">>, '*'}, from_json} |
55 |
|
], Req, State}. |
56 |
|
|
57 |
|
-spec allowed_methods(req(), state()) -> {[binary()], req(), state()}. |
58 |
|
allowed_methods(Req, State) -> |
59 |
9 |
{[<<"OPTIONS">>, <<"GET">>, <<"POST">>], Req, State}. |
60 |
|
|
61 |
|
%% @doc Called for a method of type "GET" |
62 |
|
-spec to_json(req(), state()) -> {iodata() | stop, req(), state()}. |
63 |
|
to_json(Req, State) -> |
64 |
4 |
try_handle_request(Req, State, fun handle_get/2). |
65 |
|
|
66 |
|
%% @doc Called for a method of type "POST" |
67 |
|
-spec from_json(req(), state()) -> {true | stop, req(), state()}. |
68 |
|
from_json(Req, State) -> |
69 |
5 |
try_handle_request(Req, State, fun handle_post/2). |
70 |
|
|
71 |
|
handle_get(Req, State = #{jid := OwnerJid}) -> |
72 |
4 |
Bindings = cowboy_req:bindings(Req), |
73 |
4 |
WithJid = get_with_jid(Bindings), |
74 |
3 |
Args = parse_qs(Req), |
75 |
2 |
Limit = get_limit(Args), |
76 |
1 |
Before = get_before(Args), |
77 |
:-( |
{ok, {Rows, _Limit}} = |
78 |
|
mongoose_stanza_api:lookup_recent_messages(OwnerJid, WithJid, Before, Limit, false), |
79 |
:-( |
Resp = [make_json_msg(Msg, MAMId) || #{id := MAMId, packet := Msg} <- Rows], |
80 |
:-( |
{jiffy:encode(Resp), Req, State}. |
81 |
|
|
82 |
|
handle_post(Req, State = #{jid := UserJid}) -> |
83 |
5 |
Args = parse_body(Req), |
84 |
5 |
To = get_to(Args), |
85 |
3 |
Body = get_body(Args), |
86 |
2 |
{ok, Resp} = mongoose_stanza_api:send_chat_message(UserJid, undefined, To, Body), |
87 |
2 |
Req2 = cowboy_req:set_resp_body(jiffy:encode(Resp), Req), |
88 |
2 |
{true, Req2, State}. |
89 |
|
|
90 |
|
get_limit(#{limit := LimitBin}) -> |
91 |
1 |
try |
92 |
1 |
Limit = binary_to_integer(LimitBin), |
93 |
:-( |
true = Limit >= 0, |
94 |
:-( |
Limit |
95 |
|
catch |
96 |
1 |
_:_ -> throw_error(bad_request, <<"Invalid limit">>) |
97 |
|
end; |
98 |
1 |
get_limit(#{}) -> 50. |
99 |
|
|
100 |
|
get_before(#{before := BeforeBin}) -> |
101 |
1 |
try |
102 |
1 |
1000 * binary_to_integer(BeforeBin) |
103 |
|
catch |
104 |
1 |
_:_ -> throw_error(bad_request, <<"Invalid value of 'before'">>) |
105 |
|
end; |
106 |
:-( |
get_before(#{}) -> undefined. |
107 |
|
|
108 |
|
get_with_jid(#{with := With}) -> |
109 |
4 |
case jid:from_binary(With) of |
110 |
1 |
error -> throw_error(bad_request, <<"Invalid interlocutor JID">>); |
111 |
3 |
WithJid -> WithJid |
112 |
|
end; |
113 |
:-( |
get_with_jid(#{}) -> undefined. |
114 |
|
|
115 |
|
get_to(#{to := To}) -> |
116 |
4 |
case jid:from_binary(To) of |
117 |
1 |
error -> throw_error(bad_request, <<"Invalid recipient JID">>); |
118 |
3 |
ToJid -> ToJid |
119 |
|
end; |
120 |
1 |
get_to(#{}) -> throw_error(bad_request, <<"Missing recipient JID">>). |
121 |
|
|
122 |
2 |
get_body(#{body := Body}) -> Body; |
123 |
1 |
get_body(#{}) -> throw_error(bad_request, <<"Missing message body">>). |
124 |
|
|
125 |
|
make_json_msg(Msg, MAMId) -> |
126 |
:-( |
{Microsec, _} = mod_mam_utils:decode_compact_uuid(MAMId), |
127 |
:-( |
encode(Msg, Microsec div 1000). |
128 |
|
|
129 |
|
-spec encode(exml:item(), integer()) -> map(). |
130 |
|
encode(Msg, Timestamp) -> |
131 |
3 |
BodyTag = exml_query:path(Msg, [{element, <<"body">>}]), |
132 |
3 |
L = [{<<"from">>, exml_query:attr(Msg, <<"from">>)}, |
133 |
|
{<<"to">>, exml_query:attr(Msg, <<"to">>)}, |
134 |
|
{<<"id">>, exml_query:attr(Msg, <<"id">>)}, |
135 |
|
{<<"body">>, exml_query:cdata(BodyTag)}, |
136 |
|
{<<"timestamp">>, Timestamp}] ++ extensions(Msg) ++ thread_and_parent(Msg), |
137 |
3 |
maps:from_list(L). |
138 |
|
|
139 |
|
extensions(Msg) -> |
140 |
3 |
SmackNS = <<"http://www.jivesoftware.com/xmlns/xmpp/properties">>, |
141 |
3 |
RawMsgProps = exml_query:subelement_with_name_and_ns(Msg, <<"properties">>, SmackNS), |
142 |
3 |
case RawMsgProps of |
143 |
|
#xmlel{children = Children} -> |
144 |
:-( |
Props = [convert_prop_child(Child) || Child <- Children], |
145 |
:-( |
[{<<"properties">>, maps:from_list(Props)}]; |
146 |
|
_ -> |
147 |
3 |
[] |
148 |
|
end. |
149 |
|
|
150 |
|
thread_and_parent(Msg) -> |
151 |
3 |
case exml_query:path(Msg, [{element, <<"thread">>}, cdata]) of |
152 |
3 |
undefined -> []; |
153 |
:-( |
Thread -> [{<<"thread">>, Thread} | parent(Msg)] |
154 |
|
end. |
155 |
|
|
156 |
|
parent(Msg) -> |
157 |
:-( |
case exml_query:path(Msg, [{element, <<"thread">>}, {attr, <<"parent">>}]) of |
158 |
:-( |
undefined -> []; |
159 |
:-( |
ThreadParent -> [{<<"parent">>, ThreadParent}] |
160 |
|
end. |
161 |
|
|
162 |
|
convert_prop_child(Child)-> |
163 |
:-( |
Name = exml_query:path(Child, [{element, <<"name">>}, cdata]), |
164 |
:-( |
Value = exml_query:path(Child, [{element, <<"value">>}, cdata]), |
165 |
:-( |
{Name, Value}. |