972 B · text History 4b6fe3b
defmodule OhMyExOhIrcd.Scrobbles.RequestLog do
  @moduledoc """
  Append-only log of authenticated scrobble API requests and their
  outcome, for debugging from the channel's scrobble page. Only requests
  that pass token auth are logged (we know the channel then); a request
  with a bad URL or unknown/revoked token never reaches the controller,
  so an empty log points at the URL or token rather than the channel.
  """

  use Ecto.Schema
  import Ecto.Changeset

  alias OhMyExOhIrcd.Channels.ChannelRecord

  schema "scrobble_request_logs" do
    field :who, :string
    field :status, :string
    field :detail, :string

    belongs_to :channel, ChannelRecord

    timestamps(type: :utc_datetime, updated_at: false)
  end

  def changeset(record, attrs) do
    record
    |> cast(attrs, [:channel_id, :who, :status, :detail])
    |> validate_required([:channel_id, :status])
    |> validate_length(:who, max: 64)
    |> validate_length(:detail, max: 256)
  end
end