mastodon websocket: use pattern match to get query data, robustly handle errors

This commit is contained in:
William Pitcock 2019-02-28 16:02:48 +00:00
parent 9aec00d711
commit f1d37a5e23

View file

@ -23,9 +23,8 @@ defmodule Pleroma.Web.MastodonAPI.WebsocketHandler do
] ]
@anonymous_streams ["public", "public:local", "hashtag"] @anonymous_streams ["public", "public:local", "hashtag"]
def init(req, _state) do def init(%{qs: qs} = req, _state) do
with {qs, req} <- :cowboy_req.qs(req), with 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),
@ -39,6 +38,7 @@ def init(req, _state) do
error -> error ->
Logger.debug("#{__MODULE__} denied connection: #{inspect(error)} - #{inspect(req)}") Logger.debug("#{__MODULE__} denied connection: #{inspect(error)} - #{inspect(req)}")
{:ok, req} = :cowboy_req.reply(400, req)
{:stop, req} {:stop, req}
end end
end end