2022-07-25 20:08:40 -04:00
|
|
|
defmodule Memex.Notes.Note do
|
2022-11-17 22:38:52 -05:00
|
|
|
@moduledoc """
|
|
|
|
Schema for a user-written note
|
|
|
|
"""
|
2022-07-25 20:08:40 -04:00
|
|
|
use Ecto.Schema
|
|
|
|
import Ecto.Changeset
|
2022-11-26 14:51:18 -05:00
|
|
|
import MemexWeb.Gettext
|
2022-11-17 22:38:52 -05:00
|
|
|
alias Ecto.{Changeset, UUID}
|
2022-11-28 00:20:57 -05:00
|
|
|
alias Memex.{Accounts.User, Repo}
|
2022-07-25 20:08:40 -04:00
|
|
|
|
|
|
|
@primary_key {:id, :binary_id, autogenerate: true}
|
|
|
|
@foreign_key_type :binary_id
|
|
|
|
schema "notes" do
|
2022-11-26 14:51:18 -05:00
|
|
|
field :slug, :string
|
2022-07-25 20:08:40 -04:00
|
|
|
field :content, :string
|
2022-11-17 22:38:52 -05:00
|
|
|
field :tags, {:array, :string}
|
|
|
|
field :tags_string, :string, virtual: true
|
2022-07-25 20:08:40 -04:00
|
|
|
field :visibility, Ecto.Enum, values: [:public, :private, :unlisted]
|
|
|
|
|
2022-11-17 22:38:52 -05:00
|
|
|
belongs_to :user, User
|
|
|
|
|
2022-07-25 20:08:40 -04:00
|
|
|
timestamps()
|
|
|
|
end
|
|
|
|
|
2022-11-24 12:43:34 -05:00
|
|
|
@type t :: %__MODULE__{
|
2022-11-26 14:51:18 -05:00
|
|
|
slug: slug(),
|
2022-11-24 12:43:34 -05:00
|
|
|
content: String.t(),
|
|
|
|
tags: [String.t()] | nil,
|
|
|
|
tags_string: String.t(),
|
|
|
|
visibility: :public | :private | :unlisted,
|
|
|
|
user: User.t() | Ecto.Association.NotLoaded.t(),
|
|
|
|
user_id: User.id(),
|
|
|
|
inserted_at: NaiveDateTime.t(),
|
|
|
|
updated_at: NaiveDateTime.t()
|
|
|
|
}
|
2022-11-17 22:38:52 -05:00
|
|
|
@type id :: UUID.t()
|
2022-11-26 14:51:18 -05:00
|
|
|
@type slug :: String.t()
|
2022-11-17 22:38:52 -05:00
|
|
|
@type changeset :: Changeset.t(t())
|
|
|
|
|
2022-07-25 20:08:40 -04:00
|
|
|
@doc false
|
2022-11-17 22:38:52 -05:00
|
|
|
@spec create_changeset(attrs :: map(), User.t()) :: changeset()
|
|
|
|
def create_changeset(attrs, %User{id: user_id}) do
|
2022-11-24 12:43:34 -05:00
|
|
|
%__MODULE__{}
|
2022-11-26 14:51:18 -05:00
|
|
|
|> cast(attrs, [:slug, :content, :tags, :visibility])
|
2022-11-17 22:38:52 -05:00
|
|
|
|> change(user_id: user_id)
|
|
|
|
|> cast_tags_string(attrs)
|
2022-11-26 14:51:18 -05:00
|
|
|
|> validate_format(:slug, ~r/^[\p{L}\p{N}\-]+$/,
|
|
|
|
message: dgettext("errors", "invalid format: only numbers, letters and hyphen are accepted")
|
|
|
|
)
|
|
|
|
|> validate_required([:slug, :content, :user_id, :visibility])
|
2022-11-28 00:20:57 -05:00
|
|
|
|> unique_constraint(:slug)
|
|
|
|
|> unsafe_validate_unique(:slug, Repo)
|
2022-11-17 22:38:52 -05:00
|
|
|
end
|
|
|
|
|
2022-11-24 12:43:34 -05:00
|
|
|
@spec update_changeset(t(), attrs :: map(), User.t()) :: changeset()
|
2022-11-17 22:38:52 -05:00
|
|
|
def update_changeset(%{user_id: user_id} = note, attrs, %User{id: user_id}) do
|
2022-07-25 20:08:40 -04:00
|
|
|
note
|
2022-11-26 14:51:18 -05:00
|
|
|
|> cast(attrs, [:slug, :content, :tags, :visibility])
|
2022-11-17 22:38:52 -05:00
|
|
|
|> cast_tags_string(attrs)
|
2022-11-26 14:51:18 -05:00
|
|
|
|> validate_format(:slug, ~r/^[\p{L}\p{N}\-]+$/,
|
|
|
|
message: dgettext("errors", "invalid format: only numbers, letters and hyphen are accepted")
|
|
|
|
)
|
|
|
|
|> validate_required([:slug, :content, :visibility])
|
2022-11-28 00:20:57 -05:00
|
|
|
|> unique_constraint(:slug)
|
|
|
|
|> unsafe_validate_unique(:slug, Repo)
|
2022-07-25 20:08:40 -04:00
|
|
|
end
|
2022-11-17 22:38:52 -05:00
|
|
|
|
|
|
|
defp cast_tags_string(changeset, %{"tags_string" => tags_string})
|
|
|
|
when tags_string |> is_binary() do
|
|
|
|
tags =
|
|
|
|
tags_string
|
|
|
|
|> String.split(",", trim: true)
|
|
|
|
|> Enum.map(fn str -> str |> String.trim() end)
|
|
|
|
|> Enum.sort()
|
|
|
|
|
|
|
|
changeset |> change(tags: tags)
|
|
|
|
end
|
|
|
|
|
|
|
|
defp cast_tags_string(changeset, _attrs), do: changeset
|
2022-07-25 20:08:40 -04:00
|
|
|
end
|