forked from AkkomaGang/akkoma
124 lines
2.8 KiB
Elixir
124 lines
2.8 KiB
Elixir
# 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 changeset(struct, %{"type" => "Book"} = data) do
|
|
data =
|
|
if Map.has_key?(data, "title") do
|
|
Map.put(data, "name", data["title"])
|
|
else
|
|
data
|
|
end
|
|
|
|
struct
|
|
|> cast(data, [:type, :name])
|
|
|> validate_required([:type, :name])
|
|
end
|
|
|
|
def changeset(struct, %{"type" => "Edition"} = data) do
|
|
# Sometimes it's title, sometimes name
|
|
# Don't ask me.
|
|
data =
|
|
if Map.has_key?(data, "title") do
|
|
Map.put(data, "name", data["title"])
|
|
else
|
|
data
|
|
end
|
|
|
|
struct
|
|
|> cast(data, [:type, :name])
|
|
|> validate_required([:type, :name])
|
|
end
|
|
|
|
def changeset(struct, %{"type" => "Work"} = data) do
|
|
data =
|
|
if Map.has_key?(data, "title") do
|
|
Map.put(data, "name", data["title"])
|
|
else
|
|
data
|
|
end
|
|
|
|
struct
|
|
|> cast(data, [:type, :name])
|
|
|> validate_required([:type, :name])
|
|
end
|
|
|
|
def changeset(struct, %{"type" => "Author"} = data) do
|
|
struct
|
|
|> cast(data, [:type, :name])
|
|
|> validate_required([:type, :name])
|
|
end
|
|
|
|
def icon_changeset(struct, data) do
|
|
struct
|
|
|> cast(data, [:type, :url])
|
|
|> validate_inclusion(:type, ~w[Image])
|
|
|> validate_required([:type, :url])
|
|
end
|
|
end
|