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