Merge branch 'develop' of gitssh.ihatebeinga.live:IHBAGang/pleroma into develop
[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 alias Pleroma.User
11
12 import Ecto.Changeset
13 import Pleroma.Web.ActivityPub.ObjectValidators.CommonValidations
14
15 @primary_key false
16
17 embedded_schema do
18 quote do
19 unquote do
20 import Elixir.Pleroma.Web.ActivityPub.ObjectValidators.CommonFields
21 message_fields()
22 activity_fields()
23 end
24 end
25
26 field(:deleted_activity_id, ObjectValidators.ObjectID)
27 end
28
29 def cast_data(data) do
30 %__MODULE__{}
31 |> cast(data, __schema__(:fields))
32 end
33
34 def add_deleted_activity_id(cng) do
35 object =
36 cng
37 |> get_field(:object)
38
39 with %Activity{id: id} <- Activity.get_create_by_object_ap_id(object) do
40 cng
41 |> put_change(:deleted_activity_id, id)
42 else
43 _ -> cng
44 end
45 end
46
47 @deletable_types ~w{
48 Answer
49 Article
50 Audio
51 ChatMessage
52 Event
53 Note
54 Page
55 Question
56 Tombstone
57 Video
58 }
59 defp validate_data(cng) do
60 cng
61 |> validate_required([:id, :type, :actor, :to, :cc, :object])
62 |> validate_inclusion(:type, ["Delete"])
63 |> validate_delete_actor(:actor)
64 |> validate_modification_rights()
65 |> validate_object_or_user_presence(allowed_types: @deletable_types)
66 |> add_deleted_activity_id()
67 end
68
69 def do_not_federate?(cng) do
70 !same_domain?(cng)
71 end
72
73 def cast_and_validate(data) do
74 data
75 |> cast_data
76 |> validate_data
77 end
78
79 defp validate_delete_actor(cng, field_name) do
80 validate_change(cng, field_name, fn field_name, actor ->
81 case User.get_cached_by_ap_id(actor) do
82 %User{} -> []
83 _ -> [{field_name, "can't find user"}]
84 end
85 end)
86 end
87 end