akkoma/lib/pleroma/web/mastodon_api/mastodon_socket.ex

42 lines
1.1 KiB
Elixir
Raw Normal View History

2017-11-11 13:59:25 +00:00
defmodule Pleroma.Web.MastodonAPI.MastodonSocket do
use Phoenix.Socket
2017-11-16 12:48:58 +00:00
alias Pleroma.Web.OAuth.Token
alias Pleroma.{User, Repo}
transport :streaming, Phoenix.Transports.WebSocket.Raw,
timeout: :infinity # We never receive data.
2017-11-11 13:59:25 +00:00
def connect(params, socket) do
2017-11-16 12:48:58 +00:00
with token when not is_nil(token) <- params["access_token"],
%Token{user_id: user_id} <- Repo.get_by(Token, token: token),
%User{} = user <- Repo.get(User, user_id),
stream when stream in ["public", "public:local", "user"] <- params["stream"] do
socket = socket
|> assign(:topic, params["stream"])
2017-11-16 12:48:58 +00:00
|> assign(:user, user)
Pleroma.Web.Streamer.add_socket(params["stream"], socket)
{:ok, socket}
else
2017-11-16 12:48:58 +00:00
_e -> :error
end
2017-11-11 13:59:25 +00:00
end
def id(socket), do: nil
def handle(:text, message, state) do
IO.inspect message
#| :ok
#| state
#| {:text, message}
#| {:text, message, state}
#| {:close, "Goodbye!"}
{:text, message}
end
def handle(:closed, reason, %{socket: socket}) do
topic = socket.assigns[:topic]
Pleroma.Web.Streamer.remove_socket(topic, socket)
2017-11-11 13:59:25 +00:00
end
end