1 |
|
-module(mongoose_client_api_rooms_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 |
|
-export([encode/2]). |
17 |
|
|
18 |
|
-ignore_xref([from_json/2, to_json/2, trails/0]). |
19 |
|
|
20 |
|
-import(mongoose_client_api, [parse_body/1, parse_qs/1, try_handle_request/3, throw_error/2]). |
21 |
|
|
22 |
|
-type req() :: cowboy_req:req(). |
23 |
|
-type state() :: map(). |
24 |
|
|
25 |
|
-include("jlib.hrl"). |
26 |
|
-include_lib("exml/include/exml.hrl"). |
27 |
|
|
28 |
|
-spec routes() -> mongoose_http_handler:routes(). |
29 |
|
routes() -> |
30 |
108 |
[{"/rooms/[:id]/messages", ?MODULE, #{}}]. |
31 |
|
|
32 |
|
trails() -> |
33 |
108 |
mongoose_client_api_rooms_messages_doc:trails(). |
34 |
|
|
35 |
|
-spec init(req(), state()) -> {cowboy_rest, req(), state()}. |
36 |
|
init(Req, Opts) -> |
37 |
30 |
mongoose_client_api:init(Req, Opts). |
38 |
|
|
39 |
|
-spec is_authorized(req(), state()) -> {true | {false, iodata()}, req(), state()}. |
40 |
|
is_authorized(Req, State) -> |
41 |
30 |
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 |
30 |
mongoose_client_api_rooms:content_types_provided(Req, State). |
47 |
|
|
48 |
|
-spec content_types_accepted(req(), state()) -> |
49 |
|
{[{{binary(), binary(), '*'}, atom()}], req(), state()}. |
50 |
|
content_types_accepted(Req, State) -> |
51 |
17 |
mongoose_client_api_rooms:content_types_accepted(Req, State). |
52 |
|
|
53 |
|
-spec allowed_methods(req(), state()) -> {[binary()], req(), state()}. |
54 |
|
allowed_methods(Req, State) -> |
55 |
30 |
{[<<"OPTIONS">>, <<"GET">>, <<"POST">>], Req, State}. |
56 |
|
|
57 |
|
%% @doc Called for a method of type "GET" |
58 |
|
-spec to_json(req(), state()) -> {iodata() | stop, req(), state()}. |
59 |
|
to_json(Req, State) -> |
60 |
13 |
try_handle_request(Req, State, fun handle_get/2). |
61 |
|
|
62 |
|
%% @doc Called for a method of type "POST" |
63 |
|
-spec from_json(req(), state()) -> {true | stop, req(), state()}. |
64 |
|
from_json(Req, State) -> |
65 |
17 |
try_handle_request(Req, State, fun handle_post/2). |
66 |
|
|
67 |
|
%% Internal functions |
68 |
|
|
69 |
|
handle_get(Req, State = #{jid := UserJid}) -> |
70 |
13 |
Bindings = cowboy_req:bindings(Req), |
71 |
13 |
RoomJid = mongoose_client_api_rooms:get_room_jid(Bindings, State, required), |
72 |
13 |
Args = parse_qs(Req), |
73 |
12 |
Limit = get_limit(Args), |
74 |
11 |
Before = get_before(Args), |
75 |
10 |
case mod_muc_light_api:get_room_messages(RoomJid, UserJid, Limit, Before) of |
76 |
|
{ok, Msgs} -> |
77 |
8 |
JSONData = [make_json_item(Msg) || Msg <- Msgs], |
78 |
8 |
{jiffy:encode(JSONData), Req, State}; |
79 |
|
{room_not_found, Msg} -> |
80 |
1 |
throw_error(not_found, Msg); |
81 |
|
{not_room_member, Msg} -> |
82 |
1 |
throw_error(denied, Msg) |
83 |
|
end. |
84 |
|
|
85 |
|
handle_post(Req, State = #{jid := UserJid}) -> |
86 |
17 |
Bindings = cowboy_req:bindings(Req), |
87 |
17 |
RoomJid = mongoose_client_api_rooms:get_room_jid(Bindings, State, required), |
88 |
17 |
Args = parse_body(Req), |
89 |
16 |
Children = verify_children(get_body(Args) ++ get_marker(Args) ++ get_markable(Args)), |
90 |
12 |
UUID = uuid:uuid_to_string(uuid:get_v4(), binary_standard), |
91 |
12 |
Attrs = [{<<"id">>, UUID}], |
92 |
12 |
case mod_muc_light_api:send_message(RoomJid, UserJid, Children, Attrs) of |
93 |
|
{ok, _} -> |
94 |
10 |
Resp = #{id => UUID}, |
95 |
10 |
Req3 = cowboy_req:set_resp_body(jiffy:encode(Resp), Req), |
96 |
10 |
{true, Req3, State}; |
97 |
|
{room_not_found, Msg} -> |
98 |
1 |
throw_error(not_found, Msg); |
99 |
|
{not_room_member, Msg} -> |
100 |
1 |
throw_error(denied, Msg) |
101 |
|
end. |
102 |
|
|
103 |
|
get_limit(#{limit := LimitBin}) -> |
104 |
6 |
try |
105 |
6 |
Limit = binary_to_integer(LimitBin), |
106 |
5 |
true = Limit >= 0 andalso Limit =< 500, |
107 |
5 |
Limit |
108 |
|
catch |
109 |
1 |
_:_ -> throw_error(bad_request, <<"Invalid limit">>) |
110 |
|
end; |
111 |
6 |
get_limit(#{}) -> 50. |
112 |
|
|
113 |
|
get_before(#{before := BeforeBin}) -> |
114 |
3 |
try |
115 |
3 |
1000 * binary_to_integer(BeforeBin) |
116 |
|
catch |
117 |
1 |
_:_ -> throw_error(bad_request, <<"Invalid value of 'before'">>) |
118 |
|
end; |
119 |
8 |
get_before(#{}) -> undefined. |
120 |
|
|
121 |
|
get_body(#{body := Body}) when is_binary(Body) -> |
122 |
9 |
[#xmlel{ name = <<"body">>, children = [#xmlcdata{ content = Body }] }]; |
123 |
1 |
get_body(#{body := _}) -> throw_error(bad_request, <<"Invalid message body">>); |
124 |
6 |
get_body(#{}) -> []. |
125 |
|
|
126 |
|
get_marker(#{chat_marker := #{type := Type, id := Id}}) |
127 |
|
when Type == <<"received">>; |
128 |
|
Type == <<"displayed">>; |
129 |
|
Type == <<"acknowledged">> -> |
130 |
3 |
[#xmlel{ name = Type, attrs = [{<<"xmlns">>, ?NS_CHAT_MARKERS}, {<<"id">>, Id}] }]; |
131 |
1 |
get_marker(#{chat_marker := _}) -> throw_error(bad_request, <<"Invalid chat marker">>); |
132 |
11 |
get_marker(#{}) -> []. |
133 |
|
|
134 |
|
get_markable(#{body := _, markable := true}) -> |
135 |
1 |
[#xmlel{ name = <<"markable">>, attrs = [{<<"xmlns">>, ?NS_CHAT_MARKERS}] }]; |
136 |
13 |
get_markable(#{}) -> []. |
137 |
|
|
138 |
2 |
verify_children([]) -> throw_error(bad_request, <<"No valid message elements">>); |
139 |
12 |
verify_children(Children) -> Children. |
140 |
|
|
141 |
|
-spec encode(Packet :: exml:element(), Timestamp :: integer()) -> map(). |
142 |
|
encode(Packet, Timestamp) -> |
143 |
2 |
From = exml_query:attr(Packet, <<"from">>), |
144 |
2 |
FromJID = jid:from_binary(From), |
145 |
2 |
Msg = make_json_item(Packet, FromJID, Timestamp), |
146 |
2 |
Msg#{room => FromJID#jid.luser}. |
147 |
|
|
148 |
|
-spec make_json_item(mod_mam:message_row()) -> term(). |
149 |
|
make_json_item(#{id := MAMID, jid := JID, packet := Msg}) -> |
150 |
26 |
{Microsec, _} = mod_mam_utils:decode_compact_uuid(MAMID), |
151 |
26 |
make_json_item(Msg, JID, Microsec div 1000). |
152 |
|
|
153 |
|
make_json_item(Msg, JID, Timestamp) -> |
154 |
28 |
Item = #{id => exml_query:attr(Msg, <<"id">>), |
155 |
|
from => make_from(JID), |
156 |
|
timestamp => Timestamp}, |
157 |
28 |
add_body_and_type(Item, Msg). |
158 |
|
|
159 |
|
make_from(#jid{lresource = <<>>} = JID) -> |
160 |
6 |
jid:to_binary(JID); |
161 |
|
make_from(#jid{lresource = Sender}) -> |
162 |
22 |
Sender. |
163 |
|
|
164 |
|
add_body_and_type(Item, Msg) -> |
165 |
28 |
case exml_query:path(Msg, [{element, <<"x">>}, {element, <<"user">>}]) of |
166 |
|
undefined -> |
167 |
22 |
add_regular_message_body(Item, Msg); |
168 |
|
#xmlel{} = AffChange -> |
169 |
6 |
add_aff_change_body(Item, AffChange) |
170 |
|
end. |
171 |
|
|
172 |
|
add_regular_message_body(Item0, Msg) -> |
173 |
22 |
Item1 = Item0#{type => <<"message">>}, |
174 |
22 |
Item2 = |
175 |
|
case exml_query:path(Msg, [{element, <<"body">>}, cdata]) of |
176 |
|
undefined -> |
177 |
3 |
Item1; |
178 |
|
Body -> |
179 |
19 |
Item1#{body => Body} |
180 |
|
end, |
181 |
22 |
add_chat_marker(Item2, Msg). |
182 |
|
|
183 |
|
add_chat_marker(Item0, Msg) -> |
184 |
22 |
case exml_query:subelement_with_ns(Msg, ?NS_CHAT_MARKERS) of |
185 |
|
undefined -> |
186 |
18 |
Item0; |
187 |
|
#xmlel{ name = <<"markable">> } -> |
188 |
1 |
Item0#{ markable => true }; |
189 |
|
#xmlel{ name = Type } = Marker -> |
190 |
3 |
Item0#{ chat_marker => #{ type => Type, id => exml_query:attr(Marker, <<"id">>) } } |
191 |
|
end. |
192 |
|
|
193 |
|
add_aff_change_body(Item, #xmlel{attrs = Attrs} = User) -> |
194 |
6 |
Item#{type => <<"affiliation">>, |
195 |
|
affiliation => proplists:get_value(<<"affiliation">>, Attrs), |
196 |
|
user => exml_query:cdata(User)}. |