TwitterAPI: Add search.
This commit is contained in:
parent
44693c100d
commit
5d1f8dcd1c
@ -119,6 +119,7 @@ defmodule Pleroma.Web.Router do
|
||||
post "/account/register", TwitterAPI.Controller, :register
|
||||
end
|
||||
|
||||
get "/search", TwitterAPI.Controller, :search
|
||||
get "/externalprofile/show", TwitterAPI.Controller, :external_profile
|
||||
end
|
||||
|
||||
|
@ -5,6 +5,7 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPI do
|
||||
alias Pleroma.Web.TwitterAPI.UserView
|
||||
alias Pleroma.Web.{OStatus, CommonAPI}
|
||||
alias Pleroma.Formatter
|
||||
import Ecto.Query
|
||||
|
||||
@httpoison Application.get_env(:pleroma, :httpoison)
|
||||
|
||||
@ -192,6 +193,32 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPI do
|
||||
end
|
||||
end
|
||||
|
||||
defp parse_int(string, default \\ nil)
|
||||
defp parse_int(string, default) when is_binary(string) do
|
||||
with {n, _} <- Integer.parse(string) do
|
||||
n
|
||||
else
|
||||
_e -> default
|
||||
end
|
||||
end
|
||||
defp parse_int(_, default), do: default
|
||||
|
||||
def search(user, %{"q" => query} = params) do
|
||||
limit = parse_int(params["rpp"], 20)
|
||||
page = parse_int(params["page"], 1)
|
||||
offset = (page - 1) * limit
|
||||
|
||||
q = from a in Activity,
|
||||
where: fragment("?->>'type' = 'Create'", a.data),
|
||||
where: fragment("to_tsvector('english', ?->'object'->>'content') @@ plainto_tsquery('english', ?)", a.data, ^query),
|
||||
limit: ^limit,
|
||||
offset: ^offset,
|
||||
order_by: [desc: :id]
|
||||
|
||||
activities = Repo.all(q)
|
||||
activities_to_statuses(activities, %{for: user})
|
||||
end
|
||||
|
||||
defp activities_to_statuses(activities, opts) do
|
||||
Enum.map(activities, fn(activity) ->
|
||||
activity_to_status(activity, opts)
|
||||
|
@ -263,6 +263,11 @@ defmodule Pleroma.Web.TwitterAPI.Controller do
|
||||
end
|
||||
end
|
||||
|
||||
def search(%{assigns: %{user: user}} = conn, %{"q" => query} = params) do
|
||||
conn
|
||||
|> json(TwitterAPI.search(user, params))
|
||||
end
|
||||
|
||||
defp bad_request_reply(conn, error_message) do
|
||||
json = error_json(conn, error_message)
|
||||
json_reply(conn, 400, json)
|
||||
|
@ -5,6 +5,7 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do
|
||||
alias Pleroma.{Repo, Activity, User, Object}
|
||||
alias Pleroma.Web.ActivityPub.ActivityPub
|
||||
alias Pleroma.Web.TwitterAPI.UserView
|
||||
alias Pleroma.Web.CommonAPI
|
||||
|
||||
import Pleroma.Factory
|
||||
|
||||
@ -473,4 +474,20 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do
|
||||
header_content = "Basic " <> Base.encode64("#{username}:#{password}")
|
||||
put_req_header(conn, "authorization", header_content)
|
||||
end
|
||||
|
||||
describe "GET /api/search.json" do
|
||||
test "it returns search results", %{conn: conn} do
|
||||
user = insert(:user)
|
||||
user_two = insert(:user, %{nickname: "shp@shitposter.club"})
|
||||
|
||||
{:ok, activity} = CommonAPI.post(user, %{"status" => "This is about 2hu"})
|
||||
{:ok, _} = CommonAPI.post(user_two, %{"status" => "This isn't"})
|
||||
|
||||
conn = conn
|
||||
|> get("/api/search.json", %{"q" => "2hu", "page" => "1", "rpp" => "1"})
|
||||
|
||||
assert [status] = json_response(conn, 200)
|
||||
assert status["id"] == activity.id
|
||||
end
|
||||
end
|
||||
end
|
||||
|
Loading…
Reference in New Issue
Block a user