From afff7c4fb39ae5644953e6fb955a8348c4472cea Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Wed, 25 Aug 2021 17:22:16 -0400 Subject: [PATCH] Fix display names/usernames being switched --- .../templates/frontend/_status.html.eex | 2 +- lib/clacks_web/views/frontend_view.ex | 22 +++++++++++++++---- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/lib/clacks_web/templates/frontend/_status.html.eex b/lib/clacks_web/templates/frontend/_status.html.eex index 76a7194..e344045 100644 --- a/lib/clacks_web/templates/frontend/_status.html.eex +++ b/lib/clacks_web/templates/frontend/_status.html.eex @@ -3,7 +3,7 @@

- <%= @author.data["preferredUsername"] %> + <%= display_name(@author) %>

diff --git a/lib/clacks_web/views/frontend_view.ex b/lib/clacks_web/views/frontend_view.ex index 48b9acc..f2b01b8 100644 --- a/lib/clacks_web/views/frontend_view.ex +++ b/lib/clacks_web/views/frontend_view.ex @@ -7,13 +7,27 @@ defmodule ClacksWeb.FrontendView do @spec display_username(actor :: Actor.t()) :: String.t() - def display_username(%Actor{local: true, data: %{"name" => name}}) do - "@" <> name + def display_username(%Actor{local: true, data: %{"preferredUsername" => username}}) do + "@" <> username end - def display_username(%Actor{local: false, ap_id: ap_id, data: %{"name" => name}}) do + def display_username(%Actor{ + local: false, + ap_id: ap_id, + data: %{"preferredUsername" => username} + }) do %URI{host: host} = URI.parse(ap_id) - "@" <> name <> "@" <> host + "@" <> username <> "@" <> host + end + + @spec display_name(actor :: Actor.t()) :: String.t() + + def display_name(%Actor{data: %{"name" => name, "preferredUsername" => username}}) do + if is_binary(name) && String.length(name) > 0 do + name + else + username + end end def local_actor_link(%Actor{local: true, ap_id: ap_id}), do: ap_id