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

View file

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

View file

@ -38,7 +38,7 @@ defmodule NullaWeb.OutboxController do
|> put_resp_content_type("application/activity+json") |> put_resp_content_type("application/activity+json")
|> send_resp( |> send_resp(
200, 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 conn
|> put_resp_content_type("application/activity+json") |> 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 end
end end