clacks/lib/clacks_web/views/frontend_view.ex

47 lines
1.1 KiB
Elixir

defmodule ClacksWeb.FrontendView do
use ClacksWeb, :view
alias Clacks.Actor
@spec display_username(actor :: Actor.t()) :: String.t()
def display_username(%Actor{local: true, data: %{"name" => name}}) do
"@" <> name
end
def display_username(%Actor{local: false, ap_id: ap_id, data: %{"name" => name}}) do
%URI{host: host} = URI.parse(ap_id)
"@" <> name <> "@" <> host
end
@absolute_timestamp_threshold 24 * 60 * 60
def display_timestamp(str) when is_binary(str) do
display_timestamp(Timex.parse!(str, "{ISO:Extended}"))
end
def display_timestamp(datetime) do
diff = Timex.diff(Timex.now(), datetime, :seconds)
cond do
diff < 60 ->
# less than a minute, seconds
"#{diff}sec"
diff < 60 * 60 ->
# less than an hour, minutes
"#{Integer.floor_div(diff, 60)}min"
diff < 60 * 60 * 24 ->
# less than a day, hours
"#{Integer.floor_div(diff, 60 * 60)}hr"
diff < 60 * 60 * 24 * 7 ->
# less than a week, days
"#{Integer.floor_div(diff, 60 * 60 * 24)}d"
true ->
Timex.format!(datetime, "%FT%T%:z", :strftime)
end
end
end