1 defmodule Pleroma.Web.Websub do
4 def verify(subscription, getter \\ &HTTPoison.get/3 ) do
5 challenge = Base.encode16(:crypto.strong_rand_bytes(8))
6 lease_seconds = NaiveDateTime.diff(subscription.inserted_at, subscription.valid_until)
7 with {:ok, response} <- getter.(subscription.callback, [], [params: %{
8 "hub.challenge": challenge,
9 "hub.lease_seconds": lease_seconds,
10 "hub.topic": subscription.topic,
11 "hub.mode": "subscribe"
13 ^challenge <- response.body
15 changeset = Ecto.Changeset.change(subscription, %{state: "active"})
16 Repo.update(changeset)
18 changeset = Ecto.Changeset.change(subscription, %{state: "rejected"})
19 {:ok, subscription } = Repo.update(changeset)
20 {:error, subscription}