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)
29 def cast_data(data) do
34 def cast_and_apply(data) do
37 |> apply_action(:insert)
40 def cast_and_validate(data, meta \\ []) do
43 |> validate_data(meta)
46 def changeset(struct, data) do
48 |> cast(data, __schema__(:fields))
51 def validate_data(cng, meta \\ []) do
53 |> validate_required([:actor, :type, :object])
54 |> validate_inclusion(:type, ["Create"])
55 |> validate_actor_is_active()
56 |> validate_any_presence([:to, :cc])
57 |> validate_actors_match(meta)
58 |> validate_object_nonexistence()
59 |> validate_object_containment()
62 def validate_object_containment(cng) do
63 actor = get_field(cng, :actor)
66 |> validate_change(:object, fn :object, object_id ->
67 %URI{host: object_id_host} = URI.parse(object_id)
68 %URI{host: actor_host} = URI.parse(actor)
70 if object_id_host == actor_host do
73 [{:object, "The host of the object id doesn't match with the host of the actor"}]
78 def validate_object_nonexistence(cng) do
80 |> validate_change(:object, fn :object, object_id ->
81 if Object.get_cached_by_ap_id(object_id) do
82 [{:object, "The object to create already exists"}]
89 def validate_actors_match(cng, meta) do
90 attributed_to = meta[:object_data]["attributedTo"] || meta[:object_data]["actor"]
93 |> validate_change(:actor, fn :actor, actor ->
94 if actor == attributed_to do
97 [{:actor, "Actor doesn't match with object attributedTo"}]