1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 # Code based on CreateChatMessageValidator
7 # - doesn't embed, will only get the object id
8 defmodule Pleroma.Web.ActivityPub.ObjectValidators.CreateGenericValidator do
11 alias Pleroma.EctoType.ActivityPub.ObjectValidators
15 import Pleroma.Web.ActivityPub.ObjectValidators.CommonValidations
20 field(:id, ObjectValidators.ObjectID, primary_key: true)
21 field(:actor, ObjectValidators.ObjectID)
23 field(:to, ObjectValidators.Recipients, default: [])
24 field(:cc, ObjectValidators.Recipients, default: [])
25 field(:object, ObjectValidators.ObjectID)
26 field(:expires_at, ObjectValidators.DateTime)
28 # Should be moved to object, done for CommonAPI.Utils.make_context
29 field(:context, :string)
32 def cast_data(data) do
37 def cast_and_apply(data) do
40 |> apply_action(:insert)
43 def cast_and_validate(data, meta \\ []) do
46 |> validate_data(meta)
49 def changeset(struct, data) do
51 |> cast(data, __schema__(:fields))
54 def validate_data(cng, meta \\ []) do
56 |> validate_required([:actor, :type, :object])
57 |> validate_inclusion(:type, ["Create"])
58 |> validate_actor_is_active()
59 |> validate_any_presence([:to, :cc])
60 |> validate_actors_match(meta)
61 |> validate_context_match(meta)
62 |> validate_object_nonexistence()
63 |> validate_object_containment()
66 def validate_object_containment(cng) do
67 actor = get_field(cng, :actor)
70 |> validate_change(:object, fn :object, object_id ->
71 %URI{host: object_id_host} = URI.parse(object_id)
72 %URI{host: actor_host} = URI.parse(actor)
74 if object_id_host == actor_host do
77 [{:object, "The host of the object id doesn't match with the host of the actor"}]
82 def validate_object_nonexistence(cng) do
84 |> validate_change(:object, fn :object, object_id ->
85 if Object.get_cached_by_ap_id(object_id) do
86 [{:object, "The object to create already exists"}]
93 def validate_actors_match(cng, meta) do
94 attributed_to = meta[:object_data]["attributedTo"] || meta[:object_data]["actor"]
97 |> validate_change(:actor, fn :actor, actor ->
98 if actor == attributed_to do
101 [{:actor, "Actor doesn't match with object attributedTo"}]
106 def validate_context_match(cng, %{object_data: %{"context" => object_context}}) do
108 |> validate_change(:context, fn :context, context ->
109 if context == object_context do
112 [{:context, "context field not matching between Create and object (#{object_context})"}]
117 def validate_context_match(cng, _), do: cng