1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2018 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.Websub.WebsubController do
6 use Pleroma.Web, :controller
7 alias Pleroma.{Repo, User}
8 alias Pleroma.Web.{Websub, Federator}
9 alias Pleroma.Web.Websub.WebsubClientSubscription
13 Pleroma.Web.FederatingPlug
15 :websub_subscription_request,
16 :websub_subscription_confirmation,
21 def websub_subscription_request(conn, %{"nickname" => nickname} = params) do
22 user = User.get_cached_by_nickname(nickname)
24 with {:ok, _websub} <- Websub.incoming_subscription_request(user, params) do
26 |> send_resp(202, "Accepted")
30 |> send_resp(500, reason)
34 # TODO: Extract this into the Websub module
35 def websub_subscription_confirmation(
39 "hub.mode" => "subscribe",
40 "hub.challenge" => challenge,
44 Logger.debug("Got WebSub confirmation")
45 Logger.debug(inspect(params))
48 if params["hub.lease_seconds"] do
49 String.to_integer(params["hub.lease_seconds"])
55 with %WebsubClientSubscription{} = websub <-
56 Repo.get_by(WebsubClientSubscription, id: id, topic: topic) do
57 valid_until = NaiveDateTime.add(NaiveDateTime.utc_now(), lease_seconds)
58 change = Ecto.Changeset.change(websub, %{state: "accepted", valid_until: valid_until})
59 {:ok, _websub} = Repo.update(change)
62 |> send_resp(200, challenge)
66 |> send_resp(500, "Error")
70 def websub_incoming(conn, %{"id" => id}) do
71 with "sha1=" <> signature <- hd(get_req_header(conn, "x-hub-signature")),
72 signature <- String.downcase(signature),
73 %WebsubClientSubscription{} = websub <- Repo.get(WebsubClientSubscription, id),
74 {:ok, body, _conn} = read_body(conn),
75 ^signature <- Websub.sign(websub.secret, body) do
76 Federator.enqueue(:incoming_doc, body)
79 |> send_resp(200, "OK")
82 Logger.debug("Can't handle incoming subscription post")
85 |> send_resp(500, "Error")