2017-03-21 09:21:52 +01:00
|
|
|
defmodule Pleroma.Web.ActivityPub.ActivityPub do
|
|
|
|
alias Pleroma.Repo
|
|
|
|
alias Pleroma.Activity
|
2017-03-21 17:53:20 +01:00
|
|
|
import Ecto.Query
|
2017-03-21 09:21:52 +01:00
|
|
|
|
|
|
|
def insert(map) when is_map(map) do
|
|
|
|
Repo.insert(%Activity{data: map})
|
|
|
|
end
|
2017-03-21 17:53:20 +01:00
|
|
|
|
2017-03-21 20:31:48 +01:00
|
|
|
def fetch_public_activities(opts \\ %{}) do
|
2017-03-22 14:45:17 +01:00
|
|
|
public = ["https://www.w3.org/ns/activitystreams#Public"]
|
|
|
|
fetch_activities(public, opts)
|
|
|
|
end
|
|
|
|
|
|
|
|
def fetch_activities(recipients, opts \\ %{}) do
|
2017-03-21 21:09:20 +01:00
|
|
|
since_id = opts["since_id"] || 0
|
2017-03-21 20:31:48 +01:00
|
|
|
|
2017-03-21 17:53:20 +01:00
|
|
|
query = from activity in Activity,
|
2017-03-21 20:22:05 +01:00
|
|
|
limit: 20,
|
|
|
|
order_by: [desc: :inserted_at]
|
2017-03-21 17:53:20 +01:00
|
|
|
|
2017-03-22 14:45:17 +01:00
|
|
|
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
|
|
|
|
|
2017-03-21 17:53:20 +01:00
|
|
|
Repo.all(query)
|
2017-03-21 20:22:05 +01:00
|
|
|
|> Enum.reverse
|
2017-03-21 17:53:20 +01:00
|
|
|
end
|
2017-03-21 09:21:52 +01:00
|
|
|
end
|