c0254c8e6471aa239b641ec0cb0f3863cf291764
[akkoma] / lib / pleroma / web / mastodon_api / websocket_handler.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.MastodonAPI.WebsocketHandler do
6 require Logger
7
8 alias Pleroma.Web.OAuth.Token
9 alias Pleroma.{User, Repo}
10
11 @behaviour :cowboy_websocket_handler
12
13 @streams [
14 "public",
15 "public:local",
16 "public:media",
17 "public:local:media",
18 "user",
19 "direct",
20 "list",
21 "hashtag"
22 ]
23 @anonymous_streams ["public", "public:local", "hashtag"]
24
25 # Handled by periodic keepalive in Pleroma.Web.Streamer.
26 @timeout :infinity
27
28 def init(_type, _req, _opts) do
29 {:upgrade, :protocol, :cowboy_websocket}
30 end
31
32 def websocket_init(_type, req, _opts) do
33 with {qs, req} <- :cowboy_req.qs(req),
34 params <- :cow_qs.parse_qs(qs),
35 access_token <- List.keyfind(params, "access_token", 0),
36 {_, stream} <- List.keyfind(params, "stream", 0),
37 {:ok, user} <- allow_request(stream, access_token),
38 topic when is_binary(topic) <- expand_topic(stream, params) do
39 send(self(), :subscribe)
40 {:ok, req, %{user: user, topic: topic}, @timeout}
41 else
42 {:error, code} ->
43 Logger.debug("#{__MODULE__} denied connection: #{inspect(code)} - #{inspect(req)}")
44 {:ok, req} = :cowboy_req.reply(code, req)
45 {:shutdown, req}
46
47 error ->
48 Logger.debug("#{__MODULE__} denied connection: #{inspect(error)} - #{inspect(req)}")
49 {:shutdown, req}
50 end
51 end
52
53 # We never receive messages.
54 def websocket_handle(_frame, req, state) do
55 {:ok, req, state}
56 end
57
58 def websocket_info(:subscribe, req, state) do
59 Logger.debug(
60 "#{__MODULE__} accepted websocket connection for user #{
61 (state.user || %{id: "anonymous"}).id
62 }, topic #{state.topic}"
63 )
64
65 Pleroma.Web.Streamer.add_socket(state.topic, streamer_socket(state))
66 {:ok, req, state}
67 end
68
69 def websocket_info({:text, message}, req, state) do
70 {:reply, {:text, message}, req, state}
71 end
72
73 def websocket_terminate(reason, _req, state) do
74 Logger.debug(
75 "#{__MODULE__} terminating websocket connection for user #{
76 (state.user || %{id: "anonymous"}).id
77 }, topic #{state.topic || "?"}: #{inspect(reason)}"
78 )
79
80 Pleroma.Web.Streamer.remove_socket(state.topic, streamer_socket(state))
81 :ok
82 end
83
84 # Public streams without authentication.
85 defp allow_request(stream, nil) when stream in @anonymous_streams do
86 {:ok, nil}
87 end
88
89 # Authenticated streams.
90 defp allow_request(stream, {"access_token", access_token}) when stream in @streams do
91 with %Token{user_id: user_id} <- Repo.get_by(Token, token: access_token),
92 user = %User{} <- Repo.get(User, user_id) do
93 {:ok, user}
94 else
95 _ -> {:error, 403}
96 end
97 end
98
99 # Not authenticated.
100 defp allow_request(stream, _) when stream in @streams, do: {:error, 403}
101
102 # No matching stream.
103 defp allow_request(_, _), do: {:error, 404}
104
105 defp expand_topic("hashtag", params) do
106 case List.keyfind(params, "tag", 0) do
107 {_, tag} -> "hashtag:#{tag}"
108 _ -> nil
109 end
110 end
111
112 defp expand_topic("list", params) do
113 case List.keyfind(params, "list", 0) do
114 {_, list} -> "list:#{list}"
115 _ -> nil
116 end
117 end
118
119 defp expand_topic(topic, _), do: topic
120
121 defp streamer_socket(state) do
122 %{transport_pid: self(), assigns: state}
123 end
124 end