From 266d9c008d2a85395bb7ab773d5d548c89e7ed97 Mon Sep 17 00:00:00 2001 From: Roger Braun Date: Thu, 9 Nov 2017 16:48:45 +0100 Subject: [PATCH] MastoAPI: Fetch statuses in search. --- .../web/mastodon_api/mastodon_api_controller.ex | 12 ++++++++++-- .../mastodon_api/mastodon_api_controller_test.exs | 9 +++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex index feaf9a900..c28e20ed1 100644 --- a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex +++ b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex @@ -6,7 +6,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do alias Pleroma.Web.MastodonAPI.{StatusView, AccountView} alias Pleroma.Web.ActivityPub.ActivityPub alias Pleroma.Web.TwitterAPI.TwitterAPI - alias Pleroma.Web.CommonAPI + alias Pleroma.Web.{CommonAPI, OStatus} import Ecto.Query import Logger @@ -361,11 +361,19 @@ def blocks(%{assigns: %{user: user}} = conn, _) do def search(%{assigns: %{user: user}} = conn, %{"q" => query} = params) do accounts = User.search(query, params["resolve"] == "true") + fetched = if Regex.match?(~r/https?:/, query) do + with {:ok, activities} <- OStatus.fetch_activity_from_url(query) do + activities + else + _e -> [] + end + end || [] + 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: 20 - statuses = Repo.all(q) + statuses = Repo.all(q) ++ fetched res = %{ "accounts" => AccountView.render("accounts.json", users: accounts, for: user, as: :user), diff --git a/test/web/mastodon_api/mastodon_api_controller_test.exs b/test/web/mastodon_api/mastodon_api_controller_test.exs index d118026eb..8bbdecaa8 100644 --- a/test/web/mastodon_api/mastodon_api_controller_test.exs +++ b/test/web/mastodon_api/mastodon_api_controller_test.exs @@ -386,6 +386,15 @@ test "search", %{conn: conn} do assert status["id"] == to_string(activity.id) end + test "search fetches remote statuses", %{conn: conn} do + conn = conn + |> get("/api/v1/search", %{"q" => "https://shitposter.club/notice/2827873"}) + assert results = json_response(conn, 200) + + [status] = results["statuses"] + assert status["uri"] == "tag:shitposter.club,2017-05-05:noticeId=2827873:objectType=comment" + end + test "search fetches remote accounts", %{conn: conn} do conn = conn |> get("/api/v1/search", %{"q" => "shp@social.heldscal.la", "resolve" => "true"})