Merge branch 'runtime-config' into 'develop'
[akkoma] / lib / pleroma / web / websub / websub_controller.ex
1 defmodule Pleroma.Web.Websub.WebsubController do
2 use Pleroma.Web, :controller
3 alias Pleroma.{Repo, User}
4 alias Pleroma.Web.{Websub, Federator}
5 alias Pleroma.Web.Websub.WebsubClientSubscription
6 require Logger
7
8 plug(
9 Pleroma.Web.FederatingPlug
10 when action in [
11 :websub_subscription_request,
12 :websub_subscription_confirmation,
13 :websub_incoming
14 ]
15 )
16
17 def websub_subscription_request(conn, %{"nickname" => nickname} = params) do
18 user = User.get_cached_by_nickname(nickname)
19
20 with {:ok, _websub} <- Websub.incoming_subscription_request(user, params) do
21 conn
22 |> send_resp(202, "Accepted")
23 else
24 {:error, reason} ->
25 conn
26 |> send_resp(500, reason)
27 end
28 end
29
30 # TODO: Extract this into the Websub module
31 def websub_subscription_confirmation(
32 conn,
33 %{
34 "id" => id,
35 "hub.mode" => "subscribe",
36 "hub.challenge" => challenge,
37 "hub.topic" => topic
38 } = params
39 ) do
40 Logger.debug("Got WebSub confirmation")
41 Logger.debug(inspect(params))
42
43 lease_seconds =
44 if params["hub.lease_seconds"] do
45 String.to_integer(params["hub.lease_seconds"])
46 else
47 # Guess 3 days
48 60 * 60 * 24 * 3
49 end
50
51 with %WebsubClientSubscription{} = websub <-
52 Repo.get_by(WebsubClientSubscription, id: id, topic: topic) do
53 valid_until = NaiveDateTime.add(NaiveDateTime.utc_now(), lease_seconds)
54 change = Ecto.Changeset.change(websub, %{state: "accepted", valid_until: valid_until})
55 {:ok, _websub} = Repo.update(change)
56
57 conn
58 |> send_resp(200, challenge)
59 else
60 _e ->
61 conn
62 |> send_resp(500, "Error")
63 end
64 end
65
66 def websub_incoming(conn, %{"id" => id}) do
67 with "sha1=" <> signature <- hd(get_req_header(conn, "x-hub-signature")),
68 signature <- String.downcase(signature),
69 %WebsubClientSubscription{} = websub <- Repo.get(WebsubClientSubscription, id),
70 {:ok, body, _conn} = read_body(conn),
71 ^signature <- Websub.sign(websub.secret, body) do
72 Federator.enqueue(:incoming_doc, body)
73
74 conn
75 |> send_resp(200, "OK")
76 else
77 _e ->
78 Logger.debug("Can't handle incoming subscription post")
79
80 conn
81 |> send_resp(500, "Error")
82 end
83 end
84 end