1
0
Fork 0
forked from mirrors/akkoma

activitypub: add outbox endpoint

This commit is contained in:
William Pitcock 2018-03-22 05:23:05 +00:00
parent 381ba256bf
commit 3fcdfb75d0
3 changed files with 63 additions and 0 deletions

View file

@ -65,6 +65,17 @@ def followers(conn, %{"nickname" => nickname}) do
end
end
def outbox(conn, %{"nickname" => nickname, "max_id" => max_id}) do
with %User{} = user <- User.get_cached_by_nickname(nickname),
{:ok, user} <- Pleroma.Web.WebFinger.ensure_keys_present(user) do
conn
|> put_resp_header("content-type", "application/activity+json")
|> json(UserView.render("outbox.json", %{user: user, max_id: max_id}))
end
end
def outbox(conn, %{"nickname" => nickname}) do outbox(conn, %{"nickname" => nickname, "max_id" => nil}) end
# TODO: Ensure that this inbox is a recipient of the message
def inbox(%{assigns: %{valid_signature: true}} = conn, params) do
Federator.enqueue(:incoming_ap_doc, params)

View file

@ -3,6 +3,8 @@ defmodule Pleroma.Web.ActivityPub.UserView do
alias Pleroma.Web.Salmon
alias Pleroma.Web.WebFinger
alias Pleroma.User
alias Pleroma.Web.ActivityPub.ActivityPub
alias Pleroma.Web.ActivityPub.Transmogrifier
alias Pleroma.Web.ActivityPub.Utils
def render("user.json", %{user: user}) do
@ -91,4 +93,53 @@ def render("followers.json", %{user: user}) do
}
|> Map.merge(Utils.make_json_ld_header())
end
def render("outbox.json", %{user: user, max_id: max_qid}) do
# XXX: technically note_count is wrong for this, but it's better than nothing
info = User.user_info(user)
params = %{
"type" => ["Create", "Announce"],
"actor_id" => user.ap_id,
"whole_db" => true,
"limit" => "10"
}
if max_qid != nil do
params = Map.put(params, "max_id", max_qid)
end
activities = ActivityPub.fetch_public_activities(params)
min_id = Enum.at(activities, 0).id
activities = Enum.reverse(activities)
max_id = Enum.at(activities, 0).id
collection = Enum.map(activities, fn (act) ->
{:ok, data} = Transmogrifier.prepare_outgoing(act.data)
data
end)
iri = "#{user.ap_id}/outbox"
page = %{
"id" => "#{iri}?max_id=#{max_id}",
"type" => "OrderedCollectionPage",
"partOf" => iri,
"totalItems" => info.note_count,
"orderedItems" => collection,
"next" => "#{iri}?max_id=#{min_id-1}",
}
if max_qid == nil do
%{
"id" => iri,
"type" => "OrderedCollection",
"totalItems" => info.note_count,
"first" => page
}
|> Map.merge(Utils.make_json_ld_header())
else
page |> Map.merge(Utils.make_json_ld_header())
end
end
end

View file

@ -255,6 +255,7 @@ def user_fetcher(username) do
get "/users/:nickname/followers", ActivityPubController, :followers
get "/users/:nickname/following", ActivityPubController, :following
get "/users/:nickname/outbox", ActivityPubController, :outbox
end
if @federating do