1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 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
25 import Elixir.Pleroma.Web.ActivityPub.ObjectValidators.CommonFields
31 field(:context, :string)
32 field(:published, ObjectValidators.DateTime)
35 def cast_and_validate(data) do
41 def cast_data(data) do
50 def changeset(struct, data) do
52 |> cast(data, __schema__(:fields))
58 |> CommonFixes.fix_actor()
59 |> CommonFixes.fix_activity_addressing()
61 with %Object{} = object <- Object.normalize(data["object"]) do
63 |> CommonFixes.fix_activity_context(object)
64 |> CommonFixes.fix_object_action_recipients(object)
70 defp validate_data(data_cng) do
72 |> validate_inclusion(:type, ["Announce"])
73 |> validate_required([:id, :type, :object, :actor, :to, :cc])
74 |> validate_actor_presence()
75 |> validate_object_presence()
76 |> validate_existing_announce()
77 |> validate_announcable()
80 defp validate_announcable(cng) do
81 with actor when is_binary(actor) <- get_field(cng, :actor),
82 object when is_binary(object) <- get_field(cng, :object),
83 %User{} = actor <- User.get_cached_by_ap_id(actor),
84 %Object{} = object <- Object.get_cached_by_ap_id(object),
85 false <- Visibility.is_public?(object) do
86 same_actor = object.data["actor"] == actor.ap_id
87 recipients = get_field(cng, :to) ++ get_field(cng, :cc)
88 local_public = Utils.as_local_public()
91 Enum.member?(recipients, Pleroma.Constants.as_public()) or
92 Enum.member?(recipients, local_public)
95 same_actor && is_public ->
97 |> add_error(:actor, "can not announce this object publicly")
101 |> add_error(:actor, "can not announce this object")
111 defp validate_existing_announce(cng) do
112 actor = get_field(cng, :actor)
113 object = get_field(cng, :object)
115 if actor && object && Utils.get_existing_announce(actor, %{data: %{"id" => object}}) do
117 |> add_error(:actor, "already announced this object")
118 |> add_error(:object, "already announced by this actor")