MastodonAPI: Stream fixes.
[akkoma] / lib / pleroma / web / mastodon_api / mastodon_socket.ex
1 defmodule Pleroma.Web.MastodonAPI.MastodonSocket do
2 use Phoenix.Socket
3
4 alias Pleroma.Web.OAuth.Token
5 alias Pleroma.{User, Repo}
6
7 transport :streaming, Phoenix.Transports.WebSocket.Raw,
8 timeout: :infinity # We never receive data.
9
10 def connect(params, socket) do
11 with token when not is_nil(token) <- params["access_token"],
12 %Token{user_id: user_id} <- Repo.get_by(Token, token: token),
13 %User{} = user <- Repo.get(User, user_id),
14 stream when stream in ["public", "public:local"] <- params["stream"] do
15 socket = socket
16 |> assign(:topic, params["stream"])
17 |> assign(:user, user)
18 Pleroma.Web.Streamer.add_socket(params["stream"], socket)
19 {:ok, socket}
20 else
21 _e -> :error
22 end
23 end
24
25 def id(socket), do: nil
26
27 def handle(:text, message, state) do
28 IO.inspect message
29 #| :ok
30 #| state
31 #| {:text, message}
32 #| {:text, message, state}
33 #| {:close, "Goodbye!"}
34 {:text, message}
35 end
36
37 def handle(:closed, reason, %{socket: socket}) do
38 topic = socket.assigns[:topic]
39 Pleroma.Web.Streamer.remove_socket(topic, socket)
40 end
41 end