Use ActivityPub.get_or_fetch_actor_by_url/2 instead of

directly Actors.get_actor_by_url

So that we can refresh actors when they're stale

Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
Thomas Citharel 2020-06-15 11:13:20 +02:00
parent 9063772e35
commit 5990633fb9
No known key found for this signature in database
GPG key ID: A061B9DDE0CA0773
7 changed files with 30 additions and 21 deletions

View file

@ -163,6 +163,7 @@ defmodule Mobilizon.Federation.ActivityPub do
end end
end end
@spec get_or_fetch_actor_by_url(String.t(), boolean()) :: {:ok, Actor.t()} | {:error, any()}
def get_or_fetch_actor_by_url(url, preload) do def get_or_fetch_actor_by_url(url, preload) do
with {:ok, %Actor{} = cached_actor} <- Actors.get_actor_by_url(url, preload), with {:ok, %Actor{} = cached_actor} <- Actors.get_actor_by_url(url, preload),
false <- Actors.needs_update?(cached_actor) do false <- Actors.needs_update?(cached_actor) do
@ -295,7 +296,7 @@ defmodule Mobilizon.Federation.ActivityPub do
local \\ true, local \\ true,
public \\ true public \\ true
) do ) do
with {:ok, %Actor{id: object_owner_actor_id}} <- Actors.get_actor_by_url(object["actor"]), with {:ok, %Actor{id: object_owner_actor_id}} <- get_or_fetch_actor_by_url(object["actor"]),
{:ok, %Share{} = _share} <- Share.create(object["id"], actor.id, object_owner_actor_id), {:ok, %Share{} = _share} <- Share.create(object["id"], actor.id, object_owner_actor_id),
announce_data <- make_announce_data(actor, object, activity_id, public), announce_data <- make_announce_data(actor, object, activity_id, public),
{:ok, activity} <- create_activity(announce_data, local), {:ok, activity} <- create_activity(announce_data, local),

View file

@ -11,7 +11,7 @@ defmodule Mobilizon.Federation.ActivityPub.Federator do
use GenServer use GenServer
alias Mobilizon.Actors alias Mobilizon.Actors
alias Mobilizon.Actors.Actor
alias Mobilizon.Federation.ActivityPub alias Mobilizon.Federation.ActivityPub
alias Mobilizon.Federation.ActivityPub.{Activity, Transmogrifier} alias Mobilizon.Federation.ActivityPub.{Activity, Transmogrifier}
@ -43,7 +43,7 @@ defmodule Mobilizon.Federation.ActivityPub.Federator do
Logger.debug(inspect(activity)) Logger.debug(inspect(activity))
Logger.debug(fn -> "Running publish for #{activity.data["id"]}" end) Logger.debug(fn -> "Running publish for #{activity.data["id"]}" end)
with actor when not is_nil(actor) <- Actors.get_actor_by_url!(activity.data["actor"]) do with {:ok, %Actor{} = actor} <- ActivityPub.get_or_fetch_actor_by_url(activity.data["actor"]) do
Logger.info(fn -> "Sending #{activity.data["id"]} out via AP" end) Logger.info(fn -> "Sending #{activity.data["id"]} out via AP" end)
ActivityPub.publish(actor, activity) ActivityPub.publish(actor, activity)
end end

View file

