1 defmodule Pleroma.Web.Websub.WebsubController do
2 use Pleroma.Web, :controller
3 alias Pleroma.{Repo, User}
4 alias Pleroma.Web.Websub
5 alias Pleroma.Web.Websub.WebsubClientSubscription
8 @ostatus Application.get_env(:pleroma, :ostatus)
10 def websub_subscription_request(conn, %{"nickname" => nickname} = params) do
11 user = User.get_cached_by_nickname(nickname)
13 with {:ok, _websub} <- Websub.incoming_subscription_request(user, params)
16 |> send_resp(202, "Accepted")
17 else {:error, reason} ->
19 |> send_resp(500, reason)
23 def websub_subscription_confirmation(conn, %{"id" => id, "hub.mode" => "subscribe", "hub.challenge" => challenge, "hub.topic" => topic}) do
24 with %WebsubClientSubscription{} = websub <- Repo.get_by(WebsubClientSubscription, id: id, topic: topic) do
25 change = Ecto.Changeset.change(websub, %{state: "accepted"})
26 {:ok, _websub} = Repo.update(change)
28 |> send_resp(200, challenge)
31 |> send_resp(500, "Error")
35 def websub_incoming(conn, %{"id" => id}) do
36 with "sha1=" <> signature <- hd(get_req_header(conn, "x-hub-signature")),
37 %WebsubClientSubscription{} = websub <- Repo.get(WebsubClientSubscription, id),
38 {:ok, body, _conn} = read_body(conn),
39 ^signature <- Websub.sign(websub.secret, body) do
40 @ostatus.handle_incoming(body)
42 |> send_resp(200, "OK")
44 Logger.debug("Can't handle incoming subscription post")
46 |> send_resp(500, "Error")