Support blurhash
[akkoma] / lib / pleroma / web / activity_pub / object_validators / update_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.UpdateValidator do
6 use Ecto.Schema
7
8 alias Pleroma.EctoType.ActivityPub.ObjectValidators
9
10 import Ecto.Changeset
11 import Pleroma.Web.ActivityPub.ObjectValidators.CommonValidations
12
13 @primary_key false
14
15 embedded_schema do
16 field(:id, ObjectValidators.ObjectID, primary_key: true)
17 field(:type, :string)
18 field(:actor, ObjectValidators.ObjectID)
19 field(:to, ObjectValidators.Recipients, default: [])
20 field(:cc, ObjectValidators.Recipients, default: [])
21 # In this case, we save the full object in this activity instead of just a
22 # reference, so we can always see what was actually changed by this.
23 field(:object, :map)
24 end
25
26 def cast_data(data) do
27 %__MODULE__{}
28 |> cast(data, __schema__(:fields))
29 end
30
31 def validate_data(cng) do
32 cng
33 |> validate_required([:id, :type, :actor, :to, :cc, :object])
34 |> validate_inclusion(:type, ["Update"])
35 |> validate_actor_presence()
36 |> validate_updating_rights()
37 end
38
39 def cast_and_validate(data) do
40 data
41 |> cast_data
42 |> validate_data
43 end
44
45 # For now we only support updating users, and here the rule is easy:
46 # object id == actor id
47 def validate_updating_rights(cng) do
48 with actor = get_field(cng, :actor),
49 object = get_field(cng, :object),
50 {:ok, object_id} <- ObjectValidators.ObjectID.cast(object),
51 true <- actor == object_id do
52 cng
53 else
54 _e ->
55 cng
56 |> add_error(:object, "Can't be updated by this actor")
57 end
58 end
59 end