forked from AkkomaGang/akkoma
mastodon api: websocket: update code for cowboy 2.x
This commit is contained in:
parent
e10ca35b3d
commit
bc53dff5b6
1 changed files with 18 additions and 21 deletions
|
@ -9,7 +9,7 @@ defmodule Pleroma.Web.MastodonAPI.WebsocketHandler do
|
||||||
alias Pleroma.Repo
|
alias Pleroma.Repo
|
||||||
alias Pleroma.User
|
alias Pleroma.User
|
||||||
|
|
||||||
@behaviour :cowboy_websocket_handler
|
@behaviour :cowboy_websocket
|
||||||
|
|
||||||
@streams [
|
@streams [
|
||||||
"public",
|
"public",
|
||||||
|
@ -23,40 +23,37 @@ defmodule Pleroma.Web.MastodonAPI.WebsocketHandler do
|
||||||
]
|
]
|
||||||
@anonymous_streams ["public", "public:local", "hashtag"]
|
@anonymous_streams ["public", "public:local", "hashtag"]
|
||||||
|
|
||||||
# Handled by periodic keepalive in Pleroma.Web.Streamer.
|
def init(req, _state) do
|
||||||
@timeout :infinity
|
|
||||||
|
|
||||||
def init(_type, _req, _opts) do
|
|
||||||
{:upgrade, :protocol, :cowboy_websocket}
|
|
||||||
end
|
|
||||||
|
|
||||||
def websocket_init(_type, req, _opts) do
|
|
||||||
with {qs, req} <- :cowboy_req.qs(req),
|
with {qs, req} <- :cowboy_req.qs(req),
|
||||||
params <- :cow_qs.parse_qs(qs),
|
params <- :cow_qs.parse_qs(qs),
|
||||||
access_token <- List.keyfind(params, "access_token", 0),
|
access_token <- List.keyfind(params, "access_token", 0),
|
||||||
{_, stream} <- List.keyfind(params, "stream", 0),
|
{_, stream} <- List.keyfind(params, "stream", 0),
|
||||||
{:ok, user} <- allow_request(stream, access_token),
|
{:ok, user} <- allow_request(stream, access_token),
|
||||||
topic when is_binary(topic) <- expand_topic(stream, params) do
|
topic when is_binary(topic) <- expand_topic(stream, params) do
|
||||||
send(self(), :subscribe)
|
{:cowboy_websocket, req, %{user: user, topic: topic}}
|
||||||
{:ok, req, %{user: user, topic: topic}, @timeout}
|
|
||||||
else
|
else
|
||||||
{:error, code} ->
|
{:error, code} ->
|
||||||
Logger.debug("#{__MODULE__} denied connection: #{inspect(code)} - #{inspect(req)}")
|
Logger.debug("#{__MODULE__} denied connection: #{inspect(code)} - #{inspect(req)}")
|
||||||
{:ok, req} = :cowboy_req.reply(code, req)
|
{:ok, req} = :cowboy_req.reply(code, req)
|
||||||
{:shutdown, req}
|
{:stop, req}
|
||||||
|
|
||||||
error ->
|
error ->
|
||||||
Logger.debug("#{__MODULE__} denied connection: #{inspect(error)} - #{inspect(req)}")
|
Logger.debug("#{__MODULE__} denied connection: #{inspect(error)} - #{inspect(req)}")
|
||||||
{:shutdown, req}
|
{:stop, req}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# We never receive messages.
|
def websocket_init(state) do
|
||||||
def websocket_handle(_frame, req, state) do
|
send(self(), :subscribe)
|
||||||
{:ok, req, state}
|
{:ok, state}
|
||||||
end
|
end
|
||||||
|
|
||||||
def websocket_info(:subscribe, req, state) do
|
# We never receive messages.
|
||||||
|
def websocket_handle(_frame, state) do
|
||||||
|
{:ok, state}
|
||||||
|
end
|
||||||
|
|
||||||
|
def websocket_info(:subscribe, state) do
|
||||||
Logger.debug(
|
Logger.debug(
|
||||||
"#{__MODULE__} accepted websocket connection for user #{
|
"#{__MODULE__} accepted websocket connection for user #{
|
||||||
(state.user || %{id: "anonymous"}).id
|
(state.user || %{id: "anonymous"}).id
|
||||||
|
@ -64,14 +61,14 @@ def websocket_info(:subscribe, req, state) do
|
||||||
)
|
)
|
||||||
|
|
||||||
Pleroma.Web.Streamer.add_socket(state.topic, streamer_socket(state))
|
Pleroma.Web.Streamer.add_socket(state.topic, streamer_socket(state))
|
||||||
{:ok, req, state}
|
{:ok, state}
|
||||||
end
|
end
|
||||||
|
|
||||||
def websocket_info({:text, message}, req, state) do
|
def websocket_info({:text, message}, state) do
|
||||||
{:reply, {:text, message}, req, state}
|
{:reply, {:text, message}, state}
|
||||||
end
|
end
|
||||||
|
|
||||||
def websocket_terminate(reason, _req, state) do
|
def terminate(reason, _req, state) do
|
||||||
Logger.debug(
|
Logger.debug(
|
||||||
"#{__MODULE__} terminating websocket connection for user #{
|
"#{__MODULE__} terminating websocket connection for user #{
|
||||||
(state.user || %{id: "anonymous"}).id
|
(state.user || %{id: "anonymous"}).id
|
||||||
|
|
Loading…
Reference in a new issue