clacks/lib/clacks_web/views/frontend_view.ex

68 lines
1.7 KiB
Elixir
Raw Normal View History

2019-10-06 23:41:18 +00:00
defmodule ClacksWeb.FrontendView do
use ClacksWeb, :view
2020-04-25 19:47:22 +00:00
alias Clacks.{Actor, Activity}
2020-04-25 16:30:47 +00:00
@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
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
2020-04-25 19:47:22 +00:00
def prev_page_path(conn, activities) do
if Map.has_key?(conn.query_params, "max_id") do
Phoenix.Controller.current_path(conn, %{
since_id: activities |> List.first() |> activity_id()
})
else
nil
end
end
def next_page_path(conn, activities) do
if length(activities) < 20 do
nil
else
Phoenix.Controller.current_path(conn, %{
max_id: activities |> List.last() |> activity_id()
})
end
end
defp activity_id(%Activity{id: id}), do: id
defp activity_id({%Activity{id: id}, _}), do: id
2019-10-06 23:41:18 +00:00
end