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.Utils
13 import Pleroma.Web.ActivityPub.ObjectValidators.CommonValidations
18 field(:id, ObjectValidators.ObjectID, primary_key: true)
20 field(:object, ObjectValidators.ObjectID)
21 field(:actor, ObjectValidators.ObjectID)
22 field(:context, :string)
23 field(:to, ObjectValidators.Recipients, default: [])
24 field(:cc, ObjectValidators.Recipients, default: [])
27 def cast_and_validate(data) do
33 def cast_data(data) do
38 def changeset(struct, data) do
40 |> cast(data, __schema__(:fields))
44 def fix_after_cast(cng) do
50 def fix_context(cng) do
51 object = get_field(cng, :object)
53 with nil <- get_field(cng, :context),
54 %Object{data: %{"context" => context}} <- Object.get_cached_by_ap_id(object) do
56 |> put_change(:context, context)
63 def fix_recipients(cng) do
64 to = get_field(cng, :to)
65 cc = get_field(cng, :cc)
66 object = get_field(cng, :object)
68 with {[], []} <- {to, cc},
69 %Object{data: %{"actor" => actor}} <- Object.get_cached_by_ap_id(object),
70 {:ok, actor} <- ObjectValidators.ObjectID.cast(actor) do
72 |> put_change(:to, [actor])
79 def validate_data(data_cng) do
81 |> validate_inclusion(:type, ["Like"])
82 |> validate_required([:id, :type, :object, :actor, :context, :to, :cc])
83 |> validate_actor_presence()
84 |> validate_object_presence()
85 |> validate_existing_like()
88 def validate_existing_like(%{changes: %{actor: actor, object: object}} = cng) do
89 if Utils.get_existing_like(actor, %{data: %{"id" => object}}) do
91 |> add_error(:actor, "already liked this object")
92 |> add_error(:object, "already liked by this actor")
98 def validate_existing_like(cng), do: cng