Fix activitypub.ex

This commit is contained in:
Mirai Kumiko 2025-06-19 07:39:11 +02:00
parent 1b4034dac6
commit d8e66d0dcb
Signed by: miraikumiko
GPG key ID: 3F178B1B5E0CB278
3 changed files with 33 additions and 39 deletions

View file

@ -62,8 +62,8 @@ defmodule Nulla.ActivityPub do
)
end
@spec note(String.t(), Note.t()) :: Jason.OrderedObject.t()
def note(domain, note) do
@spec note(Note.t()) :: Jason.OrderedObject.t()
def note(note) do
attachment =
case note.media_attachments do
[] ->
@ -76,7 +76,7 @@ defmodule Nulla.ActivityPub do
Jason.OrderedObject.new(
type: "Document",
mediaType: att.mime_type,
url: "https://#{domain}/files/#{att.file}"
url: "https://#{note.actor.domain}/files/#{att.file}"
)
end)
]
@ -87,18 +87,18 @@ defmodule Nulla.ActivityPub do
"https://www.w3.org/ns/activitystreams",
Jason.OrderedObject.new(sensitive: "as:sensitive")
],
id: "https://#{domain}/users/#{note.actor.preferredUsername}/statuses/#{note.id}",
id: "#{note.actor.ap_id}/statuses/#{note.id}",
type: "Note",
summary: nil,
inReplyTo: nil,
published: note.inserted_at,
url: "https://#{domain}/@#{note.actor.preferredUsername}/#{note.id}",
attributedTo: "https://#{domain}/users/#{note.actor.preferredUsername}",
url: "#{note.actor.ap_id}/#{note.id}",
attributedTo: note.actor.ap_id,
to: [
"https://www.w3.org/ns/activitystreams#Public"
],
cc: [
"https://#{domain}/users/#{note.actor.preferredUsername}/followers"
"#{note.actor.ap_id}/followers"
],
sensetive: false,
content: note.content,
@ -107,11 +107,11 @@ defmodule Nulla.ActivityPub do
)
end
@spec activity(String.t(), Activity.t()) :: Jason.OrderedObject.t()
def activity(domain, activity) do
@spec activity(Activity.t()) :: Jason.OrderedObject.t()
def activity(activity) do
Jason.OrderedObject.new(
"@context": "https://www.w3.org/ns/activitystreams",
id: "https://#{domain}/activities/#{activity.id}",
id: activity.ap_id,
type: activity.type,
actor: activity.actor,
object: activity.object
@ -212,19 +212,19 @@ defmodule Nulla.ActivityPub do
data = [
subject: "#{actor.preferredUsername}@#{actor.domain}",
aliases: [
"https://#{actor.domain}/@#{actor.preferredUsername}",
"https://#{actor.domain}/users/#{actor.preferredUsername}"
actor.url,
actor.ap_id
],
links: [
Jason.OrderedObject.new(
rel: "http://webfinger.net/rel/profile-page",
type: "text/html",
href: "https://#{actor.domain}/users/#{actor.preferredUsername}"
href: actor.url
),
Jason.OrderedObject.new(
rel: "self",
type: "application/activity+json",
href: "https://#{actor.domain}/users/#{actor.preferredUsername}"
href: actor.ap_id
)
]
]
@ -295,21 +295,20 @@ defmodule Nulla.ActivityPub do
)
end
@spec outbox(String.t(), String.t(), Integer.t()) :: Jason.OrderedObject.t()
def outbox(domain, username, total) do
@spec outbox(Actor.t(), Integer.t()) :: Jason.OrderedObject.t()
def outbox(actor, total) do
Jason.OrderedObject.new(
"@context": "https://www.w3.org/ns/activitystreams",
id: "https://#{domain}/users/#{username}/outbox",
id: "#{actor.ap_id}/outbox",
type: "OrderedCollection",
totalItems: total,
first: "https://#{domain}/users/#{username}/outbox?page=true",
last: "https://#{domain}/users/#{username}/outbox?min_id=0&page=true"
first: "#{actor.ap_id}/outbox?page=true",
last: "#{actor.ap_id}/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
@spec outbox(Actor.t(), Integer.t(), Integer.t(), List.t()) :: Jason.OrderedObject.t()
def outbox(actor, max_id, min_id, items) do
Jason.OrderedObject.new(
"@context": [
"https://www.w3.org/ns/activitystreams",
@ -318,11 +317,11 @@ defmodule Nulla.ActivityPub do
Hashtag: "as:Hashtag"
)
],
id: "https://#{domain}/users/#{username}/outbox?page=true",
id: "#{actor.ap_id}/outbox?page=true",
type: "OrderedCollectionPage",
next: "https://#{domain}/users/#{username}/outbox?max_id=#{max_id}&page=true",
prev: "https://#{domain}/users/#{username}/outbox?min_id=#{min_id}&page=true",
partOf: "https://#{domain}/users/#{username}/outbox",
next: "#{actor.ap_id}/outbox?max_id=#{max_id}&page=true",
prev: "#{actor.ap_id}/outbox?min_id=#{min_id}&page=true",
partOf: "#{actor.ap_id}/outbox",
orderedItems: items
)
end
@ -330,22 +329,20 @@ defmodule Nulla.ActivityPub do
@spec activity_note(Note.t()) :: Jason.OrderedObject.t()
def activity_note(note) do
Jason.OrderedObject.new(
id:
"https://#{note.actor.domain}/users/#{note.actor.preferredUsername}/#{note.id}/activity",
id: "#{note.actor.ap_id}/statuses/#{note.id}/activity",
type: "Create",
actor: "https://#{note.actor.domain}/users/#{note.actor.preferredUsername}",
actor: note.actor.ap_id,
published: note.inserted_at |> DateTime.to_iso8601(),
to: [
"https://www.w3.org/ns/activitystreams#Public"
],
object:
Jason.OrderedObject.new(
id:
"https://#{note.actor.domain}/users/#{note.actor.preferredUsername}/statuses/#{note.id}",
id: "#{note.actor.ap_id}/statuses/#{note.id}",
type: "Note",
content: note.content,
published: note.inserted_at |> DateTime.to_iso8601(),
attributedTo: "https://#{note.actor.domain}/users/#{note.actor.preferredUsername}",
attributedTo: note.actor.ap_id,
to: [
"https://www.w3.org/ns/activitystreams#Public"
]

View file

@ -3,12 +3,9 @@ defmodule NullaWeb.NoteController do
alias Nulla.Repo
alias Nulla.ActivityPub
alias Nulla.Models.Note
alias Nulla.Models.InstanceSettings
def show(conn, %{"username" => username, "id" => id}) do
accept = List.first(get_req_header(conn, "accept"))
instance_settings = InstanceSettings.get_instance_settings!()
domain = instance_settings.domain
note = Note.get_note!(id) |> Repo.preload([:user, :media_attachments])
if username != note.user.username do
@ -21,9 +18,9 @@ defmodule NullaWeb.NoteController do
if accept in ["application/activity+json", "application/ld+json"] do
conn
|> put_resp_content_type("application/activity+json")
|> json(ActivityPub.note(domain, note))
|> json(ActivityPub.note(note))
else
render(conn, :show, domain: domain, note: note, layout: false)
render(conn, :show, note: note, layout: false)
end
end
end

View file

@ -38,7 +38,7 @@ defmodule NullaWeb.OutboxController do
|> put_resp_content_type("application/activity+json")
|> send_resp(
200,
Jason.encode!(ActivityPub.outbox(domain, username, next_max_id, min_id || 0, items))
Jason.encode!(ActivityPub.outbox(actor, next_max_id, min_id || 0, items))
)
_ ->
@ -49,7 +49,7 @@ defmodule NullaWeb.OutboxController do
conn
|> put_resp_content_type("application/activity+json")
|> send_resp(200, Jason.encode!(ActivityPub.outbox(domain, username, total)))
|> send_resp(200, Jason.encode!(ActivityPub.outbox(actor, total)))
end
end
end