This commit is contained in:
Mirai Kumiko 2025-06-08 13:44:23 +02:00
parent cd1e5887c5
commit f8bedff913
Signed by: miraikumiko
GPG key ID: 3F178B1B5E0CB278
4 changed files with 61 additions and 25 deletions

View file

@ -85,47 +85,58 @@ defmodule Nulla.ActivityPub do
) )
end end
@spec note(String.t(), Nulla.Models.User.t(), Nulla.Models.Note.t()) :: Jason.OrderedObject.t() @spec note(String.t(), Nulla.Models.Note.t()) :: Jason.OrderedObject.t()
def note(domain, user, note) do def note(domain, 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://#{domain}/files/#{att.file}"
)
end)
]
end
Jason.OrderedObject.new( Jason.OrderedObject.new(
"@context": [ "@context": [
"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}/@#{user.username}/#{note.id}", id: "https://#{domain}/@#{note.user.username}/#{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}/@#{user.username}/#{note.id}", url: "https://#{domain}/@#{note.user.username}/#{note.id}",
attributedTo: "https://#{domain}/@#{user.username}", attributedTo: "https://#{domain}/@#{note.user.username}",
to: [ to: [
"https://www.w3.org/ns/activitystreams#Public" "https://www.w3.org/ns/activitystreams#Public"
], ],
cc: [ cc: [
"https://#{domain}/@#{user.username}/followers" "https://#{domain}/@#{note.user.username}/followers"
], ],
sensetive: false, sensetive: false,
content: note.content, content: note.content,
contentMap: Jason.OrderedObject.new("#{note.language}": note.content), contentMap: Jason.OrderedObject.new("#{note.language}": note.content),
attachment: [ attachment: attachment
Jason.OrderedObject.new(
type: "Document",
mediaType: "#{note.media_attachment.mime_type}",
url: "https://#{domain}/files/#{note.media_attachment.file}"
)
]
) )
end end
def activity(domain, action) do @spec activity(String.t(), Nulla.Models.Activity.t()) :: Jason.OrderedObject.t()
def activity(domain, 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/#{action.id}", id: "https://#{domain}/activities/#{activity.id}",
type: action.type, type: activity.type,
actor: action.actor, actor: activity.actor,
object: action.object, object: activity.object,
to: action.to to: activity.to
) )
end end
end end

View file

@ -29,5 +29,7 @@ defmodule Nulla.Models.Note do
|> validate_required([:content, :visibility, :sensitive, :language, :in_reply_to, :user_id]) |> validate_required([:content, :visibility, :sensitive, :language, :in_reply_to, :user_id])
end end
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_all_notes!(user_id), do: Repo.all(from n in Note, where: n.user_id == ^user_id)
end end

View file

@ -1,8 +1,9 @@
defmodule NullaWeb.NoteController do defmodule NullaWeb.NoteController do
use NullaWeb, :controller use NullaWeb, :controller
alias Nulla.Repo
alias Nulla.Notes
alias Nulla.Models.Note alias Nulla.Models.Note
alias Nulla.Models.InstanceSettings
alias Nulla.ActivityPub
def index(conn, _params) do def index(conn, _params) do
notes = Notes.list_notes() notes = Notes.list_notes()
@ -26,11 +27,33 @@ defmodule NullaWeb.NoteController do
end end
end end
def show(conn, %{"id" => id}) do def show(conn, %{"username" => username, "note_id" => note_id}) do
note = Notes.get_note!(id) accept = List.first(get_req_header(conn, "accept"))
render(conn, :show, note: note) instance_settings = InstanceSettings.get_instance_settings!()
domain = instance_settings.domain
note = Note.get_note!(note_id) |> Repo.preload([:user, :media_attachments])
if username != note.user.username do
conn
|> put_status(:not_found)
|> json(%{error: "Note not found"})
|> halt()
end
if accept in ["application/activity+json", "application/ld+json"] do
conn
|> put_resp_content_type("application/activity+json")
|> json(ActivityPub.note(domain, note))
else
render(conn, :show, domain: domain, note: note, layout: false)
end
end end
#def show(conn, %{"id" => id}) do
# note = Notes.get_note!(id)
# render(conn, :show, note: note)
#end
def edit(conn, %{"id" => id}) do def edit(conn, %{"id" => id}) do
note = Notes.get_note!(id) note = Notes.get_note!(id)
changeset = Notes.change_note(note) changeset = Notes.change_note(note)

View file

@ -22,7 +22,7 @@ defmodule NullaWeb.Router do
pipe_through :browser pipe_through :browser
get "/@:username", UserController, :show get "/@:username", UserController, :show
resources "/users", UserController get "/@:username/:note_id", NoteController, :show
end end
# Other scopes may use custom stacks. # Other scopes may use custom stacks.