Merge remote-tracking branch 'pleroma/develop' into notice-routes
[akkoma] / lib / pleroma / web / activity_pub / object_validators / announce_validator.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.ActivityPub.ObjectValidators.AnnounceValidator do
6 use Ecto.Schema
7
8 alias Pleroma.EctoType.ActivityPub.ObjectValidators
9 alias Pleroma.Object
10 alias Pleroma.User
11 alias Pleroma.Web.ActivityPub.Utils
12 alias Pleroma.Web.ActivityPub.Visibility
13
14 import Ecto.Changeset
15 import Pleroma.Web.ActivityPub.ObjectValidators.CommonValidations
16
17 require Pleroma.Constants
18
19 @primary_key false
20
21 embedded_schema do
22 field(:id, ObjectValidators.ObjectID, primary_key: true)
23 field(:type, :string)
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)
30 end
31
32 def cast_and_validate(data) do
33 data
34 |> cast_data()
35 |> validate_data()
36 end
37
38 def cast_data(data) do
39 %__MODULE__{}
40 |> changeset(data)
41 end
42
43 def changeset(struct, data) do
44 struct
45 |> cast(data, __schema__(:fields))
46 |> fix_after_cast()
47 end
48
49 def fix_after_cast(cng) do
50 cng
51 end
52
53 defp validate_data(data_cng) do
54 data_cng
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()
61 end
62
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()
72
73 is_public =
74 Enum.member?(recipients, Pleroma.Constants.as_public()) or
75 Enum.member?(recipients, local_public)
76
77 cond do
78 same_actor && is_public ->
79 cng
80 |> add_error(:actor, "can not announce this object publicly")
81
82 !same_actor ->
83 cng
84 |> add_error(:actor, "can not announce this object")
85
86 true ->
87 cng
88 end
89 else
90 _ -> cng
91 end
92 end
93
94 def validate_existing_announce(cng) do
95 actor = get_field(cng, :actor)
96 object = get_field(cng, :object)
97
98 if actor && object && Utils.get_existing_announce(actor, %{data: %{"id" => object}}) do
99 cng
100 |> add_error(:actor, "already announced this object")
101 |> add_error(:object, "already announced by this actor")
102 else
103 cng
104 end
105 end
106 end