1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 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
14 alias Pleroma.Web.ActivityPub.ObjectValidators.CommonFixes
15 alias Pleroma.Web.ActivityPub.ObjectValidators.CommonValidations
16 alias Pleroma.Web.ActivityPub.Transmogrifier
23 field(:id, ObjectValidators.ObjectID, primary_key: true)
24 field(:actor, ObjectValidators.ObjectID)
26 field(:to, ObjectValidators.Recipients, default: [])
27 field(:cc, ObjectValidators.Recipients, default: [])
28 field(:bto, ObjectValidators.Recipients, default: [])
29 field(:bcc, ObjectValidators.Recipients, default: [])
30 field(:object, ObjectValidators.ObjectID)
31 field(:expires_at, ObjectValidators.DateTime)
33 # Should be moved to object, done for CommonAPI.Utils.make_context
34 field(:context, :string)
37 def cast_data(data, meta \\ []) do
38 data = fix(data, meta)
44 def cast_and_apply(data) do
47 |> apply_action(:insert)
50 def cast_and_validate(data, meta \\ []) do
53 |> validate_data(meta)
56 def changeset(struct, data) do
58 |> cast(data, __schema__(:fields))
61 # CommonFixes.fix_activity_addressing adapted for Create specific behavior
62 defp fix_addressing(data, object) do
63 %User{follower_address: follower_collection} = User.get_cached_by_ap_id(data["actor"])
66 |> CommonFixes.cast_and_filter_recipients("to", follower_collection, object["to"])
67 |> CommonFixes.cast_and_filter_recipients("cc", follower_collection, object["cc"])
68 |> CommonFixes.cast_and_filter_recipients("bto", follower_collection, object["bto"])
69 |> CommonFixes.cast_and_filter_recipients("bcc", follower_collection, object["bcc"])
70 |> Transmogrifier.fix_implicit_addressing(follower_collection)
73 def fix(data, meta) do
74 object = meta[:object_data]
77 |> CommonFixes.fix_actor()
78 |> Map.put_new("context", object["context"])
79 |> fix_addressing(object)
82 defp validate_data(cng, meta) do
83 object = meta[:object_data]
86 |> validate_required([:actor, :type, :object, :to, :cc])
87 |> validate_inclusion(:type, ["Create"])
88 |> CommonValidations.validate_actor_presence()
89 |> validate_actors_match(object)
90 |> validate_context_match(object)
91 |> validate_addressing_match(object)
92 |> validate_object_nonexistence()
93 |> validate_object_containment()
96 def validate_object_containment(cng) do
97 actor = get_field(cng, :actor)
100 |> validate_change(:object, fn :object, object_id ->
101 %URI{host: object_id_host} = URI.parse(object_id)
102 %URI{host: actor_host} = URI.parse(actor)
104 if object_id_host == actor_host do
107 [{:object, "The host of the object id doesn't match with the host of the actor"}]
112 def validate_object_nonexistence(cng) do
114 |> validate_change(:object, fn :object, object_id ->
115 if Object.get_cached_by_ap_id(object_id) do
116 [{:object, "The object to create already exists"}]
123 def validate_actors_match(cng, object) do
124 attributed_to = object["attributedTo"] || object["actor"]
127 |> validate_change(:actor, fn :actor, actor ->
128 if actor == attributed_to do
131 [{:actor, "Actor doesn't match with object attributedTo"}]
136 def validate_context_match(cng, %{"context" => object_context}) do
138 |> validate_change(:context, fn :context, context ->
139 if context == object_context do
142 [{:context, "context field not matching between Create and object (#{object_context})"}]
147 def validate_addressing_match(cng, object) do
148 [:to, :cc, :bcc, :bto]
149 |> Enum.reduce(cng, fn field, cng ->
150 object_data = object[to_string(field)]
152 validate_change(cng, field, fn field, data ->
153 if data == object_data do
156 [{field, "field doesn't match with object (#{inspect(object_data)})"}]