Fix error when fucking Mastodon sends the same activity/object to us
multiple times
This commit is contained in:
parent
ce80f0600e
commit
59116cce2e
|
@ -2,6 +2,14 @@ defmodule Clacks.Inbox do
|
||||||
require Logger
|
require Logger
|
||||||
alias Clacks.{Repo, Activity, Object, Actor, ActivityPub, Notification}
|
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)
|
defp store_activity(%{"actor" => actor, "id" => ap_id} = activity, local \\ false)
|
||||||
when is_binary(actor) do
|
when is_binary(actor) do
|
||||||
# remove the embedded object (if there is one) from the activity
|
# 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
|
def handle(%{"type" => "Create", "object" => object} = activity) do
|
||||||
object = Clacks.Inbox.Transformer.restrict_incoming_object(object)
|
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, _activity} <- store_activity(activity) do
|
||||||
:ok
|
:ok
|
||||||
else
|
else
|
||||||
|
|
|
@ -17,6 +17,7 @@ defmodule Clacks.Object do
|
||||||
schema
|
schema
|
||||||
|> cast(attrs, [:data])
|
|> cast(attrs, [:data])
|
||||||
|> validate_required([:data])
|
|> validate_required([:data])
|
||||||
|
|> unique_constraint(:ap_id, name: :objects_unique_ap_id_index)
|
||||||
end
|
end
|
||||||
|
|
||||||
@spec changeset_for_creating(data :: map()) :: Ecto.Changeset.t()
|
@spec changeset_for_creating(data :: map()) :: Ecto.Changeset.t()
|
||||||
|
|
Loading…
Reference in New Issue