Transmogrifier: Fetch missing actors for chatmessages.
This commit is contained in:
parent
53e3063bd0
commit
a88734a0a2
@ -11,6 +11,7 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidator do
|
|||||||
|
|
||||||
alias Pleroma.Object
|
alias Pleroma.Object
|
||||||
alias Pleroma.User
|
alias Pleroma.User
|
||||||
|
alias Pleroma.Web.ActivityPub.ObjectValidators.Types
|
||||||
alias Pleroma.Web.ActivityPub.ObjectValidators.ChatMessageValidator
|
alias Pleroma.Web.ActivityPub.ObjectValidators.ChatMessageValidator
|
||||||
alias Pleroma.Web.ActivityPub.ObjectValidators.CreateChatMessageValidator
|
alias Pleroma.Web.ActivityPub.ObjectValidators.CreateChatMessageValidator
|
||||||
alias Pleroma.Web.ActivityPub.ObjectValidators.LikeValidator
|
alias Pleroma.Web.ActivityPub.ObjectValidators.LikeValidator
|
||||||
@ -67,8 +68,14 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidator do
|
|||||||
|> Map.new(fn {key, val} -> {to_string(key), val} end)
|
|> Map.new(fn {key, val} -> {to_string(key), val} end)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def fetch_actor(object) do
|
||||||
|
with {:ok, actor} <- Types.ObjectID.cast(object["actor"]) do
|
||||||
|
User.get_or_fetch_by_ap_id(actor)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def fetch_actor_and_object(object) do
|
def fetch_actor_and_object(object) do
|
||||||
User.get_or_fetch_by_ap_id(object["actor"])
|
fetch_actor(object)
|
||||||
Object.normalize(object["object"])
|
Object.normalize(object["object"])
|
||||||
:ok
|
:ok
|
||||||
end
|
end
|
||||||
|
@ -12,6 +12,7 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.CreateChatMessageValidator do
|
|||||||
alias Pleroma.Web.ActivityPub.ObjectValidators.Types
|
alias Pleroma.Web.ActivityPub.ObjectValidators.Types
|
||||||
|
|
||||||
import Ecto.Changeset
|
import Ecto.Changeset
|
||||||
|
import Pleroma.Web.ActivityPub.ObjectValidators.CommonValidations
|
||||||
|
|
||||||
@primary_key false
|
@primary_key false
|
||||||
|
|
||||||
@ -42,6 +43,7 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.CreateChatMessageValidator do
|
|||||||
cng
|
cng
|
||||||
|> validate_required([:id, :actor, :to, :type, :object])
|
|> validate_required([:id, :actor, :to, :type, :object])
|
||||||
|> validate_inclusion(:type, ["Create"])
|
|> validate_inclusion(:type, ["Create"])
|
||||||
|
|> validate_actor_presence()
|
||||||
|> validate_recipients_match(meta)
|
|> validate_recipients_match(meta)
|
||||||
|> validate_object_nonexistence()
|
|> validate_object_nonexistence()
|
||||||
end
|
end
|
||||||
|
@ -647,10 +647,10 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier do
|
|||||||
%{"type" => "Create", "object" => %{"type" => "ChatMessage"}} = data,
|
%{"type" => "Create", "object" => %{"type" => "ChatMessage"}} = data,
|
||||||
_options
|
_options
|
||||||
) do
|
) do
|
||||||
case Pipeline.common_pipeline(data, local: false) do
|
with {:ok, %User{}} <- ObjectValidator.fetch_actor(data),
|
||||||
{:ok, activity, _} ->
|
{:ok, activity, _} <- Pipeline.common_pipeline(data, local: false) do
|
||||||
{:ok, activity}
|
{:ok, activity}
|
||||||
|
else
|
||||||
e ->
|
e ->
|
||||||
e
|
e
|
||||||
end
|
end
|
||||||
|
@ -26,8 +26,15 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier.ChatMessageTest do
|
|||||||
data
|
data
|
||||||
|> Map.put("object", object)
|
|> Map.put("object", object)
|
||||||
|
|
||||||
_author = insert(:user, ap_id: data["actor"], local: false)
|
_author =
|
||||||
_recipient = insert(:user, ap_id: List.first(data["to"]), local: true)
|
insert(:user, ap_id: data["actor"], local: false, last_refreshed_at: DateTime.utc_now())
|
||||||
|
|
||||||
|
_recipient =
|
||||||
|
insert(:user,
|
||||||
|
ap_id: List.first(data["to"]),
|
||||||
|
local: true,
|
||||||
|
last_refreshed_at: DateTime.utc_now()
|
||||||
|
)
|
||||||
|
|
||||||
{:error, _} = Transmogrifier.handle_incoming(data)
|
{:error, _} = Transmogrifier.handle_incoming(data)
|
||||||
end
|
end
|
||||||
@ -37,8 +44,15 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier.ChatMessageTest do
|
|||||||
File.read!("test/fixtures/create-chat-message.json")
|
File.read!("test/fixtures/create-chat-message.json")
|
||||||
|> Poison.decode!()
|
|> Poison.decode!()
|
||||||
|
|
||||||
_author = insert(:user, ap_id: data["actor"], local: false)
|
_author =
|
||||||
_recipient = insert(:user, ap_id: List.first(data["to"]), local: false)
|
insert(:user, ap_id: data["actor"], local: false, last_refreshed_at: DateTime.utc_now())
|
||||||
|
|
||||||
|
_recipient =
|
||||||
|
insert(:user,
|
||||||
|
ap_id: List.first(data["to"]),
|
||||||
|
local: false,
|
||||||
|
last_refreshed_at: DateTime.utc_now()
|
||||||
|
)
|
||||||
|
|
||||||
{:error, _} = Transmogrifier.handle_incoming(data)
|
{:error, _} = Transmogrifier.handle_incoming(data)
|
||||||
end
|
end
|
||||||
@ -59,12 +73,28 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier.ChatMessageTest do
|
|||||||
refute Object.get_by_ap_id(data["object"]["id"])
|
refute Object.get_by_ap_id(data["object"]["id"])
|
||||||
end
|
end
|
||||||
|
|
||||||
|
test "it fetches the actor if they aren't in our system" do
|
||||||
|
Tesla.Mock.mock(fn env -> apply(HttpRequestMock, :request, [env]) end)
|
||||||
|
|
||||||
|
data =
|
||||||
|
File.read!("test/fixtures/create-chat-message.json")
|
||||||
|
|> Poison.decode!()
|
||||||
|
|> Map.put("actor", "http://mastodon.example.org/users/admin")
|
||||||
|
|> put_in(["object", "actor"], "http://mastodon.example.org/users/admin")
|
||||||
|
|
||||||
|
_recipient = insert(:user, ap_id: List.first(data["to"]), local: true)
|
||||||
|
|
||||||
|
{:ok, %Activity{} = _activity} = Transmogrifier.handle_incoming(data)
|
||||||
|
end
|
||||||
|
|
||||||
test "it inserts it and creates a chat" do
|
test "it inserts it and creates a chat" do
|
||||||
data =
|
data =
|
||||||
File.read!("test/fixtures/create-chat-message.json")
|
File.read!("test/fixtures/create-chat-message.json")
|
||||||
|> Poison.decode!()
|
|> Poison.decode!()
|
||||||
|
|
||||||
author = insert(:user, ap_id: data["actor"], local: false)
|
author =
|
||||||
|
insert(:user, ap_id: data["actor"], local: false, last_refreshed_at: DateTime.utc_now())
|
||||||
|
|
||||||
recipient = insert(:user, ap_id: List.first(data["to"]), local: true)
|
recipient = insert(:user, ap_id: List.first(data["to"]), local: true)
|
||||||
|
|
||||||
{:ok, %Activity{} = activity} = Transmogrifier.handle_incoming(data)
|
{:ok, %Activity{} = activity} = Transmogrifier.handle_incoming(data)
|
||||||
|
Loading…
Reference in New Issue
Block a user