./ct_report/coverage/mongoose_client_api_rooms_messages.COVER.html

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 4 [{"/rooms/[:id]/messages", ?MODULE, #{}}].
31
32 trails() ->
33 4 mongoose_client_api_rooms_messages_doc:trails().
34
35 -spec init(req(), state()) -> {cowboy_rest, req(), state()}.
36 init(Req, Opts) ->
37
:-(
mongoose_client_api:init(Req, Opts).
38
39 -spec is_authorized(req(), state()) -> {true | {false, iodata()}, req(), state()}.
40 is_authorized(Req, State) ->
41
:-(
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
:-(
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
:-(
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
:-(
{[<<"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
:-(
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
:-(
try_handle_request(Req, State, fun handle_post/2).
66
67 %% Internal functions
68
69 handle_get(Req, State = #{jid := UserJid}) ->
70
:-(
Bindings = cowboy_req:bindings(Req),
71
:-(
RoomJid = mongoose_client_api_rooms:get_room_jid(Bindings, State, required),
72
:-(
Args = parse_qs(Req),
73
:-(
Limit = get_limit(Args),
74
:-(
Before = get_before(Args),
75
:-(
case mod_muc_light_api:get_room_messages(RoomJid, UserJid, Limit, Before) of
76 {ok, Msgs} ->
77
:-(
JSONData = [make_json_item(Msg) || Msg <- Msgs],
78
:-(
{jiffy:encode(JSONData), Req, State};
79 {room_not_found, Msg} ->
80
:-(
throw_error(not_found, Msg);
81 {not_room_member, Msg} ->
82
:-(
throw_error(denied, Msg)
83 end.
84
85 handle_post(Req, State = #{jid := UserJid}) ->
86
:-(
Bindings = cowboy_req:bindings(Req),
87
:-(
RoomJid = mongoose_client_api_rooms:get_room_jid(Bindings, State, required),
88
:-(
Args = parse_body(Req),
89
:-(
Children = verify_children(get_body(Args) ++ get_marker(Args) ++ get_markable(Args)),
90
:-(
UUID = uuid:uuid_to_string(uuid:get_v4(), binary_standard),
91
:-(
Attrs = [{<<"id">>, UUID}],
92
:-(
case mod_muc_light_api:send_message(RoomJid, UserJid, Children, Attrs) of
93 {ok, _} ->
94
:-(
Resp = #{id => UUID},
95
:-(
Req3 = cowboy_req:set_resp_body(jiffy:encode(Resp), Req),
96
:-(
{true, Req3, State};
97 {room_not_found, Msg} ->
98
:-(
throw_error(not_found, Msg);
99 {not_room_member, Msg} ->
100
:-(
throw_error(denied, Msg)
101 end.
102
103 get_limit(#{limit := LimitBin}) ->
104
:-(
try
105
:-(
Limit = binary_to_integer(LimitBin),
106
:-(
true = Limit >= 0 andalso Limit =< 500,
107
:-(
Limit
108 catch
109
:-(
_:_ -> throw_error(bad_request, <<"Invalid limit">>)
110 end;
111
:-(
get_limit(#{}) -> 50.
112
113 get_before(#{before := BeforeBin}) ->
114
:-(
try
115
:-(
1000 * binary_to_integer(BeforeBin)
116 catch
117
:-(
_:_ -> throw_error(bad_request, <<"Invalid value of 'before'">>)
118 end;
119
:-(
get_before(#{}) -> undefined.
120
121 get_body(#{body := Body}) when is_binary(Body) ->
122
:-(
[#xmlel{ name = <<"body">>, children = [#xmlcdata{ content = Body }] }];
123
:-(
get_body(#{body := _}) -> throw_error(bad_request, <<"Invalid message body">>);
124
:-(
get_body(#{}) -> [].
125
126 get_marker(#{chat_marker := #{type := Type, id := Id}})
127 when Type == <<"received">>;
128 Type == <<"displayed">>;
129 Type == <<"acknowledged">> ->
130
:-(
[#xmlel{ name = Type, attrs = [{<<"xmlns">>, ?NS_CHAT_MARKERS}, {<<"id">>, Id}] }];
131
:-(
get_marker(#{chat_marker := _}) -> throw_error(bad_request, <<"Invalid chat marker">>);
132
:-(
get_marker(#{}) -> [].
133
134 get_markable(#{body := _, markable := true}) ->
135
:-(
[#xmlel{ name = <<"markable">>, attrs = [{<<"xmlns">>, ?NS_CHAT_MARKERS}] }];
136
:-(
get_markable(#{}) -> [].
137
138
:-(
verify_children([]) -> throw_error(bad_request, <<"No valid message elements">>);
139
:-(
verify_children(Children) -> Children.
140
141 -spec encode(Packet :: exml:element(), Timestamp :: integer()) -> map().
142 encode(Packet, Timestamp) ->
143
:-(
From = exml_query:attr(Packet, <<"from">>),
144
:-(
FromJID = jid:from_binary(From),
145
:-(
Msg = make_json_item(Packet, FromJID, Timestamp),
146
:-(
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
:-(
{Microsec, _} = mod_mam_utils:decode_compact_uuid(MAMID),
151
:-(
make_json_item(Msg, JID, Microsec div 1000).
152
153 make_json_item(Msg, JID, Timestamp) ->
154
:-(
Item = #{id => exml_query:attr(Msg, <<"id">>),
155 from => make_from(JID),
156 timestamp => Timestamp},
157
:-(
add_body_and_type(Item, Msg).
158
159 make_from(#jid{lresource = <<>>} = JID) ->
160
:-(
jid:to_binary(JID);
161 make_from(#jid{lresource = Sender}) ->
162
:-(
Sender.
163
164 add_body_and_type(Item, Msg) ->
165
:-(
case exml_query:path(Msg, [{element, <<"x">>}, {element, <<"user">>}]) of
166 undefined ->
167
:-(
add_regular_message_body(Item, Msg);
168 #xmlel{} = AffChange ->
169
:-(
add_aff_change_body(Item, AffChange)
170 end.
171
172 add_regular_message_body(Item0, Msg) ->
173
:-(
Item1 = Item0#{type => <<"message">>},
174
:-(
Item2 =
175 case exml_query:path(Msg, [{element, <<"body">>}, cdata]) of
176 undefined ->
177
:-(
Item1;
178 Body ->
179
:-(
Item1#{body => Body}
180 end,
181
:-(
add_chat_marker(Item2, Msg).
182
183 add_chat_marker(Item0, Msg) ->
184
:-(
case exml_query:subelement_with_ns(Msg, ?NS_CHAT_MARKERS) of
185 undefined ->
186
:-(
Item0;
187 #xmlel{ name = <<"markable">> } ->
188
:-(
Item0#{ markable => true };
189 #xmlel{ name = Type } = Marker ->
190
:-(
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
:-(
Item#{type => <<"affiliation">>,
195 affiliation => proplists:get_value(<<"affiliation">>, Attrs),
196 user => exml_query:cdata(User)}.
Line Hits Source