2017-11-11 06:59:25 -07:00
|
|
|
defmodule Pleroma.Web.MastodonAPI.MastodonSocket do
|
|
|
|
use Phoenix.Socket
|
|
|
|
|
2017-11-16 05:48:58 -07:00
|
|
|
alias Pleroma.Web.OAuth.Token
|
|
|
|
alias Pleroma.{User, Repo}
|
|
|
|
|
2018-03-30 07:01:53 -06:00
|
|
|
transport(
|
|
|
|
:streaming,
|
|
|
|
Phoenix.Transports.WebSocket.Raw,
|
|
|
|
# We never receive data.
|
|
|
|
timeout: :infinity
|
|
|
|
)
|
2017-11-11 06:59:25 -07:00
|
|
|
|
|
|
|
def connect(params, socket) do
|
2017-11-16 05:48:58 -07: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),
|
2018-06-17 07:01:27 -06:00
|
|
|
stream
|
|
|
|
when stream in [
|
|
|
|
"public",
|
|
|
|
"public:local",
|
|
|
|
"public:media",
|
|
|
|
"public:local:media",
|
|
|
|
"user",
|
|
|
|
"direct",
|
2018-08-07 14:45:40 -06:00
|
|
|
"list",
|
|
|
|
"hashtag"
|
2018-06-17 07:01:27 -06:00
|
|
|
] <- params["stream"] do
|
2018-11-10 20:33:14 -07:00
|
|
|
topic =
|
|
|
|
case stream do
|
|
|
|
"hashtag" -> "hashtag:#{params["tag"]}"
|
|
|
|
"list" -> "list:#{params["list"]}"
|
|
|
|
_ -> stream
|
|
|
|
end
|
2018-05-30 07:33:37 -06:00
|
|
|
|
2018-03-30 07:01:53 -06:00
|
|
|
socket =
|
|
|
|
socket
|
2018-05-30 07:33:37 -06:00
|
|
|
|> assign(:topic, topic)
|
2018-03-30 07:01:53 -06:00
|
|
|
|> assign(:user, user)
|
|
|
|
|
2018-11-10 20:33:14 -07:00
|
|
|
Pleroma.Web.Streamer.add_socket(topic, socket)
|
2017-11-11 12:00:11 -07:00
|
|
|
{:ok, socket}
|
|
|
|
else
|
2017-11-16 05:48:58 -07:00
|
|
|
_e -> :error
|
2017-11-11 12:00:11 -07:00
|
|
|
end
|
2017-11-11 06:59:25 -07:00
|
|
|
end
|
|
|
|
|
2017-11-18 18:22:07 -07:00
|
|
|
def id(_), do: nil
|
2017-11-11 06:59:25 -07:00
|
|
|
|
2017-11-18 18:22:07 -07:00
|
|
|
def handle(:text, message, _state) do
|
2018-03-30 07:01:53 -06:00
|
|
|
# | :ok
|
|
|
|
# | state
|
|
|
|
# | {:text, message}
|
|
|
|
# | {:text, message, state}
|
|
|
|
# | {:close, "Goodbye!"}
|
2017-11-11 06:59:25 -07:00
|
|
|
{:text, message}
|
|
|
|
end
|
|
|
|
|
2017-11-18 18:22:07 -07:00
|
|
|
def handle(:closed, _, %{socket: socket}) do
|
2017-11-11 12:00:11 -07:00
|
|
|
topic = socket.assigns[:topic]
|
|
|
|
Pleroma.Web.Streamer.remove_socket(topic, socket)
|
2017-11-11 06:59:25 -07:00
|
|
|
end
|
|
|
|
end
|