Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion rebar.config
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
{deps, [
{kura, {git, "https://github.com/Taure/kura.git", {tag, "v2.20.12"}}},
{kura_postgres, {git, "https://github.com/Taure/kura_postgres.git", {tag, "v1.1.3"}}},
{datastar, {git, "https://github.com/Taure/datastar.git", {branch, "main"}}},
{datastar, {git, "https://github.com/Taure/datastar.git", {tag, "v0.1.0"}}},
{datastar_nova, {git, "https://github.com/Taure/datastar_nova.git", {tag, "v0.1.0"}}},
nova
]}.
Expand Down
2 changes: 1 addition & 1 deletion rebar.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
{<<"cowlib">>,{pkg,<<"cowlib">>,<<"2.19.0">>},2},
{<<"datastar">>,
{git,"https://github.com/Taure/datastar.git",
{ref,"fcc11f399294b305620a7c99ed633b898ce3889b"}},
{ref,"b1f08c5a4998aab33389eef1788619110829bb5b"}},
0},
{<<"datastar_nova">>,
{git,"https://github.com/Taure/datastar_nova.git",
Expand Down
43 changes: 30 additions & 13 deletions src/controllers/ldf_www_controller.erl
Original file line number Diff line number Diff line change
Expand Up @@ -78,19 +78,36 @@ submit_history(Req0) ->

%% Convert a stored message to the requested ETSI XML and inject it, pretty
%% printed, under its row. Any other format (e.g. "hide") clears the block.
message_etsi(#{bindings := #{~"messageid" := MessageId, ~"format" := Format}}) when
Format =:= ~"103707"; Format =:= ~"103120"
->
Block =
case ldf_db:get_message(MessageId) of
{ok, [#{payload := Payload} | _]} ->
render_etsi(Format, MessageId, safe_decode(Payload));
_ ->
etsi_error(~"Message not found.")
end,
{datastar, [patch_xml(MessageId, Block)]};
message_etsi(#{bindings := #{~"messageid" := MessageId}}) ->
{datastar, [patch_xml(MessageId, ~"")]}.
message_etsi(#{bindings := #{~"messageid" := MessageId, ~"format" := Format}}) ->
case is_message_id(MessageId) of
true -> {datastar, [patch_xml(MessageId, etsi_content(Format, MessageId))]};
false -> {status, 404}
end.

etsi_content(Format, MessageId) when Format =:= ~"103707"; Format =:= ~"103120" ->
case ldf_db:get_message(MessageId) of
{ok, [#{payload := Payload} | _]} ->
render_etsi(Format, MessageId, safe_decode(Payload));
_ ->
etsi_error(~"Message not found.")
end;
etsi_content(_Format, _MessageId) ->
~"".

is_message_id(<<A:8/binary, $-, B:4/binary, $-, C:4/binary, $-, D:4/binary, $-, E:12/binary>>) ->
[] =:=
[
Digit
|| <<Digit>> <= <<A/binary, B/binary, C/binary, D/binary, E/binary>>,
not is_hex_digit(Digit)
];
is_message_id(_) ->
false.

is_hex_digit(Digit) ->
(Digit >= $0 andalso Digit =< $9) orelse
(Digit >= $a andalso Digit =< $f) orelse
(Digit >= $A andalso Digit =< $F).

%% --- published by the message API on each intercepted message -------------

Expand Down
98 changes: 98 additions & 0 deletions test/ldf_www_controller_tests.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
-module(ldf_www_controller_tests).

-include_lib("eunit/include/eunit.hrl").
-include_lib("nova/include/nova_pubsub.hrl").

-define(MESSAGE_ID, ~"1e87fd7c-de4f-4e97-be27-2bdcdbad23b7").

hiding_a_message_clears_its_block_test() ->
{datastar, Events} = ldf_www_controller:message_etsi(request(?MESSAGE_ID, ~"hide")),
[Event] = sse_events(iolist_to_binary(Events)),
?assertEqual([~"#xml-1e87fd7c-de4f-4e97-be27-2bdcdbad23b7"], data(~"selector", Event)),
?assertEqual([~"inner"], data(~"mode", Event)).

upper_case_message_id_is_accepted_test() ->
Id = ~"1E87FD7C-DE4F-4E97-BE27-2BDCDBAD23B7",
{datastar, Events} = ldf_www_controller:message_etsi(request(Id, ~"hide")),
[Event] = sse_events(iolist_to_binary(Events)),
?assertEqual([~"#xml-1E87FD7C-DE4F-4E97-BE27-2BDCDBAD23B7"], data(~"selector", Event)).

message_id_that_is_not_a_uuid_is_not_found_test_() ->
[
?_assertEqual({status, 404}, ldf_www_controller:message_etsi(request(Id, Format)))
|| Id <- [
~"1e87fd7c-de4f-4e97-be27-2bdcdbad23b7\n",
~"1e87fd7c\nde4f-4e97-be27-2bdcdbad23b7",
~"1e87fd7c-de4f-4e97-be27-2bdcdbad23b7\r\nevent: x",
~"1e87fd7c-de4f-4e97-be27-2bdcdbad23b7, body",
~"1e87fd7c-de4f-4e97-be27-2bdcdbad23bg",
~"1e87fd7cde4f4e97be272bdcdbad23b7",
~""
],
Format <- [~"hide", ~"103707", ~"103120"]
].

line_breaks_in_a_message_text_stay_inside_the_row_event_test() ->
ok = nova_pubsub:start(),
ok = nova_pubsub:join(ldf_messages),
Message = #{~"id" => ?MESSAGE_ID, ~"payload" => ~"one\r\ntwo\rthree\nfour"},
ldf_www_controller:publish_message(?MESSAGE_ID, iolist_to_binary(json:encode(Message))),
Stream =
receive
#nova_pubsub{channel = ldf_messages, payload = Events} -> iolist_to_binary(Events)
after 1000 -> error(nothing_published)
end,
nova_pubsub:leave(ldf_messages),
[Placeholder, Row] = sse_events(Stream),
?assertEqual([~"data", ~"event"], field_names(Placeholder)),
?assertEqual([~"#feed-empty"], data(~"selector", Placeholder)),
?assertEqual([~"data", ~"event"], field_names(Row)),
?assertEqual([~"#messages"], data(~"selector", Row)),
?assertEqual([~"append"], data(~"mode", Row)),
Html = elements(Row),
?assertMatch({0, _}, binary:match(Html, ~"<div class=\"rec new\">")),
?assertMatch(
{_, _},
binary:match(Html, ~"<div class=\"rec-body\">one\ntwo\nthree\nfour</div>")
),
?assertEqual(~"class=\"etsi\"></div></div>", binary:part(Html, byte_size(Html), -25)).

request(MessageId, Format) ->
#{bindings => #{~"messageid" => MessageId, ~"format" => Format}}.

%% Reads a stream the way an EventSource client does: a line ends at CRLF,
%% CR or LF, an empty line ends an event, and a line without a colon is a
%% field with an empty value.
sse_events(Stream) ->
events(binary:split(Stream, [~"\r\n", ~"\r", ~"\n"], [global]), []).

events([], _Unfinished) ->
[];
events([~"" | Lines], []) ->
events(Lines, []);
events([~"" | Lines], Fields) ->
[lists:reverse(Fields) | events(Lines, [])];
events([Line | Lines], Fields) ->
events(Lines, [field(Line) | Fields]).

field(Line) ->
case binary:split(Line, ~":") of
[Name, <<$\s, Value/binary>>] -> {Name, Value};
[Name, Value] -> {Name, Value};
[Name] -> {Name, ~""}
end.

field_names(Event) ->
lists:usort([Name || {Name, _Value} <- Event]).

-spec data(binary(), [{binary(), binary()}]) -> [binary()].
data(Kind, Event) ->
[
Rest
|| {~"data", Value} <- Event, [Found, Rest] <- [binary:split(Value, ~" ")], Found =:= Kind
].

-spec elements([{binary(), binary()}]) -> binary().
elements(Event) ->
Data = iolist_to_binary([[Line, $\n] || Line <- data(~"elements", Event)]),
binary:part(Data, 0, byte_size(Data) - 1).
Loading