forked from AkkomaGang/akkoma
Add tasker to get nodeinfo
This commit is contained in:
parent
e272430ef4
commit
881dbea92e
5 changed files with 26 additions and 16 deletions
|
@ -32,7 +32,7 @@ defmodule Pleroma.Instances.Instance do
|
|||
|
||||
def changeset(struct, params \\ %{}) do
|
||||
struct
|
||||
|> cast(params, [:host, :unreachable_since, :favicon, :favicon_updated_at])
|
||||
|> cast(params, [:host, :unreachable_since, :favicon, :nodeinfo, :metadata_updated_at])
|
||||
|> validate_required([:host])
|
||||
|> unique_constraint(:host)
|
||||
end
|
||||
|
@ -141,34 +141,41 @@ defp parse_datetime(datetime), do: datetime
|
|||
|
||||
def needs_update(nil), do: true
|
||||
|
||||
def needs_update(%Instance{metadata_updated_at: nil}), do: true
|
||||
|
||||
def needs_update(%Instance{metadata_updated_at: metadata_updated_at}) do
|
||||
now = NaiveDateTime.utc_now()
|
||||
NaiveDateTime.diff(now, metadata_updated_at) > 86_400
|
||||
end
|
||||
|
||||
def update_metadata(%URI{host: host} = uri) do
|
||||
Logger.info("Checking metadata for #{host}")
|
||||
existing_record = Repo.get_by(Instance, %{host: host})
|
||||
|
||||
if existing_record do
|
||||
if needs_update(existing_record) do
|
||||
Logger.info("Updating metadata for #{host}")
|
||||
favicon = scrape_favicon(uri)
|
||||
nodeinfo = scrape_nodeinfo(uri)
|
||||
|
||||
%Instance{}
|
||||
|> changeset(%{host: host, favicon: favicon, nodeinfo: nodeinfo})
|
||||
existing_record
|
||||
|> changeset(%{host: host, favicon: favicon, nodeinfo: nodeinfo, metadata_updated_at: NaiveDateTime.utc_now()})
|
||||
|> Repo.update()
|
||||
else
|
||||
{:discard, "Does not require update"}
|
||||
end
|
||||
else
|
||||
favicon = scrape_favicon(uri)
|
||||
nodeinfo = scrape_nodeinfo(uri)
|
||||
|
||||
Logger.info("Creating metadata for #{host}")
|
||||
%Instance{}
|
||||
|> changeset(%{host: host, favicon: favicon, nodeinfo: nodeinfo})
|
||||
|> changeset(%{host: host, favicon: favicon, nodeinfo: nodeinfo, metadata_updated_at: NaiveDateTime.utc_now()})
|
||||
|> Repo.insert()
|
||||
end
|
||||
end
|
||||
|
||||
def get_favicon(%URI{host: host} = instance_uri) do
|
||||
def get_favicon(%URI{host: host}) do
|
||||
existing_record = Repo.get_by(Instance, %{host: host})
|
||||
|
||||
if existing_record do
|
||||
|
@ -191,7 +198,7 @@ defp scrape_nodeinfo(%URI{} = instance_uri) do
|
|||
{:ok,
|
||||
Enum.find(links, &(&1["rel"] == "http://nodeinfo.diaspora.software/ns/schema/2.0"))},
|
||||
{:ok, %Tesla.Env{body: data}} <-
|
||||
Pleroma.HTTP.get(ref, [{"accept", "application/json"}], []),
|
||||
Pleroma.HTTP.get(href, [{"accept", "application/json"}], []),
|
||||
{:ok, nodeinfo} <- Jason.decode(data) do
|
||||
nodeinfo
|
||||
else
|
||||
|
|
|
@ -209,6 +209,8 @@ def handle(%{data: %{"type" => "Create"}} = activity, meta) do
|
|||
|
||||
reply_depth = (meta[:depth] || 0) + 1
|
||||
|
||||
IO.puts("QUEUE!")
|
||||
Pleroma.Workers.NodeInfoFetcherWorker.enqueue("process", %{"domain" => activity.data["actor"]})
|
||||
# FIXME: Force inReplyTo to replies
|
||||
if Pleroma.Web.Federator.allowed_thread_distance?(reply_depth) and
|
||||
object.data["replies"] != nil do
|
||||
|
|
|
@ -1,19 +1,17 @@
|
|||
defmodule Pleroma.Workers.NodeInfoFetcherWorker do
|
||||
use Oban.Worker, queue: :backup, max_attempts: 1
|
||||
use Pleroma.Workers.WorkerHelper, queue: "nodeinfo_fetcher"
|
||||
|
||||
alias Oban.Job
|
||||
alias Pleroma.Instance
|
||||
|
||||
def process(domain) do
|
||||
%{"op" => "process", "domain" => domain}
|
||||
|> new()
|
||||
|> Oban.insert()
|
||||
end
|
||||
alias Pleroma.Instances.Instance
|
||||
|
||||
@impl Oban.Worker
|
||||
def perform(%Job{
|
||||
args: %{"op" => "process", "domain" => domain}
|
||||
}) do
|
||||
uri = URI.parse(domain)
|
||||
Instance.get_or_update_favicon(uri)
|
||||
uri = domain
|
||||
|> URI.parse()
|
||||
|> URI.merge("/")
|
||||
|
||||
Instance.update_metadata(uri)
|
||||
end
|
||||
end
|
||||
|
|
|
@ -42,6 +42,7 @@ def enqueue(op, params, worker_args \\ []) do
|
|||
unquote(caller_module)
|
||||
|> apply(:new, [params, worker_args])
|
||||
|> Oban.insert()
|
||||
|> IO.inspect()
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -4,12 +4,14 @@ defmodule Pleroma.Repo.Migrations.AddNodeinfo do
|
|||
def up do
|
||||
alter table(:instances) do
|
||||
add_if_not_exists(:nodeinfo, :map, default: %{})
|
||||
add_if_not_exists(:metadata_updated_at, :naive_datetime)
|
||||
end
|
||||
end
|
||||
|
||||
def down do
|
||||
alter table(:instances) do
|
||||
remove_if_exists(:nodeinfo, :map)
|
||||
remove_if_exists(:metadata_updated_at, :naive_datetime)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue