2020-05-05 10:11:46 +00:00
|
|
|
# Pleroma: A lightweight social networking server
|
2021-01-13 06:49:20 +00:00
|
|
|
# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
|
2020-05-05 10:11:46 +00:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
defmodule Pleroma.Web.ActivityPub.ObjectValidators.EmojiReactValidator do
|
|
|
|
use Ecto.Schema
|
|
|
|
|
2020-06-16 14:50:33 +00:00
|
|
|
alias Pleroma.EctoType.ActivityPub.ObjectValidators
|
2020-05-05 10:11:46 +00:00
|
|
|
alias Pleroma.Object
|
2021-06-09 01:43:01 +00:00
|
|
|
alias Pleroma.Web.ActivityPub.ObjectValidators.CommonFixes
|
2020-05-05 10:11:46 +00:00
|
|
|
|
|
|
|
import Ecto.Changeset
|
|
|
|
import Pleroma.Web.ActivityPub.ObjectValidators.CommonValidations
|
|
|
|
|
|
|
|
@primary_key false
|
|
|
|
|
|
|
|
embedded_schema do
|
2020-06-16 14:50:33 +00:00
|
|
|
field(:id, ObjectValidators.ObjectID, primary_key: true)
|
2020-05-05 10:11:46 +00:00
|
|
|
field(:type, :string)
|
2020-06-16 14:50:33 +00:00
|
|
|
field(:object, ObjectValidators.ObjectID)
|
|
|
|
field(:actor, ObjectValidators.ObjectID)
|
2020-05-05 10:11:46 +00:00
|
|
|
field(:context, :string)
|
|
|
|
field(:content, :string)
|
2020-08-17 21:46:42 +00:00
|
|
|
field(:to, ObjectValidators.Recipients, default: [])
|
|
|
|
field(:cc, ObjectValidators.Recipients, default: [])
|
2020-05-05 10:11:46 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def cast_and_validate(data) do
|
|
|
|
data
|
|
|
|
|> cast_data()
|
|
|
|
|> validate_data()
|
|
|
|
end
|
|
|
|
|
|
|
|
def cast_data(data) do
|
2021-06-09 01:43:01 +00:00
|
|
|
data =
|
|
|
|
data
|
|
|
|
|> fix()
|
|
|
|
|
2020-05-05 10:11:46 +00:00
|
|
|
%__MODULE__{}
|
|
|
|
|> changeset(data)
|
|
|
|
end
|
|
|
|
|
|
|
|
def changeset(struct, data) do
|
|
|
|
struct
|
|
|
|
|> cast(data, __schema__(:fields))
|
|
|
|
end
|
|
|
|
|
2021-06-09 01:43:01 +00:00
|
|
|
defp fix(data) do
|
|
|
|
data =
|
|
|
|
data
|
|
|
|
|> CommonFixes.fix_actor()
|
|
|
|
|> CommonFixes.fix_activity_addressing()
|
2020-05-05 10:11:46 +00:00
|
|
|
|
2021-06-09 01:43:01 +00:00
|
|
|
with %Object{} = object <- Object.normalize(data["object"]) do
|
|
|
|
data
|
|
|
|
|> CommonFixes.fix_activity_context(object)
|
|
|
|
|> CommonFixes.fix_object_action_recipients(object)
|
2020-05-05 10:11:46 +00:00
|
|
|
else
|
2021-06-09 01:43:01 +00:00
|
|
|
_ -> data
|
2020-05-05 10:11:46 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-06-09 01:43:01 +00:00
|
|
|
defp validate_emoji(cng) do
|
2020-05-05 10:11:46 +00:00
|
|
|
content = get_field(cng, :content)
|
|
|
|
|
|
|
|
if Pleroma.Emoji.is_unicode_emoji?(content) do
|
|
|
|
cng
|
|
|
|
else
|
|
|
|
cng
|
|
|
|
|> add_error(:content, "must be a single character emoji")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-01-12 10:14:09 +00:00
|
|
|
defp validate_data(data_cng) do
|
2020-05-05 10:11:46 +00:00
|
|
|
data_cng
|
|
|
|
|> validate_inclusion(:type, ["EmojiReact"])
|
|
|
|
|> validate_required([:id, :type, :object, :actor, :context, :to, :cc, :content])
|
|
|
|
|> validate_actor_presence()
|
|
|
|
|> validate_object_presence()
|
|
|
|
|> validate_emoji()
|
|
|
|
end
|
|
|
|
end
|