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

View file

@ -29,5 +29,7 @@ defmodule Nulla.Models.Note do
|> validate_required([:content, :visibility, :sensitive, :language, :in_reply_to, :user_id])
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)
end