Add tag timelines.
authorRoger Braun <rbraun@Bobble.local>
Thu, 14 Sep 2017 11:22:09 +0000 (13:22 +0200)
committerRoger Braun <rbraun@Bobble.local>
Thu, 14 Sep 2017 11:22:09 +0000 (13:22 +0200)
lib/pleroma/web/activity_pub/activity_pub.ex
lib/pleroma/web/mastodon_api/mastodon_api_controller.ex
lib/pleroma/web/router.ex
test/web/mastodon_api/mastodon_api_controller_test.exs

index 5acbb859eda689ae2aad298356fe3ad535edd076..2a8034457d0c9afe62cec2c6f412672e727447b8 100644 (file)
@@ -111,6 +111,12 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
   end
   defp restrict_since(query, _), do: query
 
+  defp restrict_tag(query, %{"tag" => tag}) do
+    from activity in query,
+      where: fragment("? <@ (? #> '{\"object\",\"tag\"}')", ^tag, activity.data)
+  end
+  defp restrict_tag(query, _), do: query
+
   defp restrict_recipients(query, recipients) do
     Enum.reduce(recipients, query, fn (recipient, q) ->
       map = %{ to: [recipient] }
@@ -148,6 +154,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPub do
 
     base_query
     |> restrict_recipients(recipients)
+    |> restrict_tag(opts)
     |> restrict_since(opts)
     |> restrict_local(opts)
     |> restrict_max(opts)
index 0f7674f5dbe2c9ce1ec09629ce5a5cba5ca846ef..4a5bbb7b6d282a8d76b527b122912cdf7dfc815b 100644 (file)
@@ -230,6 +230,18 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do
     end
   end
 
+  def hashtag_timeline(%{assigns: %{user: user}} = conn, params) do
+    params = params
+    |> Map.put("type", "Create")
+    |> Map.put("local_only", !!params["local"])
+
+    activities = ActivityPub.fetch_public_activities(params)
+    |> Enum.reverse
+
+    conn
+    |> render(StatusView, "index.json", %{activities: activities, for: user, as: :activity})
+  end
+
   def empty_array(conn, _) do
     Logger.debug("Unimplemented, returning an empty array")
     json(conn, [])
index 05d1e54b56a812d297484d9f2f3fe891fbf7400d..5c46d3ca20a2c71322e57f3e71b589e9fd745fc5 100644 (file)
@@ -65,6 +65,7 @@ defmodule Pleroma.Web.Router do
     post "/apps", MastodonAPIController, :create_app
 
     get "/timelines/public", MastodonAPIController, :public_timeline
+    get "/timelines/tag/:tag", MastodonAPIController, :hashtag_timeline
 
     get "/statuses/:id", MastodonAPIController, :get_status
     get "/statuses/:id/context", MastodonAPIController, :get_context
index d88714cf2c71efbd6f0fddac99a6b542e7f5584c..25b4f2b37796972bd3a511b30897ad5a77176d7c 100644 (file)
@@ -186,7 +186,6 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do
     test "returns the relationships for the current user", %{conn: conn} do
       user = insert(:user)
       other_user = insert(:user)
-
       {:ok, user} = User.follow(user, other_user)
 
       conn = conn
@@ -227,4 +226,18 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do
 
     assert media["type"] == "image"
   end
+
+  test "hashtag timeline", %{conn: conn} do
+    following = insert(:user)
+
+    {:ok, activity} = TwitterAPI.create_status(following, %{"status" => "test #2hu"})
+    {:ok, [_activity]} = OStatus.fetch_activity_from_url("https://shitposter.club/notice/2827873")
+
+    conn = conn
+    |> get("/api/v1/timelines/tag/2hu")
+
+    assert [%{"id" => id}] = json_response(conn, 200)
+
+    assert id == activity.id
+  end
 end