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
8 alias Pleroma.EctoType.ActivityPub.ObjectValidators
10 alias Pleroma.Web.ActivityPub.ObjectValidators.CommonFixes
11 alias Pleroma.Web.ActivityPub.Utils
14 import Pleroma.Web.ActivityPub.ObjectValidators.CommonValidations
19 field(:id, ObjectValidators.ObjectID, primary_key: true)
21 field(:object, ObjectValidators.ObjectID)
22 field(:actor, ObjectValidators.ObjectID)
23 field(:context, :string)
24 field(:to, ObjectValidators.Recipients, default: [])
25 field(:cc, ObjectValidators.Recipients, default: [])
28 def cast_and_validate(data) do
34 def cast_data(data) do
43 def changeset(struct, data) do
45 |> cast(data, __schema__(:fields))
51 |> CommonFixes.fix_actor()
52 |> CommonFixes.fix_activity_addressing()
54 with %Object{} = object <- Object.normalize(data["object"]) do
56 |> CommonFixes.fix_activity_context(object)
57 |> CommonFixes.fix_object_action_recipients(object)
63 defp validate_data(data_cng) do
65 |> validate_inclusion(:type, ["Like"])
66 |> validate_required([:id, :type, :object, :actor, :context, :to, :cc])
67 |> validate_actor_presence()
68 |> validate_object_presence()
69 |> validate_existing_like()
72 defp validate_existing_like(%{changes: %{actor: actor, object: object}} = cng) do
73 if Utils.get_existing_like(actor, %{data: %{"id" => object}}) do
75 |> add_error(:actor, "already liked this object")
76 |> add_error(:object, "already liked by this actor")
82 defp validate_existing_like(cng), do: cng