Fix error when fucking Mastodon sends the same activity/object to us

multiple times
This commit is contained in:
Shadowfacts 2021-08-26 22:19:46 -04:00
parent ce80f0600e
commit 59116cce2e
Signed by: shadowfacts
GPG Key ID: 94A5AB95422746E5
2 changed files with 10 additions and 2 deletions

View File

@ -2,6 +2,14 @@ defmodule Clacks.Inbox do
require Logger
alias Clacks.{Repo, Activity, Object, Actor, ActivityPub, Notification}
@spec store_object(map()) :: {:ok, Object.t()}
defp store_object(%{"id" => ap_id} = object) do
changeset = Object.changeset(Object.get_cached_by_ap_id(ap_id) || %Object{}, %{data: object})
{:ok, object} = Repo.insert_or_update(changeset)
{:ok, object}
end
@spec store_activity(map(), boolean()) :: {:ok, Activity.t()} | {:error, term()}
defp store_activity(%{"actor" => actor, "id" => ap_id} = activity, local \\ false)
when is_binary(actor) do
# remove the embedded object (if there is one) from the activity
@ -37,9 +45,8 @@ defmodule Clacks.Inbox do
def handle(%{"type" => "Create", "object" => object} = activity) do
object = Clacks.Inbox.Transformer.restrict_incoming_object(object)
changeset = Object.changeset_for_creating(object)
with {:ok, _object} <- Repo.insert(changeset),
with {:ok, _object} <- store_object(object),
{:ok, _activity} <- store_activity(activity) do
:ok
else

View File

@ -17,6 +17,7 @@ defmodule Clacks.Object do
schema
|> cast(attrs, [:data])
|> validate_required([:data])
|> unique_constraint(:ap_id, name: :objects_unique_ap_id_index)
end
@spec changeset_for_creating(data :: map()) :: Ecto.Changeset.t()