Add basic search.
This commit is contained in:
parent
d659fcc195
commit
9f0a2a714b
|
@ -287,6 +287,27 @@ def unfollow(%{assigns: %{user: follower}} = conn, %{"id" => id}) do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def search(%{assigns: %{user: user}} = conn, %{"q" => query}) do
|
||||||
|
q = from u in User,
|
||||||
|
where: fragment("(to_tsvector(?) || to_tsvector(?)) @@ plainto_tsquery(?)", u.nickname, u.name, ^query),
|
||||||
|
limit: 20
|
||||||
|
accounts = Repo.all(q)
|
||||||
|
|
||||||
|
q = from a in Activity,
|
||||||
|
where: fragment("?->>'type' = 'Create'", a.data),
|
||||||
|
where: fragment("to_tsvector(?->'object'->>'content') @@ plainto_tsquery(?)", a.data, ^query),
|
||||||
|
limit: 20
|
||||||
|
statuses = Repo.all(q)
|
||||||
|
|
||||||
|
res = %{
|
||||||
|
"accounts" => AccountView.render("accounts.json", users: accounts, for: user, as: :user),
|
||||||
|
"statuses" => StatusView.render("index.json", activities: statuses, for: user, as: :activity),
|
||||||
|
"hashtags" => []
|
||||||
|
}
|
||||||
|
|
||||||
|
json(conn, res)
|
||||||
|
end
|
||||||
|
|
||||||
def relationship_noop(%{assigns: %{user: user}} = conn, %{"id" => id}) do
|
def relationship_noop(%{assigns: %{user: user}} = conn, %{"id" => id}) do
|
||||||
Logger.debug("Unimplemented, returning unmodified relationship")
|
Logger.debug("Unimplemented, returning unmodified relationship")
|
||||||
with %User{} = target <- Repo.get(User, id) do
|
with %User{} = target <- Repo.get(User, id) do
|
||||||
|
|
|
@ -87,6 +87,8 @@ def user_fetcher(username) do
|
||||||
get "/accounts/:id/followers", MastodonAPIController, :followers
|
get "/accounts/:id/followers", MastodonAPIController, :followers
|
||||||
get "/accounts/:id/following", MastodonAPIController, :following
|
get "/accounts/:id/following", MastodonAPIController, :following
|
||||||
get "/accounts/:id", MastodonAPIController, :user
|
get "/accounts/:id", MastodonAPIController, :user
|
||||||
|
|
||||||
|
get "/search", MastodonAPIController, :search
|
||||||
end
|
end
|
||||||
|
|
||||||
scope "/api", Pleroma.Web do
|
scope "/api", Pleroma.Web do
|
||||||
|
|
|
@ -310,4 +310,26 @@ test "unimplemented mutes, follow_requests, blocks, domain blocks" do
|
||||||
assert [] = json_response(conn, 200)
|
assert [] = json_response(conn, 200)
|
||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
test "search", %{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"})
|
||||||
|
|
||||||
|
{:ok, activity} = CommonAPI.post(user, %{"status" => "This is about 2hu"})
|
||||||
|
{:ok, _} = CommonAPI.post(user_two, %{"status" => "This isn't"})
|
||||||
|
|
||||||
|
conn = conn
|
||||||
|
|> get("/api/v1/search", %{"q" => "2hu"})
|
||||||
|
|
||||||
|
assert results = json_response(conn, 200)
|
||||||
|
|
||||||
|
[account] = results["accounts"]
|
||||||
|
assert account["id"] == user_three.id
|
||||||
|
|
||||||
|
assert results["hashtags"] == []
|
||||||
|
|
||||||
|
[status] = results["statuses"]
|
||||||
|
assert status["id"] == activity.id
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue