Merge develop branch upstream
[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.ObjectValidators.CommonFixes
12 alias Pleroma.Web.ActivityPub.Utils
13 alias Pleroma.Web.ActivityPub.Visibility
14
15 import Ecto.Changeset
16 import Pleroma.Web.ActivityPub.ObjectValidators.CommonValidations
17
18 require Pleroma.Constants
19
20 @primary_key false
21
22 embedded_schema do
23 field(:id, ObjectValidators.ObjectID, primary_key: true)
24 field(:type, :string)
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)
31 end
32
33 def cast_and_validate(data) do
34 data
35 |> cast_data()
36 |> validate_data()
37 end
38
39 def cast_data(data) do
40 data =
41 data
42 |> fix()
43
44 %__MODULE__{}
45 |> changeset(data)
46 end
47
48 def changeset(struct, data) do
49 struct
50 |> cast(data, __schema__(:fields))
51 end
52
53 defp fix(data) do
54 data =
55 data
56 |> CommonFixes.fix_actor()
57 |> CommonFixes.fix_activity_addressing()
58
59 with %Object{} = object <- Object.normalize(data["object"]) do
60 data
61 |> CommonFixes.fix_activity_context(object)
62 |> CommonFixes.fix_object_action_recipients(object)
63 else
64 _ -> data
65 end
66 end
67
68 defp validate_data(data_cng) do
69 data_cng
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()
76 end
77
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()
87
88 is_public =
89 Enum.member?(recipients, Pleroma.Constants.as_public()) or
90 Enum.member?(recipients, local_public)
91
92 cond do
93 same_actor && is_public ->
94 cng
95 |> add_error(:actor, "can not announce this object publicly")
96
97 !same_actor ->
98 cng
99 |> add_error(:actor, "can not announce this object")
100
101 true ->
102 cng
103 end
104 else
105 _ -> cng
106 end
107 end
108
109 defp validate_existing_announce(cng) do
110 actor = get_field(cng, :actor)
111 object = get_field(cng, :object)
112
113 if actor && object && Utils.get_existing_announce(actor, %{data: %{"id" => object}}) do
114 cng
115 |> add_error(:actor, "already announced this object")
116 |> add_error(:object, "already announced by this actor")
117 else
118 cng
119 end
120 end
121 end