Only have one subscription per callback.
This commit is contained in:
parent
1feb193731
commit
8fb73c28bb
6 changed files with 107 additions and 48 deletions
|
|
@ -3,12 +3,15 @@ defmodule Pleroma.Web.Websub do
|
|||
alias Pleroma.Websub
|
||||
alias Pleroma.Web.Websub.WebsubServerSubscription
|
||||
alias Pleroma.Web.OStatus.FeedRepresenter
|
||||
alias Pleroma.Web.OStatus
|
||||
|
||||
import Ecto.Query
|
||||
|
||||
@websub_verifier Application.get_env(:pleroma, :websub_verifier)
|
||||
|
||||
def verify(subscription, getter \\ &HTTPoison.get/3 ) do
|
||||
challenge = Base.encode16(:crypto.strong_rand_bytes(8))
|
||||
lease_seconds = NaiveDateTime.diff(subscription.valid_until, subscription.inserted_at) |> to_string
|
||||
lease_seconds = NaiveDateTime.diff(subscription.valid_until, subscription.updated_at) |> to_string
|
||||
|
||||
params = %{
|
||||
"hub.challenge": challenge,
|
||||
|
|
@ -48,4 +51,53 @@ defmodule Pleroma.Web.Websub do
|
|||
])
|
||||
end)
|
||||
end
|
||||
|
||||
def incoming_subscription_request(user, params) do
|
||||
with {:ok, topic} <- valid_topic(params, user),
|
||||
{:ok, lease_time} <- lease_time(params),
|
||||
secret <- params["hub.secret"],
|
||||
callback <- params["hub.callback"]
|
||||
do
|
||||
subscription = get_subscription(topic, callback)
|
||||
data = %{
|
||||
state: subscription.state || "requested",
|
||||
topic: topic,
|
||||
secret: secret,
|
||||
callback: callback
|
||||
}
|
||||
|
||||
change = Ecto.Changeset.change(subscription, data)
|
||||
websub = Repo.insert_or_update!(change)
|
||||
|
||||
change = Ecto.Changeset.change(websub, %{valid_until: NaiveDateTime.add(websub.updated_at, lease_time)})
|
||||
websub = Repo.update!(change)
|
||||
|
||||
# Just spawn that for now, maybe pool later.
|
||||
spawn(fn -> @websub_verifier.verify(websub) end)
|
||||
|
||||
{:ok, websub}
|
||||
else {:error, reason} ->
|
||||
{:error, reason}
|
||||
end
|
||||
end
|
||||
|
||||
defp get_subscription(topic, callback) do
|
||||
Repo.get_by(WebsubServerSubscription, topic: topic, callback: callback) || %WebsubServerSubscription{}
|
||||
end
|
||||
|
||||
defp lease_time(%{"hub.lease_seconds" => lease_seconds}) do
|
||||
{:ok, String.to_integer(lease_seconds)}
|
||||
end
|
||||
|
||||
defp lease_time(_) do
|
||||
{:ok, 60 * 60 * 24 * 3} # three days
|
||||
end
|
||||
|
||||
defp valid_topic(%{"hub.topic" => topic}, user) do
|
||||
if topic == OStatus.feed_path(user) do
|
||||
{:ok, topic}
|
||||
else
|
||||
{:error, "Wrong topic requested, expected #{OStatus.feed_path(user)}, got #{topic}"}
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -1,32 +1,13 @@
|
|||
defmodule Pleroma.Web.Websub.WebsubController do
|
||||
use Pleroma.Web, :controller
|
||||
alias Pleroma.Web.Websub.WebsubServerSubscription
|
||||
alias Pleroma.{Repo, User}
|
||||
alias Pleroma.Web.OStatus
|
||||
alias Pleroma.User
|
||||
alias Pleroma.Web.Websub
|
||||
|
||||
def websub_subscription_request(conn, %{"nickname" => nickname} = params) do
|
||||
user = User.get_cached_by_nickname(nickname)
|
||||
|
||||
with {:ok, topic} <- valid_topic(params, user),
|
||||
{:ok, lease_time} <- lease_time(params),
|
||||
secret <- params["hub.secret"]
|
||||
with {:ok, _websub} <- Websub.incoming_subscription_request(user, params)
|
||||
do
|
||||
data = %{
|
||||
state: "requested",
|
||||
topic: topic,
|
||||
secret: secret,
|
||||
callback: params["hub.callback"]
|
||||
}
|
||||
|
||||
change = Ecto.Changeset.change(%WebsubServerSubscription{}, data)
|
||||
websub = Repo.insert!(change)
|
||||
|
||||
change = Ecto.Changeset.change(websub, %{valid_until: NaiveDateTime.add(websub.inserted_at, lease_time)})
|
||||
websub = Repo.update!(change)
|
||||
|
||||
# Just spawn that for now, maybe pool later.
|
||||
spawn(fn -> Websub.verify(websub) end)
|
||||
|
||||
conn
|
||||
|> send_resp(202, "Accepted")
|
||||
else {:error, reason} ->
|
||||
|
|
@ -34,20 +15,4 @@ defmodule Pleroma.Web.Websub.WebsubController do
|
|||
|> send_resp(500, reason)
|
||||
end
|
||||
end
|
||||
|
||||
defp lease_time(%{"hub.lease_seconds" => lease_seconds}) do
|
||||
{:ok, String.to_integer(lease_seconds)}
|
||||
end
|
||||
|
||||
defp lease_time(_) do
|
||||
{:ok, 60 * 60 * 24 * 3} # three days
|
||||
end
|
||||
|
||||
defp valid_topic(%{"hub.topic" => topic}, user) do
|
||||
if topic == OStatus.feed_path(user) do
|
||||
{:ok, topic}
|
||||
else
|
||||
{:error, "Wrong topic requested, expected #{OStatus.feed_path(user)}, got #{topic}"}
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue