Compare commits

...

3 Commits

5 changed files with 72 additions and 11 deletions

View File

@ -157,6 +157,10 @@ ul.notifications-list {
a {
text-decoration: underline;
&.local-actor-link {
text-decoration: none;
}
}
}
}

View File

@ -31,7 +31,7 @@
</p>
</div>
<div class="status-content">
<%= raw(@original_note["content"]) %>
<%= render_status_content(@original_activity) %>
</div>
<div class="status-actions">
<a href="<%= Routes.frontend_path(@conn, :reply, @original_activity.id) %>">Reply</a>

View File

@ -16,9 +16,15 @@
</p>
</div>
<div class="status-content e-content">
<%= raw(@note["content"]) %>
</div>
<div class="status-actions">
<a href="<%= Routes.frontend_path(@conn, :reply, @status.id) %>">Reply</a>
<%= unless is_nil(@conn.assigns[:user]) do %>
<%= render_status_content(@status) %>
<% else %>
<%= raw(@note["content"]) %>
<% end %>
</div>
<%= unless is_nil(@conn.assigns[:user]) do %>
<div class="status-actions">
<a href="<%= Routes.frontend_path(@conn, :reply, @status.id) %>">Reply</a>
</div>
<% end %>
</div>

View File

@ -1,10 +1,12 @@
<%= render "_status.html", conn: @conn, author: @author, status: @status, note: @status.data["object"] %>
<hr>
<%= form_tag Routes.frontend_path(@conn, :post_status), method: :post, class: "compose-status" do %>
<input type="hidden" name="in_reply_to" value="<%= @status.data["object"]["id"] %>">
<textarea id="content" name="content" rows="5" placeholder="Reply" required><%= mentions_for_replying_to(@conn, @status) %></textarea>
<%= submit "Post" %>
<%= unless is_nil(@current_user) do %>
<hr>
<%= form_tag Routes.frontend_path(@conn, :post_status), method: :post, class: "compose-status" do %>
<input type="hidden" name="in_reply_to" value="<%= @status.data["object"]["id"] %>">
<textarea id="content" name="content" rows="5" placeholder="Reply" required><%= mentions_for_replying_to(@conn, @status) %></textarea>
<%= submit "Post" %>
<hr>
<% end %>
<% end %>

View File

@ -3,6 +3,7 @@ defmodule ClacksWeb.FrontendView do
alias Clacks.{Actor, Activity, Repo, Notification}
alias ClacksWeb.Router.Helpers, as: Routes
alias ClacksWeb.Endpoint
require Logger
@spec display_username(actor :: Actor.t()) :: String.t()
@ -126,4 +127,52 @@ defmodule ClacksWeb.FrontendView do
end
defp mentions_for_replying_to(_), do: ""
@spec render_status_content(activity :: Activity.t()) :: String.t()
defp render_status_content(%Activity{
data: %{
"type" => "Create",
"object" => %{"type" => "Note", "content" => content} = note
}
}) do
with %{"tag" => tags} <- note,
{:ok, tree} <- Floki.parse_fragment(content) do
tree
|> Floki.traverse_and_update(fn
{"a", attrs, _children} = orig_tree ->
{"href", href} = Enum.find(attrs, fn {name, _} -> name == "href" end)
has_matching_tag =
Enum.any?(tags, fn
%{"type" => "Mention", "href" => ^href} -> true
_ -> false
end)
with true <- has_matching_tag,
%Actor{local: false} = actor <- Actor.get_cached_by_ap_id(href) do
{
"span",
[],
[
orig_tree,
{"a", [{"href", local_actor_link(actor)}, {"class", "local-actor-link"}], ["🔗"]}
]
}
else
_ ->
orig_tree
end
tree ->
tree
end)
|> Floki.raw_html()
# remove the <html> and </html> from the floki rendered output
|> String.slice(6..-8)
|> raw()
else
_ ->
content
end
end
end