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
9 Pleroma.Web.FederatingPlug
11 :websub_subscription_request,
12 :websub_subscription_confirmation,
17 def websub_subscription_request(conn, %{"nickname" => nickname} = params) do
18 user = User.get_cached_by_nickname(nickname)
20 with {:ok, _websub} <- Websub.incoming_subscription_request(user, params) do
22 |> send_resp(202, "Accepted")
26 |> send_resp(500, reason)
30 # TODO: Extract this into the Websub module
31 def websub_subscription_confirmation(
35 "hub.mode" => "subscribe",
36 "hub.challenge" => challenge,
40 Logger.debug("Got WebSub confirmation")
41 Logger.debug(inspect(params))
44 if params["hub.lease_seconds"] do
45 String.to_integer(params["hub.lease_seconds"])
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)
58 |> send_resp(200, challenge)
62 |> send_resp(500, "Error")
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)
75 |> send_resp(200, "OK")
78 Logger.debug("Can't handle incoming subscription post")
81 |> send_resp(500, "Error")