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 defmodule Pleroma.Web.ActivityPub.ObjectValidators.AnnounceValidator do
8 alias Pleroma.EctoType.ActivityPub.ObjectValidators
11 alias Pleroma.Web.ActivityPub.Utils
12 alias Pleroma.Web.ActivityPub.Visibility
15 import Pleroma.Web.ActivityPub.ObjectValidators.CommonValidations
17 require Pleroma.Constants
22 field(:id, ObjectValidators.ObjectID, primary_key: true)
24 field(:object, ObjectValidators.ObjectID)
25 field(:actor, ObjectValidators.ObjectID)
26 field(:context, :string, autogenerate: {Utils, :generate_context_id, []})
27 field(:to, ObjectValidators.Recipients, default: [])
28 field(:cc, ObjectValidators.Recipients, default: [])
29 field(:published, ObjectValidators.DateTime)
32 def cast_and_validate(data) do
38 def cast_data(data) do
43 def changeset(struct, data) do
45 |> cast(data, __schema__(:fields))
49 def fix_after_cast(cng) do
53 defp validate_data(data_cng) do
55 |> validate_inclusion(:type, ["Announce"])
56 |> validate_required([:id, :type, :object, :actor, :to, :cc])
57 |> validate_actor_presence()
58 |> validate_object_presence()
59 |> validate_existing_announce()
60 |> validate_announcable()
63 def validate_announcable(cng) do
64 with actor when is_binary(actor) <- get_field(cng, :actor),
65 object when is_binary(object) <- get_field(cng, :object),
66 %User{} = actor <- User.get_cached_by_ap_id(actor),
67 %Object{} = object <- Object.get_cached_by_ap_id(object),
68 false <- Visibility.is_public?(object) do
69 same_actor = object.data["actor"] == actor.ap_id
70 recipients = get_field(cng, :to) ++ get_field(cng, :cc)
71 local_public = Pleroma.Constants.as_local_public()
74 Enum.member?(recipients, Pleroma.Constants.as_public()) or
75 Enum.member?(recipients, local_public)
78 same_actor && is_public ->
80 |> add_error(:actor, "can not announce this object publicly")
84 |> add_error(:actor, "can not announce this object")
94 def validate_existing_announce(cng) do
95 actor = get_field(cng, :actor)
96 object = get_field(cng, :object)
98 if actor && object && Utils.get_existing_announce(actor, %{data: %{"id" => object}}) do
100 |> add_error(:actor, "already announced this object")
101 |> add_error(:object, "already announced by this actor")