Move user search to User module.

This commit is contained in:
Roger Braun 2017-10-30 19:23:16 +01:00
parent efe12e1a73
commit 502cb38cd6
4 changed files with 27 additions and 15 deletions

View file

@ -274,4 +274,14 @@ defmodule Pleroma.User do
Repo.all(query) Repo.all(query)
end end
def search(query, resolve) do
if resolve do
User.get_or_fetch_by_nickname(query)
end
q = from u in User,
where: fragment("(to_tsvector('english', ?) || to_tsvector('english', ?)) @@ plainto_tsquery('english', ?)", u.nickname, u.name, ^query),
limit: 20
Repo.all(q)
end
end end

View file

@ -308,19 +308,8 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do
end end
end end
def dousersearch(%{assigns: %{user: user}} = conn, %{"q" => query} = params) do
if params["resolve"] == "true" do
User.get_or_fetch_by_nickname(query)
end
q = from u in User,
where: fragment("(to_tsvector('english', ?) || to_tsvector('english', ?)) @@ plainto_tsquery('english', ?)", u.nickname, u.name, ^query),
limit: 20
accounts = Repo.all(q)
end
def search(%{assigns: %{user: user}} = conn, %{"q" => query} = params) do def search(%{assigns: %{user: user}} = conn, %{"q" => query} = params) do
accounts = Pleroma.Web.MastodonAPI.MastodonAPIController.dousersearch(conn, params) accounts = User.search(query, params["resolve"] == "true")
q = from a in Activity, q = from a in Activity,
where: fragment("?->>'type' = 'Create'", a.data), where: fragment("?->>'type' = 'Create'", a.data),
@ -337,8 +326,8 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do
json(conn, res) json(conn, res)
end end
def accountsearch(%{assigns: %{user: user}} = conn, %{"q" => query} = params) do def account_search(%{assigns: %{user: user}} = conn, %{"q" => query} = params) do
accounts = Pleroma.Web.MastodonAPI.MastodonAPIController.dousersearch(conn, params) accounts = User.search(query, params["resolve"] == "true")
res = AccountView.render("accounts.json", users: accounts, for: user, as: :user) res = AccountView.render("accounts.json", users: accounts, for: user, as: :user)

View file

@ -55,7 +55,7 @@ defmodule Pleroma.Web.Router do
get "/accounts/verify_credentials", MastodonAPIController, :verify_credentials get "/accounts/verify_credentials", MastodonAPIController, :verify_credentials
get "/accounts/relationships", MastodonAPIController, :relationships get "/accounts/relationships", MastodonAPIController, :relationships
get "/accounts/search", MastodonAPIController, :accountsearch get "/accounts/search", MastodonAPIController, :account_search
post "/accounts/:id/follow", MastodonAPIController, :follow post "/accounts/:id/follow", MastodonAPIController, :follow
post "/accounts/:id/unfollow", MastodonAPIController, :unfollow post "/accounts/:id/unfollow", MastodonAPIController, :unfollow
post "/accounts/:id/block", MastodonAPIController, :relationship_noop post "/accounts/:id/block", MastodonAPIController, :relationship_noop

View file

@ -319,6 +319,19 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do
end) end)
end end
test "account seach", %{conn: conn} do
user = insert(:user)
user_two = insert(:user, %{nickname: "shp@shitposter.club"})
user_three = insert(:user, %{nickname: "shp@heldscal.la", name: "I love 2hu"})
conn = conn
|> assign(:user, user)
|> get("/api/v1/accounts/search", %{"q" => "2hu"})
assert [account] = json_response(conn, 200)
assert account["id"] == user_three.id
end
test "search", %{conn: conn} do test "search", %{conn: conn} do
user = insert(:user) user = insert(:user)
user_two = insert(:user, %{nickname: "shp@shitposter.club"}) user_two = insert(:user, %{nickname: "shp@shitposter.club"})