Create Question: Add context field to create

This commit is contained in:
Haelwenn (lanodan) Monnier 2020-07-02 05:45:19 +02:00
parent 922ca23298
commit e4beff90f5
No known key found for this signature in database
GPG key ID: D5B7A8E43C997DEE
4 changed files with 42 additions and 1 deletions

View file

@ -80,6 +80,13 @@ def delete(actor, object_id) do
end end
def create(actor, object, recipients) do def create(actor, object, recipients) do
context =
if is_map(object) do
object["context"]
else
nil
end
{:ok, {:ok,
%{ %{
"id" => Utils.generate_activity_id(), "id" => Utils.generate_activity_id(),
@ -88,7 +95,8 @@ def create(actor, object, recipients) do
"object" => object, "object" => object,
"type" => "Create", "type" => "Create",
"published" => DateTime.utc_now() |> DateTime.to_iso8601() "published" => DateTime.utc_now() |> DateTime.to_iso8601()
}, []} }
|> Pleroma.Maps.put_if_present("context", context), []}
end end
def chat_message(actor, recipient, content, opts \\ []) do def chat_message(actor, recipient, content, opts \\ []) do

View file

@ -24,6 +24,9 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.CreateGenericValidator do
field(:cc, ObjectValidators.Recipients, default: []) field(:cc, ObjectValidators.Recipients, default: [])
field(:object, ObjectValidators.ObjectID) field(:object, ObjectValidators.ObjectID)
field(:expires_at, ObjectValidators.DateTime) field(:expires_at, ObjectValidators.DateTime)
# Should be moved to object, done for CommonAPI.Utils.make_context
field(:context, :string)
end end
def cast_data(data) do def cast_data(data) do
@ -55,6 +58,7 @@ def validate_data(cng, meta \\ []) do
|> validate_actor_is_active() |> validate_actor_is_active()
|> validate_any_presence([:to, :cc]) |> validate_any_presence([:to, :cc])
|> validate_actors_match(meta) |> validate_actors_match(meta)
|> validate_context_match(meta)
|> validate_object_nonexistence() |> validate_object_nonexistence()
|> validate_object_containment() |> validate_object_containment()
end end
@ -98,4 +102,17 @@ def validate_actors_match(cng, meta) do
end end
end) end)
end end
def validate_context_match(cng, %{object_data: %{"context" => object_context}}) do
cng
|> validate_change(:context, fn :context, context ->
if context == object_context do
[]
else
[{:context, "context field not matching between Create and object (#{object_context})"}]
end
end)
end
def validate_context_match(cng, _), do: cng
end end

View file

@ -643,6 +643,8 @@ def handle_incoming(
|> Map.put("object", fix_object(object)) |> Map.put("object", fix_object(object))
|> fix_addressing() |> fix_addressing()
data = Map.put_new(data, "context", data["object"]["context"])
with {:ok, %User{}} <- ObjectValidator.fetch_actor(data), with {:ok, %User{}} <- ObjectValidator.fetch_actor(data),
{:ok, activity, _} <- Pipeline.common_pipeline(data, local: false) do {:ok, activity, _} <- Pipeline.common_pipeline(data, local: false) do
{:ok, activity} {:ok, activity}

View file

@ -8,6 +8,9 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier.QuestionHandlingTest do
alias Pleroma.Activity alias Pleroma.Activity
alias Pleroma.Object alias Pleroma.Object
alias Pleroma.Web.ActivityPub.Transmogrifier alias Pleroma.Web.ActivityPub.Transmogrifier
alias Pleroma.Web.CommonAPI
import Pleroma.Factory
setup_all do setup_all do
Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end) Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
@ -23,6 +26,8 @@ test "Mastodon Question activity" do
assert object.data["closed"] == "2019-05-11T09:03:36Z" assert object.data["closed"] == "2019-05-11T09:03:36Z"
assert object.data["context"] == activity.data["context"]
assert object.data["context"] == assert object.data["context"] ==
"tag:mastodon.sdf.org,2019-05-10:objectId=15095122:objectType=Conversation" "tag:mastodon.sdf.org,2019-05-10:objectId=15095122:objectType=Conversation"
@ -53,6 +58,15 @@ test "Mastodon Question activity" do
"type" => "Note" "type" => "Note"
} }
]) ])
user = insert(:user)
{:ok, reply_activity} = CommonAPI.post(user, %{status: "hewwo", in_reply_to_id: activity.id})
reply_object = Object.normalize(reply_activity, false)
assert reply_object.data["context"] == object.data["context"]
assert reply_object.data["context_id"] == object.data["context_id"]
end end
test "Mastodon Question activity with HTML tags in plaintext" do test "Mastodon Question activity with HTML tags in plaintext" do