Add instance rendering in user json

This commit is contained in:
FloatingGhost 2022-11-02 22:37:44 +00:00
parent 881dbea92e
commit d736e55746
6 changed files with 80 additions and 23 deletions

View File

@ -156,7 +156,8 @@ defmodule Pleroma.Application do
build_cachex("emoji_packs", expiration: emoji_packs_expiration(), limit: 10), build_cachex("emoji_packs", expiration: emoji_packs_expiration(), limit: 10),
build_cachex("failed_proxy_url", limit: 2500), build_cachex("failed_proxy_url", limit: 2500),
build_cachex("banned_urls", default_ttl: :timer.hours(24 * 30), limit: 5_000), build_cachex("banned_urls", default_ttl: :timer.hours(24 * 30), limit: 5_000),
build_cachex("translations", default_ttl: :timer.hours(24 * 30), limit: 2500) build_cachex("translations", default_ttl: :timer.hours(24 * 30), limit: 2500),
build_cachex("instances", default_ttl: :timer.hours(24), limit: 2500)
] ]
end end

View File

@ -5,6 +5,8 @@
defmodule Pleroma.Instances.Instance do defmodule Pleroma.Instances.Instance do
@moduledoc "Instance." @moduledoc "Instance."
@cachex Pleroma.Config.get([:cachex, :provider], Cachex)
alias Pleroma.Instances alias Pleroma.Instances
alias Pleroma.Instances.Instance alias Pleroma.Instances.Instance
alias Pleroma.Repo alias Pleroma.Repo
@ -158,9 +160,17 @@ defmodule Pleroma.Instances.Instance do
favicon = scrape_favicon(uri) favicon = scrape_favicon(uri)
nodeinfo = scrape_nodeinfo(uri) nodeinfo = scrape_nodeinfo(uri)
existing_record {:ok, instance} =
|> changeset(%{host: host, favicon: favicon, nodeinfo: nodeinfo, metadata_updated_at: NaiveDateTime.utc_now()}) existing_record
|> Repo.update() |> changeset(%{
host: host,
favicon: favicon,
nodeinfo: nodeinfo,
metadata_updated_at: NaiveDateTime.utc_now()
})
|> Repo.update()
@cachex.put(:instances_cache, "instances:#{host}", instance)
else else
{:discard, "Does not require update"} {:discard, "Does not require update"}
end end
@ -169,9 +179,18 @@ defmodule Pleroma.Instances.Instance do
nodeinfo = scrape_nodeinfo(uri) nodeinfo = scrape_nodeinfo(uri)
Logger.info("Creating metadata for #{host}") Logger.info("Creating metadata for #{host}")
%Instance{}
|> changeset(%{host: host, favicon: favicon, nodeinfo: nodeinfo, metadata_updated_at: NaiveDateTime.utc_now()}) {:ok, instance} =
|> Repo.insert() %Instance{}
|> changeset(%{
host: host,
favicon: favicon,
nodeinfo: nodeinfo,
metadata_updated_at: NaiveDateTime.utc_now()
})
|> Repo.insert()
@cachex.put(:instances_cache, "instances:#{host}", instance)
end end
end end
@ -255,4 +274,21 @@ defmodule Pleroma.Instances.Instance do
end) end)
|> Stream.run() |> Stream.run()
end end
def get_by_url(url_or_host) do
url = host(url_or_host)
Repo.get_by(Instance, host: url)
end
def get_cached_by_url(url_or_host) do
url = host(url_or_host)
@cachex.fetch!(:instances_cache, "instances:#{url}", fn _ ->
with %Instance{} = instance <- get_by_url(url) do
{:ok, instance}
else
_ -> {:ignore, nil}
end
end)
end
end end

View File

@ -210,7 +210,11 @@ defmodule Pleroma.Web.ActivityPub.SideEffects do
reply_depth = (meta[:depth] || 0) + 1 reply_depth = (meta[:depth] || 0) + 1
IO.puts("QUEUE!") IO.puts("QUEUE!")
Pleroma.Workers.NodeInfoFetcherWorker.enqueue("process", %{"domain" => activity.data["actor"]})
Pleroma.Workers.NodeInfoFetcherWorker.enqueue("process", %{
"domain" => activity.data["actor"]
})
# FIXME: Force inReplyTo to replies # FIXME: Force inReplyTo to replies
if Pleroma.Web.Federator.allowed_thread_distance?(reply_depth) and if Pleroma.Web.Federator.allowed_thread_distance?(reply_depth) and
object.data["replies"] != nil do object.data["replies"] != nil do

View File

@ -186,6 +186,16 @@ defmodule Pleroma.Web.MastodonAPI.AccountView do
render_many(targets, AccountView, "relationship.json", render_opts) render_many(targets, AccountView, "relationship.json", render_opts)
end end
def render("instance.json", %{instance: %Pleroma.Instances.Instance{} = instance}) do
%{
name: instance.host,
favicon: instance.favicon |> MediaProxy.url(),
nodeinfo: instance.nodeinfo
}
end
def render("instance.json", _), do: nil
defp do_render("show.json", %{user: user} = opts) do defp do_render("show.json", %{user: user} = opts) do
user = User.sanitize_html(user, User.html_filter_policy(opts[:for])) user = User.sanitize_html(user, User.html_filter_policy(opts[:for]))
display_name = user.name || user.nickname display_name = user.name || user.nickname
@ -230,17 +240,19 @@ defmodule Pleroma.Web.MastodonAPI.AccountView do
%{} %{}
end end
favicon = instance = with {:ok, instance} <- Pleroma.Instances.Instance.get_cached_by_url(user.ap_id) do
if Pleroma.Config.get([:instances_favicons, :enabled]) do instance
user else
|> Map.get(:ap_id, "") _ ->
|> URI.parse() nil
|> URI.merge("/") end
|> Pleroma.Instances.Instance.get_or_update_favicon()
|> MediaProxy.url() favicon = if is_nil(instance) do
else nil
nil else
end instance.favicon
|> MediaProxy.url()
end
%{ %{
id: to_string(user.id), id: to_string(user.id),
@ -271,7 +283,9 @@ defmodule Pleroma.Web.MastodonAPI.AccountView do
} }
}, },
last_status_at: user.last_status_at, last_status_at: user.last_status_at,
akkoma: %{
instance: render("instance.json", %{instance: instance})
},
# Pleroma extensions # Pleroma extensions
# Note: it's insecure to output :email but fully-qualified nickname may serve as safe stub # Note: it's insecure to output :email but fully-qualified nickname may serve as safe stub
fqn: User.full_nickname(user), fqn: User.full_nickname(user),

View File

@ -421,6 +421,7 @@ defmodule Pleroma.Web.MastodonAPI.StatusView do
nil nil
end end
def render("history.json", %{activity: %{data: %{"object" => _object}} = activity} = opts) do def render("history.json", %{activity: %{data: %{"object" => _object}} = activity} = opts) do
object = Object.normalize(activity, fetch: false) object = Object.normalize(activity, fetch: false)

View File

@ -8,9 +8,10 @@ defmodule Pleroma.Workers.NodeInfoFetcherWorker do
def perform(%Job{ def perform(%Job{
args: %{"op" => "process", "domain" => domain} args: %{"op" => "process", "domain" => domain}
}) do }) do
uri = domain uri =
|> URI.parse() domain
|> URI.merge("/") |> URI.parse()
|> URI.merge("/")
Instance.update_metadata(uri) Instance.update_metadata(uri)
end end