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.ObjectValidators.CommonFixes
12 alias Pleroma.Web.ActivityPub.Utils
13 alias Pleroma.Web.ActivityPub.Visibility
16 import Pleroma.Web.ActivityPub.ObjectValidators.CommonValidations
18 require Pleroma.Constants
23 field(:id, ObjectValidators.ObjectID, primary_key: true)
25 field(:object, ObjectValidators.ObjectID)
26 field(:actor, ObjectValidators.ObjectID)
27 field(:context, :string)
28 field(:to, ObjectValidators.Recipients, default: [])
29 field(:cc, ObjectValidators.Recipients, default: [])
30 field(:published, ObjectValidators.DateTime)
33 def cast_and_validate(data) do
39 def cast_data(data) do
48 def changeset(struct, data) do
50 |> cast(data, __schema__(:fields))
56 |> CommonFixes.fix_actor()
57 |> CommonFixes.fix_activity_addressing()
59 with %Object{} = object <- Object.normalize(data["object"]) do
61 |> CommonFixes.fix_activity_context(object)
62 |> CommonFixes.fix_object_action_recipients(object)
68 defp validate_data(data_cng) do
70 |> validate_inclusion(:type, ["Announce"])
71 |> validate_required([:id, :type, :object, :actor, :to, :cc])
72 |> validate_actor_presence()
73 |> validate_object_presence()
74 |> validate_existing_announce()
75 |> validate_announcable()
78 defp validate_announcable(cng) do
79 with actor when is_binary(actor) <- get_field(cng, :actor),
80 object when is_binary(object) <- get_field(cng, :object),
81 %User{} = actor <- User.get_cached_by_ap_id(actor),
82 %Object{} = object <- Object.get_cached_by_ap_id(object),
83 false <- Visibility.is_public?(object) do
84 same_actor = object.data["actor"] == actor.ap_id
85 recipients = get_field(cng, :to) ++ get_field(cng, :cc)
86 local_public = Utils.as_local_public()
89 Enum.member?(recipients, Pleroma.Constants.as_public()) or
90 Enum.member?(recipients, local_public)
93 same_actor && is_public ->
95 |> add_error(:actor, "can not announce this object publicly")
99 |> add_error(:actor, "can not announce this object")
109 defp validate_existing_announce(cng) do
110 actor = get_field(cng, :actor)
111 object = get_field(cng, :object)
113 if actor && object && Utils.get_existing_announce(actor, %{data: %{"id" => object}}) do
115 |> add_error(:actor, "already announced this object")
116 |> add_error(:object, "already announced by this actor")