Add Pleroma user search api for PleromaFE.
authorlain <lain@soykaf.club>
Wed, 14 Nov 2018 19:33:23 +0000 (20:33 +0100)
committerlain <lain@soykaf.club>
Wed, 14 Nov 2018 19:33:23 +0000 (20:33 +0100)
lib/pleroma/web/router.ex
lib/pleroma/web/twitter_api/twitter_api_controller.ex
test/web/twitter_api/twitter_api_controller_test.exs

index 5e81db00b431cf5d1dba2131cf11aa84e83948ba..5ca86787163d222c6453667009275926d14d6945 100644 (file)
@@ -248,9 +248,15 @@ defmodule Pleroma.Web.Router do
     )
 
     get("/statuses/networkpublic_timeline", TwitterAPI.Controller, :public_and_external_timeline)
+
   end
 
-  scope "/api", Pleroma.Web do
+  scope "/api", Pleroma.Web, as: :twitter_api_search do
+    pipe_through(:api)
+    get("/pleroma/search_user", TwitterAPI.Controller, :search_user)
+  end
+
+  scope "/api", Pleroma.Web, as: :authenticated_twitter_api do
     pipe_through(:authenticated_api)
 
     get("/account/verify_credentials", TwitterAPI.Controller, :verify_credentials)
index dfcafdcc90a030a0eb8c572fcd5e46fdb427184b..69e576048e00e369f57749f0655378580240a74a 100644 (file)
@@ -529,6 +529,13 @@ defmodule Pleroma.Web.TwitterAPI.Controller do
     |> render(ActivityView, "index.json", %{activities: activities, for: user})
   end
 
+  def search_user(%{assigns: %{user: user}} = conn, %{"query" => query}) do
+    users = User.search(query, true)
+
+    conn
+    |> render(UserView, "index.json", %{users: users, for: user})
+  end
+
   defp bad_request_reply(conn, error_message) do
     json = error_json(conn, error_message)
     json_reply(conn, 400, json)
index 788e3a6eb9ec56347dca0eea6b00d5980b867b92..07f9c781461237fd4d3b1c7a5a0edff33252bcd0 100644 (file)
@@ -1211,4 +1211,19 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do
       assert relationship["follows_you"] == false
     end
   end
+
+  describe "GET /api/pleroma/search_user" do
+    test "it returns users, ordered by similarity", %{conn: conn} do
+      user = insert(:user, %{name: "eal"})
+      user_two = insert(:user, %{name: "ean"})
+      user_three = insert(:user, %{name: "ebn"})
+
+      resp = conn
+      |> get(twitter_api_search__path(conn, :search_user), query: "eal")
+      |> json_response(200)
+
+      assert length(resp) == 3
+      assert [user.id, user_two.id, user_three.id] == Enum.map(resp, fn (%{"id" => id}) -> id end)
+    end
+  end
 end