Merge branch 'develop' into dtluna/pleroma-feature/unfollow-activity
[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 def websub_subscription_request(conn, %{"nickname" => nickname} = params) do
9 user = User.get_cached_by_nickname(nickname)
10
11 with {:ok, _websub} <- Websub.incoming_subscription_request(user, params)
12 do
13 conn
14 |> send_resp(202, "Accepted")
15 else {:error, reason} ->
16 conn
17 |> send_resp(500, reason)
18 end
19 end
20
21 def websub_subscription_confirmation(conn, %{"id" => id, "hub.mode" => "subscribe", "hub.challenge" => challenge, "hub.topic" => topic}) do
22 with %WebsubClientSubscription{} = websub <- Repo.get_by(WebsubClientSubscription, id: id, topic: topic) do
23 change = Ecto.Changeset.change(websub, %{state: "accepted"})
24 {:ok, _websub} = Repo.update(change)
25 conn
26 |> send_resp(200, challenge)
27 else _e ->
28 conn
29 |> send_resp(500, "Error")
30 end
31 end
32
33 def websub_incoming(conn, %{"id" => id}) do
34 with "sha1=" <> signature <- hd(get_req_header(conn, "x-hub-signature")),
35 signature <- String.downcase(signature),
36 %WebsubClientSubscription{} = websub <- Repo.get(WebsubClientSubscription, id),
37 {:ok, body, _conn} = read_body(conn),
38 ^signature <- Websub.sign(websub.secret, body) do
39 Federator.enqueue(:incoming_doc, body)
40 conn
41 |> send_resp(200, "OK")
42 else _e ->
43 Logger.debug("Can't handle incoming subscription post")
44 conn
45 |> send_resp(500, "Error")
46 end
47 end
48 end