TwitterAPI: Add search.

This commit is contained in:
Roger Braun 2017-09-16 14:33:47 +02:00
parent 44693c100d
commit 5d1f8dcd1c
4 changed files with 50 additions and 0 deletions

View file

@ -119,6 +119,7 @@ defmodule Pleroma.Web.Router do
post "/account/register", TwitterAPI.Controller, :register post "/account/register", TwitterAPI.Controller, :register
end end
get "/search", TwitterAPI.Controller, :search
get "/externalprofile/show", TwitterAPI.Controller, :external_profile get "/externalprofile/show", TwitterAPI.Controller, :external_profile
end end

View file

@ -5,6 +5,7 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPI do
alias Pleroma.Web.TwitterAPI.UserView alias Pleroma.Web.TwitterAPI.UserView
alias Pleroma.Web.{OStatus, CommonAPI} alias Pleroma.Web.{OStatus, CommonAPI}
alias Pleroma.Formatter alias Pleroma.Formatter
import Ecto.Query
@httpoison Application.get_env(:pleroma, :httpoison) @httpoison Application.get_env(:pleroma, :httpoison)
@ -192,6 +193,32 @@ defmodule Pleroma.Web.TwitterAPI.TwitterAPI do
end end
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 defp activities_to_statuses(activities, opts) do
Enum.map(activities, fn(activity) -> Enum.map(activities, fn(activity) ->
activity_to_status(activity, opts) activity_to_status(activity, opts)

View file

@ -263,6 +263,11 @@ defmodule Pleroma.Web.TwitterAPI.Controller do
end end
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 defp bad_request_reply(conn, error_message) do
json = error_json(conn, error_message) json = error_json(conn, error_message)
json_reply(conn, 400, json) json_reply(conn, 400, json)

View file

@ -5,6 +5,7 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do
alias Pleroma.{Repo, Activity, User, Object} alias Pleroma.{Repo, Activity, User, Object}
alias Pleroma.Web.ActivityPub.ActivityPub alias Pleroma.Web.ActivityPub.ActivityPub
alias Pleroma.Web.TwitterAPI.UserView alias Pleroma.Web.TwitterAPI.UserView
alias Pleroma.Web.CommonAPI
import Pleroma.Factory import Pleroma.Factory
@ -473,4 +474,20 @@ defmodule Pleroma.Web.TwitterAPI.ControllerTest do
header_content = "Basic " <> Base.encode64("#{username}:#{password}") header_content = "Basic " <> Base.encode64("#{username}:#{password}")
put_req_header(conn, "authorization", header_content) put_req_header(conn, "authorization", header_content)
end 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 end