mirror of
https://git.pleroma.social/pleroma/pleroma.git
synced 2025-04-06 02:29:51 +00:00
Merge branch 'develop' into 'develop'
Draft: feat: replies collection views for object See merge request pleroma/pleroma!4322
This commit is contained in:
commit
6b98110758
1 changed files with 48 additions and 0 deletions
|
@ -37,4 +37,52 @@ defmodule Pleroma.Web.ActivityPub.ObjectView do
|
|||
|
||||
Map.merge(base, additional)
|
||||
end
|
||||
|
||||
def render("replies.json", %{object: object, page: page} = opts) do
|
||||
query = Object.replies(object)
|
||||
query = from(object in query, select: [:ap_id])
|
||||
replies = Repo.all(query)
|
||||
|
||||
total = length(replies)
|
||||
|
||||
collection(replies, "#{object.ap_id}/replies", page, true, total)
|
||||
|> Map.merge(Utils.make_json_ld_header())
|
||||
end
|
||||
|
||||
def render("replies.json", %{object: object} = opts) do
|
||||
query = Object.replies(object)
|
||||
query = from(object in query, select: [:ap_id])
|
||||
replies = Repo.all(query)
|
||||
|
||||
total = length(replies)
|
||||
|
||||
%{
|
||||
"id" => "#{object.ap_id}/replies",
|
||||
"type" => "OrderedCollection",
|
||||
"totalItems" => total,
|
||||
"first" => collection(replies, "#{object.ap_id}/replies", 1, true)
|
||||
}
|
||||
|> Map.merge(Utils.make_json_ld_header())
|
||||
end
|
||||
|
||||
def collection(collection, iri, page, show_items \\ true, total \\ nil) do
|
||||
offset = (page - 1) * 10
|
||||
items = Enum.slice(collection, offset, 10)
|
||||
items = Enum.map(items, fn item -> item.ap_id end)
|
||||
total = total || length(collection)
|
||||
|
||||
map = %{
|
||||
"id" => "#{iri}?page=#{page}",
|
||||
"type" => "OrderedCollectionPage",
|
||||
"partOf" => iri,
|
||||
"totalItems" => total,
|
||||
"orderedItems" => if(show_items, do: items, else: [])
|
||||
}
|
||||
|
||||
if offset < total do
|
||||
Map.put(map, "next", "#{iri}?page=#{page + 1}")
|
||||
else
|
||||
map
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue