Add fetching for recipients.

This commit is contained in:
Roger Braun 2017-03-22 14:45:17 +01:00
parent 2db28df4cf
commit 569d9bac17
3 changed files with 28 additions and 2 deletions

View File

@ -8,14 +8,26 @@ def insert(map) when is_map(map) do
end
def fetch_public_activities(opts \\ %{}) do
public = ["https://www.w3.org/ns/activitystreams#Public"]
fetch_activities(public, opts)
end
def fetch_activities(recipients, opts \\ %{}) do
since_id = opts["since_id"] || 0
query = from activity in Activity,
where: fragment(~s(? @> '{"to": ["https://www.w3.org/ns/activitystreams#Public"]}'), activity.data),
where: activity.id > ^since_id,
limit: 20,
order_by: [desc: :inserted_at]
query = Enum.reduce(recipients, query, fn (recipient, q) ->
map = %{ to: [recipient] }
from activity in q,
or_where: fragment(~s(? @> ?), activity.data, ^map)
end)
query = from activity in query,
where: activity.id > ^since_id
Repo.all(query)
|> Enum.reverse
end

View File

@ -22,6 +22,7 @@ def user_fetcher(username) do
scope "/api", Pleroma.Web do
pipe_through :api
get "/statuses/public_timeline.json", TwitterAPI.Controller, :public_timeline
get "/statuses/public_and_external_timeline.json", TwitterAPI.Controller, :public_timeline
end
scope "/api", Pleroma.Web do

View File

@ -15,6 +15,19 @@ test "inserts a given map into the activity database" do
end
end
describe "fetch activities for recipients" do
test "retrieve the activities for certain recipients" do
{:ok, activity_one} = ActivityBuilder.insert(%{"to" => ["someone"]})
{:ok, activity_two} = ActivityBuilder.insert(%{"to" => ["someone_else"]})
{:ok, activity_three} = ActivityBuilder.insert(%{"to" => ["noone"]})
activities = ActivityPub.fetch_activities(["someone", "someone_else"])
assert length(activities) == 2
assert activities == [activity_one, activity_two]
end
end
describe "public fetch activities" do
test "retrieves public activities" do
%{public: public} = ActivityBuilder.public_and_non_public