Skip to content

Commit

Permalink
Turn user avatar into a link to their profile
Browse files Browse the repository at this point in the history
  • Loading branch information
MareStare committed Mar 10, 2025
1 parent 6f4709e commit bf7cae3
Show file tree
Hide file tree
Showing 11 changed files with 49 additions and 34 deletions.
4 changes: 2 additions & 2 deletions lib/philomena_web/templates/admin/report/show.html.slime
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ p
article.block.communication
.block__content.communication__content.flex.flex--no-wrap
.communication__body__avatar.hide-mobile
= render PhilomenaWeb.UserAttributionView, "_anon_user_avatar.html", object: @report, conn: @conn
= render PhilomenaWeb.UserAttributionView, "_anon_user_avatar.html", object: @report, conn: @conn, profile_link: true
.flex__grow.communication__body
.communication__body__avatar.hide-desktop
= render PhilomenaWeb.UserAttributionView, "_anon_user_avatar.html", object: @report, conn: @conn
= render PhilomenaWeb.UserAttributionView, "_anon_user_avatar.html", object: @report, conn: @conn, profile_link: true
.communication__body__sender
span.communication__body__sender-name = render PhilomenaWeb.UserAttributionView, "_anon_user.html", object: @report, awards: true, conn: @conn
br
Expand Down
4 changes: 2 additions & 2 deletions lib/philomena_web/templates/comment/_comment.html.slime
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ article.block.communication id="comment_#{@comment.id}"

.block__content.communication__content.flex.flex--no-wrap class=communication_body_class(@comment)
.communication__body__avatar.hide-mobile
= render PhilomenaWeb.UserAttributionView, "_anon_user_avatar.html", object: @comment, conn: @conn
= render PhilomenaWeb.UserAttributionView, "_anon_user_avatar.html", object: @comment, conn: @conn, profile_link: true
.flex__grow.communication__body
.communication__body__avatar.hide-desktop
= render PhilomenaWeb.UserAttributionView, "_anon_user_avatar.html", object: @comment, conn: @conn
= render PhilomenaWeb.UserAttributionView, "_anon_user_avatar.html", object: @comment, conn: @conn, profile_link: true
.communication__body__sender
span.communication__body__sender-name = render PhilomenaWeb.UserAttributionView, "_anon_user.html", object: @comment, awards: true, conn: @conn
br
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ elixir:
.block.commission
.block__content.flex.flex--no-wrap
.flex__fixed.spacing-right
= render PhilomenaWeb.UserAttributionView, "_user_avatar.html", object: c, conn: @conn
= render PhilomenaWeb.UserAttributionView, "_user_avatar.html", object: c, conn: @conn, profile_link: true

.flex__grow.commission__listing_body
span.commission__listing_artist
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ h1
article.block.communication
.block__content.communication__content.flex.flex--no-wrap
.communication__body__avatar.hide-mobile
= render PhilomenaWeb.UserAttributionView, "_anon_user_avatar.html", object: @comment, conn: @conn
= render PhilomenaWeb.UserAttributionView, "_anon_user_avatar.html", object: @comment, conn: @conn, profile_link: true

.flex__grow.communication__body
.communication__body__avatar.hide-desktop
= render PhilomenaWeb.UserAttributionView, "_anon_user_avatar.html", object: @comment, conn: @conn
= render PhilomenaWeb.UserAttributionView, "_anon_user_avatar.html", object: @comment, conn: @conn, profile_link: true
.communication__body__sender
span.communication__body__sender-name = render PhilomenaWeb.UserAttributionView, "_anon_user.html", object: @comment, awards: true, conn: @conn
br
Expand Down
4 changes: 2 additions & 2 deletions lib/philomena_web/templates/message/_message.html.slime
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ article.block.communication

.block__content.communication__content.flex.flex--no-wrap
.communication__body__avatar.hide-mobile
= render PhilomenaWeb.UserAttributionView, "_user_avatar.html", object: %{user: @message.from}, conn: @conn, class: "avatar--100px"
= render PhilomenaWeb.UserAttributionView, "_user_avatar.html", object: %{user: @message.from}, conn: @conn, class: "avatar--100px", profile_link: true

.flex__grow.communication__body
.communication__body__avatar.hide-desktop
= render PhilomenaWeb.UserAttributionView, "_user_avatar.html", object: %{user: @message.from}, conn: @conn, class: "avatar--100px"
= render PhilomenaWeb.UserAttributionView, "_user_avatar.html", object: %{user: @message.from}, conn: @conn, class: "avatar--100px", profile_link: true
.communication__body__sender
span.communication__body__sender-name = render PhilomenaWeb.UserAttributionView, "_user.html", object: %{user: @message.from}, badges: true, conn: @conn
br
Expand Down
4 changes: 2 additions & 2 deletions lib/philomena_web/templates/post/_post.html.slime
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ article.block.communication id="post_#{@post.id}"

