diff --git a/lib/philomena_web/controllers/profile_controller.ex b/lib/philomena_web/controllers/profile_controller.ex index cfbf0536..dd2de025 100644 --- a/lib/philomena_web/controllers/profile_controller.ex +++ b/lib/philomena_web/controllers/profile_controller.ex @@ -5,6 +5,7 @@ defmodule PhilomenaWeb.ProfileController do alias Philomena.Textile.Renderer alias Philomena.UserStatistics.UserStatistic alias Philomena.Users.User + alias Philomena.Bans alias Philomena.Galleries.Gallery alias Philomena.Posts.Post alias Philomena.Comments.Comment @@ -99,6 +100,11 @@ defmodule PhilomenaWeb.ProfileController do interactions = Interactions.user_interactions([recent_uploads, recent_faves], current_user) + bans = + Bans.User + |> where(user_id: ^user.id) + |> Repo.all() + render( conn, "show.html", @@ -114,6 +120,7 @@ defmodule PhilomenaWeb.ProfileController do statistics: statistics, about_me: about_me, tags: tags, + bans: bans, layout_class: "layout--medium" ) end diff --git a/lib/philomena_web/templates/profile/show.html.slime b/lib/philomena_web/templates/profile/show.html.slime index 2aa8eb64..f9cc3657 100644 --- a/lib/philomena_web/templates/profile/show.html.slime +++ b/lib/philomena_web/templates/profile/show.html.slime @@ -35,6 +35,13 @@ li = link("Tag changes", to: Routes.profile_tag_change_path(@conn, :index, @user)) li = link("Source changes", to: Routes.profile_source_change_path(@conn, :index, @user)) += if (current?(@user, @conn.assigns.current_user) or can?(@conn, :index, UserBan)) and Enum.any?(@bans) do + .block + .block__header + ' Ban History + .block__content + = render PhilomenaWeb.BanView, "_bans.html", bans: @bans, conn: @conn + .column-layout .column-layout__left .block diff --git a/lib/philomena_web/views/app_view.ex b/lib/philomena_web/views/app_view.ex index c643da93..39df7621 100644 --- a/lib/philomena_web/views/app_view.ex +++ b/lib/philomena_web/views/app_view.ex @@ -32,35 +32,39 @@ defmodule PhilomenaWeb.AppView do } def pretty_time(time) do - seconds = NaiveDateTime.diff(NaiveDateTime.utc_now(), time, :second) + now = NaiveDateTime.utc_now() + seconds = NaiveDateTime.diff(now, time, :second) relation = if seconds < 0, do: "from now", else: "ago" time = time |> DateTime.from_naive!("Etc/UTC") - seconds = abs(seconds) - minutes = abs(div(seconds, 60)) - hours = abs(div(minutes, 60)) - days = abs(div(hours, 24)) - months = abs(div(days, 30)) - years = abs(div(days, 365)) - - words = - cond do - seconds < 45 -> String.replace(@time_strings[:seconds], "%d", to_string(seconds)) - seconds < 90 -> String.replace(@time_strings[:minute], "%d", to_string(1)) - minutes < 45 -> String.replace(@time_strings[:minutes], "%d", to_string(minutes)) - minutes < 90 -> String.replace(@time_strings[:hour], "%d", to_string(1)) - hours < 24 -> String.replace(@time_strings[:hours], "%d", to_string(hours)) - hours < 42 -> String.replace(@time_strings[:day], "%d", to_string(1)) - days < 30 -> String.replace(@time_strings[:days], "%d", to_string(days)) - days < 45 -> String.replace(@time_strings[:month], "%d", to_string(1)) - days < 365 -> String.replace(@time_strings[:months], "%d", to_string(months)) - days < 548 -> String.replace(@time_strings[:year], "%d", to_string(1)) - true -> String.replace(@time_strings[:years], "%d", to_string(years)) - end + words = distance_of_time_in_words(now, time) content_tag(:time, "#{words} #{relation}", datetime: DateTime.to_iso8601(time), title: datetime_string(time)) end + def distance_of_time_in_words(time_2, time_1) do + seconds = abs(NaiveDateTime.diff(time_2, time_1, :second)) + minutes = div(seconds, 60) + hours = div(minutes, 60) + days = div(hours, 24) + months = div(days, 30) + years = div(days, 365) + + cond do + seconds < 45 -> String.replace(@time_strings[:seconds], "%d", to_string(seconds)) + seconds < 90 -> String.replace(@time_strings[:minute], "%d", to_string(1)) + minutes < 45 -> String.replace(@time_strings[:minutes], "%d", to_string(minutes)) + minutes < 90 -> String.replace(@time_strings[:hour], "%d", to_string(1)) + hours < 24 -> String.replace(@time_strings[:hours], "%d", to_string(hours)) + hours < 42 -> String.replace(@time_strings[:day], "%d", to_string(1)) + days < 30 -> String.replace(@time_strings[:days], "%d", to_string(days)) + days < 45 -> String.replace(@time_strings[:month], "%d", to_string(1)) + days < 365 -> String.replace(@time_strings[:months], "%d", to_string(months)) + days < 548 -> String.replace(@time_strings[:year], "%d", to_string(1)) + true -> String.replace(@time_strings[:years], "%d", to_string(years)) + end + end + def can?(conn, action, model) do Canada.Can.can?(conn.assigns.current_user, action, model) end diff --git a/lib/philomena_web/views/ban/_bans.html.slime b/lib/philomena_web/views/ban/_bans.html.slime new file mode 100644 index 00000000..68243432 --- /dev/null +++ b/lib/philomena_web/views/ban/_bans.html.slime @@ -0,0 +1,21 @@ +table.table + thead + tr + th Ban Enacted + th Ban Expires + th Duration + th Active? + th Reason + th Ban ID + tbody + = for b <- @bans + tr + td = pretty_time b.created_at + td = pretty_time b.valid_until + td = distance_of_time_in_words b.created_at, b.valid_until + = if active?(b) do + td.success Yes + - else + td.danger No + td = b.reason + td = b.generated_ban_id \ No newline at end of file diff --git a/lib/philomena_web/views/ban_view.ex b/lib/philomena_web/views/ban_view.ex new file mode 100644 index 00000000..d858a954 --- /dev/null +++ b/lib/philomena_web/views/ban_view.ex @@ -0,0 +1,7 @@ +defmodule PhilomenaWeb.BanView do + use PhilomenaWeb, :view + + def active?(ban) do + NaiveDateTime.diff(ban.valid_until, NaiveDateTime.utc_now()) > 0 + end +end