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 |
57 |
[{"/messages/[:with]", ?MODULE, #{}}]. |
31 |
|
|
32 |
|
trails() -> |
33 |
57 |
mongoose_client_api_messages_doc:trails(). |
34 |
|
|
35 |
|
-spec init(req(), state()) -> {cowboy_rest, req(), state()}. |
36 |
|
init(Req, State) -> |
37 |
26 |
mongoose_client_api:init(Req, State). |
38 |
|
|
39 |
|
-spec is_authorized(req(), state()) -> {true | {false, iodata()}, req(), state()}. |
40 |
|
is_authorized(Req, State) -> |
41 |
26 |
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 |
26 |
{[ |
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 |
11 |
{[ |
54 |
|
{{<<"application">>, <<"json">>, '*'}, from_json} |
55 |
|
], Req, State}. |
56 |
|
|
57 |
|
-spec allowed_methods(req(), state()) -> {[binary()], req(), state()}. |
58 |
|
allowed_methods(Req, State) -> |
59 |
26 |
{[<<"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 |
15 |
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 |
11 |
try_handle_request(Req, State, fun handle_post/2). |
70 |
|
|
71 |
|
handle_get(Req, State = #{jid := OwnerJid}) -> |
72 |
15 |
Bindings = cowboy_req:bindings(Req), |
73 |
15 |
WithJid = get_with_jid(Bindings), |
74 |
14 |
Args = parse_qs(Req), |
75 |
13 |
Limit = get_limit(Args), |
76 |
12 |
Before = get_before(Args), |
77 |
11 |
{ok, {Rows, _Limit}} = |
78 |
|
mongoose_stanza_api:lookup_recent_messages(OwnerJid, WithJid, Before, Limit, false), |
79 |
11 |
Resp = [make_json_msg(Msg, MAMId) || #{id := MAMId, packet := Msg} <- Rows], |
80 |
11 |
{jiffy:encode(Resp), Req, State}. |
81 |
|
|
82 |
|
handle_post(Req, State = #{jid := UserJid}) -> |
83 |
11 |
Args = parse_body(Req), |
84 |
11 |
To = get_to(Args), |
85 |
9 |
Body = get_body(Args), |
86 |
8 |
{ok, Resp} = mongoose_stanza_api:send_chat_message(UserJid, undefined, To, Body), |
87 |
8 |
Req2 = cowboy_req:set_resp_body(jiffy:encode(Resp), Req), |
88 |
8 |
{true, Req2, State}. |
89 |
|
|
90 |
|
get_limit(#{limit := LimitBin}) -> |
91 |
10 |
try |
92 |
10 |
Limit = binary_to_integer(LimitBin), |
93 |
9 |
true = Limit >= 0, |
94 |
9 |
Limit |
95 |
|
catch |
96 |
1 |
_:_ -> throw_error(bad_request, <<"Invalid limit">>) |
97 |
|
end; |
98 |
3 |
get_limit(#{}) -> 50. |
99 |
|
|
100 |
|
get_before(#{before := BeforeBin}) -> |
101 |
3 |
try |
102 |
3 |
1000 * binary_to_integer(BeforeBin) |
103 |
|
catch |
104 |
1 |
_:_ -> throw_error(bad_request, <<"Invalid value of 'before'">>) |
105 |
|
end; |
106 |
9 |
get_before(#{}) -> undefined. |
107 |
|
|
108 |
|
get_with_jid(#{with := With}) -> |
109 |
14 |
case jid:from_binary(With) of |
110 |
1 |
error -> throw_error(bad_request, <<"Invalid interlocutor JID">>); |
111 |
13 |
WithJid -> WithJid |
112 |
|
end; |
113 |
1 |
get_with_jid(#{}) -> undefined. |
114 |
|
|
115 |
|
get_to(#{to := To}) -> |
116 |
10 |
case jid:from_binary(To) of |
117 |
1 |
error -> throw_error(bad_request, <<"Invalid recipient JID">>); |
118 |
9 |
ToJid -> ToJid |
119 |
|
end; |
120 |
1 |
get_to(#{}) -> throw_error(bad_request, <<"Missing recipient JID">>). |
121 |
|
|
122 |
8 |
get_body(#{body := Body}) -> Body; |
123 |
1 |
get_body(#{}) -> throw_error(bad_request, <<"Missing message body">>). |
124 |
|
|
125 |
|
make_json_msg(Msg, MAMId) -> |
126 |
24 |
{Microsec, _} = mod_mam_utils:decode_compact_uuid(MAMId), |
127 |
24 |
encode(Msg, Microsec div 1000). |
128 |
|
|
129 |
|
-spec encode(exml:item(), integer()) -> map(). |
130 |
|
encode(Msg, Timestamp) -> |
131 |
27 |
BodyTag = exml_query:path(Msg, [{element, <<"body">>}]), |
132 |
27 |
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 |
27 |
maps:from_list(L). |
138 |
|
|
139 |
|
extensions(Msg) -> |
140 |
27 |
SmackNS = <<"http://www.jivesoftware.com/xmlns/xmpp/properties">>, |
141 |
27 |
RawMsgProps = exml_query:subelement_with_name_and_ns(Msg, <<"properties">>, SmackNS), |
142 |
27 |
case RawMsgProps of |
143 |
|
#xmlel{children = Children} -> |
144 |
1 |
Props = [convert_prop_child(Child) || Child <- Children], |
145 |
1 |
[{<<"properties">>, maps:from_list(Props)}]; |
146 |
|
_ -> |
147 |
26 |
[] |
148 |
|
end. |
149 |
|
|
150 |
|
thread_and_parent(Msg) -> |
151 |
27 |
case exml_query:path(Msg, [{element, <<"thread">>}, cdata]) of |
152 |
25 |
undefined -> []; |
153 |
2 |
Thread -> [{<<"thread">>, Thread} | parent(Msg)] |
154 |
|
end. |
155 |
|
|
156 |
|
parent(Msg) -> |
157 |
2 |
case exml_query:path(Msg, [{element, <<"thread">>}, {attr, <<"parent">>}]) of |
158 |
1 |
undefined -> []; |
159 |
1 |
ThreadParent -> [{<<"parent">>, ThreadParent}] |
160 |
|
end. |
161 |
|
|
162 |
|
convert_prop_child(Child)-> |
163 |
2 |
Name = exml_query:path(Child, [{element, <<"name">>}, cdata]), |
164 |
2 |
Value = exml_query:path(Child, [{element, <<"value">>}, cdata]), |
165 |
2 |
{Name, Value}. |