memEx/lib/memex/contexts/context.ex

113 lines
3.5 KiB
Elixir
Raw Normal View History

2022-07-25 20:11:08 -04:00
defmodule Memex.Contexts.Context do
2022-11-24 12:44:34 -05:00
@moduledoc """
Represents a document that synthesizes multiple concepts as defined by notes
into a single consideration
"""
2022-07-25 20:11:08 -04:00
use Ecto.Schema
import Ecto.Changeset
2022-11-26 14:51:18 -05:00
import MemexWeb.Gettext
2022-11-24 12:44:34 -05:00
alias Ecto.{Changeset, UUID}
alias Memex.{Accounts.User, Repo}
2022-07-25 20:11:08 -04:00
2023-04-13 23:29:29 -04:00
@derive {Phoenix.Param, key: :slug}
2022-12-20 19:15:08 -05:00
@derive {Jason.Encoder,
only: [
:slug,
:content,
:tags,
:visibility,
:inserted_at,
:updated_at
]}
2022-07-25 20:11:08 -04:00
@primary_key {:id, :binary_id, autogenerate: true}
@foreign_key_type :binary_id
schema "contexts" do
2022-11-26 14:51:18 -05:00
field :slug, :string
2022-07-25 20:11:08 -04:00
field :content, :string
2022-11-24 12:44:34 -05:00
field :tags, {:array, :string}
field :tags_string, :string, virtual: true
2022-07-25 20:11:08 -04:00
field :visibility, Ecto.Enum, values: [:public, :private, :unlisted]
2023-03-16 23:48:03 -04:00
field :user_id, :binary_id
2022-11-24 12:44:34 -05:00
2022-07-25 20:11:08 -04:00
timestamps()
end
2022-11-24 12:44:34 -05:00
@type t :: %__MODULE__{
2022-11-26 14:51:18 -05:00
slug: slug(),
2022-11-24 12:44:34 -05:00
content: String.t(),
tags: [String.t()] | nil,
2022-12-19 22:29:26 -05:00
tags_string: String.t() | nil,
2022-11-24 12:44:34 -05:00
visibility: :public | :private | :unlisted,
user_id: User.id(),
inserted_at: NaiveDateTime.t(),
updated_at: NaiveDateTime.t()
}
@type id :: UUID.t()
2022-11-26 14:51:18 -05:00
@type slug :: String.t()
2022-11-24 12:44:34 -05:00
@type changeset :: Changeset.t(t())
2022-07-25 20:11:08 -04:00
@doc false
2022-11-24 12:44:34 -05:00
@spec create_changeset(attrs :: map(), User.t()) :: changeset()
def create_changeset(attrs, %User{id: user_id}) do
%__MODULE__{}
2022-11-26 14:51:18 -05:00
|> cast(attrs, [:slug, :content, :tags, :visibility])
2022-11-24 12:44:34 -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, :user_id, :visibility])
|> unique_constraint(:slug)
|> unsafe_validate_unique(:slug, Repo)
2022-11-24 12:44:34 -05:00
end
@spec update_changeset(t(), attrs :: map(), User.t()) :: changeset()
def update_changeset(%{user_id: user_id} = note, attrs, %User{id: user_id}) do
note
2022-11-26 14:51:18 -05:00
|> cast(attrs, [:slug, :content, :tags, :visibility])
2022-11-24 12:44:34 -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, :visibility])
|> unique_constraint(:slug)
|> unsafe_validate_unique(:slug, Repo)
2022-07-25 20:11:08 -04:00
end
2022-11-24 12:44:34 -05:00
2022-12-19 22:29:26 -05:00
defp cast_tags_string(changeset, attrs) do
changeset
|> put_change(:tags_string, changeset |> get_field(:tags) |> get_tags_string())
|> cast(attrs, [:tags_string])
2023-11-04 22:07:25 -04:00
|> validate_format(:tags_string, ~r/^[\p{L}\p{N}\-\, ]+$/,
2022-12-19 22:29:26 -05:00
message:
dgettext(
"errors",
2023-11-04 22:07:25 -04:00
"invalid format: only numbers, letters and hyphen are accepted. tags must be comma or space delimited"
2022-12-19 22:29:26 -05:00
)
)
|> cast_tags()
end
defp cast_tags(%{valid?: false} = changeset), do: changeset
2022-11-24 12:44:34 -05:00
2022-12-19 22:29:26 -05:00
defp cast_tags(%{valid?: true} = changeset) do
tags = changeset |> get_field(:tags_string) |> process_tags()
changeset |> put_change(:tags, tags)
2022-11-24 12:44:34 -05:00
end
2022-12-19 22:29:26 -05:00
defp process_tags(tags_string) when tags_string |> is_binary() do
tags_string
2023-11-04 22:07:25 -04:00
|> String.split([",", " "], trim: true)
2022-12-19 22:29:26 -05:00
|> Enum.map(fn str -> str |> String.trim() end)
2023-11-04 22:07:25 -04:00
|> Enum.reject(fn str -> str in [nil, ""] end)
2022-12-19 22:29:26 -05:00
|> Enum.sort()
end
defp process_tags(_other_tags_string), do: []
@spec get_tags_string([String.t()] | nil) :: String.t()
def get_tags_string(nil), do: ""
def get_tags_string(tags) when tags |> is_list(), do: tags |> Enum.join(",")
2022-07-25 20:11:08 -04:00
end