2019-11-12 02:27:09 +01:00
|
|
|
defmodule PhilomenaWeb.ProfileView do
|
|
|
|
use PhilomenaWeb, :view
|
|
|
|
|
|
|
|
def award_order(awards) do
|
2020-03-13 10:17:43 +01:00
|
|
|
Enum.sort_by(awards, &{&1.badge.priority, DateTime.to_unix(&1.awarded_on)}, &>=/2)
|
2019-11-12 02:27:09 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def badge_image(badge, options \\ []) do
|
|
|
|
img_tag(badge_url_root() <> "/" <> badge.image, options)
|
|
|
|
end
|
|
|
|
|
2019-12-05 19:32:53 +01:00
|
|
|
def current?(%{id: id}, %{id: id}), do: true
|
|
|
|
def current?(_user1, _user2), do: false
|
|
|
|
|
2019-12-15 21:02:13 +01:00
|
|
|
def manages_awards?(conn),
|
|
|
|
do: can?(conn, :create, Philomena.Badges.Award)
|
|
|
|
|
2019-12-21 15:54:13 +01:00
|
|
|
def manages_links?(conn, user),
|
|
|
|
do: can?(conn, :edit_links, user)
|
|
|
|
|
2020-08-27 23:47:07 +02:00
|
|
|
def should_see_link?(conn, user, link),
|
|
|
|
do: link.public or can?(conn, :edit, link) or current?(user, conn.assigns.current_user)
|
|
|
|
|
|
|
|
def link_block_class(%{public: false}), do: "block__content--destroyed"
|
|
|
|
def link_block_class(_link), do: nil
|
|
|
|
|
2019-11-12 02:40:31 +01:00
|
|
|
def award_title(%{badge_name: nil} = award),
|
|
|
|
do: award.badge.title
|
2020-01-11 05:20:19 +01:00
|
|
|
|
2019-11-12 02:40:31 +01:00
|
|
|
def award_title(%{badge_name: ""} = award),
|
|
|
|
do: award.badge.title
|
2020-01-11 05:20:19 +01:00
|
|
|
|
2019-11-12 02:40:31 +01:00
|
|
|
def award_title(award),
|
|
|
|
do: award.badge_name
|
|
|
|
|
2019-12-06 00:11:15 +01:00
|
|
|
def commission_status(%{open: true}), do: "Open"
|
|
|
|
def commission_status(_commission), do: "Closed"
|
|
|
|
|
2024-05-31 20:07:00 +02:00
|
|
|
defp sparkline_y(val, max) do
|
|
|
|
# Filter out negative values
|
|
|
|
calc = max(val, 0)
|
2019-12-05 14:55:49 +01:00
|
|
|
|
2024-05-31 20:07:00 +02:00
|
|
|
# Lerp or 0 if not present
|
|
|
|
height = zero_div(calc * 20, max)
|
2019-12-05 14:55:49 +01:00
|
|
|
|
2024-05-31 20:07:00 +02:00
|
|
|
# In SVG coords, y grows down
|
|
|
|
20 - height
|
|
|
|
end
|
2019-12-05 14:55:49 +01:00
|
|
|
|
2024-05-31 20:07:00 +02:00
|
|
|
def sparkline_data(data, width \\ 375, height \\ 20) do
|
|
|
|
# Normalize range
|
|
|
|
max = max(Enum.max(data), 0)
|
|
|
|
sx = width / 90
|
|
|
|
sy = height / 20
|
|
|
|
factor = 100 / 90
|
|
|
|
|
|
|
|
content_tag :svg, id: "js-sparkline-svg", width: "100%", preserveAspectRatio: "xMinYMin", viewBox: "0 0 #{width} #{height}" do
|
|
|
|
first = List.first(data)
|
|
|
|
last = List.last(data)
|
|
|
|
first_y = sparkline_y(first, max) * sy
|
|
|
|
last_y = sparkline_y(last, max) * sy
|
|
|
|
indexed_data = data
|
|
|
|
|> Enum.with_index()
|
|
|
|
points =
|
|
|
|
indexed_data
|
|
|
|
|> Enum.chunk_every(2, 1, :discard)
|
|
|
|
|> Enum.map(fn [{cv, ci}, {nv, ni}] ->
|
|
|
|
cy = sparkline_y(cv, max)
|
|
|
|
ny = sparkline_y(nv, max)
|
|
|
|
|
|
|
|
"C #{(ci * sx) + 0.5 * sx},#{cy * sy} #{(ni * sx) - 0.5 * sx},#{ny * sy} #{ni * sx},#{ny * sy}"
|
|
|
|
end)
|
|
|
|
|> Enum.join("")
|
|
|
|
|
|
|
|
circles = for {val, i} <- indexed_data do
|
|
|
|
y = sparkline_y(val, max) * sy
|
|
|
|
|
|
|
|
content_tag :circle, class: "barline__dot", cx: "#{i * factor}%", cy: y * sy + 1.25, r: 2.5 do
|
2020-01-11 05:20:19 +01:00
|
|
|
content_tag(:title, val)
|
2019-12-05 14:55:49 +01:00
|
|
|
end
|
|
|
|
end
|
2024-05-31 20:07:00 +02:00
|
|
|
|
|
|
|
graph = content_tag :path, "", id: "js-barline-graph", class: "barline__bar", d: "M0,#{first_y}#{points}L#{width - sx},#{last_y}L#{width - sx},#{height}L0,#{height}L0,#{first_y}"
|
|
|
|
|
|
|
|
[graph, circles]
|
2019-12-05 14:55:49 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-12-07 23:07:53 +01:00
|
|
|
def tag_disjunction(tags) do
|
2019-12-20 16:33:39 +01:00
|
|
|
tags
|
|
|
|
|> Enum.map(& &1.name)
|
|
|
|
|> Enum.uniq()
|
|
|
|
|> Enum.join(" || ")
|
2019-12-07 23:07:53 +01:00
|
|
|
end
|
|
|
|
|
2019-12-13 20:26:43 +01:00
|
|
|
def can_ban?(conn),
|
|
|
|
do: can?(conn, :index, Philomena.Bans.User)
|
|
|
|
|
2019-12-17 06:44:24 +01:00
|
|
|
def can_index_user?(conn),
|
|
|
|
do: can?(conn, :index, Philomena.Users.User)
|
|
|
|
|
2019-12-17 18:29:18 +01:00
|
|
|
def can_read_mod_notes?(conn),
|
|
|
|
do: can?(conn, :index, Philomena.ModNotes.ModNote)
|
|
|
|
|
2019-12-17 06:44:24 +01:00
|
|
|
def enabled_text(true), do: "Enabled"
|
|
|
|
def enabled_text(_else), do: "Disabled"
|
|
|
|
|
2019-12-08 19:18:10 +01:00
|
|
|
def user_abbrv(conn, %{name: name} = user) do
|
2020-01-11 05:20:19 +01:00
|
|
|
abbrv =
|
|
|
|
String.upcase(initials_abbrv(name) || uppercase_abbrv(name) || first_letters_abbrv(name))
|
|
|
|
|
2019-12-08 19:18:10 +01:00
|
|
|
abbrv = "(" <> abbrv <> ")"
|
|
|
|
|
|
|
|
link(abbrv, to: Routes.profile_path(conn, :show, user))
|
2019-12-08 18:45:37 +01:00
|
|
|
end
|
2020-01-11 05:20:19 +01:00
|
|
|
|
2019-12-08 19:18:10 +01:00
|
|
|
def user_abbrv(_conn, _user), do: content_tag(:span, "(n/a)")
|
2019-12-08 18:45:37 +01:00
|
|
|
|
|
|
|
defp initials_abbrv(name) do
|
|
|
|
case String.split(name, " ", parts: 4) do
|
2020-01-11 05:20:19 +01:00
|
|
|
[
|
|
|
|
<<a1::utf8, _r1::binary>>,
|
|
|
|
<<a2::utf8, _r2::binary>>,
|
|
|
|
<<a3::utf8, _r3::binary>>,
|
|
|
|
<<a4::utf8, _r4::binary>>
|
|
|
|
] ->
|
2019-12-08 18:45:37 +01:00
|
|
|
<<a1::utf8, a2::utf8, a3::utf8, a4::utf8>>
|
|
|
|
|
2019-12-08 18:48:39 +01:00
|
|
|
[<<a1::utf8, _r1::binary>>, <<a2::utf8, _r2::binary>>, <<a3::utf8, _r3::binary>>] ->
|
2019-12-08 18:45:37 +01:00
|
|
|
<<a1::utf8, a2::utf8, a3::utf8>>
|
|
|
|
|
2019-12-08 18:48:39 +01:00
|
|
|
[<<a1::utf8, _r1::binary>>, <<a2::utf8, _r2::binary>>] ->
|
2019-12-08 18:45:37 +01:00
|
|
|
<<a1::utf8, a2::utf8>>
|
|
|
|
|
|
|
|
_ ->
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defp uppercase_abbrv(name) do
|
2019-12-08 19:18:10 +01:00
|
|
|
case Regex.scan(~r/([A-Z])/, name, capture: :all_but_first) do
|
2019-12-08 18:45:37 +01:00
|
|
|
[] ->
|
|
|
|
nil
|
|
|
|
|
|
|
|
list ->
|
2021-04-01 03:30:16 +02:00
|
|
|
list
|
|
|
|
|> Enum.take(4)
|
|
|
|
|> Enum.join()
|
2019-12-08 18:45:37 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defp first_letters_abbrv(name) do
|
|
|
|
String.slice(name, 0, 4)
|
|
|
|
end
|
|
|
|
|
2019-12-05 14:55:49 +01:00
|
|
|
defp zero_div(_num, 0), do: 0
|
|
|
|
defp zero_div(num, den), do: div(num, den)
|
|
|
|
|
2019-11-12 02:27:09 +01:00
|
|
|
defp badge_url_root do
|
|
|
|
Application.get_env(:philomena, :badge_url_root)
|
|
|
|
end
|
2019-12-13 20:26:43 +01:00
|
|
|
end
|