.block__content.communication__content.flex.flex--no-wrap class=communication_body_class(@post)
.communication__body__avatar.hide-mobile
= render PhilomenaWeb.UserAttributionView, "_anon_user_avatar.html", object: @post, conn: @conn
= render PhilomenaWeb.UserAttributionView, "_anon_user_avatar.html", object: @post, conn: @conn, profile_link: true
.flex__grow.communication__body
.communication__body__avatar.hide-desktop
= render PhilomenaWeb.UserAttributionView, "_anon_user_avatar.html", object: @post, conn: @conn
= render PhilomenaWeb.UserAttributionView, "_anon_user_avatar.html", object: @post, conn: @conn, profile_link: true
.communication__body__sender
span.communication__body__sender-name = render PhilomenaWeb.UserAttributionView, "_anon_user.html", object: @post, awards: true, conn: @conn
br
Expand Down
4 changes: 2 additions & 2 deletions lib/philomena_web/templates/post/preview/create.html.slime
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
.block.flex.communication__content
.communication__body__avatar.hide-mobile
= render PhilomenaWeb.UserAttributionView, "_anon_user_avatar.html", object: @post, conn: @conn
= render PhilomenaWeb.UserAttributionView, "_anon_user_avatar.html", object: @post, conn: @conn, profile_link: true

.flex__grow.communication__body
.communication__body__avatar.hide-desktop
= render PhilomenaWeb.UserAttributionView, "_anon_user_avatar.html", object: @post, conn: @conn
= render PhilomenaWeb.UserAttributionView, "_anon_user_avatar.html", object: @post, conn: @conn, profile_link: true
.communication__body__sender
span.communication__body__sender-name
= render PhilomenaWeb.UserAttributionView, "_anon_user.html", object: @post, conn: @conn, awards: true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ h1
article.block.communication
.block__content.flex.flex--no-wrap.communication__content
.communication__body__avatar.hide-mobile
= render PhilomenaWeb.UserAttributionView, "_anon_user_avatar.html", object: @post, conn: @conn
= render PhilomenaWeb.UserAttributionView, "_anon_user_avatar.html", object: @post, conn: @conn, profile_link: true

.flex__grow.communication__body
.communication__body__avatar.hide-desktop
= render PhilomenaWeb.UserAttributionView, "_anon_user_avatar.html", object: @post, conn: @conn
= render PhilomenaWeb.UserAttributionView, "_anon_user_avatar.html", object: @post, conn: @conn, profile_link: true
.communication__body__sender
span.communication__body__sender-name = render PhilomenaWeb.UserAttributionView, "_anon_user.html", object: @post, awards: true, conn: @conn
br
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
- opts = [class: assigns[:class], profile_link: assigns[:profile_link]]
= if !!@object.user and !anonymous?(@object) do
= user_avatar(@object, assigns[:class] || "avatar--100px")
= user_avatar(@object, opts)
- else
= anonymous_avatar(anonymous_name(@object), assigns[:class] || "avatar--100px")
= anonymous_avatar(anonymous_name(@object), @object[:user], opts)
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
= if !!@object.user do
= user_avatar(@object, assigns[:class] || "avatar--100px")
= user_avatar(@object, class: assigns[:class], profile_link: assigns[:profile_link])
46 changes: 30 additions & 16 deletions lib/philomena_web/views/user_attribution_view.ex
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
defmodule PhilomenaWeb.UserAttributionView do
use PhilomenaWeb, :view
require Logger

alias Philomena.Attribution
alias PhilomenaWeb.AvatarGeneratorView
Expand Down Expand Up @@ -40,39 +41,52 @@ defmodule PhilomenaWeb.UserAttributionView do
end
end

def anonymous_avatar(name, class \\ "avatar--100px") do
class = Enum.join(["image-constrained", class], " ")
def anonymous_avatar(name, user, opts) do
content = AvatarGeneratorView.generated_avatar(name)
make_user_avatar(user, content, opts)
end

def user_avatar(object, opts \\ []) do
user = Map.get(object, :user)

content_tag :div, class: class do
AvatarGeneratorView.generated_avatar(name)
case user do
nil -> anonymous_avatar(anonymous_name(object), nil, opts)
%{avatar: nil} -> anonymous_avatar(user.name, user, opts)
_ -> named_user_avatar(user, opts)
end
end

def user_avatar(object, class \\ "avatar--100px")
defp named_user_avatar(user, opts) do
content = img_tag(avatar_url_root() <> "/" <> user.avatar)
make_user_avatar(user, content, opts)
end

def user_avatar(%{user: nil} = object, class),
do: anonymous_avatar(anonymous_name(object), class)
defp make_user_avatar(user, content, opts) do
class = Keyword.get(opts, :class) || "avatar--100px"
profile_link = Keyword.get(opts, :profile_link) || false

def user_avatar(%{user: %{avatar: nil}} = object, class),
do: anonymous_avatar(object.user.name, class)
{tag, attrs} =
if profile_link and !!user do
{:a, [href: ~p"/profiles/#{user}"]}
else
{:div, []}
end

def user_avatar(%{user: %{avatar: avatar}}, class) do
class = Enum.join(["image-constrained", class], " ")
attrs = Keyword.put(attrs, :class, class)

content_tag :div, class: class do
img_tag(avatar_url_root() <> "/" <> avatar)
end
content_tag(tag, content, attrs)
end

def user_avatar_url(%{user: %{avatar: nil}} = object) do
defp user_avatar_url(%{user: %{avatar: nil}} = object) do
anonymous_avatar_url(object.user.name)
end

def user_avatar_url(%{user: %{avatar: avatar}}) do
defp user_avatar_url(%{user: %{avatar: avatar}}) do
avatar_url_root() <> "/" <> avatar
end

def anonymous_avatar_url(name) do
defp anonymous_avatar_url(name) do
svg =
name
|> AvatarGeneratorView.generated_avatar()
Expand Down

0 comments on commit bf7cae3

Please sign in to comment.