add bubble timeline #100

Merged
floatingghost merged 4 commits from bubble-keikaku into develop 2022-07-22 14:55:38 +00:00
6 changed files with 112 additions and 3 deletions
Showing only changes of commit 359e399b08 - Show all commits

View file

@ -951,8 +951,8 @@
%{ %{
key: :local_bubble, key: :local_bubble,
type: {:list, :string}, type: {:list, :string},
description: "List of instances that make up your local bubble (closely-related instances). Used to populate the 'bubble' timeline.", description:
suggestions: ["otp.akkoma.dev"] "List of instances that make up your local bubble (closely-related instances). Used to populate the 'bubble' timeline (domain only)."
} }
] ]
}, },

View file

@ -1154,6 +1154,13 @@ defp restrict_instance(query, %{instance: instance}) when is_binary(instance) do
) )
end end
defp restrict_instance(query, %{instance: instance}) when is_list(instance) do
from(
activity in query,
where: fragment("split_part(actor::text, '/'::text, 3) = ANY(?)", ^instance)
)
end
defp restrict_instance(query, _), do: query defp restrict_instance(query, _), do: query
defp restrict_filtered(query, %{user: %User{} = user}) do defp restrict_filtered(query, %{user: %User{} = user}) do

View file

@ -75,6 +75,26 @@ def public_operation do
} }
end end
def bubble_operation do
%Operation{
tags: ["Timelines"],
summary: "Bubble timeline",
security: [%{"oAuth" => ["read:statuses"]}],
parameters: [
only_media_param(),
remote_param(),
with_muted_param(),
exclude_visibilities_param(),
reply_visibility_param() | pagination_params()
],
operationId: "TimelineController.bubble",
responses: %{
200 => Operation.response("Array of Status", "application/json", array_of_statuses()),
401 => Operation.response("Error", "application/json", ApiError)
}
}
end
def hashtag_operation do def hashtag_operation do
%Operation{ %Operation{
tags: ["Timelines"], tags: ["Timelines"],

View file

@ -26,8 +26,9 @@ defmodule Pleroma.Web.MastodonAPI.TimelineController do
plug(RateLimiter, [name: :timeline, bucket_name: :home_timeline] when action == :home) plug(RateLimiter, [name: :timeline, bucket_name: :home_timeline] when action == :home)
plug(RateLimiter, [name: :timeline, bucket_name: :hashtag_timeline] when action == :hashtag) plug(RateLimiter, [name: :timeline, bucket_name: :hashtag_timeline] when action == :hashtag)
plug(RateLimiter, [name: :timeline, bucket_name: :list_timeline] when action == :list) plug(RateLimiter, [name: :timeline, bucket_name: :list_timeline] when action == :list)
plug(RateLimiter, [name: :timeline, bucket_name: :bubble_timeline] when action == :bubble)
plug(OAuthScopesPlug, %{scopes: ["read:statuses"]} when action in [:home, :direct]) plug(OAuthScopesPlug, %{scopes: ["read:statuses"]} when action in [:home, :direct, :bubble])
plug(OAuthScopesPlug, %{scopes: ["read:lists"]} when action == :list) plug(OAuthScopesPlug, %{scopes: ["read:lists"]} when action == :list)
plug( plug(
@ -125,6 +126,33 @@ def public(%{assigns: %{user: user}} = conn, params) do
end end
end end
# GET /api/v1/timelines/bubble
def bubble(%{assigns: %{user: user}} = conn, params) do
bubble_instances = Config.get([:instance, :local_bubble], [])
if is_nil(user) do
fail_on_bad_auth(conn)
else
activities =
params
|> Map.put(:type, ["Create"])
|> Map.put(:blocking_user, user)
|> Map.put(:muting_user, user)
|> Map.put(:reply_filtering_user, user)
|> Map.put(:instance, bubble_instances)
|> ActivityPub.fetch_public_activities()
conn
|> add_link_headers(activities)
|> render("index.json",
activities: activities,
for: user,
as: :activity,
with_muted: Map.get(params, :with_muted, false)
)
end
end
defp fail_on_bad_auth(conn) do defp fail_on_bad_auth(conn) do
render_error(conn, :unauthorized, "authorization required for timeline view") render_error(conn, :unauthorized, "authorization required for timeline view")
end end

View file

@ -560,6 +560,7 @@ defmodule Pleroma.Web.Router do
get("/timelines/home", TimelineController, :home) get("/timelines/home", TimelineController, :home)
get("/timelines/direct", TimelineController, :direct) get("/timelines/direct", TimelineController, :direct)
get("/timelines/list/:list_id", TimelineController, :list) get("/timelines/list/:list_id", TimelineController, :list)
get("/timelines/bubble", TimelineController, :bubble)
get("/announcements", AnnouncementController, :index) get("/announcements", AnnouncementController, :index)
post("/announcements/:id/dismiss", AnnouncementController, :mark_read) post("/announcements/:id/dismiss", AnnouncementController, :mark_read)

View file

@ -994,6 +994,59 @@ test "with `%{local: true, federated: false}`, forbids unauthenticated access to
end end
end end
describe "bubble" do
setup do: oauth_access(["read:statuses"])
test "it returns nothing if no bubble is configured", %{user: user, conn: conn} do
clear_config([:instance, :local_bubble], [])
{:ok, _} = CommonAPI.post(user, %{status: ".", visibility: "public"})
conn = get(conn, "/api/v1/timelines/bubble")
assert [] = json_response_and_validate_schema(conn, :ok)
end
test "filtering", %{conn: conn, user: user} do
clear_config([:instance, :local_bubble], [])
local_user = insert(:user)
remote_user = insert(:user, %{ap_id: "https://example.com/users/remote_user"})
{:ok, user, local_user} = User.follow(user, local_user)
{:ok, _user, remote_user} = User.follow(user, remote_user)
{:ok, local_activity} = CommonAPI.post(local_user, %{status: "Status"})
remote_activity = create_remote_activity(remote_user)
resp =
conn
|> get("/api/v1/timelines/bubble")
|> json_response_and_validate_schema(200)
|> Enum.map(& &1["id"])
assert Enum.empty?(resp)
clear_config([:instance, :local_bubble], ["localhost:4001"])
one_instance =
conn
|> get("/api/v1/timelines/bubble")
|> json_response_and_validate_schema(200)
|> Enum.map(& &1["id"])
assert local_activity.id in one_instance
clear_config([:instance, :local_bubble], ["localhost:4001", "example.com"])
two_instances =
conn
|> get("/api/v1/timelines/bubble")
|> json_response_and_validate_schema(200)
|> Enum.map(& &1["id"])
assert local_activity.id in two_instances
assert remote_activity.id in two_instances
end
end
defp create_remote_activity(user) do defp create_remote_activity(user) do
obj = obj =
insert(:note, %{ insert(:note, %{