Merge pull request 'Allow reacting with emojis containing a number' (#6) from sn0w...
[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 quote do
24 unquote do
25 import Elixir.Pleroma.Web.ActivityPub.ObjectValidators.CommonFields
26 message_fields()
27 activity_fields()
28 end
29 end
30
31 field(:context, :string)
32 field(:published, ObjectValidators.DateTime)
33 end
34
35 def cast_and_validate(data) do
36 data
37 |> cast_data()
38 |> validate_data()
39 end
40
41 def cast_data(data) do
42 data =
43 data
44 |> fix()
45
46 %__MODULE__{}
47 |> changeset(data)
48 end
49
50 def changeset(struct, data) do
51 struct
52 |> cast(data, __schema__(:fields))
53 end
54
55 defp fix(data) do
56 data =
57 data
58 |> CommonFixes.fix_actor()
59 |> CommonFixes.fix_activity_addressing()
60
61 with %Object{} = object <- Object.normalize(data["object"]) do
62 data
63 |> CommonFixes.fix_activity_context(object)
64 |> CommonFixes.fix_object_action_recipients(object)
65 else
66 _ -> data
67 end
68 end
69
70 defp validate_data(data_cng) do
71 data_cng
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()
78 end
79
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()
89
90 is_public =
91 Enum.member?(recipients, Pleroma.Constants.as_public()) or
92 Enum.member?(recipients, local_public)
93
94 cond do
95 same_actor && is_public ->
96 cng
97 |> add_error(:actor, "can not announce this object publicly")
98
99 !same_actor ->
100 cng
101 |> add_error(:actor, "can not announce this object")
102
103 true ->
104 cng
105 end
106 else
107 _ -> cng
108 end
109 end
110
111 defp validate_existing_announce(cng) do
112 actor = get_field(cng, :actor)
113 object = get_field(cng, :object)
114
115 if actor && object && Utils.get_existing_announce(actor, %{data: %{"id" => object}}) do
116 cng
117 |> add_error(:actor, "already announced this object")
118 |> add_error(:object, "already announced by this actor")
119 else
120 cng
121 end
122 end
123 end