post "/account/register", TwitterAPI.Controller, :register
end
+ get "/search", TwitterAPI.Controller, :search
get "/externalprofile/show", TwitterAPI.Controller, :external_profile
end
alias Pleroma.Web.TwitterAPI.UserView
alias Pleroma.Web.{OStatus, CommonAPI}
alias Pleroma.Formatter
+ import Ecto.Query
@httpoison Application.get_env(:pleroma, :httpoison)
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)
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)
alias Pleroma.{Repo, Activity, User, Object}
alias Pleroma.Web.ActivityPub.ActivityPub
alias Pleroma.Web.TwitterAPI.UserView
+ alias Pleroma.Web.CommonAPI
import Pleroma.Factory
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