From 5d1f8dcd1c3137976e382bf99058e202ed62477e Mon Sep 17 00:00:00 2001 From: Roger Braun Date: Sat, 16 Sep 2017 14:33:47 +0200 Subject: [PATCH] TwitterAPI: Add search. --- lib/pleroma/web/router.ex | 1 + lib/pleroma/web/twitter_api/twitter_api.ex | 27 +++++++++++++++++++ .../web/twitter_api/twitter_api_controller.ex | 5 ++++ .../twitter_api_controller_test.exs | 17 ++++++++++++ 4 files changed, 50 insertions(+) diff --git a/lib/pleroma/web/router.ex b/lib/pleroma/web/router.ex index e8460db16..04f4e213c 100644 --- a/lib/pleroma/web/router.ex +++ b/lib/pleroma/web/router.ex @@ -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 diff --git a/lib/pleroma/web/twitter_api/twitter_api.ex b/lib/pleroma/web/twitter_api/twitter_api.ex index 017130370..e849c9db0 100644 --- a/lib/pleroma/web/twitter_api/twitter_api.ex +++ b/lib/pleroma/web/twitter_api/twitter_api.ex @@ -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) diff --git a/lib/pleroma/web/twitter_api/twitter_api_controller.ex b/lib/pleroma/web/twitter_api/twitter_api_controller.ex index 62a2b4f50..bca74850c 100644 --- a/lib/pleroma/web/twitter_api/twitter_api_controller.ex +++ b/lib/pleroma/web/twitter_api/twitter_api_controller.ex @@ -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) diff --git a/test/web/twitter_api/twitter_api_controller_test.exs b/test/web/twitter_api/twitter_api_controller_test.exs index 2c89509ff..14c1d4df7 100644 --- a/test/web/twitter_api/twitter_api_controller_test.exs +++ b/test/web/twitter_api/twitter_api_controller_test.exs @@ -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 -- 2.45.2