2017-11-11 14:59:25 +01:00
|
|
|
defmodule Pleroma.Web.MastodonAPI.MastodonSocket do
|
|
|
|
use Phoenix.Socket
|
|
|
|
|
2017-11-16 13:48:58 +01:00
|
|
|
alias Pleroma.Web.OAuth.Token
|
|
|
|
alias Pleroma.{User, Repo}
|
|
|
|
|
2017-11-11 20:00:11 +01:00
|
|
|
transport :streaming, Phoenix.Transports.WebSocket.Raw,
|
|
|
|
timeout: :infinity # We never receive data.
|
2017-11-11 14:59:25 +01:00
|
|
|
|
|
|
|
def connect(params, socket) do
|
2017-11-16 13:48:58 +01: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),
|
2017-11-16 16:49:51 +01:00
|
|
|
stream when stream in ["public", "public:local", "user"] <- params["stream"] do
|
2017-11-11 20:00:11 +01:00
|
|
|
socket = socket
|
|
|
|
|> assign(:topic, params["stream"])
|
2017-11-16 13:48:58 +01:00
|
|
|
|> assign(:user, user)
|
2017-11-11 20:00:11 +01:00
|
|
|
Pleroma.Web.Streamer.add_socket(params["stream"], socket)
|
|
|
|
{:ok, socket}
|
|
|
|
else
|
2017-11-16 13:48:58 +01:00
|
|
|
_e -> :error
|
2017-11-11 20:00:11 +01:00
|
|
|
end
|
2017-11-11 14:59:25 +01:00
|
|
|
end
|
|
|
|
|
2017-11-19 02:22:07 +01:00
|
|
|
def id(_), do: nil
|
2017-11-11 14:59:25 +01:00
|
|
|
|
2017-11-19 02:22:07 +01:00
|
|
|
def handle(:text, message, _state) do
|
2017-11-11 14:59:25 +01:00
|
|
|
IO.inspect message
|
|
|
|
#| :ok
|
|
|
|
#| state
|
|
|
|
#| {:text, message}
|
|
|
|
#| {:text, message, state}
|
|
|
|
#| {:close, "Goodbye!"}
|
|
|
|
{:text, message}
|
|
|
|
end
|
|
|
|
|
2017-11-19 02:22:07 +01:00
|
|
|
def handle(:closed, _, %{socket: socket}) do
|
2017-11-11 20:00:11 +01:00
|
|
|
topic = socket.assigns[:topic]
|
|
|
|
Pleroma.Web.Streamer.remove_socket(topic, socket)
|
2017-11-11 14:59:25 +01:00
|
|
|
end
|
|
|
|
end
|