add basic federation to websub.
This commit is contained in:
parent
39dc74f967
commit
77cb260628
|
@ -1,15 +1,26 @@
|
||||||
defmodule Pleroma.Web.Websub do
|
defmodule Pleroma.Web.Websub do
|
||||||
alias Pleroma.Repo
|
alias Pleroma.Repo
|
||||||
|
alias Pleroma.Websub
|
||||||
|
alias Pleroma.Web.Websub.WebsubServerSubscription
|
||||||
|
alias Pleroma.Web.OStatus.FeedRepresenter
|
||||||
|
|
||||||
|
import Ecto.Query
|
||||||
|
|
||||||
def verify(subscription, getter \\ &HTTPoison.get/3 ) do
|
def verify(subscription, getter \\ &HTTPoison.get/3 ) do
|
||||||
challenge = Base.encode16(:crypto.strong_rand_bytes(8))
|
challenge = Base.encode16(:crypto.strong_rand_bytes(8))
|
||||||
lease_seconds = NaiveDateTime.diff(subscription.inserted_at, subscription.valid_until)
|
lease_seconds = NaiveDateTime.diff(subscription.valid_until, subscription.inserted_at) |> to_string
|
||||||
with {:ok, response} <- getter.(subscription.callback, [], [params: %{
|
|
||||||
|
params = %{
|
||||||
"hub.challenge": challenge,
|
"hub.challenge": challenge,
|
||||||
"hub.lease_seconds": lease_seconds,
|
"hub.lease_seconds": lease_seconds,
|
||||||
"hub.topic": subscription.topic,
|
"hub.topic": subscription.topic,
|
||||||
"hub.mode": "subscribe"
|
"hub.mode": "subscribe"
|
||||||
}]),
|
}
|
||||||
|
|
||||||
|
url = hd(String.split(subscription.callback, "?"))
|
||||||
|
query = URI.parse(subscription.callback).query || ""
|
||||||
|
params = Map.merge(params, URI.decode_query(query))
|
||||||
|
with {:ok, response} <- getter.(url, [], [params: params]),
|
||||||
^challenge <- response.body
|
^challenge <- response.body
|
||||||
do
|
do
|
||||||
changeset = Ecto.Changeset.change(subscription, %{state: "active"})
|
changeset = Ecto.Changeset.change(subscription, %{state: "active"})
|
||||||
|
@ -20,4 +31,21 @@ def verify(subscription, getter \\ &HTTPoison.get/3 ) do
|
||||||
{:error, subscription}
|
{:error, subscription}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def publish(topic, user, activity) do
|
||||||
|
query = from sub in WebsubServerSubscription,
|
||||||
|
where: sub.topic == ^topic and sub.state == "active"
|
||||||
|
subscriptions = Repo.all(query)
|
||||||
|
Enum.each(subscriptions, fn(sub) ->
|
||||||
|
response = FeedRepresenter.to_simple_form(user, [activity], [user])
|
||||||
|
|> :xmerl.export_simple(:xmerl_xml)
|
||||||
|
|
||||||
|
signature = :crypto.hmac(:sha, sub.secret, response) |> Base.encode16
|
||||||
|
|
||||||
|
HTTPoison.post(sub.callback, response, [
|
||||||
|
{"Content-Type", "application/atom+xml"},
|
||||||
|
{"X-Hub-Signature", "sha1=#{signature}"}
|
||||||
|
])
|
||||||
|
end)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -15,7 +15,7 @@ test "a verification of a request that is accepted" do
|
||||||
"hub.mode": "subscribe"
|
"hub.mode": "subscribe"
|
||||||
} = Keyword.get(options, :params)
|
} = Keyword.get(options, :params)
|
||||||
|
|
||||||
assert is_number(seconds)
|
assert String.to_integer(seconds) > 0
|
||||||
|
|
||||||
{:ok, %HTTPoison.Response{
|
{:ok, %HTTPoison.Response{
|
||||||
status_code: 200,
|
status_code: 200,
|
||||||
|
|
Loading…
Reference in a new issue