Browse Source

Add option to return activity from object fetch, don't unnecessarily synthesize Create if one already exists

master
Shadowfacts 1 year ago
parent
commit
f51d4a6be4
Signed by: shadowfacts GPG Key ID: 94A5AB95422746E5
1 changed files with 40 additions and 15 deletions
  1. +40
    -15
      lib/clacks/object.ex

+ 40
- 15
lib/clacks/object.ex View File

@ -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)
changeset =
Clacks.Activity.changeset(%Clacks.Activity{}, %{
data: create,
local: false,
actor: actor
})
{:ok, _create} = Repo.insert_or_update(changeset)
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
})
{: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…
Cancel
Save