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
19 field(:id, Types.ObjectID, primary_key: true)
20 field(:actor, Types.ObjectID)
22 field(:to, Types.Recipients, default: [])
23 field(:object, Types.ObjectID)
26 def cast_and_apply(data) do
29 |> apply_action(:insert)
32 def cast_data(data) do
33 cast(%__MODULE__{}, data, __schema__(:fields))
36 def cast_and_validate(data, meta \\ []) do
38 |> validate_data(meta)
41 def validate_data(cng, meta \\ []) do
43 |> validate_required([:id, :actor, :to, :type, :object])
44 |> validate_inclusion(:type, ["Create"])
45 |> validate_recipients_match(meta)
46 |> validate_object_nonexistence()
49 def validate_object_nonexistence(cng) do
51 |> validate_change(:object, fn :object, object_id ->
52 if Object.get_cached_by_ap_id(object_id) do
53 [{:object, "The object to create already exists"}]
60 def validate_recipients_match(cng, meta) do
61 object_recipients = meta[:object_data]["to"] || []
64 |> validate_change(:to, fn :to, recipients ->
65 activity_set = MapSet.new(recipients)
66 object_set = MapSet.new(object_recipients)
68 if MapSet.equal?(activity_set, object_set) do
71 [{:to, "Recipients don't match with object recipients"}]