2018-12-23 20:04:54 +00:00
|
|
|
# Pleroma: A lightweight social networking server
|
2023-01-02 20:38:50 +00:00
|
|
|
# Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
|
2018-12-23 20:04:54 +00:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2018-12-17 13:39:59 +00:00
|
|
|
defmodule Pleroma.Web.MastodonAPI.WebsocketHandler do
|
|
|
|
require Logger
|
|
|
|
|
2019-02-09 15:16:26 +00:00
|
|
|
alias Pleroma.Repo
|
|
|
|
alias Pleroma.User
|
2019-03-05 02:52:23 +00:00
|
|
|
alias Pleroma.Web.OAuth.Token
|
2019-09-16 10:03:37 +00:00
|
|
|
alias Pleroma.Web.Streamer
|
2023-04-01 02:55:52 +00:00
|
|
|
alias Pleroma.Web.StreamerView
|
2018-12-17 13:39:59 +00:00
|
|
|
|
2024-02-14 00:21:45 +00:00
|
|
|
@behaviour Phoenix.Socket.Transport
|
2018-12-17 13:39:59 +00:00
|
|
|
|
2020-05-11 14:28:53 +00:00
|
|
|
# Client ping period.
|
|
|
|
@tick :timer.seconds(30)
|
2019-10-15 10:10:22 +00:00
|
|
|
|
2024-02-14 00:21:45 +00:00
|
|
|
@impl Phoenix.Socket.Transport
|
|
|
|
def child_spec(_opts), do: :ignore
|
|
|
|
|
|
|
|
# This only prepares the connection and is not in the process yet
|
|
|
|
@impl Phoenix.Socket.Transport
|
|
|
|
def connect(%{params: params} = transport_info) do
|
|
|
|
with access_token <- Map.get(params, "access_token"),
|
|
|
|
{:ok, user, oauth_token} <- authenticate_request(access_token),
|
|
|
|
{:ok, topic} <-
|
|
|
|
Streamer.get_topic(params["stream"], user, oauth_token, params) do
|
2023-04-01 01:47:37 +00:00
|
|
|
topics =
|
|
|
|
if topic do
|
|
|
|
[topic]
|
|
|
|
else
|
|
|
|
[]
|
|
|
|
end
|
|
|
|
|
2024-02-14 00:21:45 +00:00
|
|
|
state = %{
|
|
|
|
user: user,
|
|
|
|
topics: topics,
|
|
|
|
oauth_token: oauth_token,
|
|
|
|
count: 0,
|
|
|
|
timer: nil
|
|
|
|
}
|
|
|
|
|
|
|
|
{:ok, state}
|
2018-12-17 13:39:59 +00:00
|
|
|
else
|
2020-05-12 16:04:47 +00:00
|
|
|
{:error, :bad_topic} ->
|
2024-02-14 00:21:45 +00:00
|
|
|
Logger.debug("#{__MODULE__} bad topic #{inspect(transport_info)}")
|
|
|
|
|
|
|
|
{:error, :bad_topic}
|
2018-12-17 13:39:59 +00:00
|
|
|
|
2020-05-12 16:04:47 +00:00
|
|
|
{:error, :unauthorized} ->
|
2024-02-14 00:21:45 +00:00
|
|
|
Logger.debug("#{__MODULE__} authentication error: #{inspect(transport_info)}")
|
|
|
|
{:error, :unauthorized}
|
2018-12-17 13:39:59 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-02-14 00:21:45 +00:00
|
|
|
# All subscriptions/links and messages cannot be created
|
|
|
|
# until the processed is launched with init/1
|
|
|
|
@impl Phoenix.Socket.Transport
|
|
|
|
def init(state) do
|
2023-04-01 01:47:37 +00:00
|
|
|
Enum.each(state.topics, fn topic -> Streamer.add_socket(topic, state.oauth_token) end)
|
2020-05-11 14:28:53 +00:00
|
|
|
|
2024-02-14 00:21:45 +00:00
|
|
|
Process.send_after(self(), :ping, @tick)
|
2019-02-28 15:17:01 +00:00
|
|
|
|
2024-02-14 00:21:45 +00:00
|
|
|
{:ok, state}
|
|
|
|
end
|
2020-09-10 10:47:53 +00:00
|
|
|
|
2024-02-14 00:21:45 +00:00
|
|
|
@impl Phoenix.Socket.Transport
|
|
|
|
def handle_in({text, [opcode: :text]}, state) do
|
2023-04-01 02:55:52 +00:00
|
|
|
with {:ok, %{} = event} <- Jason.decode(text) do
|
|
|
|
handle_client_event(event, state)
|
|
|
|
else
|
|
|
|
_ ->
|
|
|
|
Logger.error("#{__MODULE__} received non-JSON event: #{inspect(text)}")
|
|
|
|
{:ok, state}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-02-14 00:21:45 +00:00
|
|
|
def handle_in(frame, state) do
|
2020-05-11 14:28:53 +00:00
|
|
|
Logger.error("#{__MODULE__} received frame: #{inspect(frame)}")
|
2019-02-28 15:17:01 +00:00
|
|
|
{:ok, state}
|
2018-12-17 13:39:59 +00:00
|
|
|
end
|
|
|
|
|
2024-02-14 00:21:45 +00:00
|
|
|
@impl Phoenix.Socket.Transport
|
|
|
|
def handle_info({:render_with_user, view, template, item, topic}, state) do
|
2020-05-07 09:13:32 +00:00
|
|
|
user = %User{} = User.get_cached_by_ap_id(state.user.ap_id)
|
2018-12-17 13:39:59 +00:00
|
|
|
|
2020-05-07 09:13:32 +00:00
|
|
|
unless Streamer.filtered_by_user?(user, item) do
|
2024-02-14 00:21:45 +00:00
|
|
|
message = view.render(template, item, user, topic)
|
|
|
|
{:push, {:text, message}, %{state | user: user}}
|
2020-05-07 09:13:32 +00:00
|
|
|
else
|
|
|
|
{:ok, state}
|
|
|
|
end
|
2018-12-17 13:39:59 +00:00
|
|
|
end
|
|
|
|
|
2024-02-14 00:21:45 +00:00
|
|
|
def handle_info({:text, text}, state) do
|
|
|
|
{:push, {:text, text}, state}
|
2018-12-17 13:39:59 +00:00
|
|
|
end
|
|
|
|
|
2024-02-14 00:21:45 +00:00
|
|
|
def handle_info(:ping, state) do
|
|
|
|
Process.send_after(self(), :ping, @tick)
|
|
|
|
|
|
|
|
{:push, {:ping, ""}, state}
|
2020-05-11 14:28:53 +00:00
|
|
|
end
|
|
|
|
|
2024-02-14 00:21:45 +00:00
|
|
|
def handle_info(:close, state) do
|
|
|
|
{:stop, {:closed, 'connection closed by server'}, state}
|
2022-08-19 17:19:38 +00:00
|
|
|
end
|
|
|
|
|
2024-02-14 00:21:45 +00:00
|
|
|
def handle_info(msg, state) do
|
|
|
|
Logger.debug("#{__MODULE__} received info: #{inspect(msg)}")
|
2020-09-10 10:48:24 +00:00
|
|
|
|
2024-02-14 00:21:45 +00:00
|
|
|
{:ok, state}
|
|
|
|
end
|
|
|
|
|
|
|
|
@impl Phoenix.Socket.Transport
|
|
|
|
def terminate(reason, state) do
|
2018-12-17 13:39:59 +00:00
|
|
|
Logger.debug(
|
2024-02-14 00:21:45 +00:00
|
|
|
"#{__MODULE__} terminating websocket connection for user #{(state.user || %{id: "anonymous"}).id}, topics #{state.topics || "?"}: #{inspect(reason)})"
|
2018-12-17 13:39:59 +00:00
|
|
|
)
|
|
|
|
|
2023-04-01 01:47:37 +00:00
|
|
|
Enum.each(state.topics, fn topic -> Streamer.remove_socket(topic) end)
|
2018-12-17 13:39:59 +00:00
|
|
|
:ok
|
|
|
|
end
|
|
|
|
|
|
|
|
# Public streams without authentication.
|
2024-02-14 00:21:45 +00:00
|
|
|
defp authenticate_request(nil) do
|
2020-09-19 16:16:55 +00:00
|
|
|
{:ok, nil, nil}
|
2018-12-17 13:39:59 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# Authenticated streams.
|
2024-02-14 00:21:45 +00:00
|
|
|
defp authenticate_request(access_token) do
|
|
|
|
with oauth_token = %Token{user_id: user_id} <- Repo.get_by(Token, token: access_token),
|
2019-04-22 07:20:43 +00:00
|
|
|
user = %User{} <- User.get_cached_by_id(user_id) do
|
2020-09-19 16:16:55 +00:00
|
|
|
{:ok, user, oauth_token}
|
2018-12-17 13:39:59 +00:00
|
|
|
else
|
2020-05-12 16:04:47 +00:00
|
|
|
_ -> {:error, :unauthorized}
|
2018-12-17 13:39:59 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-04-01 02:55:52 +00:00
|
|
|
defp handle_client_event(%{"type" => "subscribe", "stream" => _topic} = params, state) do
|
|
|
|
with {_, {:ok, topic}} <-
|
|
|
|
{:topic, Streamer.get_topic(params["stream"], state.user, state.oauth_token, params)},
|
|
|
|
{_, false} <- {:subscribed, topic in state.topics} do
|
|
|
|
Streamer.add_socket(topic, state.oauth_token)
|
|
|
|
|
2024-02-14 00:21:45 +00:00
|
|
|
message =
|
|
|
|
StreamerView.render("pleroma_respond.json", %{type: "subscribe", result: "success"})
|
|
|
|
|
|
|
|
{:reply, :ok, {:text, message}, %{state | topics: [topic | state.topics]}}
|
2023-04-01 02:55:52 +00:00
|
|
|
else
|
|
|
|
{:subscribed, true} ->
|
2024-02-14 00:21:45 +00:00
|
|
|
message =
|
|
|
|
StreamerView.render("pleroma_respond.json", %{type: "subscribe", result: "ignored"})
|
|
|
|
|
|
|
|
{:reply, :error, {:text, message}, state}
|
2023-04-01 02:55:52 +00:00
|
|
|
|
|
|
|
{:topic, {:error, error}} ->
|
2024-02-14 00:21:45 +00:00
|
|
|
message =
|
|
|
|
StreamerView.render("pleroma_respond.json", %{
|
|
|
|
type: "subscribe",
|
|
|
|
result: "error",
|
|
|
|
error: error
|
|
|
|
})
|
|
|
|
|
|
|
|
{:reply, :error, {:text, message}, state}
|
2023-04-01 02:55:52 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defp handle_client_event(%{"type" => "unsubscribe", "stream" => _topic} = params, state) do
|
|
|
|
with {_, {:ok, topic}} <-
|
|
|
|
{:topic, Streamer.get_topic(params["stream"], state.user, state.oauth_token, params)},
|
|
|
|
{_, true} <- {:subscribed, topic in state.topics} do
|
|
|
|
Streamer.remove_socket(topic)
|
|
|
|
|
2024-02-14 00:21:45 +00:00
|
|
|
message =
|
|
|
|
StreamerView.render("pleroma_respond.json", %{type: "unsubscribe", result: "success"})
|
|
|
|
|
|
|
|
{:reply, :ok, {:text, message}, %{state | topics: List.delete(state.topics, topic)}}
|
2023-04-01 02:55:52 +00:00
|
|
|
else
|
|
|
|
{:subscribed, false} ->
|
2024-02-14 00:21:45 +00:00
|
|
|
message =
|
|
|
|
StreamerView.render("pleroma_respond.json", %{type: "unsubscribe", result: "ignored"})
|
|
|
|
|
|
|
|
{:reply, :error, {:text, message}, state}
|
2023-04-01 02:55:52 +00:00
|
|
|
|
|
|
|
{:topic, {:error, error}} ->
|
2024-02-14 00:21:45 +00:00
|
|
|
message =
|
|
|
|
StreamerView.render("pleroma_respond.json", %{
|
|
|
|
type: "unsubscribe",
|
|
|
|
result: "error",
|
|
|
|
error: error
|
|
|
|
})
|
|
|
|
|
|
|
|
{:reply, :error, {:text, message}, state}
|
2023-04-01 02:55:52 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-04-01 03:19:57 +00:00
|
|
|
defp handle_client_event(
|
2023-04-01 05:29:11 +00:00
|
|
|
%{"type" => "pleroma:authenticate", "token" => access_token} = _params,
|
2023-04-01 03:19:57 +00:00
|
|
|
state
|
|
|
|
) do
|
|
|
|
with {:auth, nil, nil} <- {:auth, state.user, state.oauth_token},
|
2024-02-14 00:21:45 +00:00
|
|
|
{:ok, user, oauth_token} <- authenticate_request(access_token) do
|
|
|
|
message =
|
|
|
|
StreamerView.render("pleroma_respond.json", %{
|
|
|
|
type: "pleroma:authenticate",
|
|
|
|
result: "success"
|
|
|
|
})
|
|
|
|
|
|
|
|
{:reply, :ok, {:text, message}, %{state | user: user, oauth_token: oauth_token}}
|
2023-04-01 03:19:57 +00:00
|
|
|
else
|
|
|
|
{:auth, _, _} ->
|
2024-02-14 00:21:45 +00:00
|
|
|
message =
|
|
|
|
StreamerView.render("pleroma_respond.json", %{
|
|
|
|
type: "pleroma:authenticate",
|
|
|
|
result: "error",
|
|
|
|
error: :already_authenticated
|
|
|
|
})
|
|
|
|
|
|
|
|
{:reply, :error, {:text, message}, state}
|
2023-04-01 03:19:57 +00:00
|
|
|
|
|
|
|
_ ->
|
2024-02-14 00:21:45 +00:00
|
|
|
message =
|
|
|
|
StreamerView.render("pleroma_respond.json", %{
|
|
|
|
type: "pleroma:authenticate",
|
|
|
|
result: "error",
|
|
|
|
error: :unauthorized
|
|
|
|
})
|
|
|
|
|
|
|
|
{:reply, :error, {:text, message}, state}
|
2023-04-01 03:19:57 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-04-01 02:55:52 +00:00
|
|
|
defp handle_client_event(params, state) do
|
|
|
|
Logger.error("#{__MODULE__} received unknown event: #{inspect(params)}")
|
2024-02-14 00:21:45 +00:00
|
|
|
{:ok, state}
|
|
|
|
end
|
|
|
|
|
|
|
|
def handle_error(conn, :unauthorized) do
|
|
|
|
Plug.Conn.send_resp(conn, 401, "Unauthorized")
|
|
|
|
end
|
|
|
|
|
|
|
|
def handle_error(conn, _reason) do
|
|
|
|
Plug.Conn.send_resp(conn, 404, "Not Found")
|
2023-04-01 02:55:52 +00:00
|
|
|
end
|
2018-12-17 13:39:59 +00:00
|
|
|
end
|