Websub controller beginnings.
authorRoger Braun <roger@rogerbraun.net>
Thu, 20 Apr 2017 15:47:33 +0000 (17:47 +0200)
committerRoger Braun <roger@rogerbraun.net>
Thu, 20 Apr 2017 15:47:33 +0000 (17:47 +0200)
lib/pleroma/web/ostatus/feed_representer.ex
lib/pleroma/web/ostatus/ostatus.ex
lib/pleroma/web/router.ex
lib/pleroma/web/websub/websub_controller.ex [new file with mode: 0644]
lib/pleroma/web/websub/websub_server_subscription.ex
test/web/ostatus/feed_representer_test.exs
test/web/websub/websub_controller_test.exs [new file with mode: 0644]

index def6844051da7382746906a39ae726417f6cd0e6..1576b47108eb0c725925d0c0bacee8e81273ba48 100644 (file)
@@ -20,7 +20,7 @@ defmodule Pleroma.Web.OStatus.FeedRepresenter do
         {:id, h.(OStatus.feed_path(user))},
         {:title, ['#{user.nickname}\'s timeline']},
         {:updated, h.(most_recent_update)},
-        {:link, [rel: 'hub', href: h.(OStatus.pubsub_path)], []},
+        {:link, [rel: 'hub', href: h.(OStatus.pubsub_path(user))], []},
         {:author, UserRepresenter.to_simple_form(user)}
       ] ++ entries
     }]
index 9fcbe6cb09f1913b6ab5e49e3af7c7e649c6479e..d21b9078f4bc81799c7aee86e648de56171ab177 100644 (file)
@@ -5,8 +5,8 @@ defmodule Pleroma.Web.OStatus do
     "#{user.ap_id}/feed.atom"
   end
 
-  def pubsub_path() do
-    "#{Web.base_url}/push/hub"
+  def pubsub_path(user) do
+    "#{Web.base_url}/push/hub/#{user.nickname}"
   end
 
   def user_path(user) do
index 0264d8d3f1469d9a778e42190bdda21eed836d2e..33e395218026c142e7a4ee912d60020b2e7914db 100644 (file)
@@ -62,7 +62,7 @@ defmodule Pleroma.Web.Router do
     pipe_through :ostatus
 
     get "/users/:nickname/feed", OStatus.OStatusController, :feed
-    post "/push/hub", OStatus.OStatusController, :temp
+    post "/push/hub/:nickname", Websub.WebsubController, :websub_subscription_request
   end
 
   scope "/.well-known", Pleroma.Web do
diff --git a/lib/pleroma/web/websub/websub_controller.ex b/lib/pleroma/web/websub/websub_controller.ex
new file mode 100644 (file)
index 0000000..09305c3
--- /dev/null
@@ -0,0 +1,48 @@
+defmodule Pleroma.Web.Websub.WebsubController do
+  use Pleroma.Web, :controller
+  alias Pleroma.Web.Websub.WebsubServerSubscription
+  alias Pleroma.{Repo, User}
+  alias Pleroma.Web.OStatus
+  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"]
+    do
+      data = %{
+        state: "requested",
+        topic: topic,
+        secret: secret
+      }
+
+      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)
+
+      conn
+      |> send_resp(202, "Accepted")
+    else {:error, reason} ->
+      conn
+      |> send_resp(500, reason)
+    end
+  end
+
+  defp lease_time(%{"hub.lease_seconds" => lease_seconds}) do
+    {:ok, 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
index 2562239ad1d0bf0fb08cacaa85a87f3cdb110cbf..a29dd586069837e743d36c1295169e906884c7cf 100644 (file)
@@ -7,5 +7,7 @@ defmodule Pleroma.Web.Websub.WebsubServerSubscription do
     field :secret, :string
     field :valid_until, :naive_datetime
     field :state, :string
+
+    timestamps()
   end
 end
index dddc63ebfbdc833b22089588647cf686ffeb81c6..3d8eaac6e40dbed2a1f93b6173d2c012cd23405f 100644 (file)
@@ -26,7 +26,7 @@ defmodule Pleroma.Web.OStatus.FeedRepresenterTest do
       <id>#{OStatus.feed_path(user)}</id>
       <title>#{user.nickname}'s timeline</title>
       <updated>#{most_recent_update}</updated>
-      <link rel="hub" href="#{OStatus.pubsub_path}" />
+      <link rel="hub" href="#{OStatus.pubsub_path(user)}" />
       <author>
         #{user_xml}
       </author>
diff --git a/test/web/websub/websub_controller_test.exs b/test/web/websub/websub_controller_test.exs
new file mode 100644 (file)
index 0000000..4eff598
--- /dev/null
@@ -0,0 +1,30 @@
+defmodule Pleroma.Web.Websub.WebsubControllerTest do
+  use Pleroma.Web.ConnCase
+  import Pleroma.Factory
+  alias Pleroma.Repo
+  alias Pleroma.Web.Websub.WebsubServerSubscription
+
+  test "websub subscription request", %{conn: conn} do
+    user = insert(:user)
+
+    path = Pleroma.Web.OStatus.pubsub_path(user)
+
+    data = %{
+      "hub.callback": "http://example.org/sub",
+      "hub.mode": "subscription",
+      "hub.topic": Pleroma.Web.OStatus.feed_path(user),
+      "hub.secret": "a random secret",
+      "hub.lease_seconds": 100
+    }
+
+    conn = conn
+    |> post(path, data)
+
+    assert response(conn, 202) == "Accepted"
+    subscription = Repo.one!(WebsubServerSubscription)
+    assert subscription.topic == Pleroma.Web.OStatus.feed_path(user)
+    assert subscription.state == "requested"
+    assert subscription.secret == "a random secret"
+    assert subscription.valid_until == NaiveDateTime.add(subscription.inserted_at, 100)
+  end
+end