2017-03-21 08:21:52 +00:00
|
|
|
defmodule Pleroma.Web.ActivityPub.ActivityPub do
|
|
|
|
alias Pleroma.Repo
|
2017-03-29 00:05:51 +00:00
|
|
|
alias Pleroma.{Activity, Object, Upload}
|
2017-03-21 16:53:20 +00:00
|
|
|
import Ecto.Query
|
2017-03-21 08:21:52 +00:00
|
|
|
|
|
|
|
def insert(map) when is_map(map) do
|
2017-03-23 16:56:49 +00:00
|
|
|
map = Map.put_new_lazy(map, "id", &generate_activity_id/0)
|
|
|
|
|
2017-03-23 20:22:49 +00:00
|
|
|
map = if map["object"] do
|
|
|
|
object = Map.put_new_lazy(map["object"], "id", &generate_object_id/0)
|
|
|
|
Map.put(map, "object", object)
|
|
|
|
else
|
|
|
|
map
|
|
|
|
end
|
|
|
|
|
2017-03-21 08:21:52 +00:00
|
|
|
Repo.insert(%Activity{data: map})
|
|
|
|
end
|
2017-03-21 16:53:20 +00:00
|
|
|
|
2017-03-23 16:56:49 +00:00
|
|
|
def generate_activity_id do
|
2017-03-23 22:34:10 +00:00
|
|
|
generate_id("activities")
|
|
|
|
end
|
|
|
|
|
|
|
|
def generate_context_id do
|
|
|
|
generate_id("contexts")
|
2017-03-23 16:56:49 +00:00
|
|
|
end
|
|
|
|
|
2017-03-23 20:22:49 +00:00
|
|
|
def generate_object_id do
|
2017-03-23 22:34:10 +00:00
|
|
|
generate_id("objects")
|
|
|
|
end
|
|
|
|
|
|
|
|
def generate_id(type) do
|
2017-03-23 20:22:49 +00:00
|
|
|
host =
|
|
|
|
Application.get_env(:pleroma, Pleroma.Web.Endpoint)
|
|
|
|
|> Keyword.fetch!(:url)
|
|
|
|
|> Keyword.fetch!(:host)
|
2017-03-29 00:05:51 +00:00
|
|
|
|
|
|
|
protocol = Application.get_env(:pleroma, Pleroma.Web.Endpoint) |> Keyword.fetch!(:protocol)
|
|
|
|
"#{protocol}://#{host}/#{type}/#{Ecto.UUID.generate}"
|
2017-03-23 20:22:49 +00:00
|
|
|
end
|
|
|
|
|
2017-03-21 19:31:48 +00:00
|
|
|
def fetch_public_activities(opts \\ %{}) do
|
2017-03-22 13:45:17 +00:00
|
|
|
public = ["https://www.w3.org/ns/activitystreams#Public"]
|
|
|
|
fetch_activities(public, opts)
|
|
|
|
end
|
|
|
|
|
|
|
|
def fetch_activities(recipients, opts \\ %{}) do
|
2017-03-21 20:09:20 +00:00
|
|
|
since_id = opts["since_id"] || 0
|
2017-03-21 19:31:48 +00:00
|
|
|
|
2017-03-21 16:53:20 +00:00
|
|
|
query = from activity in Activity,
|
2017-03-21 19:22:05 +00:00
|
|
|
limit: 20,
|
|
|
|
order_by: [desc: :inserted_at]
|
2017-03-21 16:53:20 +00:00
|
|
|
|
2017-03-22 13:45:17 +00: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 16:53:20 +00:00
|
|
|
Repo.all(query)
|
2017-03-21 19:22:05 +00:00
|
|
|
|> Enum.reverse
|
2017-03-21 16:53:20 +00:00
|
|
|
end
|
2017-03-23 23:09:08 +00:00
|
|
|
|
|
|
|
def fetch_activities_for_context(context) do
|
|
|
|
query = from activity in Activity,
|
|
|
|
where: fragment("? @> ?", activity.data, ^%{ context: context })
|
|
|
|
Repo.all(query)
|
|
|
|
end
|
2017-03-29 00:05:51 +00:00
|
|
|
|
|
|
|
def upload(%Plug.Upload{} = file) do
|
|
|
|
data = Upload.store(file)
|
|
|
|
Repo.insert(%Object{data: data})
|
|
|
|
end
|
2017-03-21 08:21:52 +00:00
|
|
|
end
|