Prepare to join servers
This commit is contained in:
parent
3afaf346c7
commit
984ac15084
12 changed files with 137 additions and 17 deletions
|
|
@ -4,11 +4,30 @@ defmodule BirdyChatWeb.Api.Messages.Controller do
|
|||
def create(conn, params) do
|
||||
case BirdyChat.Message.validate(params) do
|
||||
{:ok, changeset} ->
|
||||
case BirdyChat.MessageWriter.write(changeset.changes) do
|
||||
:ok ->
|
||||
case BirdyChat.Message.find_peer(changeset.changes) do
|
||||
{:ok, :local} ->
|
||||
case BirdyChat.MessageWriter.write(changeset.changes) do
|
||||
:ok ->
|
||||
conn
|
||||
|> put_status(:created)
|
||||
|> render(:create, message: changeset.changes)
|
||||
end
|
||||
|
||||
{:ok, peer_name} ->
|
||||
BirdyChatWeb.ServerChannel.broadcast!(peer_name, changeset.changes)
|
||||
|
||||
conn
|
||||
|> put_status(:created)
|
||||
|> render(:create, message: changeset.changes)
|
||||
|
||||
{:error, :not_found} ->
|
||||
changeset =
|
||||
changeset
|
||||
|> Ecto.Changeset.add_error(:to, "Unknown 'to' server")
|
||||
|
||||
conn
|
||||
|> put_status(:unprocessable_entity)
|
||||
|> render(:error, changeset: changeset)
|
||||
end
|
||||
|
||||
{:error, changeset} ->
|
||||
|
|
|
|||
|
|
@ -24,6 +24,13 @@ defmodule BirdyChatWeb.ServerChannel do
|
|||
end
|
||||
end
|
||||
|
||||
# This is how we send messages
|
||||
def broadcast!(peer, message) do
|
||||
channel = "server:#{peer}"
|
||||
encoded_message = :erlang.term_to_binary(message)
|
||||
BirdyChatWeb.Endpoint.broadcast!(channel, "new_message", {:binary, encoded_message})
|
||||
end
|
||||
|
||||
# Simple token-based authentication. Servers should use the same Phoenix secret key so they will
|
||||
# have the basis for generating tokens.
|
||||
defp authorised?(%{"token" => token}, server_id) do
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue