Users can now register beacon monitors on the settings page. Each monitor gets a unique random token the remote program will use to authenticate its reports. Name is the only user-supplied field for now; more will be added as the monitor protocol is defined. Also adds :gen_smtp to deps. Swoosh's SMTP adapter depends on :mimemail which lives in that package, and production was 500'ing when trying to send confirmation emails without it.
36 lines
962 B
Elixir
36 lines
962 B
Elixir
defmodule Microwaveprop.BeaconMonitors.BeaconMonitor do
|
|
@moduledoc """
|
|
A beacon monitor is a remote station running the monitor program
|
|
that reports beacon reception data. Each monitor is owned by a user
|
|
and identified by a random token the program uses to authenticate.
|
|
"""
|
|
|
|
use Ecto.Schema
|
|
|
|
import Ecto.Changeset
|
|
|
|
alias Microwaveprop.Accounts.User
|
|
|
|
@primary_key {:id, :binary_id, autogenerate: true}
|
|
@foreign_key_type :binary_id
|
|
schema "beacon_monitors" do
|
|
field :name, :string
|
|
field :token, :string
|
|
field :last_seen_at, :utc_datetime
|
|
|
|
belongs_to :user, User
|
|
|
|
timestamps(type: :utc_datetime)
|
|
end
|
|
|
|
@doc """
|
|
Changeset for user-controlled fields (name only for now).
|
|
Token and user_id are assigned by the context, not cast from user input.
|
|
"""
|
|
def changeset(monitor, attrs) do
|
|
monitor
|
|
|> cast(attrs, [:name])
|
|
|> validate_required([:name])
|
|
|> validate_length(:name, min: 1, max: 100)
|
|
end
|
|
end
|