@ -175,7 +175,8 @@ defmodule Mobilizon.Federation.ActivityPub.Transmogrifier do
{:member, Actors.is_member?(object_data.creator_id, object_data.actor_id)}, {:member, Actors.is_member?(object_data.creator_id, object_data.actor_id)},
{:ok, %Activity{} = activity, %Resource{} = resource} <- {:ok, %Activity{} = activity, %Resource{} = resource} <-
ActivityPub.create(:resource, object_data, false), ActivityPub.create(:resource, object_data, false),
{:ok, %Actor{type: :Group, id: group_id} = group} <- Actors.get_actor_by_url(group_url), {:ok, %Actor{type: :Group, id: group_id} = group} <-
ActivityPub.get_or_fetch_actor_by_url(group_url),
announce_id <- "#{object_url}/announces/#{group_id}", announce_id <- "#{object_url}/announces/#{group_id}",
{:ok, _activity, _resource} <- ActivityPub.announce(group, object, announce_id) do {:ok, _activity, _resource} <- ActivityPub.announce(group, object, announce_id) do
{:ok, activity, resource} {:ok, activity, resource}
@ -262,7 +263,8 @@ defmodule Mobilizon.Federation.ActivityPub.Transmogrifier do
) do ) do
with actor <- Utils.get_actor(data), with actor <- Utils.get_actor(data),
# TODO: Is the following line useful? # TODO: Is the following line useful?
{:ok, %Actor{id: actor_id} = _actor} <- ActivityPub.get_or_fetch_actor_by_url(actor), {:ok, %Actor{id: actor_id, suspended: false} = _actor} <-
ActivityPub.get_or_fetch_actor_by_url(actor),
:ok <- Logger.debug("Fetching contained object"), :ok <- Logger.debug("Fetching contained object"),
{:ok, object} <- fetch_obj_helper_as_activity_streams(object), {:ok, object} <- fetch_obj_helper_as_activity_streams(object),
:ok <- Logger.debug("Handling contained object"), :ok <- Logger.debug("Handling contained object"),
@ -271,7 +273,8 @@ defmodule Mobilizon.Federation.ActivityPub.Transmogrifier do
{:ok, _activity, entity} <- handle_incoming(create_data), {:ok, _activity, entity} <- handle_incoming(create_data),
:ok <- Logger.debug("Finished processing contained object"), :ok <- Logger.debug("Finished processing contained object"),
{:ok, activity} <- ActivityPub.create_activity(data, false), {:ok, activity} <- ActivityPub.create_activity(data, false),
{:ok, %Actor{id: object_owner_actor_id}} <- Actors.get_actor_by_url(object["actor"]), {:ok, %Actor{id: object_owner_actor_id}} <-
ActivityPub.get_or_fetch_actor_by_url(object["actor"]),
{:ok, %Mobilizon.Share{} = _share} <- {:ok, %Mobilizon.Share{} = _share} <-
Mobilizon.Share.create(object["id"], actor_id, object_owner_actor_id) do Mobilizon.Share.create(object["id"], actor_id, object_owner_actor_id) do
{:ok, activity, entity} {:ok, activity, entity}
@ -288,7 +291,8 @@ defmodule Mobilizon.Federation.ActivityPub.Transmogrifier do
"actor" => _actor_id "actor" => _actor_id
}) })
when object_type in ["Person", "Group", "Application", "Service", "Organization"] do when object_type in ["Person", "Group", "Application", "Service", "Organization"] do
with {:ok, %Actor{} = old_actor} <- Actors.get_actor_by_url(object["id"]), with {:ok, %Actor{suspended: false} = old_actor} <-
ActivityPub.get_or_fetch_actor_by_url(object["id"]),
object_data <- object_data <-
object |> Converter.Actor.as_to_model_data(), object |> Converter.Actor.as_to_model_data(),
{:ok, %Activity{} = activity, %Actor{} = new_actor} <- {:ok, %Activity{} = activity, %Actor{} = new_actor} <-
@ -306,7 +310,8 @@ defmodule Mobilizon.Federation.ActivityPub.Transmogrifier do
update_data update_data
) do ) do
with actor <- Utils.get_actor(update_data), with actor <- Utils.get_actor(update_data),
{:ok, %Actor{url: actor_url}} <- Actors.get_actor_by_url(actor), {:ok, %Actor{url: actor_url, suspended: false}} <-
ActivityPub.get_or_fetch_actor_by_url(actor),
{:ok, %Event{} = old_event} <- {:ok, %Event{} = old_event} <-
object |> Utils.get_url() |> ActivityPub.fetch_object_from_url(), object |> Utils.get_url() |> ActivityPub.fetch_object_from_url(),
object_data <- Converter.Event.as_to_model_data(object), object_data <- Converter.Event.as_to_model_data(object),
@ -351,8 +356,8 @@ defmodule Mobilizon.Federation.ActivityPub.Transmogrifier do
"id" => id "id" => id
} = _data } = _data
) do ) do
with {:ok, %Actor{domain: nil} = followed} <- Actors.get_actor_by_url(followed), with {:ok, %Actor{domain: nil} = followed} <- ActivityPub.get_or_fetch_actor_by_url(followed),
{:ok, %Actor{} = follower} <- Actors.get_actor_by_url(follower), {:ok, %Actor{} = follower} <- ActivityPub.get_or_fetch_actor_by_url(follower),
{:ok, activity, object} <- ActivityPub.unfollow(follower, followed, id, false) do {:ok, activity, object} <- ActivityPub.unfollow(follower, followed, id, false) do
{:ok, activity, object} {:ok, activity, object}
else else
@ -371,7 +376,7 @@ defmodule Mobilizon.Federation.ActivityPub.Transmogrifier do
%{"type" => "Delete", "object" => object, "actor" => _actor, "id" => _id} = data %{"type" => "Delete", "object" => object, "actor" => _actor, "id" => _id} = data
) do ) do
with actor <- Utils.get_actor(data), with actor <- Utils.get_actor(data),
{:ok, %Actor{url: actor_url}} <- Actors.get_actor_by_url(actor), {:ok, %Actor{url: actor_url}} <- ActivityPub.get_or_fetch_actor_by_url(actor),
object_id <- Utils.get_url(object), object_id <- Utils.get_url(object),
{:origin_check, true} <- {:origin_check, true} <-
{:origin_check, Utils.origin_check_from_id?(actor_url, object_id)}, {:origin_check, Utils.origin_check_from_id?(actor_url, object_id)},
@ -399,7 +404,8 @@ defmodule Mobilizon.Federation.ActivityPub.Transmogrifier do
} = data } = data
) do ) do
with actor <- Utils.get_actor(data), with actor <- Utils.get_actor(data),
{:ok, %Actor{url: _actor_url} = actor} <- Actors.get_actor_by_url(actor), {:ok, %Actor{url: _actor_url, suspended: false} = actor} <-
ActivityPub.get_or_fetch_actor_by_url(actor),
object <- Utils.get_url(object), object <- Utils.get_url(object),
{:ok, object} <- ActivityPub.fetch_object_from_url(object), {:ok, object} <- ActivityPub.fetch_object_from_url(object),
{:ok, activity, object} <- {:ok, activity, object} <-
@ -416,7 +422,7 @@ defmodule Mobilizon.Federation.ActivityPub.Transmogrifier do
%{"type" => "Leave", "object" => object, "actor" => actor, "id" => _id} = data %{"type" => "Leave", "object" => object, "actor" => actor, "id" => _id} = data
) do ) do
with actor <- Utils.get_actor(data), with actor <- Utils.get_actor(data),
{:ok, %Actor{} = actor} <- Actors.get_actor_by_url(actor), {:ok, %Actor{} = actor} <- ActivityPub.get_or_fetch_actor_by_url(actor),
object <- Utils.get_url(object), object <- Utils.get_url(object),
{:ok, object} <- ActivityPub.fetch_object_from_url(object), {:ok, object} <- ActivityPub.fetch_object_from_url(object),
{:ok, activity, object} <- ActivityPub.leave(object, actor, false) do {:ok, activity, object} <- ActivityPub.leave(object, actor, false) do
@ -443,7 +449,8 @@ defmodule Mobilizon.Federation.ActivityPub.Transmogrifier do
"target" => target "target" => target
} = data } = data
) do ) do
with {:ok, %Actor{} = actor} <- data |> Utils.get_actor() |> Actors.get_actor_by_url(), with {:ok, %Actor{} = actor} <-
data |> Utils.get_actor() |> ActivityPub.get_or_fetch_actor_by_url(),
{:ok, object} <- object |> Utils.get_url() |> ActivityPub.fetch_object_from_url(), {:ok, object} <- object |> Utils.get_url() |> ActivityPub.fetch_object_from_url(),
{:ok, %Actor{} = target} <- {:ok, %Actor{} = target} <-
target |> Utils.get_url() |> ActivityPub.get_or_fetch_actor_by_url(), target |> Utils.get_url() |> ActivityPub.get_or_fetch_actor_by_url(),

