1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.ActivityPub.ObjectValidators.LikeValidator do
9 alias Pleroma.Web.ActivityPub.ObjectValidators.CommonFixes
10 alias Pleroma.Web.ActivityPub.Utils
13 import Pleroma.Web.ActivityPub.ObjectValidators.CommonValidations
20 import Elixir.Pleroma.Web.ActivityPub.ObjectValidators.CommonFields
26 field(:context, :string)
29 def cast_and_validate(data) do
35 def cast_data(data) do
44 def changeset(struct, data) do
46 |> cast(data, __schema__(:fields))
52 |> CommonFixes.fix_actor()
53 |> CommonFixes.fix_activity_addressing()
55 with %Object{} = object <- Object.normalize(data["object"]) do
57 |> CommonFixes.fix_activity_context(object)
58 |> CommonFixes.fix_object_action_recipients(object)
64 defp validate_data(data_cng) do
66 |> validate_inclusion(:type, ["Like"])
67 |> validate_required([:id, :type, :object, :actor, :context, :to, :cc])
68 |> validate_actor_presence()
69 |> validate_object_presence()
70 |> validate_existing_like()
73 defp validate_existing_like(%{changes: %{actor: actor, object: object}} = cng) do
74 if Utils.get_existing_like(actor, %{data: %{"id" => object}}) do
76 |> add_error(:actor, "already liked this object")
77 |> add_error(:object, "already liked by this actor")
83 defp validate_existing_like(cng), do: cng