Fork of Pleroma with site-specific changes and feature branches https://git.pleroma.social/pleroma/pleroma
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

60 行
1.7KB

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