2020-04-29 17:09:51 +00:00
|
|
|
# Pleroma: A lightweight social networking server
|
2021-01-13 06:49:20 +00:00
|
|
|
# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
|
2020-04-29 17:09:51 +00:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
defmodule Pleroma.Web.ActivityPub.ObjectValidators.DeleteValidator do
|
|
|
|
use Ecto.Schema
|
|
|
|
|
2020-04-30 15:53:02 +00:00
|
|
|
alias Pleroma.Activity
|
2020-06-16 14:50:33 +00:00
|
|
|
alias Pleroma.EctoType.ActivityPub.ObjectValidators
|
2021-06-30 06:14:34 +00:00
|
|
|
alias Pleroma.User
|
2020-04-29 17:09:51 +00:00
|
|
|
|
|
|
|
import Ecto.Changeset
|
|
|
|
import Pleroma.Web.ActivityPub.ObjectValidators.CommonValidations
|
|
|
|
|
|
|
|
@primary_key false
|
|
|
|
|
|
|
|
embedded_schema do
|
2020-06-16 14:50:33 +00:00
|
|
|
field(:id, ObjectValidators.ObjectID, primary_key: true)
|
2020-04-29 17:09:51 +00:00
|
|
|
field(:type, :string)
|
2020-06-16 14:50:33 +00:00
|
|
|
field(:actor, ObjectValidators.ObjectID)
|
|
|
|
field(:to, ObjectValidators.Recipients, default: [])
|
|
|
|
field(:cc, ObjectValidators.Recipients, default: [])
|
|
|
|
field(:deleted_activity_id, ObjectValidators.ObjectID)
|
|
|
|
field(:object, ObjectValidators.ObjectID)
|
2020-04-29 17:09:51 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def cast_data(data) do
|
|
|
|
%__MODULE__{}
|
|
|
|
|> cast(data, __schema__(:fields))
|
|
|
|
end
|
|
|
|
|
2020-04-30 15:53:02 +00:00
|
|
|
def add_deleted_activity_id(cng) do
|
|
|
|
object =
|
|
|
|
cng
|
|
|
|
|> get_field(:object)
|
|
|
|
|
|
|
|
with %Activity{id: id} <- Activity.get_create_by_object_ap_id(object) do
|
|
|
|
cng
|
|
|
|
|> put_change(:deleted_activity_id, id)
|
|
|
|
else
|
|
|
|
_ -> cng
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-05-01 11:34:47 +00:00
|
|
|
@deletable_types ~w{
|
|
|
|
Answer
|
|
|
|
Article
|
|
|
|
Audio
|
2020-05-12 11:13:03 +00:00
|
|
|
ChatMessage
|
2020-05-01 11:34:47 +00:00
|
|
|
Event
|
|
|
|
Note
|
|
|
|
Page
|
|
|
|
Question
|
2020-05-11 13:06:23 +00:00
|
|
|
Tombstone
|
2020-05-12 11:13:03 +00:00
|
|
|
Video
|
2020-05-01 11:34:47 +00:00
|
|
|
}
|
2021-01-12 10:14:09 +00:00
|
|
|
defp validate_data(cng) do
|
2020-04-29 17:09:51 +00:00
|
|
|
cng
|
|
|
|
|> validate_required([:id, :type, :actor, :to, :cc, :object])
|
|
|
|
|> validate_inclusion(:type, ["Delete"])
|
2021-06-30 06:14:34 +00:00
|
|
|
|> validate_delete_actor(:actor)
|
2020-08-05 15:26:53 +00:00
|
|
|
|> validate_modification_rights()
|
2020-05-01 11:34:47 +00:00
|
|
|
|> validate_object_or_user_presence(allowed_types: @deletable_types)
|
2020-04-30 15:53:02 +00:00
|
|
|
|> add_deleted_activity_id()
|
2020-04-29 17:09:51 +00:00
|
|
|
end
|
|
|
|
|
2020-04-30 19:23:18 +00:00
|
|
|
def do_not_federate?(cng) do
|
|
|
|
!same_domain?(cng)
|
|
|
|
end
|
|
|
|
|
2020-04-29 17:09:51 +00:00
|
|
|
def cast_and_validate(data) do
|
|
|
|
data
|
|
|
|
|> cast_data
|
|
|
|
|> validate_data
|
|
|
|
end
|
2021-06-30 06:14:34 +00:00
|
|
|
|
|
|
|
defp validate_delete_actor(cng, field_name) do
|
|
|
|
validate_change(cng, field_name, fn field_name, actor ->
|
|
|
|
case User.get_cached_by_ap_id(actor) do
|
|
|
|
%User{} -> []
|
|
|
|
_ -> [{field_name, "can't find user"}]
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
end
|
2020-04-29 17:09:51 +00:00
|
|
|
end
|