architex/lib/matrix_server/room.ex

31 lines
677 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 __MODULE__
alias MatrixServerWeb.API.CreateRoom
alias Ecto.Multi
@primary_key {:id, :string, []}
schema "rooms" do
field :visibility, Ecto.Enum, values: [:public, :private]
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