def superuser?(%User{local: true, info: %User.Info{is_moderator: true}}), do: true
def superuser?(_), do: false
- def avatar_url(user) do
+ def avatar_url(user, options \\ []) do
case user.avatar do
%{"url" => [%{"href" => href} | _]} -> href
- _ -> "#{Web.base_url()}/images/avi.png"
+ _ -> !options[:no_default] && "#{Web.base_url()}/images/avi.png"
end
end
- # Do not return instance default avatar for federation
- def avatar_url_ap(user) do
- case user.avatar do
- %{"url" => [%{"href" => href} | _]} -> href
- _ -> nil
- end
- end
-
- def banner_url(user) do
+ def banner_url(user, options \\ []) do
case user.info.banner do
%{"url" => [%{"href" => href} | _]} -> href
- _ -> "#{Web.base_url()}/images/banner.png"
+ _ -> !options[:no_default] && "#{Web.base_url()}/images/banner.png"
end
end
end)
end
- # Only federate user icon if not nil
- # Prevents federating instance default avatars
- def maybe_make_icon(user) do
- if User.avatar_url_ap(user) do
- %{
- "icon" => %{
- "type" => "Image",
- "url" => User.avatar_url_ap(user)
- }
- }
- else
- %{}
- end
- end
-
#### Like-related helpers
@doc """
"publicKeyPem" => public_key
},
"endpoints" => endpoints,
- "image" => %{
- "type" => "Image",
- "url" => User.banner_url(user)
- },
"tag" => user.info.source_data["tag"] || []
}
- |> Map.merge(Utils.maybe_make_icon(user))
+ |> Map.merge(maybe_make_image(&User.avatar_url/2, "icon", user))
+ |> Map.merge(maybe_make_image(&User.banner_url/2, "image", user))
|> Map.merge(Utils.make_json_ld_header())
end
map
end
end
+
+ defp maybe_make_image(func, key, user) do
+ if image = func.(user, no_default: true) do
+ %{
+ key => %{
+ "type" => "Image",
+ "url" => image
+ }
+ }
+ else
+ %{}
+ end
+ end
end
assert String.contains?(result["publicKey"]["publicKeyPem"], "BEGIN PUBLIC KEY")
end
+ test "Does not add an avatar image if the user hasn't set one" do
+ user = insert(:user)
+ {:ok, user} = Pleroma.Web.WebFinger.ensure_keys_present(user)
+
+ result = UserView.render("user.json", %{user: user})
+ refute result["icon"]
+ refute result["image"]
+
+ user =
+ insert(:user,
+ avatar: %{"url" => [%{"href" => "https://someurl"}]},
+ info: %{
+ banner: %{"url" => [%{"href" => "https://somebanner"}]}
+ }
+ )
+
+ {:ok, user} = Pleroma.Web.WebFinger.ensure_keys_present(user)
+
+ result = UserView.render("user.json", %{user: user})
+ assert result["icon"]["url"] == "https://someurl"
+ assert result["image"]["url"] == "https://somebanner"
+ end
+
describe "endpoints" do
test "local users have a usable endpoints structure" do
user = insert(:user)