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.EmojiReactValidator do
10 alias Pleroma.Web.ActivityPub.ObjectValidators.CommonFixes
13 import Pleroma.Web.ActivityPub.ObjectValidators.CommonValidations
16 @emoji_regex ~r/:[A-Za-z_-]+:/
21 import Elixir.Pleroma.Web.ActivityPub.ObjectValidators.CommonFields
28 field(:context, :string)
29 field(:content, :string)
32 def cast_and_validate(data) do
38 def cast_data(data) do
47 def changeset(struct, data) do
49 |> cast(data, __schema__(:fields) -- [:tag])
56 |> CommonFixes.fix_actor()
57 |> CommonFixes.fix_activity_addressing()
60 if Map.has_key?(data, "tag") do
63 Map.put(data, "tag", [])
66 case Object.normalize(data["object"]) do
69 |> CommonFixes.fix_activity_context(object)
70 |> CommonFixes.fix_object_action_recipients(object)
77 defp matches_shortcode?(nil), do: false
78 defp matches_shortcode?(s), do: Regex.match?(@emoji_regex, s)
80 defp validate_emoji(cng) do
81 content = get_field(cng, :content)
83 if Emoji.is_unicode_emoji?(content) || matches_shortcode?(content) do
87 |> add_error(:content, "is not a valid emoji")
91 defp maybe_validate_tag_presence(cng) do
92 content = get_field(cng, :content)
94 if Emoji.is_unicode_emoji?(content) do
97 tag = get_field(cng, :tag)
98 emoji_name = Emoji.stripped_name(content)
100 [%{name: ^emoji_name, type: "Emoji", icon: %{url: _}}] ->
104 |> add_error(:tag, "does not contain an Emoji tag")
109 defp validate_data(data_cng) do
111 |> validate_inclusion(:type, ["EmojiReact"])
112 |> validate_required([:id, :type, :object, :actor, :context, :to, :cc, :content])
113 |> validate_actor_presence()
114 |> validate_object_presence()
116 |> maybe_validate_tag_presence()