1.0 KiB · text History 4b6fe3b
defmodule OhMyExOhIrcd.Channels.ChannelTopic do
  @moduledoc """
  Persisted channel topic, decoupled from registration.

  Unlike `ChannelRecord` (which exists only for ChanServ-registered
  channels and gates founder/modes/feature configs), a `ChannelTopic`
  row is written for *any* channel whose topic is set — so topics
  outlive the in-memory `Channel` singleton across restarts and deploys.
  Names are stored downcased.
  """

  use Ecto.Schema
  import Ecto.Changeset

  schema "channel_topics" do
    field :name, :string
    field :topic, :string, default: ""
    field :set_by, :string, default: ""
    # Unix seconds, mirroring the in-memory `topic_set_at` (RPL 333).
    field :set_at, :integer, default: 0

    timestamps(type: :utc_datetime)
  end

  def changeset(record, attrs) do
    record
    |> cast(attrs, [:name, :topic, :set_by, :set_at])
    |> validate_required([:name])
    |> update_change(:name, &String.downcase/1)
    |> validate_length(:topic, max: 390)
    |> unique_constraint(:name)
  end
end