View file

@ -8,10 +8,10 @@ defmodule Mobilizon.Federation.ActivityPub.Utils do
Various ActivityPub related utils. Various ActivityPub related utils.
""" """
alias Mobilizon.Actors
alias Mobilizon.Actors.Actor alias Mobilizon.Actors.Actor
alias Mobilizon.Media.Picture alias Mobilizon.Media.Picture
alias Mobilizon.Federation.ActivityPub
alias Mobilizon.Federation.ActivityPub.{Activity, Federator, Relay} alias Mobilizon.Federation.ActivityPub.{Activity, Federator, Relay}
alias Mobilizon.Federation.ActivityStream.Converter alias Mobilizon.Federation.ActivityStream.Converter
alias Mobilizon.Federation.HTTPSignatures alias Mobilizon.Federation.HTTPSignatures
@ -118,7 +118,7 @@ defmodule Mobilizon.Federation.ActivityPub.Utils do
to = to ++ (data["cc"] || []) to = to ++ (data["cc"] || [])
to to
|> Enum.map(fn url -> Actors.get_actor_by_url(url) end) |> Enum.map(fn url -> ActivityPub.get_or_fetch_actor_by_url(url) end)
|> Enum.map(fn {status, actor} -> |> Enum.map(fn {status, actor} ->
case status do case status do
:ok -> :ok ->

View file

@ -8,13 +8,13 @@ defmodule Mobilizon.Federation.ActivityStream.Converter.Flag do
Note: Reports are named Flag in AS. Note: Reports are named Flag in AS.
""" """
alias Mobilizon.Actors
alias Mobilizon.Actors.Actor alias Mobilizon.Actors.Actor
alias Mobilizon.Conversations alias Mobilizon.Conversations
alias Mobilizon.Events alias Mobilizon.Events
alias Mobilizon.Events.Event alias Mobilizon.Events.Event
alias Mobilizon.Reports.Report alias Mobilizon.Reports.Report
alias Mobilizon.Federation.ActivityPub
alias Mobilizon.Federation.ActivityPub.Relay alias Mobilizon.Federation.ActivityPub.Relay
alias Mobilizon.Federation.ActivityStream.{Converter, Convertible} alias Mobilizon.Federation.ActivityStream.{Converter, Convertible}
@ -65,10 +65,10 @@ defmodule Mobilizon.Federation.ActivityStream.Converter.Flag do
@spec as_to_model(map) :: map @spec as_to_model(map) :: map
def as_to_model(%{"object" => objects} = object) do def as_to_model(%{"object" => objects} = object) do
with {:ok, %Actor{} = reporter} <- Actors.get_actor_by_url(object["actor"]), with {:ok, %Actor{} = reporter} <- ActivityPub.get_or_fetch_actor_by_url(object["actor"]),
%Actor{} = reported <- %Actor{} = reported <-
Enum.reduce_while(objects, nil, fn url, _ -> Enum.reduce_while(objects, nil, fn url, _ ->
case Actors.get_actor_by_url(url) do case ActivityPub.get_or_fetch_actor_by_url(url) do
{:ok, %Actor{} = actor} -> {:ok, %Actor{} = actor} ->
{:halt, actor} {:halt, actor}

View file

@ -10,6 +10,7 @@ defmodule Mobilizon.Federation.WebFinger do
alias Mobilizon.Actors alias Mobilizon.Actors
alias Mobilizon.Actors.Actor alias Mobilizon.Actors.Actor
alias Mobilizon.Federation.ActivityPub
alias Mobilizon.Federation.WebFinger.XmlBuilder alias Mobilizon.Federation.WebFinger.XmlBuilder
alias Mobilizon.Web.Endpoint alias Mobilizon.Web.Endpoint
alias Mobilizon.Web.Router.Helpers, as: Routes alias Mobilizon.Web.Router.Helpers, as: Routes
@ -48,7 +49,7 @@ defmodule Mobilizon.Federation.WebFinger do
{:ok, represent_actor(actor, "JSON")} {:ok, represent_actor(actor, "JSON")}
else else
_e -> _e ->
case Actors.get_actor_by_url(resource) do case ActivityPub.get_or_fetch_actor_by_url(resource) do
{:ok, %Actor{} = actor} when not is_nil(actor) -> {:ok, %Actor{} = actor} when not is_nil(actor) ->
{:ok, represent_actor(actor, "JSON")} {:ok, represent_actor(actor, "JSON")}

View file

@ -8,7 +8,7 @@ defmodule Mobilizon.GraphQL.Schema.EventType do
import Absinthe.Resolution.Helpers, only: [dataloader: 1] import Absinthe.Resolution.Helpers, only: [dataloader: 1]
import Mobilizon.GraphQL.Helpers.Error import Mobilizon.GraphQL.Helpers.Error
alias Mobilizon.{Actors, Addresses, Conversations, Events} alias Mobilizon.{Actors, Addresses, Conversations}
alias Mobilizon.GraphQL.Resolvers.{Event, Picture, Tag} alias Mobilizon.GraphQL.Resolvers.{Event, Picture, Tag}
alias Mobilizon.GraphQL.Schema alias Mobilizon.GraphQL.Schema