Add limiting to activity pub fetching.

This commit is contained in:
Roger Braun 2017-03-21 20:22:05 +01:00
parent ad303783af
commit d4cf273f28
3 changed files with 39 additions and 16 deletions

View File

@ -9,8 +9,11 @@ def insert(map) when is_map(map) do
def fetch_public_activities do def fetch_public_activities do
query = from activity in Activity, query = from activity in Activity,
where: fragment(~s(? @> '{"to": ["https://www.w3.org/ns/activitystreams#Public"]}'), activity.data) where: fragment(~s(? @> '{"to": ["https://www.w3.org/ns/activitystreams#Public"]}'), activity.data),
limit: 20,
order_by: [desc: :inserted_at]
Repo.all(query) Repo.all(query)
|> Enum.reverse
end end
end end

View File

@ -1,11 +1,10 @@
defmodule Pleroma.Builders.ActivityBuilder do defmodule Pleroma.Builders.ActivityBuilder do
alias Pleroma.Builders.UserBuilder alias Pleroma.Builders.UserBuilder
alias Pleroma.Web.ActivityPub.ActivityPub alias Pleroma.Web.ActivityPub.ActivityPub
alias Pleroma.User
def public_and_non_public do def build(data \\ %{}, opts \\ %{}) do
{:ok, user} = UserBuilder.insert user = opts[:user] || UserBuilder.build
public = %{ activity = %{
"id" => 1, "id" => 1,
"actor" => user.ap_id, "actor" => user.ap_id,
"to" => ["https://www.w3.org/ns/activitystreams#Public"], "to" => ["https://www.w3.org/ns/activitystreams#Public"],
@ -14,16 +13,26 @@ def public_and_non_public do
"content" => "test" "content" => "test"
} }
} }
Map.merge(activity, data)
end
non_public = %{ def insert(data \\ %{}, opts \\ %{}) do
"id" => 2, activity = build(data, opts)
"actor" => user.ap_id, ActivityPub.insert(activity)
"to" => [], end
"object" => %{
"type" => "Note", def insert_list(times, data \\ %{}, opts \\ %{}) do
"content" => "test" Enum.map(1..times, fn (n) ->
} {:ok, activity} = insert(%{"id" => n})
} activity
end)
end
def public_and_non_public do
{:ok, user} = UserBuilder.insert
public = build(%{"id" => 1}, %{user: user})
non_public = build(%{"id" => 2, "to" => []}, %{user: user})
{:ok, public} = ActivityPub.insert(public) {:ok, public} = ActivityPub.insert(public)
{:ok, non_public} = ActivityPub.insert(non_public) {:ok, non_public} = ActivityPub.insert(non_public)

View File

@ -15,13 +15,24 @@ test "inserts a given map into the activity database" do
end end
end end
describe "fetch activities" do describe "public fetch activities" do
test "retrieves all public activities" do test "retrieves public activities" do
%{public: public} = ActivityBuilder.public_and_non_public %{public: public} = ActivityBuilder.public_and_non_public
activities = ActivityPub.fetch_public_activities activities = ActivityPub.fetch_public_activities
assert length(activities) == 1 assert length(activities) == 1
assert Enum.at(activities, 0) == public assert Enum.at(activities, 0) == public
end end
test "retrieves a maximum of 20 activities" do
activities = ActivityBuilder.insert_list(30)
last_expected = List.last(activities)
activities = ActivityPub.fetch_public_activities
last = List.last(activities)
assert length(activities) == 20
assert last == last_expected
end
end end
end end