alias Pleroma.Web.ActivityPub.ObjectValidators.AttachmentValidator
alias Pleroma.Web.ActivityPub.ObjectValidators.CommonFixes
alias Pleroma.Web.ActivityPub.ObjectValidators.CommonValidations
+ alias Pleroma.Web.ActivityPub.ObjectValidators.TagValidator
alias Pleroma.Web.ActivityPub.Transmogrifier
import Ecto.Changeset
field(:cc, ObjectValidators.Recipients, default: [])
field(:bto, ObjectValidators.Recipients, default: [])
field(:bcc, ObjectValidators.Recipients, default: [])
- # TODO: Write type
- field(:tag, {:array, :map}, default: [])
+ embeds_many(:tag, TagValidator)
field(:type, :string)
field(:name, :string)
data = fix(data)
struct
- |> cast(data, __schema__(:fields) -- [:attachment])
+ |> cast(data, __schema__(:fields) -- [:attachment, :tag])
|> cast_embed(:attachment)
+ |> cast_embed(:tag)
end
def validate_data(data_cng) do
use Ecto.Schema
alias Pleroma.EctoType.ActivityPub.ObjectValidators
- alias Pleroma.Web.ActivityPub.ObjectValidators.UrlObjectValidator
import Ecto.Changeset
alias Pleroma.Web.ActivityPub.ObjectValidators.AttachmentValidator
alias Pleroma.Web.ActivityPub.ObjectValidators.CommonFixes
alias Pleroma.Web.ActivityPub.ObjectValidators.CommonValidations
+ alias Pleroma.Web.ActivityPub.ObjectValidators.TagValidator
alias Pleroma.Web.ActivityPub.Transmogrifier
import Ecto.Changeset
field(:cc, ObjectValidators.Recipients, default: [])
field(:bto, ObjectValidators.Recipients, default: [])
field(:bcc, ObjectValidators.Recipients, default: [])
- # TODO: Write type
- field(:tag, {:array, :map}, default: [])
+ embeds_many(:tag, TagValidator)
field(:type, :string)
field(:name, :string)
data = fix(data)
struct
- |> cast(data, __schema__(:fields) -- [:attachment])
+ |> cast(data, __schema__(:fields) -- [:attachment, :tag])
|> cast_embed(:attachment)
+ |> cast_embed(:tag)
end
def validate_data(data_cng) do
alias Pleroma.Web.ActivityPub.ObjectValidators.AttachmentValidator
alias Pleroma.Web.ActivityPub.ObjectValidators.CommonFixes
alias Pleroma.Web.ActivityPub.ObjectValidators.CommonValidations
+ alias Pleroma.Web.ActivityPub.ObjectValidators.TagValidator
alias Pleroma.Web.ActivityPub.Transmogrifier
import Ecto.Changeset
field(:cc, ObjectValidators.Recipients, default: [])
field(:bto, ObjectValidators.Recipients, default: [])
field(:bcc, ObjectValidators.Recipients, default: [])
- # TODO: Write type
- field(:tag, {:array, :map}, default: [])
+ embeds_many(:tag, TagValidator)
field(:type, :string)
field(:name, :string)
data = fix(data)
struct
- |> cast(data, __schema__(:fields) -- [:attachment])
+ |> cast(data, __schema__(:fields) -- [:attachment, :tag])
|> cast_embed(:attachment)
+ |> cast_embed(:tag)
end
def validate_data(data_cng) do
alias Pleroma.Web.ActivityPub.ObjectValidators.CommonFixes
alias Pleroma.Web.ActivityPub.ObjectValidators.CommonValidations
alias Pleroma.Web.ActivityPub.ObjectValidators.QuestionOptionsValidator
+ alias Pleroma.Web.ActivityPub.ObjectValidators.TagValidator
alias Pleroma.Web.ActivityPub.Transmogrifier
import Ecto.Changeset
field(:cc, ObjectValidators.Recipients, default: [])
field(:bto, ObjectValidators.Recipients, default: [])
field(:bcc, ObjectValidators.Recipients, default: [])
- # TODO: Write type
- field(:tag, {:array, :map}, default: [])
+ embeds_many(:tag, TagValidator)
field(:type, :string)
field(:content, :string)
field(:context, :string)
data = fix(data)
struct
- |> cast(data, __schema__(:fields) -- [:anyOf, :oneOf, :attachment])
+ |> cast(data, __schema__(:fields) -- [:anyOf, :oneOf, :attachment, :tag])
|> cast_embed(:attachment)
|> cast_embed(:anyOf)
|> cast_embed(:oneOf)
+ |> cast_embed(:tag)
end
def validate_data(data_cng) do
--- /dev/null
+# Pleroma: A lightweight social networking server
+# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
+# SPDX-License-Identifier: AGPL-3.0-only
+
+defmodule Pleroma.Web.ActivityPub.ObjectValidators.TagValidator do
+ use Ecto.Schema
+
+ alias Pleroma.EctoType.ActivityPub.ObjectValidators
+
+ import Ecto.Changeset
+
+ @primary_key false
+ embedded_schema do
+ # Common
+ field(:type, :string)
+ field(:name, :string)
+
+ # Mention, Hashtag
+ field(:href, ObjectValidators.Uri)
+
+ # Emoji
+ embeds_one :icon, IconObjectValidator, primary_key: false do
+ field(:type, :string)
+ field(:url, ObjectValidators.Uri)
+ end
+
+ field(:updated, ObjectValidators.DateTime)
+ field(:id, ObjectValidators.Uri)
+ end
+
+ def cast_and_validate(data) do
+ data
+ |> cast_data()
+ end
+
+ def cast_data(data) do
+ %__MODULE__{}
+ |> changeset(data)
+ end
+
+ def changeset(struct, %{"type" => "Mention"} = data) do
+ struct
+ |> cast(data, [:type, :name, :href])
+ |> validate_required([:type, :href])
+ end
+
+ def changeset(struct, %{"type" => "Hashtag", "name" => name} = data) do
+ name =
+ cond do
+ "#" <> name -> name
+ name -> name
+ end
+ |> String.downcase()
+
+ data = Map.put(data, "name", name)
+
+ struct
+ |> cast(data, [:type, :name, :href])
+ |> validate_required([:type, :name])
+ end
+
+ def changeset(struct, %{"type" => "Emoji"} = data) do
+ data = Map.put(data, "name", String.trim(data["name"], ":"))
+
+ struct
+ |> cast(data, [:type, :name, :updated, :id])
+ |> cast_embed(:icon, with: &icon_changeset/2)
+ |> validate_required([:type, :name, :icon])
+ end
+
+ def icon_changeset(struct, data) do
+ struct
+ |> cast(data, [:type, :url])
+ |> validate_inclusion(:type, ~w[Image])
+ |> validate_required([:type, :url])
+ end
+end