1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
6 # - Can probably be a generic create validator
7 # - doesn't embed, will only get the object id
8 defmodule Pleroma.Web.ActivityPub.ObjectValidators.CreateChatMessageValidator do
12 alias Pleroma.Web.ActivityPub.ObjectValidators.Types
15 import Pleroma.Web.ActivityPub.ObjectValidators.CommonValidations
20 field(:id, Types.ObjectID, primary_key: true)
21 field(:actor, Types.ObjectID)
23 field(:to, Types.Recipients, default: [])
24 field(:object, Types.ObjectID)
27 def cast_and_apply(data) do
30 |> apply_action(:insert)
33 def cast_data(data) do
34 cast(%__MODULE__{}, data, __schema__(:fields))
37 def cast_and_validate(data, meta \\ []) do
39 |> validate_data(meta)
42 def validate_data(cng, meta \\ []) do
44 |> validate_required([:id, :actor, :to, :type, :object])
45 |> validate_inclusion(:type, ["Create"])
46 |> validate_actor_presence()
47 |> validate_recipients_match(meta)
48 |> validate_object_nonexistence()
51 def validate_object_nonexistence(cng) do
53 |> validate_change(:object, fn :object, object_id ->
54 if Object.get_cached_by_ap_id(object_id) do
55 [{:object, "The object to create already exists"}]
62 def validate_recipients_match(cng, meta) do
63 object_recipients = meta[:object_data]["to"] || []
66 |> validate_change(:to, fn :to, recipients ->
67 activity_set = MapSet.new(recipients)
68 object_set = MapSet.new(object_recipients)
70 if MapSet.equal?(activity_set, object_set) do
73 [{:to, "Recipients don't match with object recipients"}]