diff --git a/lib/nulla_web/components/templates.ex b/lib/nulla_web/components/templates.ex index 817fa17..acec111 100644 --- a/lib/nulla_web/components/templates.ex +++ b/lib/nulla_web/components/templates.ex @@ -1,92 +1,77 @@ -defmodule NullaWeb.ActorHTML do +defmodule NullaWeb.UserHTML do use NullaWeb, :html - embed_templates "templates/actor/*" + embed_templates "templates/user/*" def format_birthdate(date) do formatted = Date.to_string(date) |> String.replace("-", "/") - - today = Date.utc_today() - - age = - today.year - date.year - - if {today.month, today.day} < {date.month, date.day}, do: 1, else: 0 - + age = Timex.diff(Timex.today(), date, :years) "#{formatted} (#{age} years old)" end def format_registration_date(date) do - now = Date.utc_today() + now = Timex.now() formatted = Date.to_string(date) |> String.replace("-", "/") - - diff_days = Date.diff(now, date) - - cond do - diff_days == 0 -> - "#{formatted} (today)" - - diff_days == 1 -> - "#{formatted} (1 day ago)" - - diff_days < 30 -> - "#{formatted} (#{diff_days} days ago)" - - diff_days < 365 -> - year_diff = now.year - date.year - month_diff = now.month - date.month - day_correction = if now.day < date.day, do: -1, else: 0 - months = year_diff * 12 + month_diff + day_correction - if months == 1 do - "#{formatted} (1 month ago)" - else - "#{formatted} (#{months} months ago)" - end - - true -> - year_diff = now.year - date.year - years = if {now.month, now.day} < {date.month, date.day}, do: year_diff - 1, else: year_diff - if years == 1 do - "#{formatted} (1 year ago)" - else - "#{formatted} (#{years} years ago)" - end - end + + diff = Timex.diff(now, date, :days) + + relative = + cond do + diff == 0 -> + "today" + + diff == 1 -> + "1 day ago" + + diff < 30 -> + "#{diff} days ago" + + diff < 365 -> + months = Timex.diff(now, date, :months) + if months == 1, do: "1 month ago", else: "#{months} months ago" + + true -> + years = Timex.diff(now, date, :years) + if years == 1, do: "1 year ago", else: "#{years} years ago" + end + + "#{formatted} (#{relative})" end def format_note_datetime(datetime) do - Calendar.strftime(datetime, "%d %B %Y, %H:%M") + Timex.format!(datetime, "{0D} {Mfull} {YYYY}, {h24}:{m}", :strftime) end def format_note_datetime_diff(datetime) do - now = DateTime.utc_now() - diff_seconds = DateTime.diff(now, datetime) + now = Timex.now() + diff = Timex.diff(now, datetime, :seconds) cond do - diff_seconds < 60 -> + diff < 60 -> "now" - diff_seconds < 3600 -> - minutes = div(diff_seconds, 60) + diff < 3600 -> + minutes = div(diff, 60) "#{minutes}m ago" - diff_seconds < 86400 -> - hours = div(diff_seconds, 3600) + diff < 86400 -> + hours = div(diff, 3600) "#{hours}h ago" - diff_seconds < 604_800 -> - days = div(diff_seconds, 86400) + diff < 518_400 -> + days = div(diff, 86400) "#{days}d ago" - diff_seconds < 2_592_000 -> - weeks = div(diff_seconds, 604_800) + diff < 2_419_200 -> + weeks = div(diff, 604_800) "#{weeks}w ago" - diff_seconds < 31_536_000 -> - months = div(diff_seconds, 2_592_000) + diff < 28_512_000 -> + months = Timex.diff(now, datetime, :months) "#{months}mo ago" true -> - years = div(diff_seconds, 31_536_000) + years = Timex.diff(now, datetime, :years) "#{years}y ago" end end diff --git a/lib/nulla_web/components/templates/actor/show.html.heex b/lib/nulla_web/components/templates/user/show.html.heex similarity index 75% rename from lib/nulla_web/components/templates/actor/show.html.heex rename to lib/nulla_web/components/templates/user/show.html.heex index fb5fedf..5a41a2f 100644 --- a/lib/nulla_web/components/templates/actor/show.html.heex +++ b/lib/nulla_web/components/templates/user/show.html.heex @@ -16,10 +16,10 @@
- +
- {@actor.name} - @{@actor.preferredUsername}@{@actor.domain} + {@user.realname} + @{@user.username}@{@domain}
-

{@actor.summary}

+

{@user.bio}

- <%= if @actor.vcard_Address do %> + <%= if @user.location do %>
<.icon name="hero-map-pin" class="mt-0.5 h-5 w-5 flex-none" />
-
{@actor.vcard_Address}
+
{@user.location}
<% end %> - <%= if @actor.vcard_bday do %> + <%= if @user.birthday do %>
<.icon name="hero-cake" class="mt-0.5 h-5 w-5 flex-none" />
-
{format_birthdate(@actor.vcard_bday)}
+
{format_birthdate(@user.birthday)}
<% end %>
<.icon name="hero-calendar" class="mt-0.5 h-5 w-5 flex-none" />
-
{format_registration_date(@actor.published)}
+
{format_registration_date(@user.inserted_at)}
- <%= if @actor.attachment do %> + <%= if @user.fields do %>
- <%= for %{"type" => "PropertyValue", "name" => name, "value" => value} <- @actor.attachment do %> -
{name}
+ <%= for {key, value} <- @user.fields do %> +
{key}
<%= if Regex.match?(~r{://}, value) do %> {Regex.replace(~r{^\w+://}, value, "")} @@ -66,30 +66,30 @@
<% end %>
- Featured - Posts - Posts and replies - Media + Featured + Posts + Posts and replies + Media
<%= for note <- @notes do %>
- +
- {@actor.name} + {@user.realname} - @{@actor.preferredUsername}@{@actor.domain} + @{@user.username}@{@domain}
diff --git a/lib/nulla_web/controllers/actor_controller.ex b/lib/nulla_web/controllers/actor_controller.ex index e3c55ea..c79ae22 100644 --- a/lib/nulla_web/controllers/actor_controller.ex +++ b/lib/nulla_web/controllers/actor_controller.ex @@ -24,12 +24,13 @@ defmodule NullaWeb.ActorController do |> send_resp(200, Jason.encode!(ActivityPub.actor(actor))) else notes = Note.get_latest_notes(actor.id) - following = 0 - followers = 0 + following = Utils.count_following_by_username!(actor.preferredUsername) + followers = Utils.count_followers_by_username!(actor.preferredUsername) render( conn, :show, + domain: domain, actor: actor, notes: notes, following: following, diff --git a/mix.exs b/mix.exs index 09ceeec..da0bd5b 100644 --- a/mix.exs +++ b/mix.exs @@ -43,6 +43,7 @@ defmodule Nulla.MixProject do {:phoenix_live_dashboard, "~> 0.8.3"}, {:esbuild, "~> 0.8", runtime: Mix.env() == :dev}, {:tailwind, "~> 0.2.0", runtime: Mix.env() == :dev}, + {:timex, "~> 3.7"}, {:heroicons, github: "tailwindlabs/heroicons", tag: "v2.1.1",