Update outbox
This commit is contained in:
parent
f05741edb5
commit
50abfe4748
4 changed files with 103 additions and 10 deletions
|
@ -316,4 +316,44 @@ defmodule Nulla.ActivityPub do
|
|||
last: "https://#{domain}/@#{username}/outbox?min_id=0&page=true"
|
||||
)
|
||||
end
|
||||
|
||||
@spec outbox(String.t(), Integer.t(), Integer.t(), String.t(), List.t()) ::
|
||||
Jason.OrderedObject.t()
|
||||
def outbox(domain, username, max_id, min_id, items) do
|
||||
Jason.OrderedObject.new(
|
||||
"@context": [
|
||||
"https://www.w3.org/ns/activitystreams",
|
||||
Jason.OrderedObject.new(
|
||||
sensitive: "as:sensitive",
|
||||
Hashtag: "as:Hashtag"
|
||||
)
|
||||
],
|
||||
id: "https://#{domain}/@#{username}/outbox?page=true",
|
||||
type: "OrderedCollectionPage",
|
||||
next: "https://#{domain}/@#{username}/outbox?max_id=#{max_id}&page=true",
|
||||
prev: "https://#{domain}/@#{username}/outbox?min_id=#{min_id}&page=true",
|
||||
partOf: "https://#{domain}/@#{username}/outbox",
|
||||
orderedItems: items
|
||||
)
|
||||
end
|
||||
|
||||
@spec render_activity(String.t(), Note.t()) :: Jason.OrderedObject.t()
|
||||
def render_activity(domain, note) do
|
||||
Jason.OrderedObject.new(
|
||||
id: "https://#{domain}/@#{note.user.username}/#{note.id}/activity",
|
||||
type: "Create",
|
||||
actor: "https://#{domain}/@#{note.user.username}",
|
||||
published: note.inserted_at |> DateTime.to_iso8601(),
|
||||
to: ["https://www.w3.org/ns/activitystreams#Public"],
|
||||
object:
|
||||
Jason.OrderedObject.new(
|
||||
id: "https://#{domain}/@#{note.user.username}/#{note.id}",
|
||||
type: "Note",
|
||||
content: note.content,
|
||||
published: note.inserted_at |> DateTime.to_iso8601(),
|
||||
attributedTo: "https://#{domain}/@#{note.user.username}",
|
||||
to: ["https://www.w3.org/ns/activitystreams#Public"]
|
||||
)
|
||||
)
|
||||
end
|
||||
end
|
||||
|
|
|
@ -32,7 +32,23 @@ defmodule Nulla.Models.Note do
|
|||
|
||||
def get_note!(id), do: Repo.get!(Note, id)
|
||||
|
||||
def get_all_notes!(user_id), do: Repo.all(from n in Note, where: n.user_id == ^user_id)
|
||||
def get_latest_notes(user_id, limit \\ 20) do
|
||||
from(n in Note,
|
||||
where: n.user_id == ^user_id,
|
||||
order_by: [desc: n.inserted_at],
|
||||
limit: ^limit
|
||||
)
|
||||
|> Repo.all()
|
||||
end
|
||||
|
||||
def get_before_notes(user_id, max_id, limit \\ 20) do
|
||||
from(n in Note,
|
||||
where: n.user_id == ^user_id and n.id < ^max_id,
|
||||
order_by: [desc: n.inserted_at],
|
||||
limit: ^limit
|
||||
)
|
||||
|> Nulla.Repo.all()
|
||||
end
|
||||
|
||||
def get_total_notes_count(user_id) do
|
||||
from(n in Note, where: n.user_id == ^user_id)
|
||||
|
|
|
@ -5,7 +5,43 @@ defmodule NullaWeb.OutboxController do
|
|||
alias Nulla.Models.Note
|
||||
alias Nulla.Models.InstanceSettings
|
||||
|
||||
def show(conn, %{"username" => username}) do
|
||||
def show(conn, %{"username" => username} = params) do
|
||||
case Map.get(params, "page") do
|
||||
"true" ->
|
||||
instance_settings = InstanceSettings.get_instance_settings!()
|
||||
domain = instance_settings.domain
|
||||
user = User.get_user_by_username!(username)
|
||||
max_id = params["max_id"] && String.to_integer(params["max_id"])
|
||||
|
||||
notes =
|
||||
if max_id do
|
||||
Note.get_before_notes(user.id, max_id)
|
||||
else
|
||||
Note.get_latest_notes(user.id)
|
||||
end
|
||||
|
||||
items = Enum.map(notes, &ActivityPub.render_activity(&1, domain))
|
||||
|
||||
next_max_id =
|
||||
case List.last(notes) do
|
||||
nil -> 0
|
||||
last -> last.id
|
||||
end
|
||||
|
||||
min_id =
|
||||
case List.first(notes) do
|
||||
nil -> 0
|
||||
first -> first.id
|
||||
end
|
||||
|
||||
conn
|
||||
|> put_resp_content_type("application/activity+json")
|
||||
|> send_resp(
|
||||
200,
|
||||
Jason.encode!(ActivityPub.outbox(domain, username, next_max_id, min_id || 0, items))
|
||||
)
|
||||
|
||||
_ ->
|
||||
instance_settings = InstanceSettings.get_instance_settings!()
|
||||
domain = instance_settings.domain
|
||||
user = User.get_user_by_username!(username)
|
||||
|
@ -16,3 +52,4 @@ defmodule NullaWeb.OutboxController do
|
|||
|> send_resp(200, Jason.encode!(ActivityPub.outbox(domain, username, total)))
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -11,7 +11,7 @@ defmodule NullaWeb.UserController do
|
|||
instance_settings = InstanceSettings.get_instance_settings!()
|
||||
domain = instance_settings.domain
|
||||
user = User.get_user_by_username!(username)
|
||||
notes = Note.get_all_notes!(user.id)
|
||||
notes = Note.get_notes(user.id)
|
||||
|
||||
if accept in ["application/activity+json", "application/ld+json"] do
|
||||
conn
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue