2021-12-28 10:42:08 +00:00
|
|
|
defmodule Mobilizon.Service.Workers.RefreshInstances do
|
|
|
|
@moduledoc """
|
|
|
|
Worker to refresh the instances materialized view and the relay actors
|
|
|
|
"""
|
|
|
|
|
|
|
|
use Oban.Worker, unique: [period: :infinity, keys: [:event_uuid, :action]]
|
|
|
|
|
2022-05-02 10:15:17 +00:00
|
|
|
alias Mobilizon.Actors.Actor
|
2021-12-28 10:42:08 +00:00
|
|
|
alias Mobilizon.Federation.ActivityPub.Actor, as: ActivityPubActor
|
2022-05-02 10:15:17 +00:00
|
|
|
alias Mobilizon.Federation.ActivityPub.Relay
|
2021-12-28 10:42:08 +00:00
|
|
|
alias Mobilizon.Instances
|
|
|
|
alias Mobilizon.Instances.Instance
|
|
|
|
alias Oban.Job
|
|
|
|
|
|
|
|
@impl Oban.Worker
|
|
|
|
@spec perform(Oban.Job.t()) :: :ok
|
|
|
|
def perform(%Job{}) do
|
|
|
|
Instances.refresh()
|
|
|
|
|
|
|
|
Instances.all_domains()
|
|
|
|
|> Enum.each(&refresh_instance_actor/1)
|
|
|
|
end
|
|
|
|
|
|
|
|
@spec refresh_instance_actor(Instance.t()) ::
|
|
|
|
{:ok, Mobilizon.Actors.Actor.t()}
|
|
|
|
| {:error,
|
2022-05-02 10:15:17 +00:00
|
|
|
ActivityPubActor.make_actor_errors()
|
2021-12-28 10:42:08 +00:00
|
|
|
| Mobilizon.Federation.WebFinger.finger_errors()}
|
2022-05-02 10:15:17 +00:00
|
|
|
def refresh_instance_actor(%Instance{domain: nil}) do
|
2022-04-20 09:35:38 +00:00
|
|
|
{:error, :not_remote_instance}
|
|
|
|
end
|
2021-12-28 10:42:08 +00:00
|
|
|
|
2022-05-02 10:15:17 +00:00
|
|
|
def refresh_instance_actor(%Instance{domain: domain}) do
|
|
|
|
%Actor{url: url} = Relay.get_actor()
|
|
|
|
%URI{host: host} = URI.new!(url)
|
|
|
|
|
|
|
|
if host == domain do
|
|
|
|
{:error, :not_remote_instance}
|
|
|
|
else
|
|
|
|
ActivityPubActor.find_or_make_actor_from_nickname("relay@#{domain}")
|
|
|
|
end
|
2021-12-28 10:42:08 +00:00
|
|
|
end
|
|
|
|
end
|