From 7b170cd6161166e153ad54856d5f1cf189f4e3ae Mon Sep 17 00:00:00 2001 From: lain Date: Wed, 14 Nov 2018 20:33:23 +0100 Subject: [PATCH 1/3] Add Pleroma user search api for PleromaFE. --- lib/pleroma/web/router.ex | 8 +++++++- .../web/twitter_api/twitter_api_controller.ex | 7 +++++++ .../twitter_api/twitter_api_controller_test.exs | 15 +++++++++++++++ 3 files changed, 29 insertions(+), 1 deletion(-) diff --git a/lib/pleroma/web/router.ex b/lib/pleroma/web/router.ex index 5e81db00b..5ca867871 100644 --- a/lib/pleroma/web/router.ex +++ b/lib/pleroma/web/router.ex @@ -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) diff --git a/lib/pleroma/web/twitter_api/twitter_api_controller.ex b/lib/pleroma/web/twitter_api/twitter_api_controller.ex index dfcafdcc9..69e576048 100644 --- a/lib/pleroma/web/twitter_api/twitter_api_controller.ex +++ b/lib/pleroma/web/twitter_api/twitter_api_controller.ex @@ -529,6 +529,13 @@ def search(%{assigns: %{user: user}} = conn, %{"q" => _query} = params) 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) diff --git a/test/web/twitter_api/twitter_api_controller_test.exs b/test/web/twitter_api/twitter_api_controller_test.exs index 788e3a6eb..07f9c7814 100644 --- a/test/web/twitter_api/twitter_api_controller_test.exs +++ b/test/web/twitter_api/twitter_api_controller_test.exs @@ -1211,4 +1211,19 @@ test "it denies a friend request" 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 From 27aa136aacb43084089234df59649bc81e53d63c Mon Sep 17 00:00:00 2001 From: lain Date: Wed, 14 Nov 2018 20:41:12 +0100 Subject: [PATCH 2/3] Format. --- lib/pleroma/web/router.ex | 1 - test/web/twitter_api/twitter_api_controller_test.exs | 9 +++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/pleroma/web/router.ex b/lib/pleroma/web/router.ex index 5ca867871..462369806 100644 --- a/lib/pleroma/web/router.ex +++ b/lib/pleroma/web/router.ex @@ -248,7 +248,6 @@ defmodule Pleroma.Web.Router do ) get("/statuses/networkpublic_timeline", TwitterAPI.Controller, :public_and_external_timeline) - end scope "/api", Pleroma.Web, as: :twitter_api_search do diff --git a/test/web/twitter_api/twitter_api_controller_test.exs b/test/web/twitter_api/twitter_api_controller_test.exs index 07f9c7814..1d45d9437 100644 --- a/test/web/twitter_api/twitter_api_controller_test.exs +++ b/test/web/twitter_api/twitter_api_controller_test.exs @@ -1218,12 +1218,13 @@ test "it returns users, ordered by similarity", %{conn: conn} do 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) + 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) + assert [user.id, user_two.id, user_three.id] == Enum.map(resp, fn %{"id" => id} -> id end) end end end From e8d8c84f795152a0522e6848e75b47dcbce5aa89 Mon Sep 17 00:00:00 2001 From: lain Date: Fri, 16 Nov 2018 18:31:32 +0100 Subject: [PATCH 3/3] Add better test for user search functionlity. --- lib/pleroma/user.ex | 2 +- test/user_test.exs | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/lib/pleroma/user.ex b/lib/pleroma/user.ex index be634a8e1..6e1d5559d 100644 --- a/lib/pleroma/user.ex +++ b/lib/pleroma/user.ex @@ -498,7 +498,7 @@ def get_recipients_from_activity(%Activity{recipients: to}) do Repo.all(query) end - def search(query, resolve) do + def search(query, resolve \\ false) do # strip the beginning @ off if there is a query query = String.trim_leading(query, "@") diff --git a/test/user_test.exs b/test/user_test.exs index 7dec3462f..231f1d94d 100644 --- a/test/user_test.exs +++ b/test/user_test.exs @@ -578,4 +578,16 @@ test "User.delete() plugs any possible zombie objects" do assert cached_user != user end end + + describe "User.search" do + test "finds a user, ranking by similarity" do + user = insert(:user, %{name: "lain"}) + user_two = insert(:user, %{name: "ean"}) + user_three = insert(:user, %{name: "ebn", nickname: "lain@mastodon.social"}) + user_four = insert(:user, %{nickname: "lain@pleroma.soykaf.com"}) + + assert user_four == + User.search("lain@ple") |> List.first() |> Map.put(:search_distance, nil) + end + end end