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
1 change: 1 addition & 0 deletions config/sys.config
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
]},
{ldf, [
{chatli_path, <<"http://chatli:8090/v1">>},
{chatli_public_path, <<"http://localhost:8090/v1">>},
{ldf_callback, <<"http://ldf:8095/receiver">>}
]}
%% Please change your app.src-file instead if you intend to add app-specific configurations
Expand Down
1 change: 1 addition & 0 deletions config/sys.docker.config
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
]},
{ldf, [
{chatli_path, <<"http://localhost:8090/v1">>},
{chatli_public_path, <<"http://localhost:8090/v1">>},
{ldf_callback, <<"http://localhost:8095/receiver">>}
]}
%% Please change your app.src-file instead if you intend to add app-specific configurations
Expand Down
2 changes: 2 additions & 0 deletions priv/assets/css/ldf.css
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ tbody tr:last-child td { border-bottom: none; }
/* live message feed */
.feed td:first-child { color: var(--faint); white-space: nowrap; width: 1%; }
.msg { font-family: var(--mono); color: var(--text); word-break: break-word; }
.msg a { color: var(--live); text-decoration: none; border-bottom: 1px dotted var(--live-dim); word-break: break-all; }
.msg a:hover { border-bottom-style: solid; }
@keyframes flashin {
0% { background: rgba(46,230,166,.16); transform: translateY(-3px); opacity: 0; }
100% { background: transparent; transform: none; opacity: 1; }
Expand Down
2 changes: 1 addition & 1 deletion src/controllers/ldf_receiver_controller.erl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ create_message(#{json := #{<<"id">> := MessageId} = Json}) ->
Message =
case Json of
#{<<"payload">> := #{<<"url">> := Url} = Payload} ->
{ok, ChatliPath} = application:get_env(ldf, chatli_path),
{ok, ChatliPath} = application:get_env(ldf, chatli_public_path),
Payload2 = maps:update(<<"url">>, <<ChatliPath/binary, "/", Url/binary>>, Payload),
maps:update(<<"payload">>, Payload2, Json);
_ ->
Expand Down
51 changes: 40 additions & 11 deletions src/controllers/ldf_www_controller.erl
Original file line number Diff line number Diff line change
Expand Up @@ -78,29 +78,58 @@ submit_history(Req0) ->
%% --- published by the message API on each intercepted message -------------

publish_message(MessageId, Payload) ->
Row = render(message_row_dtl, [{ref, ref(MessageId)}, {payload, Payload}, {new, true}]),
Row = render(message_row_dtl, row_vars(ref(MessageId), Payload, true)),
datastar_nova:publish(?MESSAGES, [
datastar:patch_elements(~"", #{selector => ~"#feed-empty", mode => remove}),
datastar:patch_elements(Row, #{selector => ~"#messages", mode => append})
]).

%% --- helpers --------------------------------------------------------------

%% Replace (not append) the whole list on connect, so reconnects re-sync
%% instead of duplicating the batch. Live messages still append.
message_init([]) ->
[];
[datastar:patch_elements(feed_empty(), #{selector => ~"#messages", mode => inner})];
message_init(Msgs) ->
Rows = [
render(message_row_dtl, [
{ref, ref(maps:get(message_id, M, ~""))},
{payload, maps:get(payload, M, ~"")},
{new, false}
])
render(
message_row_dtl,
row_vars(ref(maps:get(message_id, M, ~"")), maps:get(payload, M, ~""), false)
)
|| M <- Msgs
],
[
datastar:patch_elements(~"", #{selector => ~"#feed-empty", mode => remove}),
datastar:patch_elements(Rows, #{selector => ~"#messages", mode => append})
].
[datastar:patch_elements(Rows, #{selector => ~"#messages", mode => inner})].

feed_empty() ->
~"<tr id=\"feed-empty\"><td colspan=\"2\" class=\"empty\">Awaiting interception</td></tr>".

%% The stored payload is the full encoded message; show its inner payload -
%% an attachment URL becomes a clickable link, plain text stays text.
row_vars(Ref, StoredPayload, New) ->
case display_payload(StoredPayload) of
{link, Url} -> [{ref, Ref}, {new, New}, {link, Url}, {text, ~""}];
{text, Text} -> [{ref, Ref}, {new, New}, {link, false}, {text, Text}]
end.

display_payload(StoredPayload) ->
try json:decode(StoredPayload) of
#{~"payload" := #{~"url" := Url}} when is_binary(Url) -> {link, public_url(Url)};
#{~"payload" := Text} when is_binary(Text) -> {text, Text};
_ -> {text, StoredPayload}
catch
_:_ -> {text, StoredPayload}
end.

%% Rewrite an attachment URL onto the browser-reachable chatli host,
%% regardless of which host got baked in when it was stored.
public_url(Url) ->
case binary:split(Url, ~"/chat/") of
[_Host, Rest] ->
{ok, Public} = application:get_env(ldf, chatli_public_path),
<<Public/binary, "/chat/", Rest/binary>>;
_ ->
Url
end.

broadcast_listeners() ->
{ok, Listeners} = ldf_db:get_all_li(),
Expand Down
2 changes: 1 addition & 1 deletion src/views/message_row.dtl
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<tr{% if new %} class="new"{% endif %}><td class="mono-id">{{ ref }}</td><td class="msg">{{ payload }}</td></tr>
<tr{% if new %} class="new"{% endif %}><td class="mono-id">{{ ref }}</td><td class="msg">{% if link %}<a href="{{ link }}" target="_blank" rel="noopener">{{ link }}</a>{% else %}{{ text }}{% endif %}</td></tr>
Loading