edab961f0a44faf5c80fb0252013498737541bb0
[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.Repo
10 alias Pleroma.User
11
12 @behaviour :cowboy_websocket
13
14 @streams [
15 "public",
16 "public:local",
17 "public:media",
18 "public:local:media",
19 "user",
20 "direct",
21 "list",
22 "hashtag"
23 ]
24 @anonymous_streams ["public", "public:local", "hashtag"]
25
26 def init(%{qs: qs} = req, _state) do
27 with params <- :cow_qs.parse_qs(qs),
28 access_token <- List.keyfind(params, "access_token", 0),
29 {_, stream} <- List.keyfind(params, "stream", 0),
30 {:ok, user} <- allow_request(stream, access_token),
31 topic when is_binary(topic) <- expand_topic(stream, params) do
32 {:cowboy_websocket, req, %{user: user, topic: topic}}
33 else
34 {:error, code} ->
35 Logger.debug("#{__MODULE__} denied connection: #{inspect(code)} - #{inspect(req)}")
36 {:ok, req} = :cowboy_req.reply(code, req)
37 {:stop, req}
38
39 error ->
40 Logger.debug("#{__MODULE__} denied connection: #{inspect(error)} - #{inspect(req)}")
41 {:ok, req} = :cowboy_req.reply(400, req)
42 {:stop, req}
43 end
44 end
45
46 def websocket_init(state) do
47 send(self(), :subscribe)
48 {:ok, state}
49 end
50
51 # We never receive messages.
52 def websocket_handle(_frame, state) do
53 {:ok, state}
54 end
55
56 def websocket_info(:subscribe, state) do
57 Logger.debug(
58 "#{__MODULE__} accepted websocket connection for user #{
59 (state.user || %{id: "anonymous"}).id
60 }, topic #{state.topic}"
61 )
62
63 Pleroma.Web.Streamer.add_socket(state.topic, streamer_socket(state))
64 {:ok, state}
65 end
66
67 def websocket_info({:text, message}, state) do
68 {:reply, {:text, message}, state}
69 end
70
71 def terminate(reason, _req, state) do
72 Logger.debug(
73 "#{__MODULE__} terminating websocket connection for user #{
74 (state.user || %{id: "anonymous"}).id
75 }, topic #{state.topic || "?"}: #{inspect(reason)}"
76 )
77
78 Pleroma.Web.Streamer.remove_socket(state.topic, streamer_socket(state))
79 :ok
80 end
81
82 # Public streams without authentication.
83 defp allow_request(stream, nil) when stream in @anonymous_streams do
84 {:ok, nil}
85 end
86
87 # Authenticated streams.
88 defp allow_request(stream, {"access_token", access_token}) when stream in @streams do
89 with %Token{user_id: user_id} <- Repo.get_by(Token, token: access_token),
90 user = %User{} <- Repo.get(User, user_id) do
91 {:ok, user}
92 else
93 _ -> {:error, 403}
94 end
95 end
96
97 # Not authenticated.
98 defp allow_request(stream, _) when stream in @streams, do: {:error, 403}
99
100 # No matching stream.
101 defp allow_request(_, _), do: {:error, 404}
102
103 defp expand_topic("hashtag", params) do
104 case List.keyfind(params, "tag", 0) do
105 {_, tag} -> "hashtag:#{tag}"
106 _ -> nil
107 end
108 end
109
110 defp expand_topic("list", params) do
111 case List.keyfind(params, "list", 0) do
112 {_, list} -> "list:#{list}"
113 _ -> nil
114 end
115 end
116
117 defp expand_topic(topic, _), do: topic
118
119 defp streamer_socket(state) do
120 %{transport_pid: self(), assigns: state}
121 end
122 end