From c84723b67956f731801c98a1335128d8cdbf9291 Mon Sep 17 00:00:00 2001 From: eal Date: Tue, 14 Nov 2017 15:41:16 +0200 Subject: [PATCH 1/2] MastoAPI: Add media timelines. --- lib/pleroma/web/activity_pub/activity_pub.ex | 7 +++++++ .../mastodon_api_controller_test.exs | 17 +++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/lib/pleroma/web/activity_pub/activity_pub.ex b/lib/pleroma/web/activity_pub/activity_pub.ex index 35536a1e4..5b02fc2c6 100644 --- a/lib/pleroma/web/activity_pub/activity_pub.ex +++ b/lib/pleroma/web/activity_pub/activity_pub.ex @@ -159,6 +159,12 @@ defp restrict_favorited_by(query, %{"favorited_by" => ap_id}) do end defp restrict_favorited_by(query, _), do: query + defp restrict_media(query, %{"only_media" => true}) do + from activity in query, + where: fragment("not (? #> '{\"object\",\"attachment\"}' = ?)", activity.data, ^[]) + end + defp restrict_media(query, _), do: query + # Only search through last 100_000 activities by default defp restrict_recent(query, %{"whole_db" => true}), do: query defp restrict_recent(query, _) do @@ -191,6 +197,7 @@ def fetch_activities(recipients, opts \\ %{}) do |> restrict_favorited_by(opts) |> restrict_recent(opts) |> restrict_blocked(opts) + |> restrict_media(opts) |> Repo.all |> Enum.reverse end diff --git a/test/web/mastodon_api/mastodon_api_controller_test.exs b/test/web/mastodon_api/mastodon_api_controller_test.exs index 25d92d010..1b8709941 100644 --- a/test/web/mastodon_api/mastodon_api_controller_test.exs +++ b/test/web/mastodon_api/mastodon_api_controller_test.exs @@ -249,6 +249,23 @@ test "gets a users statuses", %{conn: conn} do assert id == to_string(note_two.id) end + + test "gets an users media", %{conn: conn} do + note = insert(:note_activity) + user = User.get_by_ap_id(note.data["actor"]) + + file = %Plug.Upload{content_type: "image/jpg", path: Path.absname("test/fixtures/image.jpg"), filename: "an_image.jpg"} + media = TwitterAPI.upload(file, "json") + |> Poison.decode! + + {:ok, image_post} = TwitterAPI.create_status(user, %{"status" => "cofe", "media_ids" => [media["media_id"]]}) + + conn = conn + |> get("/api/v1/accounts/#{user.id}/statuses", %{"only_media" => true}) + + assert [%{"id" => id}] = json_response(conn, 200) + assert id == to_string(image_post.id) + end end describe "user relationships" do From 06c3ee3bac9abe5a369364743143618c473eb7a9 Mon Sep 17 00:00:00 2001 From: eal Date: Tue, 14 Nov 2017 15:50:23 +0200 Subject: [PATCH 2/2] Accept 1 as true. --- lib/pleroma/web/activity_pub/activity_pub.ex | 2 +- test/web/mastodon_api/mastodon_api_controller_test.exs | 8 +++++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/lib/pleroma/web/activity_pub/activity_pub.ex b/lib/pleroma/web/activity_pub/activity_pub.ex index 5b02fc2c6..9af0f553c 100644 --- a/lib/pleroma/web/activity_pub/activity_pub.ex +++ b/lib/pleroma/web/activity_pub/activity_pub.ex @@ -159,7 +159,7 @@ defp restrict_favorited_by(query, %{"favorited_by" => ap_id}) do end defp restrict_favorited_by(query, _), do: query - defp restrict_media(query, %{"only_media" => true}) do + defp restrict_media(query, %{"only_media" => val}) when val == "true" or val == "1" do from activity in query, where: fragment("not (? #> '{\"object\",\"attachment\"}' = ?)", activity.data, ^[]) end diff --git a/test/web/mastodon_api/mastodon_api_controller_test.exs b/test/web/mastodon_api/mastodon_api_controller_test.exs index 1b8709941..a00a11f8b 100644 --- a/test/web/mastodon_api/mastodon_api_controller_test.exs +++ b/test/web/mastodon_api/mastodon_api_controller_test.exs @@ -261,7 +261,13 @@ test "gets an users media", %{conn: conn} do {:ok, image_post} = TwitterAPI.create_status(user, %{"status" => "cofe", "media_ids" => [media["media_id"]]}) conn = conn - |> get("/api/v1/accounts/#{user.id}/statuses", %{"only_media" => true}) + |> get("/api/v1/accounts/#{user.id}/statuses", %{"only_media" => "true"}) + + assert [%{"id" => id}] = json_response(conn, 200) + assert id == to_string(image_post.id) + + conn = build_conn() + |> get("/api/v1/accounts/#{user.id}/statuses", %{"only_media" => "1"}) assert [%{"id" => id}] = json_response(conn, 200) assert id == to_string(image_post.id)