Add callback to websub subscription.

This commit is contained in:
Roger Braun 2017-04-22 12:05:48 +02:00
parent 1c00eb4a90
commit 39dc74f967
2 changed files with 9 additions and 3 deletions

View File

@ -3,6 +3,7 @@ defmodule Pleroma.Web.Websub.WebsubController do
alias Pleroma.Web.Websub.WebsubServerSubscription alias Pleroma.Web.Websub.WebsubServerSubscription
alias Pleroma.{Repo, User} alias Pleroma.{Repo, User}
alias Pleroma.Web.OStatus alias Pleroma.Web.OStatus
alias Pleroma.Web.Websub
def websub_subscription_request(conn, %{"nickname" => nickname} = params) do def websub_subscription_request(conn, %{"nickname" => nickname} = params) do
user = User.get_cached_by_nickname(nickname) user = User.get_cached_by_nickname(nickname)
@ -13,7 +14,8 @@ def websub_subscription_request(conn, %{"nickname" => nickname} = params) do
data = %{ data = %{
state: "requested", state: "requested",
topic: topic, topic: topic,
secret: secret secret: secret,
callback: params["hub.callback"]
} }
change = Ecto.Changeset.change(%WebsubServerSubscription{}, data) change = Ecto.Changeset.change(%WebsubServerSubscription{}, data)
@ -22,6 +24,9 @@ def websub_subscription_request(conn, %{"nickname" => nickname} = params) do
change = Ecto.Changeset.change(websub, %{valid_until: NaiveDateTime.add(websub.inserted_at, lease_time)}) change = Ecto.Changeset.change(websub, %{valid_until: NaiveDateTime.add(websub.inserted_at, lease_time)})
websub = Repo.update!(change) websub = Repo.update!(change)
# Just spawn that for now, maybe pool later.
spawn(fn -> Websub.verify(websub) end)
conn conn
|> send_resp(202, "Accepted") |> send_resp(202, "Accepted")
else {:error, reason} -> else {:error, reason} ->
@ -31,7 +36,7 @@ def websub_subscription_request(conn, %{"nickname" => nickname} = params) do
end end
defp lease_time(%{"hub.lease_seconds" => lease_seconds}) do defp lease_time(%{"hub.lease_seconds" => lease_seconds}) do
{:ok, lease_seconds} {:ok, String.to_integer(lease_seconds)}
end end
defp lease_time(_) do defp lease_time(_) do

View File

@ -14,7 +14,7 @@ test "websub subscription request", %{conn: conn} do
"hub.mode": "subscription", "hub.mode": "subscription",
"hub.topic": Pleroma.Web.OStatus.feed_path(user), "hub.topic": Pleroma.Web.OStatus.feed_path(user),
"hub.secret": "a random secret", "hub.secret": "a random secret",
"hub.lease_seconds": 100 "hub.lease_seconds": "100"
} }
conn = conn conn = conn
@ -25,6 +25,7 @@ test "websub subscription request", %{conn: conn} do
assert subscription.topic == Pleroma.Web.OStatus.feed_path(user) assert subscription.topic == Pleroma.Web.OStatus.feed_path(user)
assert subscription.state == "requested" assert subscription.state == "requested"
assert subscription.secret == "a random secret" assert subscription.secret == "a random secret"
assert subscription.callback == "http://example.org/sub"
assert subscription.valid_until == NaiveDateTime.add(subscription.inserted_at, 100) assert subscription.valid_until == NaiveDateTime.add(subscription.inserted_at, 100)
end end
end end