Add option to return activity from object fetch, don't unnecessarily synthesize Create if one already exists
This commit is contained in:
parent
8178fc3ba6
commit
f51d4a6be4
|
@ -24,6 +24,17 @@ defmodule Clacks.Object do
|
|||
changeset(%__MODULE__{}, %{data: data})
|
||||
end
|
||||
|
||||
@spec get_actor(object :: t()) :: Clacks.Actor.t() | nil
|
||||
def get_actor(object) do
|
||||
case object.data["actor"] || object.data["attributedTo"] do
|
||||
nil ->
|
||||
nil
|
||||
|
||||
id when is_binary(id) ->
|
||||
Clacks.Actor.get_by_ap_id(id)
|
||||
end
|
||||
end
|
||||
|
||||
@spec get_by_ap_id(
|
||||
ap_id :: String.t(),
|
||||
force_refetch :: boolean(),
|
||||
|
@ -42,8 +53,9 @@ defmodule Clacks.Object do
|
|||
Repo.one(from o in __MODULE__, where: fragment("?->>'id'", o.data) == ^ap_id)
|
||||
end
|
||||
|
||||
@spec fetch(ap_id :: String.t(), synthesize_create :: boolean()) :: t() | nil
|
||||
def fetch(ap_id, synthesize_create \\ true) do
|
||||
@spec fetch(ap_id :: String.t(), synthesize_create :: boolean(), return :: :object | :activity) ::
|
||||
t() | nil
|
||||
def fetch(ap_id, synthesize_create \\ true, return \\ :object) do
|
||||
case Clacks.ActivityPub.Fetcher.fetch_object(ap_id) do
|
||||
nil ->
|
||||
nil
|
||||
|
@ -61,21 +73,34 @@ defmodule Clacks.Object do
|
|||
actor = data["actor"] || data["attributedTo"]
|
||||
_ = Clacks.Actor.get_by_ap_id(actor)
|
||||
|
||||
if synthesize_create do
|
||||
create = Clacks.ActivityPub.synthesized_create(data)
|
||||
activity =
|
||||
case Clacks.Activity.get_by_object_ap_id(ap_id) do
|
||||
nil ->
|
||||
if synthesize_create do
|
||||
create = Clacks.ActivityPub.synthesized_create(data)
|
||||
|
||||
changeset =
|
||||
Clacks.Activity.changeset(%Clacks.Activity{}, %{
|
||||
data: create,
|
||||
local: false,
|
||||
actor: actor
|
||||
})
|
||||
changeset =
|
||||
Clacks.Activity.changeset(%Clacks.Activity{}, %{
|
||||
data: create,
|
||||
local: false,
|
||||
actor: actor
|
||||
})
|
||||
|
||||
{:ok, _create} = Repo.insert_or_update(changeset)
|
||||
{:ok, create} = Repo.insert_or_update(changeset)
|
||||
create
|
||||
else
|
||||
nil
|
||||
end
|
||||
|
||||
%Clacks.Activity{} = activity ->
|
||||
activity
|
||||
end
|
||||
|
||||
case return do
|
||||
:object -> object
|
||||
:activity -> activity
|
||||
end
|
||||
|
||||
object
|
||||
|
||||
{:error, changeset} ->
|
||||
Logger.error("Couldn't store remote object #{ap_id}: #{inspect(changeset)}")
|
||||
nil
|
||||
|
|
Loading…
Reference in New Issue