Add remote following of local profiles.
authoreal <eal@waifu.club>
Thu, 1 Feb 2018 22:00:48 +0000 (00:00 +0200)
committereal <eal@waifu.club>
Thu, 1 Feb 2018 22:14:05 +0000 (00:14 +0200)
lib/pleroma/web/router.ex
lib/pleroma/web/templates/twitter_api/util/subscribe.html.eex [new file with mode: 0644]
lib/pleroma/web/twitter_api/controllers/util_controller.ex
lib/pleroma/web/web_finger/web_finger.ex
test/web/ostatus/ostatus_test.exs

index 63dbd624540d61cad8433876c101bcd9f0e9e64b..6e9f40955a929a0b93e7d356dfa3d516fbd0c439 100644 (file)
@@ -62,6 +62,7 @@ defmodule Pleroma.Web.Router do
     pipe_through :pleroma_html
     get "/ostatus_subscribe", UtilController, :remote_follow
     post "/ostatus_subscribe", UtilController, :do_remote_follow
+    post "/main/ostatus", UtilController, :remote_subscribe
   end
 
   scope "/api/pleroma", Pleroma.Web.TwitterAPI do
diff --git a/lib/pleroma/web/templates/twitter_api/util/subscribe.html.eex b/lib/pleroma/web/templates/twitter_api/util/subscribe.html.eex
new file mode 100644 (file)
index 0000000..f60acce
--- /dev/null
@@ -0,0 +1,10 @@
+<%= if @error do %>
+  <h2>Error: <%= @error %></h2>
+<% else %>
+  <h2>Remotely follow <%= @nickname %></h2>
+  <%= form_for @conn, util_path(@conn, :remote_subscribe), [as: "user"], fn f -> %>
+  <%= hidden_input f, :nickname, value: @nickname %>
+  <%= text_input f, :profile, placeholder: "Your account ID, e.g. lain@quitter.se" %>
+  <%= submit "Follow" %>
+  <% end %>
+<% end %>
index a1d56e3abfd15d99d2d510e0959e180cca490b0a..0b7c64015bccd7151b63269653cfc01a2b662ea4 100644 (file)
@@ -3,6 +3,7 @@ defmodule Pleroma.Web.TwitterAPI.UtilController do
   require Logger
   alias Pleroma.Web
   alias Pleroma.Web.OStatus
+  alias Pleroma.Web.WebFinger
   alias Comeonin.Pbkdf2
   alias Pleroma.Formatter
   alias Pleroma.Web.ActivityPub.ActivityPub
@@ -32,6 +33,26 @@ defmodule Pleroma.Web.TwitterAPI.UtilController do
     json(conn, "ok")
   end
 
+  def remote_subscribe(conn, %{"nickname" => nick, "profile" => _}) do
+    with %User{} = user <- User.get_cached_by_nickname(nick),
+         avatar = User.avatar_url(user) do
+      conn
+      |> render("subscribe.html", %{nickname: nick, avatar: avatar, error: false})
+    else
+      _e -> render(conn, "subscribe.html", %{nickname: nick, avatar: nil, error: "Could not find user"})
+    end
+  end
+  def remote_subscribe(conn, %{"user" => %{"nickname" => nick, "profile" => profile}}) do
+    with {:ok, %{"subscribe_address" => template}} <- WebFinger.finger(profile),
+         %User{ap_id: ap_id} <- User.get_cached_by_nickname(nick) do
+      conn
+      |> Phoenix.Controller.redirect(external: String.replace(template, "{uri}", ap_id))
+    else
+      _e ->
+        render(conn, "subscribe.html", %{nickname: nick, avatar: nil, error: "Something went wrong."})
+    end
+  end
+
   def remote_follow(%{assigns: %{user: user}} = conn, %{"acct" => acct}) do
     {err, followee} = OStatus.find_or_make_user(acct)
     avatar = User.avatar_url(followee)
index 4ae3eab594b27a58f75358508cf6b0fc77f69c4a..95e717b17859086c3e2dddb7a7561718161e8424 100644 (file)
@@ -69,11 +69,13 @@ defmodule Pleroma.Web.WebFinger do
     topic = XML.string_from_xpath(~s{//Link[@rel="http://schemas.google.com/g/2010#updates-from"]/@href}, doc)
     subject = XML.string_from_xpath("//Subject", doc)
     salmon = XML.string_from_xpath(~s{//Link[@rel="salmon"]/@href}, doc)
+    subscribe_address = XML.string_from_xpath(~s{//Link[@rel="http://ostatus.org/schema/1.0/subscribe"]/@template}, doc)
     data = %{
       "magic_key" => magic_key,
       "topic" => topic,
       "subject" => subject,
-      "salmon" => salmon
+      "salmon" => salmon,
+      "subscribe_address" => subscribe_address
     }
     {:ok, data}
   end
index de01612b3a803b9ae2caff7976520ec24c1a212d..b27f8cb552cda1fa4ce85d58c8a85069f300da0c 100644 (file)
@@ -302,7 +302,8 @@ defmodule Pleroma.Web.OStatusTest do
         "host" => "social.heldscal.la",
         "fqn" => user,
         "bio" => "cofe",
-        "avatar" => %{"type" => "Image", "url" => [%{"href" => "https://social.heldscal.la/avatar/29191-original-20170421154949.jpeg", "mediaType" => "image/jpeg", "type" => "Link"}]}
+        "avatar" => %{"type" => "Image", "url" => [%{"href" => "https://social.heldscal.la/avatar/29191-original-20170421154949.jpeg", "mediaType" => "image/jpeg", "type" => "Link"}]},
+        "subscribe_address" => "https://social.heldscal.la/main/ostatussub?profile={uri}"
       }
       assert data == expected
     end
@@ -325,7 +326,8 @@ defmodule Pleroma.Web.OStatusTest do
         "host" => "social.heldscal.la",
         "fqn" => user,
         "bio" => "cofe",
-        "avatar" => %{"type" => "Image", "url" => [%{"href" => "https://social.heldscal.la/avatar/29191-original-20170421154949.jpeg", "mediaType" => "image/jpeg", "type" => "Link"}]}
+        "avatar" => %{"type" => "Image", "url" => [%{"href" => "https://social.heldscal.la/avatar/29191-original-20170421154949.jpeg", "mediaType" => "image/jpeg", "type" => "Link"}]},
+        "subscribe_address" => "https://social.heldscal.la/main/ostatussub?profile={uri}"
       }
       assert data == expected
     end