architex/lib/matrix_server/room.ex

31 lines
726 B
Elixir
Raw Normal View History

2021-07-10 21:16:00 +00:00
defmodule MatrixServer.Room do
use Ecto.Schema
import Ecto.Changeset
alias MatrixServer.{Room, Event}
2021-07-10 21:16:00 +00:00
alias MatrixServerWeb.API.CreateRoom
@primary_key {:id, :string, []}
schema "rooms" do
field :visibility, Ecto.Enum, values: [:public, :private]
has_many :events, Event, foreign_key: :event_id
2021-07-10 21:16:00 +00:00
end
2021-07-17 15:38:20 +00:00
def changeset(room, params \\ %{}) do
cast(room, params, [:visibility])
2021-07-10 21:16:00 +00:00
end
2021-07-17 15:38:20 +00:00
def create_changeset(%CreateRoom{} = input) do
visibility = input.visibility || :public
%Room{}
|> changeset(%{visibility: visibility})
|> put_change(:id, generate_room_id())
2021-07-10 21:16:00 +00:00
end
2021-07-17 15:38:20 +00:00
def generate_room_id do
"!" <> MatrixServer.random_string(18) <> "@" <> MatrixServer.server_name()
2021-07-10 21:16:00 +00:00
end
end