- Anonymous users can submit beacons (held pending admin approval) - Added notes field to beacons (textarea on form, shown on detail page) - Added Beacons link to /map sidebar nav (mobile + desktop), removed stale Rover Planner link - Moved /backfill to /admin/backfill under the admin live_session - Beacon detail map zooms in two extra levels after fitBounds
111 lines
2.7 KiB
Elixir
111 lines
2.7 KiB
Elixir
defmodule Microwaveprop.Beacons do
|
|
@moduledoc """
|
|
The Beacons context. Anyone can submit a beacon — authenticated or not —
|
|
but submissions are held as unapproved until an admin approves them.
|
|
Only approved beacons appear in the public list.
|
|
"""
|
|
|
|
import Ecto.Query, warn: false
|
|
|
|
alias Microwaveprop.Accounts.User
|
|
alias Microwaveprop.Beacons.Beacon
|
|
alias Microwaveprop.Repo
|
|
|
|
@topic "beacons"
|
|
|
|
@doc """
|
|
Subscribes to beacon change notifications. Messages:
|
|
|
|
* `{:created, %Beacon{}}`
|
|
* `{:updated, %Beacon{}}`
|
|
* `{:deleted, %Beacon{}}`
|
|
"""
|
|
def subscribe_beacons do
|
|
Phoenix.PubSub.subscribe(Microwaveprop.PubSub, @topic)
|
|
end
|
|
|
|
defp broadcast(message) do
|
|
Phoenix.PubSub.broadcast(Microwaveprop.PubSub, @topic, message)
|
|
end
|
|
|
|
@doc "Returns approved beacons ordered by frequency."
|
|
def list_beacons do
|
|
Repo.all(
|
|
from b in Beacon,
|
|
where: b.approved == true,
|
|
order_by: [asc: b.frequency_mhz, asc: b.callsign]
|
|
)
|
|
end
|
|
|
|
@doc "Returns unapproved beacons awaiting admin review."
|
|
def list_pending_beacons do
|
|
Repo.all(
|
|
from b in Beacon,
|
|
where: b.approved == false,
|
|
order_by: [asc: b.inserted_at]
|
|
)
|
|
end
|
|
|
|
@doc "Gets a single beacon. Raises if not found."
|
|
def get_beacon!(id), do: Repo.get!(Beacon, id)
|
|
|
|
@doc """
|
|
Creates a beacon. When a user is provided they are recorded as the
|
|
creator; anonymous submissions pass `nil` and leave `user_id` unset.
|
|
"""
|
|
def create_beacon(user, attrs)
|
|
|
|
def create_beacon(%User{} = user, attrs) do
|
|
%Beacon{user_id: user.id}
|
|
|> Beacon.changeset(attrs)
|
|
|> Repo.insert()
|
|
|> broadcast_if_ok(:created)
|
|
end
|
|
|
|
def create_beacon(nil, attrs) do
|
|
%Beacon{}
|
|
|> Beacon.changeset(attrs)
|
|
|> Repo.insert()
|
|
|> broadcast_if_ok(:created)
|
|
end
|
|
|
|
@doc "Updates a beacon."
|
|
def update_beacon(%Beacon{} = beacon, attrs) do
|
|
beacon
|
|
|> Beacon.changeset(attrs)
|
|
|> Repo.update()
|
|
|> broadcast_if_ok(:updated)
|
|
end
|
|
|
|
@doc "Marks a beacon as approved, making it visible in the public list."
|
|
def approve_beacon(%Beacon{} = beacon) do
|
|
beacon
|
|
|> Ecto.Changeset.change(approved: true)
|
|
|> Repo.update()
|
|
|> broadcast_if_ok(:updated)
|
|
end
|
|
|
|
@doc "Deletes a beacon."
|
|
def delete_beacon(%Beacon{} = beacon) do
|
|
case Repo.delete(beacon) do
|
|
{:ok, beacon} ->
|
|
broadcast({:deleted, beacon})
|
|
{:ok, beacon}
|
|
|
|
other ->
|
|
other
|
|
end
|
|
end
|
|
|
|
@doc "Returns an `%Ecto.Changeset{}` for tracking beacon changes."
|
|
def change_beacon(%Beacon{} = beacon, attrs \\ %{}) do
|
|
Beacon.changeset(beacon, attrs)
|
|
end
|
|
|
|
defp broadcast_if_ok({:ok, beacon} = result, type) do
|
|
broadcast({type, beacon})
|
|
result
|
|
end
|
|
|
|
defp broadcast_if_ok(other, _type), do: other
|
|
end
|