2017-04-20 15:47:33 +00:00
|
|
|
defmodule Pleroma.Web.Websub.WebsubController do
|
|
|
|
use Pleroma.Web, :controller
|
2017-04-28 13:45:10 +00:00
|
|
|
alias Pleroma.{Repo, User}
|
2017-05-06 10:34:40 +00:00
|
|
|
alias Pleroma.Web.{Websub, Federator}
|
2017-04-28 13:45:10 +00:00
|
|
|
alias Pleroma.Web.Websub.WebsubClientSubscription
|
|
|
|
require Logger
|
|
|
|
|
2017-04-20 15:47:33 +00:00
|
|
|
def websub_subscription_request(conn, %{"nickname" => nickname} = params) do
|
|
|
|
user = User.get_cached_by_nickname(nickname)
|
|
|
|
|
2017-04-22 11:44:21 +00:00
|
|
|
with {:ok, _websub} <- Websub.incoming_subscription_request(user, params)
|
2017-04-20 15:47:33 +00:00
|
|
|
do
|
|
|
|
conn
|
|
|
|
|> send_resp(202, "Accepted")
|
|
|
|
else {:error, reason} ->
|
|
|
|
conn
|
|
|
|
|> send_resp(500, reason)
|
|
|
|
end
|
|
|
|
end
|
2017-04-28 07:51:47 +00:00
|
|
|
|
2017-04-28 13:45:10 +00:00
|
|
|
def websub_subscription_confirmation(conn, %{"id" => id, "hub.mode" => "subscribe", "hub.challenge" => challenge, "hub.topic" => topic}) do
|
|
|
|
with %WebsubClientSubscription{} = websub <- Repo.get_by(WebsubClientSubscription, id: id, topic: topic) do
|
|
|
|
change = Ecto.Changeset.change(websub, %{state: "accepted"})
|
|
|
|
{:ok, _websub} = Repo.update(change)
|
|
|
|
conn
|
|
|
|
|> send_resp(200, challenge)
|
|
|
|
else _e ->
|
|
|
|
conn
|
|
|
|
|> send_resp(500, "Error")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def websub_incoming(conn, %{"id" => id}) do
|
|
|
|
with "sha1=" <> signature <- hd(get_req_header(conn, "x-hub-signature")),
|
2017-05-03 18:06:20 +00:00
|
|
|
signature <- String.downcase(signature),
|
2017-04-28 13:45:10 +00:00
|
|
|
%WebsubClientSubscription{} = websub <- Repo.get(WebsubClientSubscription, id),
|
|
|
|
{:ok, body, _conn} = read_body(conn),
|
|
|
|
^signature <- Websub.sign(websub.secret, body) do
|
2017-05-06 10:34:40 +00:00
|
|
|
Federator.enqueue(:incoming_doc, body)
|
2017-04-28 13:45:10 +00:00
|
|
|
conn
|
|
|
|
|> send_resp(200, "OK")
|
|
|
|
else _e ->
|
|
|
|
Logger.debug("Can't handle incoming subscription post")
|
|
|
|
conn
|
|
|
|
|> send_resp(500, "Error")
|
|
|
|
end
|
2017-04-28 07:51:47 +00:00
|
|
|
end
|
2017-04-20 15:47:33 +00:00
|
|
|
end
|