Add featured route
This commit is contained in:
parent
b17e177335
commit
f90a7133bc
8 changed files with 118 additions and 3 deletions
24
lib/nulla_web/controllers/activitypub/featured_controller.ex
Normal file
24
lib/nulla_web/controllers/activitypub/featured_controller.ex
Normal file
|
@ -0,0 +1,24 @@
|
|||
defmodule NullaWeb.ActivityPub.FeaturedController do
|
||||
use NullaWeb, :controller
|
||||
alias Nulla.Actors
|
||||
alias Nulla.Notes
|
||||
alias Nulla.Actors.Actor
|
||||
alias NullaWeb.ActivityPub.FeaturedJSON
|
||||
|
||||
def index(conn, %{"username" => username}) do
|
||||
domain = NullaWeb.Endpoint.host()
|
||||
actor = Actors.get_actor_by(acct: "#{username}@#{domain}")
|
||||
|
||||
case actor do
|
||||
%Actor{} = actor ->
|
||||
notes = Notes.list_notes_by(actor_id: actor.id, featured: true)
|
||||
|
||||
conn
|
||||
|> put_resp_content_type("application/activity+json")
|
||||
|> send_resp(200, Jason.encode!(FeaturedJSON.index(actor, notes)))
|
||||
|
||||
_ ->
|
||||
send_resp(conn, 404, "")
|
||||
end
|
||||
end
|
||||
end
|
51
lib/nulla_web/controllers/activitypub/featured_json.ex
Normal file
51
lib/nulla_web/controllers/activitypub/featured_json.ex
Normal file
|
@ -0,0 +1,51 @@
|
|||
defmodule NullaWeb.ActivityPub.FeaturedJSON do
|
||||
@doc """
|
||||
Renders a featured.
|
||||
"""
|
||||
def index(actor, notes) do
|
||||
Jason.OrderedObject.new(
|
||||
"@context": "https://www.w3.org/ns/activitystreams",
|
||||
id: actor.featured,
|
||||
type: "OrderedCollection",
|
||||
totalItems: length(notes),
|
||||
orderedItems: Enum.map(notes, &data/1)
|
||||
)
|
||||
end
|
||||
|
||||
defp data(note) do
|
||||
attachment =
|
||||
case note.media_attachments do
|
||||
[] ->
|
||||
[]
|
||||
|
||||
attachments ->
|
||||
[
|
||||
attachment:
|
||||
Enum.map(attachments, fn att ->
|
||||
Jason.OrderedObject.new(
|
||||
type: "Document",
|
||||
mediaType: att.mime_type,
|
||||
url: "https://#{note.actor.domain}/files/#{att.file}"
|
||||
)
|
||||
end)
|
||||
]
|
||||
end
|
||||
|
||||
Jason.OrderedObject.new(
|
||||
"@context": "https://www.w3.org/ns/activitystreams",
|
||||
id: note.ap_id,
|
||||
type: "Note",
|
||||
summary: nil,
|
||||
inReplyTo: note.inReplyTo,
|
||||
published: note.published,
|
||||
url: note.url,
|
||||
attributedTo: note.actor.ap_id,
|
||||
to: note.to,
|
||||
cc: note.cc,
|
||||
sensitive: note.sensitive,
|
||||
content: note.content,
|
||||
contentMap: Jason.OrderedObject.new("#{note.language}": note.content),
|
||||
attachment: attachment
|
||||
)
|
||||
end
|
||||
end
|
|
@ -32,7 +32,7 @@ defmodule NullaWeb.ActivityPub.NoteJSON do
|
|||
"https://www.w3.org/ns/activitystreams",
|
||||
Jason.OrderedObject.new(sensitive: "as:sensitive")
|
||||
],
|
||||
id: "#{note.actor.ap_id}/notes/#{note.id}",
|
||||
id: note.ap_id,
|
||||
type: "Note",
|
||||
summary: nil,
|
||||
inReplyTo: note.inReplyTo,
|
||||
|
|
|
@ -13,6 +13,9 @@ defmodule NullaWeb.ActivityPub.OutboxJSON do
|
|||
)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Renders outbox items.
|
||||
"""
|
||||
def show(actor, activities, max_id, min_id) do
|
||||
Jason.OrderedObject.new(
|
||||
"@context": [
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue