Remove deps from Streaming/Persisting behaviors
[akkoma] / lib / pleroma / web / activity_pub / object_validators / delete_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.DeleteValidator do
6 use Ecto.Schema
7
8 alias Pleroma.Activity
9 alias Pleroma.EctoType.ActivityPub.ObjectValidators
10
11 import Ecto.Changeset
12 import Pleroma.Web.ActivityPub.ObjectValidators.CommonValidations
13
14 @primary_key false
15
16 embedded_schema do
17 field(:id, ObjectValidators.ObjectID, primary_key: true)
18 field(:type, :string)
19 field(:actor, ObjectValidators.ObjectID)
20 field(:to, ObjectValidators.Recipients, default: [])
21 field(:cc, ObjectValidators.Recipients, default: [])
22 field(:deleted_activity_id, ObjectValidators.ObjectID)
23 field(:object, ObjectValidators.ObjectID)
24 end
25
26 def cast_data(data) do
27 %__MODULE__{}
28 |> cast(data, __schema__(:fields))
29 end
30
31 def add_deleted_activity_id(cng) do
32 object =
33 cng
34 |> get_field(:object)
35
36 with %Activity{id: id} <- Activity.get_create_by_object_ap_id(object) do
37 cng
38 |> put_change(:deleted_activity_id, id)
39 else
40 _ -> cng
41 end
42 end
43
44 @deletable_types ~w{
45 Answer
46 Article
47 Audio
48 ChatMessage
49 Event
50 Note
51 Page
52 Question
53 Tombstone
54 Video
55 }
56 def validate_data(cng) do
57 cng
58 |> validate_required([:id, :type, :actor, :to, :cc, :object])
59 |> validate_inclusion(:type, ["Delete"])
60 |> validate_actor_presence()
61 |> validate_modification_rights()
62 |> validate_object_or_user_presence(allowed_types: @deletable_types)
63 |> add_deleted_activity_id()
64 end
65
66 def do_not_federate?(cng) do
67 !same_domain?(cng)
68 end
69
70 def cast_and_validate(data) do
71 data
72 |> cast_data
73 |> validate_data
74 end
75 end